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:
parent
bad35ab17a
commit
788ec3ad34
3 changed files with 72 additions and 78 deletions
|
@ -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 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 class ASTProblemException extends RuntimeException {
|
||||||
private static final long serialVersionUID= 1L;
|
|
||||||
private IASTProblem fProblem;
|
|
||||||
ASTProblemException(IASTProblem problem) {
|
ASTProblemException(IASTProblem problem) {
|
||||||
super();
|
super(problem.getMessage());
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,7 +176,6 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
shouldVisitTypeIds = true;
|
shouldVisitTypeIds = true;
|
||||||
shouldVisitEnumerators = true;
|
shouldVisitEnumerators = true;
|
||||||
shouldVisitTranslationUnit = true;
|
shouldVisitTranslationUnit = true;
|
||||||
shouldVisitProblems = true;
|
|
||||||
|
|
||||||
shouldVisitBaseSpecifiers = true;
|
shouldVisitBaseSpecifiers = true;
|
||||||
shouldVisitNamespaces = true;
|
shouldVisitNamespaces = true;
|
||||||
|
@ -301,7 +286,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
visit(declaration);
|
declaration.accept(this);
|
||||||
scribe.startNewLine();
|
scribe.startNewLine();
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
// report, but continue
|
// 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)
|
* @see org.eclipse.cdt.core.dom.ast.ASTVisitor#visit(org.eclipse.cdt.core.dom.ast.IASTExpression)
|
||||||
*/
|
*/
|
||||||
public int visit(IASTExpression node) {
|
public int visit(IASTExpression node) {
|
||||||
scribe.printComment();
|
// scribe.printComment();
|
||||||
startNode(node);
|
startNode(node);
|
||||||
try {
|
try {
|
||||||
if (node instanceof IASTConditionalExpression) {
|
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)
|
* @see org.eclipse.cdt.core.dom.ast.ASTVisitor#visit(org.eclipse.cdt.core.dom.ast.IASTStatement)
|
||||||
*/
|
*/
|
||||||
public int visit(IASTStatement node) {
|
public int visit(IASTStatement node) {
|
||||||
scribe.printComment();
|
// scribe.printComment();
|
||||||
startNode(node);
|
startNode(node);
|
||||||
int indentLevel= scribe.indentationLevel;
|
int indentLevel= scribe.indentationLevel;
|
||||||
try {
|
try {
|
||||||
|
@ -651,14 +636,6 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
return PROCESS_SKIP;
|
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)
|
* @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) {
|
private int visit(IASTNullStatement node) {
|
||||||
if (peekNextToken() == Token.tIDENTIFIER) {
|
|
||||||
// probably a macro with empty expansion
|
|
||||||
skipToNode(node);
|
|
||||||
}
|
|
||||||
if (!fInsideFor) {
|
if (!fInsideFor) {
|
||||||
scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon);
|
scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon);
|
||||||
scribe.printTrailingComment();
|
scribe.printTrailingComment();
|
||||||
|
@ -2295,48 +2268,82 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
* @param node the AST node to be tested
|
* @param node the AST node to be tested
|
||||||
*/
|
*/
|
||||||
private void startNode(IASTNode node) {
|
private void startNode(IASTNode node) {
|
||||||
|
if (node instanceof IASTProblemHolder) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
IASTNodeLocation[] locations= node.getNodeLocations();
|
IASTNodeLocation[] locations= node.getNodeLocations();
|
||||||
if (locations.length == 0) {
|
if (locations.length == 0) {
|
||||||
} else if (node instanceof IASTProblemHolder) {
|
|
||||||
} else if (locations[0] instanceof IASTMacroExpansion) {
|
} else if (locations[0] instanceof IASTMacroExpansion) {
|
||||||
IASTFileLocation expansionLocation= locations[0].asFileLocation();
|
IASTFileLocation expansionLocation= locations[0].asFileLocation();
|
||||||
int startOffset= expansionLocation.getNodeOffset();
|
int startOffset= expansionLocation.getNodeOffset();
|
||||||
scribe.skipRange(startOffset, startOffset + expansionLocation.getNodeLength());
|
int endOffset= startOffset + expansionLocation.getNodeLength();
|
||||||
|
scribe.skipRange(startOffset, endOffset);
|
||||||
} else {
|
} else {
|
||||||
IASTFileLocation fileLocation= node.getFileLocation();
|
IASTFileLocation fileLocation= node.getFileLocation();
|
||||||
scribe.resetToOffset(fileLocation.getNodeOffset());
|
scribe.restartAtOffset(fileLocation.getNodeOffset());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void endOfNode(IASTNode node) {
|
private void endOfNode(IASTNode node) {
|
||||||
IASTNodeLocation[] locations= node.getNodeLocations();
|
if (node instanceof IASTProblemHolder) {
|
||||||
if (locations.length == 0) {
|
return;
|
||||||
} else if (node instanceof IASTProblemHolder) {
|
}
|
||||||
} else if (locations[0] instanceof IASTMacroExpansion) {
|
IASTFileLocation fileLocation= node.getFileLocation();
|
||||||
IASTFileLocation expansionLocation= locations[0].asFileLocation();
|
if (fileLocation != null) {
|
||||||
int macroEndOffset= expansionLocation.getNodeOffset() + expansionLocation.getNodeLength();
|
|
||||||
IASTFileLocation fileLocation= node.getFileLocation();
|
|
||||||
int nodeEndOffset= fileLocation.getNodeOffset() + fileLocation.getNodeLength();
|
int nodeEndOffset= fileLocation.getNodeOffset() + fileLocation.getNodeLength();
|
||||||
if (nodeEndOffset >= macroEndOffset) {
|
scribe.restartAtOffset(nodeEndOffset);
|
||||||
IASTNode parent= node.getParent();
|
}
|
||||||
IASTFileLocation parentLocation= parent.getFileLocation();
|
continueNode(node.getParent());
|
||||||
int parentEndOffset= parentLocation.getNodeOffset() + parentLocation.getNodeLength();
|
}
|
||||||
if (parentEndOffset > nodeEndOffset) {
|
|
||||||
scribe.resetToOffset(nodeEndOffset);
|
private void continueNode(IASTNode node) {
|
||||||
} else if (parentEndOffset == nodeEndOffset) {
|
if (node instanceof IASTProblemHolder || node instanceof IASTTranslationUnit) {
|
||||||
if (node instanceof IASTCompoundStatement
|
return;
|
||||||
&& !(parent instanceof IASTCompoundStatement || parent instanceof IASTDoStatement)) {
|
}
|
||||||
scribe.resetToOffset(nodeEndOffset);
|
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) {
|
private void skipNode(IASTNode node) {
|
||||||
final IASTNodeLocation fileLocation= node.getFileLocation();
|
final IASTNodeLocation fileLocation= node.getFileLocation();
|
||||||
if (fileLocation != null) {
|
if (fileLocation != null) {
|
||||||
|
@ -2463,15 +2470,16 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
private void formatStatements(final List<IASTStatement> statements, boolean insertNewLineAfterLastStatement) {
|
private void formatStatements(final List<IASTStatement> statements, boolean insertNewLineAfterLastStatement) {
|
||||||
final int statementsLength = statements.size();
|
final int statementsLength = statements.size();
|
||||||
if (statementsLength > 1) {
|
if (statementsLength > 1) {
|
||||||
IASTStatement previousStatement = statements.get(0);
|
IASTStatement previousStatement= statements.get(0);
|
||||||
try {
|
try {
|
||||||
previousStatement.accept(this);
|
previousStatement.accept(this);
|
||||||
} catch (ASTProblemException e) {
|
} catch (ASTProblemException e) {
|
||||||
skipToNode(statements.get(1));
|
skipToNode(statements.get(1));
|
||||||
}
|
}
|
||||||
final boolean previousStatementIsNullStmt = previousStatement instanceof IASTNullStatement;
|
final boolean previousStatementIsNullStmt= previousStatement instanceof IASTNullStatement;
|
||||||
for (int i = 1; i < statementsLength - 1; i++) {
|
for (int i = 1; i < statementsLength - 1; i++) {
|
||||||
final IASTStatement statement = statements.get(i);
|
final IASTStatement statement = statements.get(i);
|
||||||
|
startNode(statement);
|
||||||
final boolean statementIsNullStmt = statement instanceof IASTNullStatement;
|
final boolean statementIsNullStmt = statement instanceof IASTNullStatement;
|
||||||
if ((previousStatementIsNullStmt && !statementIsNullStmt)
|
if ((previousStatementIsNullStmt && !statementIsNullStmt)
|
||||||
|| (!previousStatementIsNullStmt && !statementIsNullStmt)) {
|
|| (!previousStatementIsNullStmt && !statementIsNullStmt)) {
|
||||||
|
@ -2547,12 +2555,6 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isGuardClause(IASTCompoundStatement block, List<IASTStatement> statements) {
|
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();
|
IASTNodeLocation fileLocation= block.getFileLocation();
|
||||||
if (fileLocation == null) {
|
if (fileLocation == null) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -1265,7 +1265,6 @@ public class Scribe {
|
||||||
public void printNextToken(int expectedTokenType, boolean considerSpaceIfAny) {
|
public void printNextToken(int expectedTokenType, boolean considerSpaceIfAny) {
|
||||||
printComment();
|
printComment();
|
||||||
if (shouldSkip(scanner.getCurrentPosition())) {
|
if (shouldSkip(scanner.getCurrentPosition())) {
|
||||||
// print(0, considerSpaceIfAny);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
currentToken= scanner.nextToken();
|
currentToken= scanner.nextToken();
|
||||||
|
@ -1283,7 +1282,6 @@ public class Scribe {
|
||||||
public void printNextToken(int[] expectedTokenTypes, boolean considerSpaceIfAny) {
|
public void printNextToken(int[] expectedTokenTypes, boolean considerSpaceIfAny) {
|
||||||
printComment();
|
printComment();
|
||||||
if (shouldSkip(scanner.getCurrentPosition())) {
|
if (shouldSkip(scanner.getCurrentPosition())) {
|
||||||
// print(0, considerSpaceIfAny);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
currentToken= scanner.nextToken();
|
currentToken= scanner.nextToken();
|
||||||
|
@ -1669,16 +1667,11 @@ public class Scribe {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param offset
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
boolean shouldSkip(int offset) {
|
boolean shouldSkip(int offset) {
|
||||||
return offset >= fSkipTokensFrom && offset <= fSkipTokensTo;
|
return offset >= fSkipTokensFrom && offset <= fSkipTokensTo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void skipRange(int offset, int endOffset) {
|
||||||
public void skipRange(int offset, int endOffset) {
|
|
||||||
if (offset == fSkipTokensFrom) {
|
if (offset == fSkipTokensFrom) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1690,14 +1683,10 @@ public class Scribe {
|
||||||
fSkipTokensTo= endOffset;
|
fSkipTokensTo= endOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resetToOffset(int offset) {
|
void restartAtOffset(int offset) {
|
||||||
if (fSkipTokensTo > 0) {
|
if (fSkipTokensTo > 0) {
|
||||||
fSkipTokensFrom= Integer.MAX_VALUE;
|
fSkipTokensFrom= Integer.MAX_VALUE;
|
||||||
fSkipTokensTo= 0;
|
fSkipTokensTo= 0;
|
||||||
while (fSkippedIndentations > 0) {
|
|
||||||
indent();
|
|
||||||
fSkippedIndentations--;
|
|
||||||
}
|
|
||||||
while (fSkippedIndentations < 0) {
|
while (fSkippedIndentations < 0) {
|
||||||
unIndent();
|
unIndent();
|
||||||
fSkippedIndentations++;
|
fSkippedIndentations++;
|
||||||
|
@ -1706,6 +1695,10 @@ public class Scribe {
|
||||||
printRaw(scanner.getCurrentPosition(), offset - scanner.getCurrentPosition());
|
printRaw(scanner.getCurrentPosition(), offset - scanner.getCurrentPosition());
|
||||||
scanner.resetTo(offset, scannerEndPosition - 1);
|
scanner.resetTo(offset, scannerEndPosition - 1);
|
||||||
}
|
}
|
||||||
|
while (fSkippedIndentations > 0) {
|
||||||
|
indent();
|
||||||
|
fSkippedIndentations--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -375,8 +375,7 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
//#define break_end(); foo = 0; }
|
//#define break_end(); foo = 0; }
|
||||||
//
|
//
|
||||||
//void break_indenter(int a, int b) {
|
//void break_indenter(int a, int b) {
|
||||||
// break_start()
|
// break_start(); // This semicolon moves to its own line.
|
||||||
// ; // This semicolon moves to its own line.
|
|
||||||
// if (a > b) {
|
// if (a > b) {
|
||||||
// indentation_remains();
|
// indentation_remains();
|
||||||
// }
|
// }
|
||||||
|
|
Loading…
Add table
Reference in a new issue