From 04fa1f9ff5d4f71e61e4d4b75cb7cbf73ba59e88 Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Thu, 9 Oct 2008 13:39:10 +0000 Subject: [PATCH] Bug 248041 - Code formatting problem with CPPUNIT - Partial Fix --- .../formatter/CodeFormatterVisitor.java | 74 +++++++++++++------ 1 file changed, 53 insertions(+), 21 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 6189ee61a10..d16b30e2ae1 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 @@ -313,12 +313,17 @@ public class CodeFormatterVisitor extends CPPASTVisitor { return PROCESS_SKIP; } - /* - * @see org.eclipse.cdt.core.dom.ast.ASTVisitor#visit(org.eclipse.cdt.core.dom.ast.IASTDeclaration) - */ @Override public int visit(IASTDeclaration node) { if (!startNode(node)) { return PROCESS_SKIP; } + try { + return formatDeclaration(node); + } finally { + endOfNode(node); + } + } + + private int formatDeclaration(IASTDeclaration node) { int indentLevel= scribe.indentationLevel; try { if (node instanceof IASTFunctionDefinition) { @@ -359,8 +364,6 @@ public class CodeFormatterVisitor extends CPPASTVisitor { scribe.unIndent(); } } - } finally { - endOfNode(node); } return PROCESS_SKIP; } @@ -1249,15 +1252,32 @@ public class CodeFormatterVisitor extends CPPASTVisitor { formatList(declarators, align, false, false); } if (fExpectSemicolonAfterDeclaration) { - if (peekNextToken() != Token.tSEMI) { - scribe.skipToToken(Token.tSEMI); + handleNodeEndingInSemicolon(node); + if (peekNextToken() == Token.tSEMI) { + scribe.printNextToken(Token.tSEMI, fInsideFor ? preferences.insert_space_before_semicolon_in_for : preferences.insert_space_before_semicolon); + scribe.printTrailingComment(); } - scribe.printNextToken(Token.tSEMI, fInsideFor ? preferences.insert_space_before_semicolon_in_for : preferences.insert_space_before_semicolon); - scribe.printTrailingComment(); } return PROCESS_SKIP; } + private void handleNodeEndingInSemicolon(IASTSimpleDeclaration node) { + if (scribe.skipRange() && peekNextToken(true) == Token.tSEMI) { + IASTNodeLocation[] locations= node.getNodeLocations(); + if (locations.length > 0) { + IASTNodeLocation lastLocation = locations[locations.length - 1]; + if (!(lastLocation instanceof IASTMacroExpansionLocation)) { + IASTFileLocation fileLocation= lastLocation.asFileLocation(); + int startOffset= fileLocation.getNodeOffset(); + int currentPosition= scribe.scanner.getCurrentPosition(); + if (currentPosition >= startOffset) { + scribe.restartAtOffset(startOffset); + } + } + } + } + } + private int visit(ICPPASTTemplateDeclaration node) { if (node.isExported()) { scribe.printNextToken(Token.t_export); @@ -1449,27 +1469,35 @@ public class CodeFormatterVisitor extends CPPASTVisitor { scribe.startNewLine(); for (int i = 0; i < memberDecls.length; i++) { IASTDeclaration declaration = memberDecls[i]; + if (preferences.indent_body_declarations_compare_to_access_specifier) { + scribe.indent(); + } + scribe.printComment(); if (declaration instanceof ICPPASTVisibilityLabel) { - if (preferences.indent_body_declarations_compare_to_access_specifier) { - scribe.indent(); - } - scribe.printComment(); if (preferences.indent_body_declarations_compare_to_access_specifier) { scribe.unIndent(); } - visit((ICPPASTVisibilityLabel)declaration); - } else { - if (preferences.indent_body_declarations_compare_to_access_specifier) { - scribe.indent(); + startNode(declaration); + try { + scribe.startNewLine(); + visit((ICPPASTVisibilityLabel)declaration); + } finally { + endOfNode(declaration); + } + } else { + startNode(declaration); + try { + scribe.startNewLine(); + formatDeclaration(declaration); + } finally { + endOfNode(declaration); } - declaration.accept(this); - scribe.printComment(); if (preferences.indent_body_declarations_compare_to_access_specifier) { scribe.unIndent(); } } - scribe.startNewLine(); } + scribe.startNewLine(); if (preferences.indent_body_declarations_compare_to_access_specifier) { scribe.indent(); } @@ -2809,6 +2837,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor { int endOffset= startOffset + expansionLocation.getNodeLength(); scribe.skipRange(startOffset, endOffset); if (locations.length == 1 && endOffset <= scribe.scanner.getCurrentPosition()) { + endOfNode(node); return false; } } else { @@ -3122,7 +3151,10 @@ public class CodeFormatterVisitor extends CPPASTVisitor { } private int peekNextToken() { - if (scribe.shouldSkip(scribe.scanner.getCurrentPosition())) { + return peekNextToken(false); + } + private int peekNextToken(boolean ignoreSkip) { + if (!ignoreSkip && scribe.shouldSkip(scribe.scanner.getCurrentPosition())) { return Token.tBADCHAR; } localScanner.resetTo(scribe.scanner.getCurrentPosition(), scribe.scannerEndPosition - 1);