Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergey Prigogin2011-12-29 21:15:35 -0500
committerSergey Prigogin2011-12-29 21:15:35 -0500
commite3bdead668b2b8094636f0e678bb724cea0ae5a9 (patch)
treefddc8c220b37be0d99b33aed24c9d501e1526f53 /core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite
parentda95eb26058862551b0f93275ec39c9e0f16a420 (diff)
downloadorg.eclipse.cdt-e3bdead668b2b8094636f0e678bb724cea0ae5a9.tar.gz
org.eclipse.cdt-e3bdead668b2b8094636f0e678bb724cea0ae5a9.tar.xz
org.eclipse.cdt-e3bdead668b2b8094636f0e678bb724cea0ae5a9.zip
Improved handling of blank lines when appending to a translation unit.
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.java20
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java1
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java52
3 files changed, 57 insertions, 16 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 dc19453bfb..83dd18fdf4 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
@@ -12,6 +12,8 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
+import java.util.List;
+
import org.eclipse.cdt.core.dom.ast.IASTASMDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
@@ -144,6 +146,24 @@ public class ASTWriter {
* @return <code>true</code> if the blank line between the nodes is needed.
*/
public static boolean requireBlankLineInBetween(IASTNode node1, IASTNode node2) {
+ if (node1 instanceof ContainerNode) {
+ List<IASTNode> nodes = ((ContainerNode) node1).getNodes();
+ if (!nodes.isEmpty()) {
+ node1 = nodes.get(nodes.size() - 1);
+ }
+ }
+ if (node2 instanceof ContainerNode) {
+ List<IASTNode> nodes = ((ContainerNode) node2).getNodes();
+ if (!nodes.isEmpty()) {
+ node2 = nodes.get(0);
+ }
+ }
+ while (node1 instanceof ICPPASTTemplateDeclaration) {
+ node1 = ((ICPPASTTemplateDeclaration) node1).getDeclaration();
+ }
+ while (node2 instanceof ICPPASTTemplateDeclaration) {
+ node2 = ((ICPPASTTemplateDeclaration) node2).getDeclaration();
+ }
if (node1 instanceof ICPPASTVisibilityLabel && node2 instanceof ICPPASTVisibilityLabel) {
return true;
}
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 4ff5321d55..cd1e5c363d 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
@@ -132,6 +132,7 @@ public class ASTWriterVisitor extends ASTVisitor {
}
public void visit(ASTLiteralNode lit) {
+ insertBlankLineIfNeeded(lit);
scribe.print(lit.getRawSignature());
}
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 cb3d3f9df9..db3ed82139 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
@@ -75,7 +75,6 @@ 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.InsertEdit;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.ReplaceEdit;
@@ -559,26 +558,47 @@ public class ChangeGenerator extends ASTVisitor {
if (modifications.isEmpty())
return;
- ASTWriter synthWriter = new ASTWriter();
- synthWriter.setModificationStore(modificationStore);
-
- IASTFileLocation targetLocation = tu.getFileLocation();
- IFile file = FileHelper.getFileFromNode(tu);
- MultiTextEdit parentEdit = getEdit(tu, file);
-
+ IASTNode prevNode = null;
IASTDeclaration[] declarations = tu.getDeclarations();
+ if (declarations.length != 0) {
+ prevNode = declarations[declarations.length - 1];
+ } else {
+ IASTPreprocessorStatement[] preprocessorStatements = tu.getAllPreprocessorStatements();
+ if (preprocessorStatements.length != 0) {
+ prevNode = preprocessorStatements[preprocessorStatements.length - 1];
+ }
+ }
+ int offset = prevNode != null ? getEndOffsetIncludingComments(prevNode) : 0;
+ String source = tu.getRawSignature();
+ int endOffset = skipTrailingBlankLines(source, offset);
+ ChangeGeneratorWriterVisitor writer =
+ new ChangeGeneratorWriterVisitor(modificationStore, commentMap);
+ IASTNode newNode = null;
for (ASTModification modification : modifications) {
- String code = synthWriter.write(modification.getNewNode(), commentMap);
-
- if (declarations.length > 0) {
- IASTDeclaration lastDecl = declarations[declarations.length - 1];
- targetLocation = lastDecl.getFileLocation();
+ boolean first = newNode == null;
+ newNode = modification.getNewNode();
+ if (first) {
+ if (prevNode != null) {
+ writer.newLine();
+ if (ASTWriter.requireBlankLineInBetween(prevNode, newNode)) {
+ writer.newLine();
+ }
+ }
+ }
+ newNode.accept(writer);
+ }
+ if (prevNode != null) {
+ IASTNode nextNode = getNextSiblingOrPreprocessorNode(prevNode);
+ if (nextNode != null && ASTWriter.requireBlankLineInBetween(newNode, nextNode)) {
+ writer.newLine();
}
- String lineDelimiter = FileHelper.determineLineDelimiter(tu.getRawSignature());
- parentEdit.addChild(new InsertEdit(endOffset(targetLocation),
- lineDelimiter + lineDelimiter + code));
}
+
+ String code = writer.toString();
+ IFile file = FileHelper.getFileFromNode(tu);
+ MultiTextEdit parentEdit = getEdit(tu, file);
+ parentEdit.addChild(new ReplaceEdit(offset, endOffset - offset, code));
}
/**

Back to the top