mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-06 15:55:47 +02:00
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>
This commit is contained in:
parent
695de049db
commit
826a0b2c0e
2 changed files with 56 additions and 3 deletions
|
@ -19,6 +19,7 @@ import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.EmptyStackException;
|
import java.util.EmptyStackException;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
|
||||||
|
@ -401,6 +402,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
}
|
}
|
||||||
|
|
||||||
private final Scanner localScanner;
|
private final Scanner localScanner;
|
||||||
|
private List<InactivePosition> fInactivePreprocessorPositions;
|
||||||
final DefaultCodeFormatterOptions preferences;
|
final DefaultCodeFormatterOptions preferences;
|
||||||
private final Scribe scribe;
|
private final Scribe scribe;
|
||||||
|
|
||||||
|
@ -424,6 +426,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
};
|
};
|
||||||
this.preferences = preferences;
|
this.preferences = preferences;
|
||||||
scribe = new Scribe(this, offset, length);
|
scribe = new Scribe(this, offset, length);
|
||||||
|
fInactivePreprocessorPositions = Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -439,8 +442,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
|
|
||||||
localScanner.setSource(compilationUnitSource);
|
localScanner.setSource(compilationUnitSource);
|
||||||
scribe.initializeScanner(compilationUnitSource);
|
scribe.initializeScanner(compilationUnitSource);
|
||||||
List<InactivePosition> inactive = collectInactiveCodePositions(unit);
|
fInactivePreprocessorPositions = collectInactiveCodePositions(unit);
|
||||||
inactive.addAll(collectNoFormatCodePositions(unit));
|
List<InactivePosition> inactive = collectNoFormatCodePositions(unit);
|
||||||
|
inactive.addAll(fInactivePreprocessorPositions);
|
||||||
scribe.setSkipInactivePositions(inactive);
|
scribe.setSkipInactivePositions(inactive);
|
||||||
|
|
||||||
fStatus = new MultiStatus(CCorePlugin.PLUGIN_ID, 0, "Formatting problem(s) in '" + unit.getFilePath() + "'", //$NON-NLS-1$//$NON-NLS-2$
|
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);
|
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) {
|
private int peekNextToken(boolean ignoreSkip) {
|
||||||
if (!ignoreSkip && scribe.shouldSkip(getCurrentPosition())) {
|
if (!ignoreSkip && scribe.shouldSkip(getCurrentPosition())) {
|
||||||
return Token.tBADCHAR;
|
return Token.tBADCHAR;
|
||||||
}
|
}
|
||||||
localScanner.resetTo(getCurrentPosition(), scribe.scannerEndPosition);
|
localScanner.resetTo(getCurrentPosition(), scribe.scannerEndPosition);
|
||||||
int token = localScanner.getNextToken();
|
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();
|
token = localScanner.getNextToken();
|
||||||
|
currentStart = localScanner.getCurrentTokenStartPosition();
|
||||||
|
p = getInactivePosAt(currentStart);
|
||||||
}
|
}
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4123,4 +4123,33 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
public void testCastFuncDeclarator2_Bug390324() throws Exception {
|
public void testCastFuncDeclarator2_Bug390324() throws Exception {
|
||||||
assertFormatterResult();
|
assertFormatterResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//#define ONE
|
||||||
|
//namespace n1 {
|
||||||
|
//namespace n2 {
|
||||||
|
//#ifdef ONE
|
||||||
|
//int v1;
|
||||||
|
//#elif defined(TWO)
|
||||||
|
//int v2;
|
||||||
|
//#endif
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
//int main(){return 0;}
|
||||||
|
|
||||||
|
//#define ONE
|
||||||
|
//namespace n1 {
|
||||||
|
//namespace n2 {
|
||||||
|
//#ifdef ONE
|
||||||
|
//int v1;
|
||||||
|
//#elif defined(TWO)
|
||||||
|
//int v2;
|
||||||
|
//#endif
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
//int main() {
|
||||||
|
// return 0;
|
||||||
|
//}
|
||||||
|
public void testNamespacesWithInactiveSections_Bug405049() throws Exception {
|
||||||
|
assertFormatterResult();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue