From da8a1d0b28cd6be12d2b771ef40861b1bd0ade19 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Fri, 4 Mar 2011 02:04:11 +0000 Subject: [PATCH] Bug fixes. --- .../formatter/CodeFormatterVisitor.java | 51 +++++++++++++++---- .../internal/formatter/align/Alignment.java | 3 +- .../cdt/ui/tests/text/CodeFormatterTest.java | 48 +++++++++++++++++ 3 files changed, 92 insertions(+), 10 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 1a06fd33efe..0bc0e2be60c 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 @@ -263,7 +263,8 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, public void run() { boolean needSpace = skipConstVolatileRestrict(); - if (node.getExceptionSpecification() != null && peekNextToken() == Token.t_throw) + int token = peekNextToken(); + if (token == Token.t_throw || token == Token.tIDENTIFIER) return; // Skip the rest (=0) if (needSpace && scribe.printComment()) { @@ -862,6 +863,8 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, */ @Override public int visit(IASTStatement node) { + if (startsWithMacroExpansion(node)) + scribe.printCommentPreservingNewLines(); if (!startNode(node)) { return PROCESS_SKIP; } int indentLevel= scribe.indentationLevel; try { @@ -1341,21 +1344,51 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, private int visit(ICPPASTFunctionDeclarator node) { final List parameters = Arrays.asList(node.getParameters()); final ListOptions options = createListOptionsForFunctionDeclarationParameters(); + Runnable tailFormatter = scribe.getTailFormatter(); formatList(parameters, options, true, node.takesVarArgs(), - new CPPFunctionDeclaratorTailFormatter(node, scribe.getTailFormatter())); + new CPPFunctionDeclaratorTailFormatter(node, tailFormatter)); IASTFileLocation fileLocation= node.getFileLocation(); if (fileLocation != null && scribe.scanner.getCurrentPosition() < fileLocation.getNodeOffset() + fileLocation.getNodeLength()) { skipConstVolatileRestrict(); - final IASTTypeId[] exceptionSpecification= node.getExceptionSpecification(); - if (exceptionSpecification != null && peekNextToken() == Token.t_throw) - formatExceptionSpecification(exceptionSpecification); - // Skip the rest (=0) - scribe.printTrailingComment(); - scribe.space(); - skipNode(node); + int token = peekNextToken(); + if (token == Token.t_throw || token == Token.tIDENTIFIER) { + final IASTTypeId[] exceptionSpecification= node.getExceptionSpecification(); + if (exceptionSpecification != null && token == Token.t_throw) + formatExceptionSpecification(exceptionSpecification); + if (peekNextToken() == Token.tIDENTIFIER) { + Alignment alignment = scribe.createAlignment( + Alignment.TRAILING_TEXT, + Alignment.M_COMPACT_SPLIT, + 1, + scribe.scanner.getCurrentPosition()); + + scribe.enterAlignment(alignment); + boolean ok = false; + do { + try { + scribe.alignFragment(alignment, 0); + // Skip the rest of the declarator. + scribe.printTrailingComment(); + scribe.space(); + if (tailFormatter != null) + tailFormatter.run(); + skipNode(node); + ok = true; + } catch (AlignmentException e) { + scribe.redoAlignment(e); + } + } while (!ok); + scribe.exitAlignment(alignment, true); + } + } else { + // Skip the rest (=0) + scribe.printTrailingComment(); + scribe.space(); + skipNode(node); + } } return PROCESS_SKIP; } diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/align/Alignment.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/align/Alignment.java index a120cd12fcb..5f68e2ea0c6 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/align/Alignment.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/align/Alignment.java @@ -30,9 +30,10 @@ public class Alignment { public static final String EXCEPTION_SPECIFICATION = "exceptionSpecification"; //$NON-NLS-1$ public static final String FIELD_REFERENCE = "fieldReference"; //$NON-NLS-1$ public static final String FOR = "for"; //$NON-NLS-1$ - public static final String MACRO_ARGUMENTS = "macroArguments"; //$NON-NLS-1$ public static final String LIST_ELEMENTS_PREFIX = "listElements_"; //$NON-NLS-1$ public static final String LIST_FALLBACK_TRAP = "listFallbackTrap"; //$NON-NLS-1$ + public static final String MACRO_ARGUMENTS = "macroArguments"; //$NON-NLS-1$ + public static final String TRAILING_TEXT = "trailingText"; //$NON-NLS-1$ /** The name of the alignment */ public String name; diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java index 493acbae9e6..550f87a8d27 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java @@ -991,6 +991,27 @@ public class CodeFormatterTest extends BaseUITestCase { assertFormatterResult(); } + //#define ABSTRACT = 0 + // + //class A { + // virtual bool function_with_a_loooooong_name(const char* parameter) ABSTRACT; + // virtual bool function_with_a_looooooong_name(const char* parameter) ABSTRACT; + //}; + + //#define ABSTRACT = 0 + // + //class A { + // virtual bool function_with_a_loooooong_name(const char* parameter) ABSTRACT; + // virtual bool function_with_a_looooooong_name(const char* parameter) + // ABSTRACT; + //}; + public void testFunctionDeclarationTrailingMacro() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE); + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION, + Integer.toString(Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN)); + assertFormatterResult(); + } + //void f1(const char* long_parameter_name,int very_looooooooong_parameter_name){} //void f2(const char* long_parameter_name,int very_loooooooooong_parameter_name){} @@ -1065,6 +1086,33 @@ public class CodeFormatterTest extends BaseUITestCase { assertFormatterResult(); } + //#define STREAM GetStream() + //class Stream { + //Stream& operator <<(const char*); + //}; + //Stream GetStream(); + // + //void test() { + // // comment + //STREAM << "text " << "text " << "text " << "text " << "text " << "text " << "text " << "text "; + //} + + //#define STREAM GetStream() + //class Stream { + // Stream& operator <<(const char*); + //}; + //Stream GetStream(); + // + //void test() { + // // comment + // STREAM << "text " << "text " << "text " << "text " << "text " << "text " + // << "text " << "text "; + //} + public void testMacroAfterComment() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE); + assertFormatterResult(); + } + //#define MY_MACRO(a, b, c) // //MY_MACRO(abcdefghijklmnopqrstuvwxyz,25,"very very very very very very very very very very long text");