From 117fd4a2229bbf124b8e4afd6b8399f411cc40e7 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Tue, 1 Mar 2011 02:11:19 +0000 Subject: [PATCH] Formatting of trailing characters in function definitions. --- .../formatter/CodeFormatterVisitor.java | 52 ++++++++++++++----- .../cdt/ui/tests/text/CodeFormatterTest.java | 15 ++++++ 2 files changed, 55 insertions(+), 12 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 a4fcec0abaf..94edd74bb39 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 @@ -186,7 +186,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, /* * Formats a given token at a given position. - * @see #formatList(List, ListAlignment, boolean, boolean, Runnable) + * @see #formatList(List, ListOptions, boolean, boolean, Runnable) */ class TrailingTokenFormatter implements Runnable { private final int tokenType; @@ -210,7 +210,10 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, scribe.restartAtOffset(tokenPosition); int token= peekNextToken(); if (token == tokenType) { - scribe.pendingSpace = false; + if (scribe.pendingSpace) { + scribe.pendingSpace = false; + scribe.needSpace = true; + } scribe.printNextToken(tokenType, spaceBeforeToken); scribe.printTrailingComment(); if (spaceAfterToken) { @@ -222,7 +225,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, /* * Formats a trailing comma. - * @see #formatList(List, ListAlignment, boolean, boolean, Runnable) + * @see #formatList(List, ListOptions, boolean, boolean, Runnable) */ class TrailingCommaFormatter extends TrailingTokenFormatter { TrailingCommaFormatter(boolean spaceBeforeComma, boolean spaceAfterComma) { @@ -232,7 +235,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, /* * Formats a trailing semicolon. - * @see #formatList(List, ListAlignment, boolean, boolean, Runnable) + * @see #formatList(List, ListOptions, boolean, boolean, Runnable) */ class TrailingSemicolonFormatter extends TrailingTokenFormatter { TrailingSemicolonFormatter(IASTNode node) { @@ -245,7 +248,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, /* * Formats the part of a function declaration following the parameter list. - * @see #formatList(List, ListAlignment, boolean, boolean, Runnable) + * @see #formatList(List, ListOptions, boolean, boolean, Runnable) */ public class CPPFunctionDeclaratorTailFormatter implements Runnable { private final ICPPASTFunctionDeclarator node; @@ -288,7 +291,10 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, if (parenPosition >= 0 && offset <= parenPosition) { if (offset < parenPosition) scribe.restartAtOffset(parenPosition); - scribe.pendingSpace = false; + if (scribe.pendingSpace) { + scribe.pendingSpace = false; + scribe.needSpace = true; + } scribe.printNextToken(Token.tRPAREN, spaceBeforeClosingParen); } if (continuationFormatter != null) @@ -1246,6 +1252,17 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, if (needSpace) { scribe.space(); } + Runnable bodyLeftBraceFormatter = null; + if (DefaultCodeFormatterConstants.END_OF_LINE.equals(preferences.brace_position_for_method_declaration) && + !hasMemberInitializers(node) && !(node instanceof ICPPASTFunctionWithTryBlock)) { + IASTStatement body = node.getBody(); + if (body instanceof IASTCompoundStatement && !startsWithMacroExpansion(body)) { + bodyLeftBraceFormatter = new TrailingTokenFormatter(Token.tLBRACE, + body.getFileLocation().getNodeOffset(), + preferences.insert_space_before_opening_brace_in_method_declaration, false); + scribe.setTailFormatter(bodyLeftBraceFormatter); + } + } declarator.accept(this); if (node instanceof ICPPASTFunctionWithTryBlock) { @@ -1277,13 +1294,18 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, scribe.unIndentForContinuation(); } } - - // body + + if (bodyLeftBraceFormatter != null) { + scribe.setTailFormatter(null); + } + // Body IASTStatement bodyStmt= node.getBody(); if (bodyStmt instanceof IASTCompoundStatement) { if (startNode(bodyStmt)) { try { - formatLeftCurlyBrace(line, preferences.brace_position_for_method_declaration); + if (scribe.scanner.getCurrentPosition() <= bodyStmt.getFileLocation().getNodeOffset()) { + formatLeftCurlyBrace(line, preferences.brace_position_for_method_declaration); + } formatBlock((IASTCompoundStatement) bodyStmt, preferences.brace_position_for_method_declaration, preferences.insert_space_before_opening_brace_in_method_declaration, @@ -3598,7 +3620,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, private void formatBlockOpening(IASTCompoundStatement block, String block_brace_position, boolean insertSpaceBeforeOpeningBrace) { if (!startsWithMacroExpansion(block)) { - formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace); + if (scribe.scanner.getCurrentPosition() <= block.getFileLocation().getNodeOffset()) { + formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace); + } } else { scribe.startNewLine(); scribe.printComment(); @@ -3641,7 +3665,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, private void formatLeftCurlyBrace(final int line, final String bracePosition) { scribe.formatBrace = true; try { - // deal with (quite unexpected) comments right before lcurly + // Deal with (quite unexpected) comments right before left curly brace. scribe.printComment(); if (DefaultCodeFormatterConstants.NEXT_LINE_ON_WRAP.equals(bracePosition) && (scribe.line > line || scribe.column >= preferences.page_width)) { @@ -3660,7 +3684,6 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, scribe.indent(); } scribe.printNextToken(Token.tLBRACE, insertSpaceBeforeBrace); - scribe.printTrailingComment(); } @@ -3881,4 +3904,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, } return positions; } + + private boolean hasMemberInitializers(IASTFunctionDefinition node) { + return node instanceof ICPPASTFunctionDefinition && + ((ICPPASTFunctionDefinition) node).getMemberInitializers().length > 0; + } } 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 fc6e5a8088f..65fda22bb23 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 @@ -974,6 +974,21 @@ public class CodeFormatterTest extends BaseUITestCase { 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){} + + //void f1(const char* long_parameter_name, int very_looooooooong_parameter_name) { + //} + //void f2(const char* long_parameter_name, + // int very_loooooooooong_parameter_name) { + //} + public void testFunctionDefinition() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE); + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION, + Integer.toString(Alignment.M_NEXT_PER_LINE_SPLIT | Alignment.M_INDENT_ON_COLUMN)); + assertFormatterResult(); + } + //int f1(int a, int b, int c, int d, int e, int f, int g); //int f2(int a, int b, int c, int d, int e, int f, int g); //