public void testCommentAtEnd() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append("function E() \n"); buf.append("{\n"); buf.append("}//comment"); IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null); JavaScriptUnit astRoot = createAST3(cu); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); AST ast = astRoot.getAST(); assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); ListRewrite listRewrite = rewrite.getListRewrite(astRoot, JavaScriptUnit.STATEMENTS_PROPERTY); FunctionDeclaration newFunction = ast.newFunctionDeclaration(); newFunction.setName(ast.newSimpleName("B")); listRewrite.insertLast(newFunction, null); String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append("function E() \n"); buf.append("{\n"); buf.append("}//comment\n"); buf.append("\n"); buf.append("function B() {\n"); buf.append("}"); assertEqualString(preview, buf.toString()); }
public void testBug95839() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append("function foo() {\n"); buf.append(" object.method(\n"); buf.append(" param1, // text about param1\n"); buf.append(" param2 // text about param2\n"); buf.append(" );\n"); buf.append("}\n"); IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null); JavaScriptUnit astRoot = createAST3(cu); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); AST ast = astRoot.getAST(); assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); FunctionDeclaration function = findMethodDeclaration(astRoot, "foo"); ExpressionStatement statement = (ExpressionStatement) function.getBody().statements().get(0); FunctionInvocation inv = (FunctionInvocation) statement.getExpression(); ListRewrite listRewrite = rewrite.getListRewrite(inv, FunctionInvocation.ARGUMENTS_PROPERTY); listRewrite.insertLast(ast.newSimpleName("param3"), null); String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append("function foo() {\n"); buf.append(" object.method(\n"); buf.append(" param1, // text about param1\n"); buf.append(" param2 // text about param2\n"); buf.append(", param3\n"); buf.append(" );\n"); buf.append("}\n"); assertEqualString(preview, buf.toString()); }
private void createConstantDeclaration() throws CoreException { Type type = getConstantType(); IExpressionFragment fragment = getSelectedExpression(); String initializerSource = fCu.getBuffer().getText(fragment.getStartPosition(), fragment.getLength()); AST ast = fCuRewrite.getAST(); VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment(); variableDeclarationFragment.setName(ast.newSimpleName(fConstantName)); variableDeclarationFragment.setInitializer( (Expression) fCuRewrite .getASTRewrite() .createStringPlaceholder(initializerSource, ASTNode.SIMPLE_NAME)); FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(variableDeclarationFragment); fieldDeclaration.setType(type); Modifier.ModifierKeyword accessModifier = Modifier.ModifierKeyword.toKeyword(fVisibility); if (accessModifier != null) fieldDeclaration.modifiers().add(ast.newModifier(accessModifier)); fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD)); fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD)); boolean createComments = JavaPreferencesSettings.getCodeGenerationSettings(fCu.getJavaScriptProject()) .createComments; if (createComments) { String comment = CodeGeneration.getFieldComment( fCu, getConstantTypeName(), fConstantName, StubUtility.getLineDelimiterUsed(fCu)); if (comment != null && comment.length() > 0) { JSdoc doc = (JSdoc) fCuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JSDOC); fieldDeclaration.setJavadoc(doc); } } AbstractTypeDeclaration parent = getContainingTypeDeclarationNode(); ListRewrite listRewrite = fCuRewrite.getASTRewrite().getListRewrite(parent, parent.getBodyDeclarationsProperty()); TextEditGroup msg = fCuRewrite.createGroupDescription( RefactoringCoreMessages.ExtractConstantRefactoring_declare_constant); if (insertFirst()) { listRewrite.insertFirst(fieldDeclaration, msg); } else { listRewrite.insertAfter(fieldDeclaration, getNodeToInsertConstantDeclarationAfter(), msg); } if (fLinkedProposalModel != null) { ASTRewrite rewrite = fCuRewrite.getASTRewrite(); LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true); nameGroup.addPosition(rewrite.track(variableDeclarationFragment.getName()), true); String[] nameSuggestions = guessConstantNames(); if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fConstantName)) { nameGroup.addProposal(fConstantName, null, nameSuggestions.length + 1); } for (int i = 0; i < nameSuggestions.length; i++) { nameGroup.addProposal(nameSuggestions[i], null, nameSuggestions.length - i); } LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true); typeGroup.addPosition(rewrite.track(type), true); ITypeBinding typeBinding = fragment.getAssociatedExpression().resolveTypeBinding(); if (typeBinding != null) { ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(ast, typeBinding); for (int i = 0; i < relaxingTypes.length; i++) { typeGroup.addProposal(relaxingTypes[i], fCuRewrite.getCu(), relaxingTypes.length - i); } } ModifierCorrectionSubProcessor.installLinkedVisibilityProposals( fLinkedProposalModel, rewrite, fieldDeclaration.modifiers(), false); } }