Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Stornelli2019-03-16 04:12:21 -0400
committerMarco Stornelli2019-05-12 08:54:29 -0400
commit826a0b2c0effc6a8d1479ddbff8d5b4bbd31a6b6 (patch)
tree55fe9ea10f8f5b15193c25fad9d93794706802b0 /core/org.eclipse.cdt.core
parent695de049db539bb34b4c4e0a3a394e7d9757db8c (diff)
downloadorg.eclipse.cdt-826a0b2c0effc6a8d1479ddbff8d5b4bbd31a6b6.tar.gz
org.eclipse.cdt-826a0b2c0effc6a8d1479ddbff8d5b4bbd31a6b6.tar.xz
org.eclipse.cdt-826a0b2c0effc6a8d1479ddbff8d5b4bbd31a6b6.zip
Bug 405409 - Formatting namespaces with inactive sections
Local scanner was peeking tokens from inactive regions confusing the formatter. Change-Id: I5975ec1042474be84b897e047a1e59cfc8bfa083 Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
Diffstat (limited to 'core/org.eclipse.cdt.core')
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java30
1 files changed, 27 insertions, 3 deletions
diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java
index 6fcac39eb3..ec20bed2b7 100644
--- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java
+++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java
@@ -19,6 +19,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EmptyStackException;
+import java.util.Iterator;
import java.util.List;
import java.util.Stack;
@@ -401,6 +402,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
}
private final Scanner localScanner;
+ private List<InactivePosition> fInactivePreprocessorPositions;
final DefaultCodeFormatterOptions preferences;
private final Scribe scribe;
@@ -424,6 +426,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
};
this.preferences = preferences;
scribe = new Scribe(this, offset, length);
+ fInactivePreprocessorPositions = Collections.emptyList();
}
/**
@@ -439,8 +442,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
localScanner.setSource(compilationUnitSource);
scribe.initializeScanner(compilationUnitSource);
- List<InactivePosition> inactive = collectInactiveCodePositions(unit);
- inactive.addAll(collectNoFormatCodePositions(unit));
+ fInactivePreprocessorPositions = collectInactiveCodePositions(unit);
+ List<InactivePosition> inactive = collectNoFormatCodePositions(unit);
+ inactive.addAll(fInactivePreprocessorPositions);
scribe.setSkipInactivePositions(inactive);
fStatus = new MultiStatus(CCorePlugin.PLUGIN_ID, 0, "Formatting problem(s) in '" + unit.getFilePath() + "'", //$NON-NLS-1$//$NON-NLS-2$
@@ -4502,14 +4506,34 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
return peekNextToken(false);
}
+ /**
+ * It returns a position range if offset is included into an inactive
+ * preprocessor region.
+ * @param offset The offset to be checked
+ * @return The region if found, null otherwise
+ */
+ private Position getInactivePosAt(int offset) {
+ for (Iterator<InactivePosition> iter = fInactivePreprocessorPositions.iterator(); iter.hasNext();) {
+ Position pos = iter.next();
+ if (pos.includes(offset)) {
+ return pos;
+ }
+ }
+ return null;
+ }
+
private int peekNextToken(boolean ignoreSkip) {
if (!ignoreSkip && scribe.shouldSkip(getCurrentPosition())) {
return Token.tBADCHAR;
}
localScanner.resetTo(getCurrentPosition(), scribe.scannerEndPosition);
int token = localScanner.getNextToken();
- while (token == Token.tBLOCKCOMMENT || token == Token.tLINECOMMENT) {
+ int currentStart = localScanner.getCurrentTokenStartPosition();
+ Position p = getInactivePosAt(currentStart);
+ while ((token == Token.tBLOCKCOMMENT || token == Token.tLINECOMMENT) || p != null) {
token = localScanner.getNextToken();
+ currentStart = localScanner.getCurrentTokenStartPosition();
+ p = getInactivePosAt(currentStart);
}
return token;
}

Back to the top