1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Follow-up for 217435

This commit is contained in:
Anton Leherbauer 2008-02-15 17:04:32 +00:00
parent bad35ab17a
commit 788ec3ad34
3 changed files with 72 additions and 78 deletions

View file

@ -146,22 +146,8 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
private static boolean DEBUG = "true".equalsIgnoreCase(Platform.getDebugOption("org.eclipse.cdt.core/debug/formatter")); //$NON-NLS-1$ //$NON-NLS-2$
private static class ASTProblemException extends RuntimeException {
private static final long serialVersionUID= 1L;
private IASTProblem fProblem;
ASTProblemException(IASTProblem problem) {
super();
fProblem= problem;
}
/*
* @see java.lang.Throwable#getMessage()
*/
public String getMessage() {
String message= fProblem.getMessage();
if (fProblem.getFileLocation() != null) {
int line= fProblem.getFileLocation().getStartingLineNumber();
message += " (line " + line + ')'; //$NON-NLS-1$
}
return message;
super(problem.getMessage());
}
}
@ -190,7 +176,6 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
shouldVisitTypeIds = true;
shouldVisitEnumerators = true;
shouldVisitTranslationUnit = true;
shouldVisitProblems = true;
shouldVisitBaseSpecifiers = true;
shouldVisitNamespaces = true;
@ -301,7 +286,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
continue;
}
try {
visit(declaration);
declaration.accept(this);
scribe.startNewLine();
} catch (RuntimeException e) {
// report, but continue
@ -517,7 +502,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
* @see org.eclipse.cdt.core.dom.ast.ASTVisitor#visit(org.eclipse.cdt.core.dom.ast.IASTExpression)
*/
public int visit(IASTExpression node) {
scribe.printComment();
// scribe.printComment();
startNode(node);
try {
if (node instanceof IASTConditionalExpression) {
@ -555,7 +540,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
* @see org.eclipse.cdt.core.dom.ast.ASTVisitor#visit(org.eclipse.cdt.core.dom.ast.IASTStatement)
*/
public int visit(IASTStatement node) {
scribe.printComment();
// scribe.printComment();
startNode(node);
int indentLevel= scribe.indentationLevel;
try {
@ -651,14 +636,6 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
return PROCESS_SKIP;
}
/*
* @see org.eclipse.cdt.core.dom.ast.ASTVisitor#visit(org.eclipse.cdt.core.dom.ast.IASTProblem)
*/
public int visit(IASTProblem problem) {
formatNode(problem);
return PROCESS_SKIP;
}
/*
* @see org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor#visit(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier)
*/
@ -1837,10 +1814,6 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
}
private int visit(IASTNullStatement node) {
if (peekNextToken() == Token.tIDENTIFIER) {
// probably a macro with empty expansion
skipToNode(node);
}
if (!fInsideFor) {
scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon);
scribe.printTrailingComment();
@ -2295,48 +2268,82 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
* @param node the AST node to be tested
*/
private void startNode(IASTNode node) {
if (node instanceof IASTProblemHolder) {
return;
}
IASTNodeLocation[] locations= node.getNodeLocations();
if (locations.length == 0) {
} else if (node instanceof IASTProblemHolder) {
} else if (locations[0] instanceof IASTMacroExpansion) {
IASTFileLocation expansionLocation= locations[0].asFileLocation();
int startOffset= expansionLocation.getNodeOffset();
scribe.skipRange(startOffset, startOffset + expansionLocation.getNodeLength());
int endOffset= startOffset + expansionLocation.getNodeLength();
scribe.skipRange(startOffset, endOffset);
} else {
IASTFileLocation fileLocation= node.getFileLocation();
scribe.resetToOffset(fileLocation.getNodeOffset());
scribe.restartAtOffset(fileLocation.getNodeOffset());
}
}
private void endOfNode(IASTNode node) {
IASTNodeLocation[] locations= node.getNodeLocations();
if (locations.length == 0) {
} else if (node instanceof IASTProblemHolder) {
} else if (locations[0] instanceof IASTMacroExpansion) {
IASTFileLocation expansionLocation= locations[0].asFileLocation();
int macroEndOffset= expansionLocation.getNodeOffset() + expansionLocation.getNodeLength();
IASTFileLocation fileLocation= node.getFileLocation();
if (node instanceof IASTProblemHolder) {
return;
}
IASTFileLocation fileLocation= node.getFileLocation();
if (fileLocation != null) {
int nodeEndOffset= fileLocation.getNodeOffset() + fileLocation.getNodeLength();
if (nodeEndOffset >= macroEndOffset) {
IASTNode parent= node.getParent();
IASTFileLocation parentLocation= parent.getFileLocation();
int parentEndOffset= parentLocation.getNodeOffset() + parentLocation.getNodeLength();
if (parentEndOffset > nodeEndOffset) {
scribe.resetToOffset(nodeEndOffset);
} else if (parentEndOffset == nodeEndOffset) {
if (node instanceof IASTCompoundStatement
&& !(parent instanceof IASTCompoundStatement || parent instanceof IASTDoStatement)) {
scribe.resetToOffset(nodeEndOffset);
scribe.restartAtOffset(nodeEndOffset);
}
continueNode(node.getParent());
}
private void continueNode(IASTNode node) {
if (node instanceof IASTProblemHolder || node instanceof IASTTranslationUnit) {
return;
}
IASTFileLocation fileLocation= node.getFileLocation();
if (fileLocation == null) {
return;
}
int nodeOffset= fileLocation.getNodeOffset();
int nodeEndOffset= nodeOffset + fileLocation.getNodeLength();
int currentOffset= scribe.scanner.getCurrentPosition();
if (currentOffset > nodeEndOffset) {
return;
}
IASTNodeLocation[] locations= node.getNodeLocations();
for (int i= 0; i < locations.length; i++) {
IASTNodeLocation nodeLocation= locations[i];
if (nodeLocation instanceof IASTMacroExpansion) {
IASTFileLocation expansionLocation= nodeLocation.asFileLocation();
int startOffset= expansionLocation.getNodeOffset();
int endOffset= startOffset + expansionLocation.getNodeLength();
if (currentOffset >= startOffset) {
if (currentOffset < endOffset) {
scribe.skipRange(startOffset, endOffset);
break;
}
else if (currentOffset == endOffset && i == locations.length - 1) {
scribe.skipRange(startOffset, endOffset);
break;
}
}
else {
int nextTokenOffset= getNextTokenOffset();
if (nextTokenOffset < nodeEndOffset && nextTokenOffset >= startOffset && nextTokenOffset <= endOffset) {
scribe.skipRange(startOffset, endOffset);
}
break;
}
}
} else {
IASTFileLocation fileLocation= node.getFileLocation();
int nodeEndOffset= fileLocation.getNodeOffset() + fileLocation.getNodeLength();
scribe.resetToOffset(nodeEndOffset);
}
}
private int getNextTokenOffset() {
localScanner.resetTo(scribe.scanner.getCurrentPosition(), scribe.scannerEndPosition - 1);
localScanner.getNextToken();
return localScanner.getCurrentTokenStartPosition();
}
private void skipNode(IASTNode node) {
final IASTNodeLocation fileLocation= node.getFileLocation();
if (fileLocation != null) {
@ -2463,15 +2470,16 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
private void formatStatements(final List<IASTStatement> statements, boolean insertNewLineAfterLastStatement) {
final int statementsLength = statements.size();
if (statementsLength > 1) {
IASTStatement previousStatement = statements.get(0);
IASTStatement previousStatement= statements.get(0);
try {
previousStatement.accept(this);
} catch (ASTProblemException e) {
skipToNode(statements.get(1));
}
final boolean previousStatementIsNullStmt = previousStatement instanceof IASTNullStatement;
final boolean previousStatementIsNullStmt= previousStatement instanceof IASTNullStatement;
for (int i = 1; i < statementsLength - 1; i++) {
final IASTStatement statement = statements.get(i);
startNode(statement);
final boolean statementIsNullStmt = statement instanceof IASTNullStatement;
if ((previousStatementIsNullStmt && !statementIsNullStmt)
|| (!previousStatementIsNullStmt && !statementIsNullStmt)) {
@ -2547,12 +2555,6 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
}
private boolean isGuardClause(IASTCompoundStatement block, List<IASTStatement> statements) {
IASTNodeLocation[] locations= block.getNodeLocations();
if (locations.length == 0) {
return false;
} else if (locations[0] instanceof IASTMacroExpansion) {
return false;
}
IASTNodeLocation fileLocation= block.getFileLocation();
if (fileLocation == null) {
return false;

View file

@ -1265,7 +1265,6 @@ public class Scribe {
public void printNextToken(int expectedTokenType, boolean considerSpaceIfAny) {
printComment();
if (shouldSkip(scanner.getCurrentPosition())) {
// print(0, considerSpaceIfAny);
return;
}
currentToken= scanner.nextToken();
@ -1283,7 +1282,6 @@ public class Scribe {
public void printNextToken(int[] expectedTokenTypes, boolean considerSpaceIfAny) {
printComment();
if (shouldSkip(scanner.getCurrentPosition())) {
// print(0, considerSpaceIfAny);
return;
}
currentToken= scanner.nextToken();
@ -1669,16 +1667,11 @@ public class Scribe {
}
}
/**
* @param offset
* @return
*/
boolean shouldSkip(int offset) {
return offset >= fSkipTokensFrom && offset <= fSkipTokensTo;
}
public void skipRange(int offset, int endOffset) {
void skipRange(int offset, int endOffset) {
if (offset == fSkipTokensFrom) {
return;
}
@ -1690,14 +1683,10 @@ public class Scribe {
fSkipTokensTo= endOffset;
}
public void resetToOffset(int offset) {
void restartAtOffset(int offset) {
if (fSkipTokensTo > 0) {
fSkipTokensFrom= Integer.MAX_VALUE;
fSkipTokensTo= 0;
while (fSkippedIndentations > 0) {
indent();
fSkippedIndentations--;
}
while (fSkippedIndentations < 0) {
unIndent();
fSkippedIndentations++;
@ -1706,6 +1695,10 @@ public class Scribe {
printRaw(scanner.getCurrentPosition(), offset - scanner.getCurrentPosition());
scanner.resetTo(offset, scannerEndPosition - 1);
}
while (fSkippedIndentations > 0) {
indent();
fSkippedIndentations--;
}
}
}

View file

@ -375,8 +375,7 @@ public class CodeFormatterTest extends BaseUITestCase {
//#define break_end(); foo = 0; }
//
//void break_indenter(int a, int b) {
// break_start()
// ; // This semicolon moves to its own line.
// break_start(); // This semicolon moves to its own line.
// if (a > b) {
// indentation_remains();
// }