Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergey Prigogin2011-11-07 14:00:43 -0500
committerSergey Prigogin2011-11-07 14:04:35 -0500
commitafb3fae823eca93ea486d6aebb4525bfbccac796 (patch)
tree1ec57b44ff4870749ca370f871373deff60ed6a4 /core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite
parent65673a50e15988ab8840f73eca11e2423db301d4 (diff)
downloadorg.eclipse.cdt-afb3fae823eca93ea486d6aebb4525bfbccac796.tar.gz
org.eclipse.cdt-afb3fae823eca93ea486d6aebb4525bfbccac796.tar.xz
org.eclipse.cdt-afb3fae823eca93ea486d6aebb4525bfbccac796.zip
Automatic formatting of code affected by refactoring.
Diffstat (limited to 'core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite')
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriter.java108
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java77
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclSpecWriter.java22
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclarationWriter.java48
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclaratorWriter.java26
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ExpressionWriter.java3
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/InitializerWriter.java3
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NameWriter.java7
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NodeWriter.java16
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/Scribe.java75
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/StatementWriter.java36
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/TemplateParameterWriter.java3
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ASTModificationHelper.java3
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java317
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclSpecWriter.java6
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclarationWriter.java17
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclaratorWriter.java38
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTExpressionWriter.java4
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTStatementWriter.java4
19 files changed, 567 insertions, 246 deletions
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriter.java
index 8752230ea2..5b7229e5de 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriter.java
@@ -8,10 +8,19 @@
*
* Contributors:
* Institute for Software - initial API and implementation
+ * Sergey Prigogin (Google)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
+import org.eclipse.cdt.core.dom.ast.IASTASMDeclaration;
+import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
+import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
+import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
import org.eclipse.cdt.core.dom.ast.IASTNode;
+import org.eclipse.cdt.core.dom.ast.IASTStatement;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisibilityLabel;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore;
import org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.ChangeGeneratorWriterVisitor;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.ASTCommenter;
@@ -19,9 +28,7 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
/**
* ASTWriter main class. Generates source code from <code>IASTNode</code>.
- * Uses a <code>hangeGeneratorWriterVisitor</code> to generate the code for the given nodes.
- *
- * @see ChangeGeneratorWriterVisitor
+ * Uses a {@link ChangeGeneratorWriterVisitor} to generate the code for the given nodes.
*
* @author Emanuel Graf
*/
@@ -54,18 +61,13 @@ public class ASTWriter {
* @throws ProblemRuntimeException if the node or one of it's children is a <code>IASTProblemNode</code>.
*/
public String write(IASTNode rootNode) throws ProblemRuntimeException {
- return write(rootNode, null, new NodeCommentMap());
- }
-
- public String write(IASTNode rootNode, NodeCommentMap commentMap) {
- return write(rootNode, null, commentMap);
+ return write(rootNode, new NodeCommentMap());
}
/**
* Generates the source code representing this node including comments.
*
* @param rootNode Node to write.
- * @param fileScope
* @param commentMap Node Comment Map <code>ASTCommenter</code>
* @return A <code>String</code> representing the source code for the node.
* @throws ProblemRuntimeException if the node or one of it's children is
@@ -73,17 +75,95 @@ public class ASTWriter {
*
* @see ASTCommenter#getCommentedNodeMap(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit)
*/
- public String write(IASTNode rootNode, String fileScope, NodeCommentMap commentMap)
+ public String write(IASTNode rootNode, NodeCommentMap commentMap)
throws ProblemRuntimeException {
- ChangeGeneratorWriterVisitor transformationVisitor = new ChangeGeneratorWriterVisitor(
- modificationStore, givenIndentation, fileScope, commentMap);
+ ChangeGeneratorWriterVisitor writer = new ChangeGeneratorWriterVisitor(
+ modificationStore, givenIndentation, null, commentMap);
if (rootNode != null) {
- rootNode.accept(transformationVisitor);
+ rootNode.accept(writer);
}
- return transformationVisitor.toString();
+ return writer.toString();
}
public void setModificationStore(ASTModificationStore modificationStore) {
this.modificationStore = modificationStore;
}
+
+ /**
+ * Returns <code>true</code> if the node should be separated by a blank line from the node
+ * before it.
+ *
+ * @param node The node.
+ * @return <code>true</code> if the node should be separated by a blank line from the node
+ * before it.
+ */
+ public static boolean requiresLeadingBlankLine(IASTNode node) {
+ if (node instanceof ICPPASTTemplateDeclaration) {
+ node = ((ICPPASTTemplateDeclaration) node).getDeclaration();
+ }
+ return node instanceof IASTASMDeclaration ||
+ node instanceof IASTFunctionDefinition ||
+ node instanceof ICPPASTVisibilityLabel;
+ }
+
+ /**
+ * Returns <code>true</code> if the node should be separated by a blank line from the node
+ * after it.
+ *
+ * @param node The node.
+ * @return <code>true</code> if the node should be separated by a blank line from the node
+ * after it.
+ */
+ public static boolean requiresTrailingBlankLine(IASTNode node) {
+ if (node instanceof ICPPASTNamespaceDefinition)
+ return true;
+ if (node instanceof IASTFunctionDefinition)
+ return true;
+ if (node instanceof IASTIfStatement) {
+ IASTIfStatement statement = ((IASTIfStatement) node);
+ IASTStatement lastClause = statement.getElseClause();
+ if (lastClause == null)
+ lastClause = statement.getThenClause();
+
+ if (!(lastClause instanceof IASTCompoundStatement) &&
+ !doNodesHaveSameOffset(lastClause, statement)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Returns <code>true</code> if there should be no blank line after this node even if a blank
+ * line is normally required before the subsequent node.
+ *
+ * @param node The node.
+ * @return <code>true</code> if there should be no blank line after this node.
+ */
+ public static boolean suppressesTrailingBlankLine(IASTNode node) {
+ return node instanceof ICPPASTVisibilityLabel;
+ }
+
+ /**
+ * Returns <code>true</code> if the two given nodes should be separated by a blank line.
+ *
+ * @param node1 The first node.
+ * @param node2 The second node.
+ * @return <code>true</code> if the blank line between the nodes is needed.
+ */
+ public static boolean requireBlankLineInBetween(IASTNode node1, IASTNode node2) {
+ if (requiresTrailingBlankLine(node1))
+ return true;
+
+ return !suppressesTrailingBlankLine(node1) && requiresLeadingBlankLine(node2);
+ }
+
+ /**
+ * Returns true if the two given nodes have the same offset. For nodes that are normally
+ * separated by other tokens this is an indication that they were produced by the same macro
+ * expansion.
+ */
+ private static boolean doNodesHaveSameOffset(IASTNode node1, IASTNode node2) {
+ return node1.getFileLocation().getNodeOffset() == node2.getFileLocation().getNodeOffset();
+ }
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java
index 553d8907c6..16f9325785 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java
@@ -9,6 +9,7 @@
* Contributors:
* Institute for Software - initial API and implementation
* Markus Schorn (Wind River Systems)
+ * Sergey Prigogin (Google)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
@@ -38,15 +39,14 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
/**
* Visits all nodes, prints leading comments and handles macro expansions. The
- * source code generation is delegated to severals <code>NodeWriters</code>.
+ * source code generation is delegated to severals {@code NodeWriter}s.
*
- * @see NodeWriter
* @see MacroExpansionHandler
*
* @author Emanuel Graf IFS
*/
public class ASTWriterVisitor extends ASTVisitor {
- protected Scribe scribe = new Scribe();
+ protected final Scribe scribe = new Scribe();
protected NodeCommentMap commentMap;
protected ExpressionWriter expWriter;
protected DeclSpecWriter declSpecWriter;
@@ -57,6 +57,9 @@ public class ASTWriterVisitor extends ASTVisitor {
protected NameWriter nameWriter;
protected TemplateParameterWriter tempParameterWriter;
protected MacroExpansionHandler macroHandler;
+ private boolean insertLeadingBlankLine;
+ private boolean suppressLeadingBlankLine;
+ private boolean spaceNeededBeforeName;
{
shouldVisitExpressions = true;
@@ -83,6 +86,7 @@ public class ASTWriterVisitor extends ASTVisitor {
scribe.setGivenIndentation(givenIndentation);
init(commentMap);
this.commentMap = commentMap;
+ this.suppressLeadingBlankLine = true;
}
private void init(NodeCommentMap commentMap) {
@@ -135,6 +139,10 @@ public class ASTWriterVisitor extends ASTVisitor {
@Override
public int visit(IASTName name) {
+ if (spaceNeededBeforeName && name.getSimpleID().length != 0) {
+ scribe.printSpace();
+ spaceNeededBeforeName = false;
+ }
writeLeadingComments(name);
if (!macroHandler.checkisMacroExpansionNode(name)) {
nameWriter.writeName(name);
@@ -166,24 +174,31 @@ public class ASTWriterVisitor extends ASTVisitor {
@Override
public int visit(IASTStatement statement) {
+ insertBlankLineIfNeeded(statement);
writeLeadingComments(statement);
- if (macroHandler.isStatementWithMixedLocation(statement) &&
- !(statement instanceof IASTCompoundStatement)) {
- return statementWriter.writeMixedStatement(statement);
- }
- if (macroHandler.checkisMacroExpansionNode(statement)) {
- return ASTVisitor.PROCESS_SKIP;
+ try {
+ if (macroHandler.isStatementWithMixedLocation(statement) &&
+ !(statement instanceof IASTCompoundStatement)) {
+ return statementWriter.writeMixedStatement(statement);
+ }
+ if (macroHandler.checkisMacroExpansionNode(statement)) {
+ return ASTVisitor.PROCESS_SKIP;
+ }
+ return statementWriter.writeStatement(statement, true);
+ } finally {
+ setLeadingBlankLineFlags(statement);
}
- return statementWriter.writeStatement(statement, true);
}
@Override
public int visit(IASTDeclaration declaration) {
+ insertBlankLineIfNeeded(declaration);
writeLeadingComments(declaration);
if (!macroHandler.checkisMacroExpansionNode(declaration)) {
declarationWriter.writeDeclaration(declaration);
+ setLeadingBlankLineFlags(declaration);
}
- return ASTVisitor.PROCESS_SKIP;
+ return ASTVisitor.PROCESS_SKIP;
}
@Override
@@ -219,9 +234,7 @@ public class ASTWriterVisitor extends ASTVisitor {
parameterDeclaration.getDeclSpecifier().accept(this);
IASTDeclarator declarator = getParameterDeclarator(parameterDeclaration);
- if (getParameterName(declarator).toString().length() != 0) {
- scribe.printSpaces(1);
- }
+ spaceNeededBeforeName = true;
declarator.accept(this);
}
return ASTVisitor.PROCESS_SKIP;
@@ -237,9 +250,11 @@ public class ASTWriterVisitor extends ASTVisitor {
@Override
public int visit(ICPPASTNamespaceDefinition namespace) {
+ insertBlankLineIfNeeded(namespace);
writeLeadingComments(namespace);
if (!macroHandler.checkisMacroExpansionNode(namespace)) {
declarationWriter.writeDeclaration(namespace);
+ setLeadingBlankLineFlags(namespace);
}
return ASTVisitor.PROCESS_SKIP;
}
@@ -257,4 +272,38 @@ public class ASTWriterVisitor extends ASTVisitor {
scribe.cleanCache();
macroHandler.reset();
}
+
+ private void insertBlankLineIfNeeded(IASTNode node) {
+ if (!suppressLeadingBlankLine &&
+ (insertLeadingBlankLine || ASTWriter.requiresLeadingBlankLine(node))) {
+ scribe.newLine();
+ }
+ insertLeadingBlankLine = false;
+ suppressLeadingBlankLine = false;
+ }
+
+ private void setLeadingBlankLineFlags(IASTNode node) {
+ insertLeadingBlankLine = ASTWriter.requiresTrailingBlankLine(node);
+ suppressLeadingBlankLine = ASTWriter.suppressesTrailingBlankLine(node);
+ }
+
+ public boolean isSuppressLeadingBlankLine() {
+ return suppressLeadingBlankLine;
+ }
+
+ public void setSuppressLeadingBlankLine(boolean value) {
+ this.suppressLeadingBlankLine = value;
+ }
+
+ public boolean isSpaceNeededBeforeName() {
+ return spaceNeededBeforeName;
+ }
+
+ public void setSpaceNeededBeforeName(boolean value) {
+ this.spaceNeededBeforeName = value;
+ }
+
+ public void newLine() {
+ scribe.newLine();
+ }
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclSpecWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclSpecWriter.java
index 17c6eab08a..ba7fb2e862 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclSpecWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclSpecWriter.java
@@ -11,7 +11,6 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
-import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
@@ -62,12 +61,12 @@ public class DeclSpecWriter extends NodeWriter {
private static final String ENUM = "enum "; //$NON-NLS-1$
private static final String _BOOL = "_Bool"; //$NON-NLS-1$
- public DeclSpecWriter(Scribe scribe, ASTVisitor visitor, NodeCommentMap commentMap) {
+ public DeclSpecWriter(Scribe scribe, ASTWriterVisitor visitor, NodeCommentMap commentMap) {
super(scribe, visitor, commentMap);
}
protected void writeDelcSpec(IASTDeclSpecifier declSpec) {
-// Write general DelcSpec Keywords
+ // Write general DelcSpec Keywords
writeDeclSpec(declSpec);
if (declSpec instanceof ICPPASTDeclSpecifier) {
writeCPPDeclSpec((ICPPASTDeclSpecifier) declSpec);
@@ -129,7 +128,6 @@ public class DeclSpecWriter extends NodeWriter {
break;
}
- System.err.println("Unknown specifier type: " + type); //$NON-NLS-1$
throw new IllegalArgumentException("Unknown specifier type: " + type); //$NON-NLS-1$
}
@@ -152,7 +150,7 @@ public class DeclSpecWriter extends NodeWriter {
}
private void writeNamedTypeSpecifier(ICPPASTNamedTypeSpecifier namedSpc) {
- if ( namedSpc.isTypename() ){
+ if (namedSpc.isTypename()) {
scribe.print(TYPENAME);
}
namedSpc.getName().accept(visitor);
@@ -179,8 +177,7 @@ public class DeclSpecWriter extends NodeWriter {
return CLASS_SPACE;
default:
- System.err.println("Unknown ElaboratedType: " + kind); //$NON-NLS-1$
- throw new IllegalArgumentException("Unknown ElaboratedType: " + kind); //$NON-NLS-1$
+ throw new IllegalArgumentException("Unknown elaborated type: " + kind); //$NON-NLS-1$
}
}
@@ -254,12 +251,13 @@ public class DeclSpecWriter extends NodeWriter {
hasTrailingComments = hasTrailingComments(baseSpecifiers[baseSpecifiers.length-1].getName());
}
}
- if (!hasTrailingComments){
+ if (!hasTrailingComments) {
scribe.newLine();
}
scribe.print('{');
scribe.newLine();
scribe.incrementIndentationLevel();
+ visitor.setSuppressLeadingBlankLine(true);
IASTDeclaration[] decls = getMembers(compDeclSpec);
if (decls.length > 0) {
@@ -306,8 +304,7 @@ public class DeclSpecWriter extends NodeWriter {
case ICPPASTCompositeTypeSpecifier.k_class:
return CLASS;
default:
- System.err.println("Unknow Specifiertype: " + key); //$NON-NLS-1$
- throw new IllegalArgumentException("Unknow Specifiertype: " + key); //$NON-NLS-1$
+ throw new IllegalArgumentException("Unknown type specifier: " + key); //$NON-NLS-1$
}
}
@@ -318,8 +315,7 @@ public class DeclSpecWriter extends NodeWriter {
case IASTCompositeTypeSpecifier.k_union:
return UNION;
default:
- System.err.println("Unknow Specifiertype: " + key); //$NON-NLS-1$
- throw new IllegalArgumentException("Unknow Specifiertype: " + key); //$NON-NLS-1$
+ throw new IllegalArgumentException("Unknown type specifier: " + key); //$NON-NLS-1$
}
}
@@ -368,7 +364,7 @@ public class DeclSpecWriter extends NodeWriter {
private void printQualifiers(IASTSimpleDeclSpecifier simpDeclSpec) {
if (simpDeclSpec.isSigned()) {
scribe.printStringSpace(SIGNED);
- } else if (simpDeclSpec.isUnsigned()){
+ } else if (simpDeclSpec.isUnsigned()) {
scribe.printStringSpace(UNSIGNED);
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclarationWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclarationWriter.java
index 6976f5fcf6..41ed66fc64 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclarationWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclarationWriter.java
@@ -9,10 +9,10 @@
* Contributors:
* Institute for Software - initial API and implementation
* Markus Schorn (Wind River Systems)
+ * Sergey Prigogin (Google)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
-import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTASMDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
@@ -47,7 +47,7 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
* @see IASTDeclaration
* @author Emanuel Graf IFS
*/
-public class DeclarationWriter extends NodeWriter{
+public class DeclarationWriter extends NodeWriter {
private static final String ASM_END = ")"; //$NON-NLS-1$
private static final String ASM_START = "asm("; //$NON-NLS-1$
private static final String TEMPLATE_DECLARATION = "template<"; //$NON-NLS-1$
@@ -57,7 +57,7 @@ public class DeclarationWriter extends NodeWriter{
private static final String USING = "using "; //$NON-NLS-1$
private boolean printSemicolon;
- public DeclarationWriter(Scribe scribe, ASTVisitor visitor, NodeCommentMap commentMap) {
+ public DeclarationWriter(Scribe scribe, ASTWriterVisitor visitor, NodeCommentMap commentMap) {
super(scribe, visitor, commentMap);
}
@@ -72,6 +72,7 @@ public class DeclarationWriter extends NodeWriter{
writeASMDeclatation((IASTASMDeclaration) declaration);
} else if (declaration instanceof IASTFunctionDefinition) {
writeFunctionDefinition((IASTFunctionDefinition) declaration);
+ addNewLine = false;
} else if (declaration instanceof IASTProblemDeclaration) {
throw new ProblemRuntimeException((IASTProblemDeclaration) declaration);
} else if (declaration instanceof IASTSimpleDeclaration) {
@@ -98,17 +99,17 @@ public class DeclarationWriter extends NodeWriter{
}
if (hasTrailingComments(declaration)) {
- writeTrailingComments(declaration, addNewLine);
- } else if (addNewLine) {
+ writeTrailingComments(declaration, false);
+ }
+ if (addNewLine) {
scribe.newLine();
}
if (hasFreestandingComments(declaration)) {
+ if (declaration instanceof IASTFunctionDefinition) {
+ scribe.newLine();
+ }
writeFreeStandingComments(declaration);
}
-
- if (declaration instanceof ICPPASTUsingDirective) {
- scribe.newLine();
- }
}
private void writeVisibilityLabel(ICPPASTVisibilityLabel visiblityLabel) {
@@ -181,17 +182,18 @@ public class DeclarationWriter extends NodeWriter{
if (!hasTrailingComments(namespaceDefinition.getName())) {
scribe.newLine();
}
- scribe.printLBrace();
- scribe.newLine();
+ scribe.print('{');
+ scribe.newLine(2);
writeDeclarationsInNamespace(namespaceDefinition, namespaceDefinition.getDeclarations());
if (hasFreestandingComments(namespaceDefinition)) {
writeFreeStandingComments(namespaceDefinition);
}
- scribe.printRBrace();
+ scribe.newLine();
+ scribe.print('}');
if (hasTrailingComments(namespaceDefinition)) {
writeTrailingComments(namespaceDefinition);
- }else{
+ } else {
scribe.newLine();
}
}
@@ -211,7 +213,7 @@ public class DeclarationWriter extends NodeWriter{
}
private void writeLinkageSpecification(ICPPASTLinkageSpecification linkageSpecification) {
- scribe.print( EXTERN);
+ scribe.print(EXTERN);
scribe.print(linkageSpecification.getLiteral());
scribe.printSpaces(1);
@@ -266,10 +268,10 @@ public class DeclarationWriter extends NodeWriter{
if (declSpecifier instanceof IASTSimpleDeclSpecifier) {
IASTSimpleDeclSpecifier simDeclSpec = (IASTSimpleDeclSpecifier) declSpecifier;
if (simDeclSpec.getType() != IASTSimpleDeclSpecifier.t_unspecified) {
- scribe.printSpace();
+ visitor.setSpaceNeededBeforeName(true);
}
- }else {
- scribe.printSpace();
+ } else {
+ visitor.setSpaceNeededBeforeName(true);
}
IASTDeclarator declarator = ASTQueries.findOutermostDeclarator(funcDef.getDeclarator());
declarator.accept(visitor);
@@ -325,10 +327,16 @@ public class DeclarationWriter extends NodeWriter{
}
if (decls.length > 0) {
- if (!noSpace) {
- scribe.printSpace();
+ if (decls.length == 1) {
+ if (!noSpace)
+ visitor.setSpaceNeededBeforeName(true);
+ decls[0].accept(visitor);
+ } else {
+ if (!noSpace) {
+ scribe.printSpace();
+ }
+ writeNodeList(decls);
}
- writeNodeList(decls);
}
printSemicolon();
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclaratorWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclaratorWriter.java
index 219af0c80e..4a9a8af385 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclaratorWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclaratorWriter.java
@@ -12,7 +12,6 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
-import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
@@ -40,14 +39,12 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
* @author Emanuel Graf IFS
*/
public class DeclaratorWriter extends NodeWriter {
- private static final String AMPERSAND_SPACE = "& "; //$NON-NLS-1$
- private static final String AMPERSAND_AMPERSAND_SPACE = "&& "; //$NON-NLS-1$
- private static final String STAR_SPACE = "* "; //$NON-NLS-1$
+ private static final String AMPERSAND_AMPERSAND = "&&"; //$NON-NLS-1$
private static final String PURE_VIRTUAL = " = 0"; //$NON-NLS-1$
private static final String MUTABLE = "mutable"; //$NON-NLS-1$
private static final String ARROW_OPERATOR = "->"; //$NON-NLS-1$
- public DeclaratorWriter(Scribe scribe, ASTVisitor visitor, NodeCommentMap commentMap) {
+ public DeclaratorWriter(Scribe scribe, ASTWriterVisitor visitor, NodeCommentMap commentMap) {
super(scribe, visitor, commentMap);
}
@@ -63,7 +60,8 @@ public class DeclaratorWriter extends NodeWriter {
} else {
writeDefaultDeclarator(declarator);
}
-
+
+ visitor.setSpaceNeededBeforeName(false);
if (hasTrailingComments(declarator)) {
writeTrailingComments(declarator, false);
}
@@ -119,6 +117,10 @@ public class DeclaratorWriter extends NodeWriter {
private void writeNestedDeclarator(IASTDeclarator funcDec) {
IASTDeclarator nestedDeclarator = funcDec.getNestedDeclarator();
if (nestedDeclarator != null) {
+ if (visitor.isSpaceNeededBeforeName()) {
+ scribe.printSpace();
+ visitor.setSpaceNeededBeforeName(false);
+ }
scribe.print('(');
nestedDeclarator.accept(visitor);
scribe.print(')');
@@ -160,10 +162,10 @@ public class DeclaratorWriter extends NodeWriter {
}
}
- protected void writeParameterDeclarations(IASTStandardFunctionDeclarator funcDec, IASTParameterDeclaration[] paraDecls) {
- writeNodeList(paraDecls);
+ protected void writeParameterDeclarations(IASTStandardFunctionDeclarator funcDec, IASTParameterDeclaration[] paramDecls) {
+ writeNodeList(paramDecls);
if (funcDec.takesVarArgs()) {
- if (paraDecls.length > 0) {
+ if (paramDecls.length > 0) {
scribe.print(COMMA_SPACE);
}
scribe.print(VAR_ARGS);
@@ -175,7 +177,7 @@ public class DeclaratorWriter extends NodeWriter {
ICPPASTPointerToMember pointerToMemberOp = (ICPPASTPointerToMember) operator;
if (pointerToMemberOp.getName() != null) {
pointerToMemberOp.getName().accept(visitor);
- scribe.print(STAR_SPACE);
+ scribe.print('*');
}
} else {
scribe.print('*');
@@ -198,9 +200,9 @@ public class DeclaratorWriter extends NodeWriter {
writePointer(pointOp);
} else if (operator instanceof ICPPASTReferenceOperator) {
if (((ICPPASTReferenceOperator) operator).isRValueReference()) {
- scribe.print(AMPERSAND_AMPERSAND_SPACE);
+ scribe.print(AMPERSAND_AMPERSAND);
} else {
- scribe.print(AMPERSAND_SPACE);
+ scribe.print('&');
}
}
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ExpressionWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ExpressionWriter.java
index 8676cae026..150f6c7454 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ExpressionWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ExpressionWriter.java
@@ -12,7 +12,6 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
-import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTArraySubscriptExpression;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTCastExpression;
@@ -115,7 +114,7 @@ public class ExpressionWriter extends NodeWriter{
private static final String THIS = "this"; //$NON-NLS-1$
private final MacroExpansionHandler macroHandler;
- public ExpressionWriter(Scribe scribe, ASTVisitor visitor, MacroExpansionHandler macroHandler, NodeCommentMap commentMap) {
+ public ExpressionWriter(Scribe scribe, ASTWriterVisitor visitor, MacroExpansionHandler macroHandler, NodeCommentMap commentMap) {
super(scribe, visitor, commentMap);
this.macroHandler = macroHandler;
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/InitializerWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/InitializerWriter.java
index 37aaff78c0..7adc8fdce8 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/InitializerWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/InitializerWriter.java
@@ -12,7 +12,6 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
-import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTEqualsInitializer;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
@@ -36,7 +35,7 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
*/
public class InitializerWriter extends NodeWriter{
- public InitializerWriter(Scribe scribe, ASTVisitor visitor, NodeCommentMap commentMap) {
+ public InitializerWriter(Scribe scribe, ASTWriterVisitor visitor, NodeCommentMap commentMap) {
super(scribe, visitor, commentMap);
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NameWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NameWriter.java
index c5c2a05f1a..941931511c 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NameWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NameWriter.java
@@ -7,11 +7,10 @@
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
- * Institute for Software - initial API and implementation
+ * Institute for Software - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
-import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IBinding;
@@ -36,7 +35,7 @@ public class NameWriter extends NodeWriter {
* @param scribe
* @param visitor
*/
- public NameWriter(Scribe scribe, ASTVisitor visitor, NodeCommentMap commentMap) {
+ public NameWriter(Scribe scribe, ASTWriterVisitor visitor, NodeCommentMap commentMap) {
super(scribe, visitor, commentMap);
}
@@ -45,7 +44,7 @@ public class NameWriter extends NodeWriter {
writeTempalteId((ICPPASTTemplateId) name);
} else if (name instanceof ICPPASTConversionName) {
scribe.print(OPERATOR);
- ((ICPPASTConversionName)name).getTypeId().accept(visitor);
+ ((ICPPASTConversionName) name).getTypeId().accept(visitor);
} else if (name instanceof ICPPASTQualifiedName){
writeQualifiedName((ICPPASTQualifiedName) name);
} else {
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NodeWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NodeWriter.java
index 5c2b384974..627e8158a5 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NodeWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NodeWriter.java
@@ -13,7 +13,6 @@ package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
import java.util.ArrayList;
-import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTComment;
import org.eclipse.cdt.core.dom.ast.IASTCopyLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode;
@@ -28,7 +27,7 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
*/
public class NodeWriter {
protected Scribe scribe;
- protected ASTVisitor visitor;
+ protected ASTWriterVisitor visitor;
protected NodeCommentMap commentMap;
protected static final String COMMA_SPACE = ", "; //$NON-NLS-1$
protected static final String EQUALS = " = "; //$NON-NLS-1$
@@ -61,7 +60,7 @@ public class NodeWriter {
protected static final String COLON_COLON = "::"; //$NON-NLS-1$
protected static final String COLON_SPACE = ": "; //$NON-NLS-1$
- public NodeWriter(Scribe scribe, ASTVisitor visitor, NodeCommentMap commentMap) {
+ public NodeWriter(Scribe scribe, ASTWriterVisitor visitor, NodeCommentMap commentMap) {
super();
this.scribe = scribe;
this.visitor = visitor;
@@ -89,12 +88,17 @@ public class NodeWriter {
}
protected void writeTrailingComments(IASTNode node, boolean newLine) {
+ boolean first = true;
for (IASTComment comment : getTrailingComments(node)) {
- scribe.printSpace();
- scribe.print(comment.getComment());
- if (newLine) {
+ if (!first) {
scribe.newLine();
}
+ scribe.printSpace();
+ scribe.print(comment.getComment());
+ first = false;
+ }
+ if (newLine) {
+ scribe.newLine();
}
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/Scribe.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/Scribe.java
index 9751cefd6a..8bc32d8396 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/Scribe.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/Scribe.java
@@ -7,7 +7,7 @@
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
- * Institute for Software - initial API and implementation
+ * Institute for Software - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
@@ -20,92 +20,93 @@ package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
*/
public class Scribe {
private int indentationLevel = 0;
- private int indentationSize = 4; //HSR tcorbat: could be a tab character too - this is not a very elegant solution
+ // Any positive indentation size is good enough since the code is going to be formatted anyway.
+ private int indentationSize = 4;
private StringBuilder buffer = new StringBuilder();
private boolean isAtLineBeginning = true;
private String newLine = System.getProperty("line.separator"); //$NON-NLS-1$
- private String givenIndentation = null;
+ private String givenIndentation;
+
+ private boolean skipLineBreaks;
+ private boolean skipSemicolons;
- private boolean noNewLine = false;
- private boolean noSemicolon = false;
-
public void newLine() {
- if (!noNewLine) {
+ if (!skipLineBreaks) {
isAtLineBeginning = true;
buffer.append(getNewline());
}
}
-
+
private void indent() {
if (givenIndentation != null) {
buffer.append(givenIndentation);
}
printSpaces(indentationLevel * indentationSize);
}
-
+
private void indentIfNewLine() {
if (isAtLineBeginning) {
isAtLineBeginning = false;
indent();
}
}
-
+
private String getNewline() {
return newLine;
}
-
+
public void print(String code) {
indentIfNewLine();
buffer.append(code);
}
-
+
public void println(String code) {
print(code);
newLine();
}
-
+
public void print(String code, String code2) {
print(code);
buffer.append(code2);
}
-
+
public void println(String code, String code2) {
print(code, code2);
newLine();
}
-
+
public void println(String code, char[] code2) {
print(code);
buffer.append(code2);
newLine();
}
-
+
public void printSpaces(int number) {
indentIfNewLine();
for (int i = 0; i < number; ++i) {
printSpace();
}
}
-
+
public void noSemicolon() {
- noSemicolon = true;
+ skipSemicolons = true;
}
-
+
public void printSemicolon() {
- if (!noSemicolon) {
+ if (!skipSemicolons) {
indentIfNewLine();
buffer.append(';');
} else {
- noSemicolon = false;
+ skipSemicolons = false;
}
}
-
+
@Override
public String toString() {
return buffer.toString();
}
-
- public void print (char code) {
+
+ public void print(char code) {
indentIfNewLine();
buffer.append(code);
}
@@ -114,19 +115,19 @@ public class Scribe {
indentIfNewLine();
buffer.append(code);
}
-
+
public void println(char[] code) {
print(code);
newLine();
}
-
+
public void printStringSpace(String code) {
print(code);
printSpace();
}
/**
- * Prints a { to the Buffer an increases the indentation level.
+ * Prints a { to the buffer an increases the indentation level.
*/
public void printLBrace() {
print('{');
@@ -134,31 +135,31 @@ public class Scribe {
}
/**
- * Prints a } to the Buffer an decrease the indentation level.
+ * Prints a } to the buffer an decrease the indentation level.
*/
public void printRBrace() {
--indentationLevel;
print('}');
}
-
+
public void incrementIndentationLevel() {
++indentationLevel;
}
-
+
public void decrementIndentationLevel() {
if (indentationLevel > 0) {
--indentationLevel;
}
}
-
+
protected void noNewLines() {
- noNewLine = true;
+ skipLineBreaks = true;
}
-
+
protected void newLines() {
- noNewLine = false;
+ skipLineBreaks = false;
}
-
+
public void newLine(int i) {
while (i > 0) {
newLine();
@@ -167,7 +168,7 @@ public class Scribe {
}
public void printSpace() {
- buffer.append(' ');
+ buffer.append(' ');
}
public String getGivenIndentation() {
@@ -179,6 +180,6 @@ public class Scribe {
}
public void cleanCache() {
- buffer = new StringBuilder();
+ buffer = new StringBuilder();
}
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/StatementWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/StatementWriter.java
index fbafe9bf15..ac8c6f21f3 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/StatementWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/StatementWriter.java
@@ -56,17 +56,17 @@ import org.eclipse.core.resources.IFile;
public class StatementWriter extends NodeWriter {
private static final String DEFAULT = "default:"; //$NON-NLS-1$
private static final String CASE = "case "; //$NON-NLS-1$
- private static final String WHILE = "while("; //$NON-NLS-1$
+ private static final String WHILE = "while ("; //$NON-NLS-1$
private static final String TRY = "try "; //$NON-NLS-1$
- private static final String CATCH = "catch("; //$NON-NLS-1$
+ private static final String CATCH = "catch ("; //$NON-NLS-1$
private static final String RETURN = "return"; //$NON-NLS-1$
private static final String GOTO = "goto "; //$NON-NLS-1$
private static final String CONTINUE = "continue"; //$NON-NLS-1$
private static final String BREAK = "break"; //$NON-NLS-1$
private static final String ELSE = "else"; //$NON-NLS-1$
- private static final String IF = "if("; //$NON-NLS-1$
- private static final String FOR = "for("; //$NON-NLS-1$
- private static final String DO_WHILE = " while("; //$NON-NLS-1$
+ private static final String IF = "if ("; //$NON-NLS-1$
+ private static final String FOR = "for ("; //$NON-NLS-1$
+ private static final String DO_WHILE = " while ("; //$NON-NLS-1$
private static final String DO = "do"; //$NON-NLS-1$
private static final String SWITCH_BRACKET = "switch ("; //$NON-NLS-1$
private boolean compoundNoNewLine = false;
@@ -74,14 +74,15 @@ public class StatementWriter extends NodeWriter {
private boolean decrementIndentationLevelOneMore = false;
private final DeclarationWriter declWriter;
- public StatementWriter(Scribe scribe, ASTVisitor visitor, NodeCommentMap commentMap) {
+ public StatementWriter(Scribe scribe, ASTWriterVisitor visitor, NodeCommentMap commentMap) {
super(scribe, visitor, commentMap);
declWriter = new DeclarationWriter(scribe, visitor, commentMap);
}
/**
+ * Prints a statement.
*
- * @param statement
+ * @param statement the statement
* @param newLine if true print a newline if statement usually have one.
* @return {@link ASTVisitor#PROCESS_SKIP}
*/
@@ -132,7 +133,7 @@ public class StatementWriter extends NodeWriter {
writeIfStatement((IASTIfStatement) statement);
newLine = false;
} else if (statement instanceof IASTWhileStatement) {
- writeWhileStatement( (IASTWhileStatement) statement );
+ writeWhileStatement((IASTWhileStatement) statement);
newLine = false;
} else if (statement instanceof IASTForStatement) {
writeForStatement((IASTForStatement) statement);
@@ -173,12 +174,12 @@ public class StatementWriter extends NodeWriter {
scribe.printSemicolon();
}
- private void writeForStatement(IASTForStatement forStatment) {
+ private void writeForStatement(IASTForStatement forStatement) {
scribe.noNewLines();
scribe.print(FOR);
- writeStatement(forStatment.getInitializerStatement(),false);
- if (forStatment instanceof ICPPASTForStatement) {
- ICPPASTForStatement cppForStatment = (ICPPASTForStatement) forStatment;
+ writeStatement(forStatement.getInitializerStatement(),false);
+ if (forStatement instanceof ICPPASTForStatement) {
+ ICPPASTForStatement cppForStatment = (ICPPASTForStatement) forStatement;
IASTDeclaration cppConditionDeclaration = cppForStatment.getConditionDeclaration();
if (cppConditionDeclaration == null) {
visitNodeIfNotNull(cppForStatment.getConditionExpression());
@@ -187,17 +188,17 @@ public class StatementWriter extends NodeWriter {
cppConditionDeclaration.accept(visitor);
}
} else {
- if (forStatment.getConditionExpression() != null) {
- forStatment.getConditionExpression().accept(visitor);
+ if (forStatement.getConditionExpression() != null) {
+ forStatement.getConditionExpression().accept(visitor);
scribe.printSemicolon();
}
}
- visitNodeIfNotNull(forStatment.getIterationExpression());
+ visitNodeIfNotNull(forStatement.getIterationExpression());
scribe.print(')');
scribe.newLines();
nextCompoundNoNewLine();
- writeBodyStatement(forStatment.getBody(), false);
+ writeBodyStatement(forStatement.getBody(), false);
}
private void writeForStatement(ICPPASTRangeBasedForStatement forStatment) {
@@ -418,10 +419,9 @@ public class StatementWriter extends NodeWriter {
scribe.newLine();
statement.accept(visitor);
scribe.decrementIndentationLevel();
- scribe.newLine();
}
}
-
+
/**
* Write no new Line after the next Compound-Statement
*/
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/TemplateParameterWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/TemplateParameterWriter.java
index f1265c5bbd..64d1167957 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/TemplateParameterWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/TemplateParameterWriter.java
@@ -11,7 +11,6 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
-import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeTemplateParameter;
@@ -35,7 +34,7 @@ public class TemplateParameterWriter extends NodeWriter {
* @param scribe
* @param visitor
*/
- public TemplateParameterWriter(Scribe scribe, ASTVisitor visitor, NodeCommentMap commentMap) {
+ public TemplateParameterWriter(Scribe scribe, ASTWriterVisitor visitor, NodeCommentMap commentMap) {
super(scribe, visitor, commentMap);
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ASTModificationHelper.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ASTModificationHelper.java
index df593865e1..74970f57e0 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ASTModificationHelper.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ASTModificationHelper.java
@@ -44,8 +44,7 @@ public class ASTModificationHelper {
T appendedTNode = cast(newNode, clazz);
if (appendedTNode != null) {
modifiedChildren.add(appendedTNode);
- }
- else if (newNode instanceof ContainerNode) {
+ } else if (newNode instanceof ContainerNode) {
ContainerNode nodeContainer = (ContainerNode) newNode;
for (IASTNode currentNode : nodeContainer.getNodes()) {
T tnode= cast(currentNode, clazz);
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java
index eda395e181..d8d2b173c0 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java
@@ -9,16 +9,23 @@
* Contributors:
* Institute for Software - initial API and implementation
* Markus Schorn (Wind River Systems)
+ * Sergey Prigogin (Google)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Map;
+import org.eclipse.cdt.core.CCorePlugin;
+import org.eclipse.cdt.core.ToolFactory;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.IASTComment;
+import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
+import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
@@ -31,7 +38,10 @@ import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
+import org.eclipse.cdt.core.formatter.CodeFormatter;
+import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification;
+import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationMap;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTRewriteAnalyzer;
@@ -41,16 +51,26 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
import org.eclipse.cdt.internal.core.dom.rewrite.util.FileContentHelper;
import org.eclipse.cdt.internal.core.dom.rewrite.util.FileHelper;
import org.eclipse.cdt.internal.core.resources.ResourceLookup;
+import org.eclipse.cdt.internal.formatter.CCodeFormatter;
import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.Document;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.TextUtilities;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.TextFileChange;
import org.eclipse.text.edits.DeleteEdit;
import org.eclipse.text.edits.InsertEdit;
+import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.ReplaceEdit;
+import org.eclipse.text.edits.TextEdit;
import org.eclipse.text.edits.TextEditGroup;
public class ChangeGenerator extends ASTVisitor {
@@ -93,8 +113,10 @@ public class ChangeGenerator extends ASTVisitor {
initParentModList();
rootNode.accept(pathProvider);
for (IFile currentFile : changes.keySet()) {
+ MultiTextEdit edit = changes.get(currentFile);
+ edit = formatChangedCode(edit, currentFile);
TextFileChange subchange= ASTRewriteAnalyzer.createCTextFileChange(currentFile);
- subchange.setEdit(changes.get(currentFile));
+ subchange.setEdit(edit);
change.add(subchange);
}
}
@@ -130,6 +152,63 @@ public class ChangeGenerator extends ASTVisitor {
return modifiedNodeParent;
}
+ /**
+ * Applies the C++ code formatter to the code affected by refactoring.
+ *
+ * @param edit The text edit produced by refactoring.
+ * @param file The file being modified.
+ * @return The text edit containing formatted refactoring changes, or the original text edit
+ * in case of errors.
+ */
+ private MultiTextEdit formatChangedCode(MultiTextEdit edit, IFile file) {
+ String code;
+ try {
+ code = FileContentHelper.getContent(file, 0);
+ } catch (IOException e) {
+ CCorePlugin.log(e);
+ return edit;
+ } catch (CoreException e) {
+ CCorePlugin.log(e);
+ return edit;
+ }
+ IDocument document = new Document(code);
+ try {
+ TextEdit tempEdit = edit.copy();
+ tempEdit.apply(document, TextEdit.UPDATE_REGIONS);
+ TextEdit[] edits = tempEdit.getChildren();
+ IRegion[] regions = new IRegion[edits.length];
+ for (int i = 0; i < edits.length; i++) {
+ regions[i] = edits[i].getRegion();
+ }
+ ICProject project = CCorePlugin.getDefault().getCoreModel().create(file.getProject());
+ Map<String, String> options = project.getOptions(true);
+ CodeFormatter formatter = ToolFactory.createCodeFormatter(options);
+ code = document.get();
+ TextEdit[] formatEdits = formatter.format(CCodeFormatter.K_TRANSLATION_UNIT, code,
+ regions, TextUtilities.getDefaultLineDelimiter(document));
+ MultiTextEdit resultEdit = new MultiTextEdit();
+ edits = edit.getChildren();
+ for (int i = 0; i < edits.length; i++) {
+ IRegion region = regions[i];
+ int offset = region.getOffset();
+ TextEdit formatEdit = formatEdits[i];
+ formatEdit.moveTree(-offset);
+ document = new Document(code.substring(offset, offset + region.getLength()));
+ formatEdit.apply(document, TextEdit.NONE);
+ TextEdit textEdit = edits[i];
+ resultEdit.addChild(
+ new ReplaceEdit(textEdit.getOffset(), textEdit.getLength(), document.get()));
+ }
+ return resultEdit;
+ } catch (MalformedTreeException e) {
+ CCorePlugin.log(e);
+ return edit;
+ } catch (BadLocationException e) {
+ CCorePlugin.log(e);
+ return edit;
+ }
+ }
+
@Override
public int visit(IASTTranslationUnit translationUnit) {
if (hasChangedChild(translationUnit)) {
@@ -145,11 +224,6 @@ public class ChangeGenerator extends ASTVisitor {
return super.leave(tu);
}
- private int getOffsetForNodeFile(IASTNode rootNode) {
- Integer offset = sourceOffsets.get(rootNode.getFileLocation().getFileName());
- return offset == null ? 0 : offset.intValue();
- }
-
@Override
public int visit(IASTDeclaration declaration) {
if (hasChangedChild(declaration)) {
@@ -160,21 +234,113 @@ public class ChangeGenerator extends ASTVisitor {
}
private void synthTreatment(IASTNode synthNode) {
- synthTreatment(synthNode, null);
+ ChangeGeneratorWriterVisitor writer =
+ new ChangeGeneratorWriterVisitor(modificationStore, commentMap);
+ synthNode.accept(writer);
+ String synthSource = writer.toString();
+ createChange(synthNode, synthSource);
+
+ IASTFileLocation fileLocation = synthNode.getFileLocation();
+ int newOffset = fileLocation.getNodeOffset() + fileLocation.getNodeLength();
+ sourceOffsets.put(fileLocation.getFileName(), Integer.valueOf(newOffset));
}
- private void synthTreatment(IASTNode synthNode, String fileScope) {
- String indent = getIndent(synthNode);
- ASTWriter synthWriter = new ASTWriter(indent);
- synthWriter.setModificationStore(modificationStore);
+ private void handleAppends(IASTNode node) {
+ ChangeGeneratorWriterVisitor writer =
+ new ChangeGeneratorWriterVisitor(modificationStore, commentMap);
+ List<ASTModification> modifications = modificationParent.get(node);
+ ReplaceEdit anchor = getAppendAnchor(node);
+ Assert.isNotNull(anchor);
+ IASTNode precedingNode = getLastNodeBeforeAppendPoint(node);
+ if (precedingNode != null &&
+ ASTWriter.requireBlankLineInBetween(precedingNode, modifications.get(0).getNewNode())) {
+ writer.newLine();
+ }
+ for (ASTModification modification : modifications) {
+ IASTNode newNode = modification.getNewNode();
+ newNode.accept(writer);
+ }
+ String code = writer.toString();
+ IFile file = FileHelper.getFileFromNode(node);
+ MultiTextEdit parentEdit = getEdit(node, file);
+ ReplaceEdit edit = new ReplaceEdit(anchor.getOffset(), anchor.getLength(),
+ code + anchor.getText());
+ parentEdit.addChild(edit);
+ IASTFileLocation fileLocation = node.getFileLocation();
+ int newOffset = fileLocation.getNodeOffset() + fileLocation.getNodeLength();
+ sourceOffsets.put(fileLocation.getFileName(), Integer.valueOf(newOffset));
+ }
- String synthSource = synthWriter.write(synthNode, fileScope, commentMap);
+ private IASTNode getLastNodeBeforeAppendPoint(IASTNode node) {
+ IASTNode[] children;
+ if (node instanceof IASTCompositeTypeSpecifier) {
+ children = ((IASTCompositeTypeSpecifier) node).getDeclarations(true);
+ } else {
+ children = node.getChildren();
+ }
+ return children.length > 0 ? children[children.length - 1] : null;
+ }
- createChange(synthNode, synthSource);
+ private boolean isAppendable(Iterable<ASTModification> modifications) {
+ for (ASTModification modification : modifications) {
+ if (!isAppendable(modification))
+ return false;
+ }
+ return true;
+ }
- IASTFileLocation fileLocation = synthNode.getFileLocation();
- int newOffset = fileLocation.getNodeOffset() + fileLocation.getNodeLength();
- sourceOffsets.put(fileLocation.getFileName(), Integer.valueOf(newOffset));
+ private boolean isAppendable(ASTModification modification) {
+ if (modification.getKind() != ModificationKind.APPEND_CHILD)
+ return false;
+ IASTNode node = modification.getNewNode();
+ return node instanceof IASTDeclaration || node instanceof IASTStatement;
+ }
+
+ /**
+ * Returns a replace edit whose offset is the position where child appended nodes should be
+ * inserted at. The text contains the content of the code region that will be disturbed by
+ * the insertion.
+ * @param node The node to append children to.
+ * @return a ReplaceEdit object, or <code>null</code> if the node does not support appending
+ * children to it.
+ */
+ private ReplaceEdit getAppendAnchor(IASTNode node) {
+ if (!(node instanceof IASTCompositeTypeSpecifier ||
+ node instanceof IASTCompoundStatement ||
+ node instanceof ICPPASTNamespaceDefinition)) {
+ return null;
+ }
+ IFile file = FileHelper.getFileFromNode(node);
+ String code = originalCodeOfNode(node, file);
+ IASTFileLocation location = node.getFileLocation();
+ int pos = location.getNodeOffset() + location.getNodeLength();
+ int len = code.endsWith("}") ? 1 : 0; //$NON-NLS-1$
+ int startOfLine = skipPrecedingBlankLines(code, code.length() - len);
+ if (startOfLine < 0) {
+ // Include the closing brace in the region that will be reformatted.
+ return new ReplaceEdit(pos - len, len, code.substring(code.length() - len));
+ }
+ return new ReplaceEdit(location.getNodeOffset() + startOfLine, 0, ""); //$NON-NLS-1$
+ }
+
+ /**
+ * Skips blank lines preceding the given position.
+ * @param text the text to scan
+ * @param pos the position after that blank lines.
+ * @return the beginning of the first blank line, or -1 if the beginning of the line
+ * corresponding to the given position contains non-whitespace characters.
+ */
+ private int skipPrecedingBlankLines(String text, int pos) {
+ int lineStart = -1;
+ while (--pos >= 0) {
+ char c = text.charAt(pos);
+ if (c == '\n') {
+ lineStart = pos + 1;
+ } else if (!Character.isWhitespace(c)) {
+ break;
+ }
+ }
+ return lineStart;
}
private void synthTreatment(IASTTranslationUnit synthTU) {
@@ -182,11 +348,12 @@ public class ChangeGenerator extends ASTVisitor {
synthWriter.setModificationStore(modificationStore);
for (ASTModification modification : modificationParent.get(synthTU)) {
- IASTFileLocation targetLocation = modification.getTargetNode().getFileLocation();
+ IASTNode targetNode = modification.getTargetNode();
+ IASTFileLocation targetLocation = targetNode.getFileLocation();
String currentFile = targetLocation.getFileName();
IPath implPath = new Path(currentFile);
IFile relevantFile= ResourceLookup.selectFileForLocation(implPath, null);
- if (relevantFile == null || !relevantFile.exists()) { // if not in workspace or local file system
+ if (relevantFile == null || !relevantFile.exists()) { // If not in workspace or local file system
throw new UnhandledASTModificationException(modification);
}
MultiTextEdit edit;
@@ -196,7 +363,7 @@ public class ChangeGenerator extends ASTVisitor {
edit = new MultiTextEdit();
changes.put(relevantFile, edit);
}
- String newNodeCode = synthWriter.write(modification.getNewNode(), null, commentMap);
+ String newNodeCode = synthWriter.write(modification.getNewNode(), commentMap);
switch (modification.getKind()) {
case REPLACE:
@@ -204,18 +371,20 @@ public class ChangeGenerator extends ASTVisitor {
targetLocation.getNodeLength(), newNodeCode));
break;
case INSERT_BEFORE:
- edit.addChild(new InsertEdit(getOffsetIncludingComments(modification.getTargetNode()),
- newNodeCode));
+ if (ASTWriter.requireBlankLineInBetween(modification.getNewNode(), targetNode)) {
+ newNodeCode = newNodeCode + "\n"; //$NON-NLS-1$
+ }
+ edit.addChild(new InsertEdit(getOffsetIncludingComments(targetNode), newNodeCode));
break;
case APPEND_CHILD:
- if (modification.getTargetNode() instanceof IASTTranslationUnit &&
- ((IASTTranslationUnit)modification.getTargetNode()).getDeclarations().length > 0) {
- IASTTranslationUnit tu = (IASTTranslationUnit)modification.getTargetNode();
+ if (targetNode instanceof IASTTranslationUnit &&
+ ((IASTTranslationUnit) targetNode).getDeclarations().length > 0) {
+ IASTTranslationUnit tu = (IASTTranslationUnit) targetNode;
IASTDeclaration lastDecl = tu.getDeclarations()[tu.getDeclarations().length - 1];
targetLocation = lastDecl.getFileLocation();
}
String lineDelimiter = FileHelper.determineLineDelimiter(
- FileHelper.getFileFromNode(modification.getTargetNode()));
+ FileHelper.getFileFromNode(targetNode));
edit.addChild(new InsertEdit(targetLocation.getNodeOffset() + targetLocation.getNodeLength(),
lineDelimiter + lineDelimiter + newNodeCode));
break;
@@ -225,30 +394,32 @@ public class ChangeGenerator extends ASTVisitor {
private void createChange(IASTNode synthNode, String synthSource) {
IFile relevantFile = FileHelper.getFileFromNode(synthNode);
-
- String originalCode = originalCodeOfNode(synthNode);
+ String originalCode = originalCodeOfNode(synthNode, relevantFile);
CodeComparer codeComparer = new CodeComparer(originalCode, synthSource);
+ codeComparer.createChange(getEdit(synthNode, relevantFile), synthNode);
+ }
- MultiTextEdit edit;
- if (changes.containsKey(relevantFile)) {
- edit = changes.get(relevantFile);
- } else {
+ private MultiTextEdit getEdit(IASTNode modifiedNode, IFile file) {
+ MultiTextEdit edit = changes.get(file);
+ if (edit == null) {
edit = new MultiTextEdit();
- changes.put(relevantFile, edit);
+ changes.put(file, edit);
}
-
- codeComparer.createChange(edit, synthNode);
+ TextEditGroup editGroup = new TextEditGroup(Messages.ChangeGenerator_group);
+ for (ASTModification currentModification : modificationParent.get(modifiedNode)) {
+ if (currentModification.getAssociatedEditGroup() != null) {
+ editGroup = currentModification.getAssociatedEditGroup();
+ edit.addChildren(editGroup.getTextEdits());
+ break;
+ }
+ }
+ return edit;
}
- public String originalCodeOfNode(IASTNode node) {
- if (node.getFileLocation() != null) {
- IFile sourceFile = FileHelper.getFileFromNode(node);
- int nodeOffset = getOffsetIncludingComments(node);
- int nodeLength = getNodeLengthIncludingComments(node);
-
- return FileContentHelper.getContent(sourceFile, nodeOffset, nodeLength);
- }
- return null;
+ private String originalCodeOfNode(IASTNode node, IFile sourceFile) {
+ int nodeOffset = getOffsetIncludingComments(node);
+ int nodeLength = getNodeLengthIncludingComments(node);
+ return FileContentHelper.getContent(sourceFile, nodeOffset, nodeLength);
}
private int getNodeLengthIncludingComments(IASTNode node) {
@@ -290,23 +461,15 @@ public class ChangeGenerator extends ASTVisitor {
return nodeOffset;
}
- private String getIndent(IASTNode nextNode) {
- IASTFileLocation fileLocation = nextNode.getFileLocation();
- int length = fileLocation.getNodeOffset() - getOffsetForNodeFile(nextNode);
-
- String originalSource = FileContentHelper.getContent(FileHelper.getFileFromNode(nextNode),
- getOffsetForNodeFile(nextNode), length);
- StringBuilder indent = new StringBuilder(originalSource);
- indent.reverse();
- String lastline = indent.substring(0, Math.max(indent.indexOf("\n"), 0)); //$NON-NLS-1$
- if (lastline.trim().length() == 0) {
- return lastline;
- }
- return ""; //$NON-NLS-1$
+ private boolean hasChangedChild(IASTNode node) {
+ return modificationParent.containsKey(node);
}
- private boolean hasChangedChild(IASTNode parent) {
- return modificationParent.containsKey(parent);
+ private boolean hasAppendsOnly(IASTNode node) {
+ List<ASTModification> modifications = modificationParent.get(node);
+ if (modifications == null)
+ return false;
+ return isAppendable(modifications);
}
@Override
@@ -329,7 +492,7 @@ public class ChangeGenerator extends ASTVisitor {
@Override
public int visit(ICPPASTNamespaceDefinition namespaceDefinition) {
- if (hasChangedChild(namespaceDefinition)) {
+ if (hasChangedChild(namespaceDefinition) && !hasAppendsOnly(namespaceDefinition)) {
synthTreatment(namespaceDefinition);
return ASTVisitor.PROCESS_SKIP;
}
@@ -337,8 +500,16 @@ public class ChangeGenerator extends ASTVisitor {
}
@Override
+ public int leave(ICPPASTNamespaceDefinition namespaceDefinition) {
+ if (hasAppendsOnly(namespaceDefinition)) {
+ handleAppends(namespaceDefinition);
+ }
+ return super.leave(namespaceDefinition);
+ }
+
+ @Override
public int visit(IASTDeclSpecifier declSpec) {
- if (hasChangedChild(declSpec)) {
+ if (hasChangedChild(declSpec) && !hasAppendsOnly(declSpec)) {
synthTreatment(declSpec);
return ASTVisitor.PROCESS_SKIP;
}
@@ -346,6 +517,14 @@ public class ChangeGenerator extends ASTVisitor {
}
@Override
+ public int leave(IASTDeclSpecifier declSpec) {
+ if (hasAppendsOnly(declSpec)) {
+ handleAppends(declSpec);
+ }
+ return super.leave(declSpec);
+ }
+
+ @Override
public int visit(IASTExpression expression) {
if (hasChangedChild(expression)) {
synthTreatment(expression);
@@ -383,13 +562,21 @@ public class ChangeGenerator extends ASTVisitor {
@Override
public int visit(IASTStatement statement) {
- if (hasChangedChild(statement)) {
+ if (hasChangedChild(statement) && !hasAppendsOnly(statement)) {
synthTreatment(statement);
return ASTVisitor.PROCESS_SKIP;
}
return super.visit(statement);
}
+ @Override
+ public int leave(IASTStatement statement) {
+ if (hasAppendsOnly(statement)) {
+ handleAppends(statement);
+ }
+ return super.leave(statement);
+ }
+
class CodeComparer {
private final StringBuilder originalCode;
private final StringBuilder synthCode;
@@ -536,14 +723,6 @@ public class ChangeGenerator extends ASTVisitor {
protected void createChange(MultiTextEdit edit, IASTNode changedNode) {
int changeOffset = getOffsetIncludingComments(changedNode);
- TextEditGroup editGroup = new TextEditGroup(Messages.ChangeGenerator_group);
- for (ASTModification currentModification : modificationParent.get(changedNode)) {
- if (currentModification.getAssociatedEditGroup() != null) {
- editGroup = currentModification.getAssociatedEditGroup();
- edit.addChildren(editGroup.getTextEdits());
- break;
- }
- }
createChange(edit, changeOffset);
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclSpecWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclSpecWriter.java
index e6de9cb327..cd59141900 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclSpecWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclSpecWriter.java
@@ -11,9 +11,9 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;
-import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
+import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriterVisitor;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.DeclSpecWriter;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.Scribe;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
@@ -21,8 +21,8 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
public class ModifiedASTDeclSpecWriter extends DeclSpecWriter {
private final ASTModificationHelper modificationHelper;
- public ModifiedASTDeclSpecWriter(Scribe scribe, ASTVisitor visitor, ModificationScopeStack stack,
- NodeCommentMap commentMap) {
+ public ModifiedASTDeclSpecWriter(Scribe scribe, ASTWriterVisitor visitor,
+ ModificationScopeStack stack, NodeCommentMap commentMap) {
super(scribe, visitor, commentMap);
this.modificationHelper = new ASTModificationHelper(stack);
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclarationWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclarationWriter.java
index 59e3953fad..0ea61f3aac 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclarationWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclarationWriter.java
@@ -12,11 +12,11 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;
-import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorChainInitializer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
+import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriterVisitor;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.DeclarationWriter;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.Scribe;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
@@ -24,21 +24,26 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
public class ModifiedASTDeclarationWriter extends DeclarationWriter {
private final ASTModificationHelper modificationHelper;
- public ModifiedASTDeclarationWriter(Scribe scribe, ASTVisitor visitor, ModificationScopeStack stack, NodeCommentMap commentMap) {
+ public ModifiedASTDeclarationWriter(Scribe scribe, ASTWriterVisitor visitor,
+ ModificationScopeStack stack, NodeCommentMap commentMap) {
super(scribe, visitor, commentMap);
this.modificationHelper = new ASTModificationHelper(stack);
}
@Override
- protected void writeDeclarationsInNamespace(ICPPASTNamespaceDefinition namespaceDefinition, IASTDeclaration[] declarations) {
- IASTDeclaration[] modifiedDeclarations = modificationHelper.createModifiedChildArray(namespaceDefinition, declarations, IASTDeclaration.class, commentMap);
+ protected void writeDeclarationsInNamespace(ICPPASTNamespaceDefinition namespaceDefinition,
+ IASTDeclaration[] declarations) {
+ IASTDeclaration[] modifiedDeclarations = modificationHelper.createModifiedChildArray(
+ namespaceDefinition, declarations, IASTDeclaration.class, commentMap);
super.writeDeclarationsInNamespace(namespaceDefinition, modifiedDeclarations);
}
@Override
protected void writeCtorChainInitializer(ICPPASTFunctionDefinition funcDec,
ICPPASTConstructorChainInitializer[] ctorInitChain) {
- ICPPASTConstructorChainInitializer[] modifiedChainInitializer = modificationHelper.createModifiedChildArray(funcDec, ctorInitChain, ICPPASTConstructorChainInitializer.class, commentMap);
- super.writeCtorChainInitializer(funcDec, modifiedChainInitializer);
+ ICPPASTConstructorChainInitializer[] modifiedInitializer =
+ modificationHelper.createModifiedChildArray(funcDec, ctorInitChain,
+ ICPPASTConstructorChainInitializer.class, commentMap);
+ super.writeCtorChainInitializer(funcDec, modifiedInitializer);
}
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclaratorWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclaratorWriter.java
index cb764ab7ed..b5d416a772 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclaratorWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclaratorWriter.java
@@ -12,7 +12,6 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;
-import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
@@ -25,6 +24,7 @@ import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
+import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriterVisitor;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.DeclaratorWriter;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.Scribe;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
@@ -32,7 +32,7 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
public class ModifiedASTDeclaratorWriter extends DeclaratorWriter {
private final ASTModificationHelper modificationHelper;
- public ModifiedASTDeclaratorWriter(Scribe scribe, ASTVisitor visitor, ModificationScopeStack stack, NodeCommentMap commentMap) {
+ public ModifiedASTDeclaratorWriter(Scribe scribe, ASTWriterVisitor visitor, ModificationScopeStack stack, NodeCommentMap commentMap) {
super(scribe, visitor, commentMap);
this.modificationHelper = new ASTModificationHelper(stack);
}
@@ -40,29 +40,31 @@ public class ModifiedASTDeclaratorWriter extends DeclaratorWriter {
@Override
protected void writeParameterDeclarations(IASTStandardFunctionDeclarator funcDec,
IASTParameterDeclaration[] paraDecls) {
- IASTParameterDeclaration[] modifiedParameters = modificationHelper
- .createModifiedChildArray(funcDec, paraDecls, IASTParameterDeclaration.class,
- commentMap);
+ IASTParameterDeclaration[] modifiedParameters = modificationHelper.createModifiedChildArray(
+ funcDec, paraDecls, IASTParameterDeclaration.class, commentMap);
super.writeParameterDeclarations(funcDec, modifiedParameters);
}
@Override
- protected void writePointerOperators(IASTDeclarator declarator,IASTPointerOperator[] unmodifiedPointerOperations) {
+ protected void writePointerOperators(IASTDeclarator declarator,
+ IASTPointerOperator[] unmodifiedPointerOperations) {
IASTPointerOperator[] modifiedPointer = modificationHelper.createModifiedChildArray(
declarator, unmodifiedPointerOperations, IASTPointerOperator.class, commentMap);
super.writePointerOperators(declarator, modifiedPointer);
}
@Override
- protected void writeArrayModifiers(IASTArrayDeclarator arrDecl,
- IASTArrayModifier[] arrMods) {
- IASTArrayModifier[] modifiedModifiers = modificationHelper.createModifiedChildArray(arrDecl, arrMods, IASTArrayModifier.class, commentMap);
+ protected void writeArrayModifiers(IASTArrayDeclarator arrDecl, IASTArrayModifier[] arrMods) {
+ IASTArrayModifier[] modifiedModifiers = modificationHelper.createModifiedChildArray(arrDecl,
+ arrMods, IASTArrayModifier.class, commentMap);
super.writeArrayModifiers(arrDecl, modifiedModifiers);
}
@Override
- protected void writeExceptionSpecification(ICPPASTFunctionDeclarator funcDec, IASTTypeId[] exceptions ) {
- IASTTypeId[] modifiedExceptions = modificationHelper.createModifiedChildArray(funcDec, exceptions, IASTTypeId.class, commentMap);
+ protected void writeExceptionSpecification(ICPPASTFunctionDeclarator funcDec,
+ IASTTypeId[] exceptions) {
+ IASTTypeId[] modifiedExceptions = modificationHelper.createModifiedChildArray(funcDec,
+ exceptions, IASTTypeId.class, commentMap);
// it makes a difference whether the exception array is identical to
// ICPPASTFunctionDeclarator.NO_EXCEPTION_SPECIFICATION
if (modifiedExceptions.length == 0 &&
@@ -74,18 +76,18 @@ public class ModifiedASTDeclaratorWriter extends DeclaratorWriter {
}
@Override
- protected void writeKnRParameterDeclarations(
- ICASTKnRFunctionDeclarator knrFunct,
+ protected void writeKnRParameterDeclarations(ICASTKnRFunctionDeclarator knrFunct,
IASTDeclaration[] knrDeclarations) {
- IASTDeclaration[] modifiedDeclarations = modificationHelper.createModifiedChildArray(knrFunct, knrDeclarations, IASTDeclaration.class, commentMap);
-
+ IASTDeclaration[] modifiedDeclarations = modificationHelper.createModifiedChildArray(
+ knrFunct, knrDeclarations, IASTDeclaration.class, commentMap);
super.writeKnRParameterDeclarations(knrFunct, modifiedDeclarations);
}
@Override
- protected void writeKnRParameterNames(
- ICASTKnRFunctionDeclarator knrFunct, IASTName[] parameterNames) {
- IASTName[] modifiedNames = modificationHelper.createModifiedChildArray(knrFunct, parameterNames, IASTName.class, commentMap);
+ protected void writeKnRParameterNames(ICASTKnRFunctionDeclarator knrFunct,
+ IASTName[] parameterNames) {
+ IASTName[] modifiedNames = modificationHelper.createModifiedChildArray(knrFunct,
+ parameterNames, IASTName.class, commentMap);
super.writeKnRParameterNames(knrFunct, modifiedNames);
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTExpressionWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTExpressionWriter.java
index 08262e57d2..609182ec19 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTExpressionWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTExpressionWriter.java
@@ -12,7 +12,6 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;
-import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionList;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
@@ -20,6 +19,7 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind;
+import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriterVisitor;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ExpressionWriter;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.MacroExpansionHandler;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.Scribe;
@@ -28,7 +28,7 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
public class ModifiedASTExpressionWriter extends ExpressionWriter {
private final ASTModificationHelper modificationHelper;
- public ModifiedASTExpressionWriter(Scribe scribe, ASTVisitor visitor, MacroExpansionHandler macroHandler,
+ public ModifiedASTExpressionWriter(Scribe scribe, ASTWriterVisitor visitor, MacroExpansionHandler macroHandler,
ModificationScopeStack stack, NodeCommentMap commentMap) {
super(scribe, visitor, macroHandler, commentMap);
this.modificationHelper = new ASTModificationHelper(stack);
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTStatementWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTStatementWriter.java
index 0f5f049194..9977fbbf7e 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTStatementWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTStatementWriter.java
@@ -11,10 +11,10 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;
-import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
+import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriterVisitor;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.Scribe;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.StatementWriter;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
@@ -22,7 +22,7 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
public class ModifiedASTStatementWriter extends StatementWriter {
private final ASTModificationHelper modificationHelper;
- public ModifiedASTStatementWriter(Scribe scribe, ASTVisitor visitor, ModificationScopeStack stack, NodeCommentMap commentMap) {
+ public ModifiedASTStatementWriter(Scribe scribe, ASTWriterVisitor visitor, ModificationScopeStack stack, NodeCommentMap commentMap) {
super(scribe, visitor, commentMap);
this.modificationHelper = new ASTModificationHelper(stack);
}

Back to the top