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

Formatting of trailing characters in function definitions.

This commit is contained in:
Sergey Prigogin 2011-03-01 02:11:19 +00:00
parent 654cb74090
commit 117fd4a222
2 changed files with 55 additions and 12 deletions

View file

@ -186,7 +186,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
/* /*
* Formats a given token at a given position. * 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 { class TrailingTokenFormatter implements Runnable {
private final int tokenType; private final int tokenType;
@ -210,7 +210,10 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.restartAtOffset(tokenPosition); scribe.restartAtOffset(tokenPosition);
int token= peekNextToken(); int token= peekNextToken();
if (token == tokenType) { if (token == tokenType) {
scribe.pendingSpace = false; if (scribe.pendingSpace) {
scribe.pendingSpace = false;
scribe.needSpace = true;
}
scribe.printNextToken(tokenType, spaceBeforeToken); scribe.printNextToken(tokenType, spaceBeforeToken);
scribe.printTrailingComment(); scribe.printTrailingComment();
if (spaceAfterToken) { if (spaceAfterToken) {
@ -222,7 +225,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
/* /*
* Formats a trailing comma. * Formats a trailing comma.
* @see #formatList(List, ListAlignment, boolean, boolean, Runnable) * @see #formatList(List, ListOptions, boolean, boolean, Runnable)
*/ */
class TrailingCommaFormatter extends TrailingTokenFormatter { class TrailingCommaFormatter extends TrailingTokenFormatter {
TrailingCommaFormatter(boolean spaceBeforeComma, boolean spaceAfterComma) { TrailingCommaFormatter(boolean spaceBeforeComma, boolean spaceAfterComma) {
@ -232,7 +235,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
/* /*
* Formats a trailing semicolon. * Formats a trailing semicolon.
* @see #formatList(List, ListAlignment, boolean, boolean, Runnable) * @see #formatList(List, ListOptions, boolean, boolean, Runnable)
*/ */
class TrailingSemicolonFormatter extends TrailingTokenFormatter { class TrailingSemicolonFormatter extends TrailingTokenFormatter {
TrailingSemicolonFormatter(IASTNode node) { 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. * 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 { public class CPPFunctionDeclaratorTailFormatter implements Runnable {
private final ICPPASTFunctionDeclarator node; private final ICPPASTFunctionDeclarator node;
@ -288,7 +291,10 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (parenPosition >= 0 && offset <= parenPosition) { if (parenPosition >= 0 && offset <= parenPosition) {
if (offset < parenPosition) if (offset < parenPosition)
scribe.restartAtOffset(parenPosition); scribe.restartAtOffset(parenPosition);
scribe.pendingSpace = false; if (scribe.pendingSpace) {
scribe.pendingSpace = false;
scribe.needSpace = true;
}
scribe.printNextToken(Token.tRPAREN, spaceBeforeClosingParen); scribe.printNextToken(Token.tRPAREN, spaceBeforeClosingParen);
} }
if (continuationFormatter != null) if (continuationFormatter != null)
@ -1246,6 +1252,17 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (needSpace) { if (needSpace) {
scribe.space(); 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); declarator.accept(this);
if (node instanceof ICPPASTFunctionWithTryBlock) { if (node instanceof ICPPASTFunctionWithTryBlock) {
@ -1278,12 +1295,17 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
} }
// body if (bodyLeftBraceFormatter != null) {
scribe.setTailFormatter(null);
}
// Body
IASTStatement bodyStmt= node.getBody(); IASTStatement bodyStmt= node.getBody();
if (bodyStmt instanceof IASTCompoundStatement) { if (bodyStmt instanceof IASTCompoundStatement) {
if (startNode(bodyStmt)) { if (startNode(bodyStmt)) {
try { 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, formatBlock((IASTCompoundStatement) bodyStmt,
preferences.brace_position_for_method_declaration, preferences.brace_position_for_method_declaration,
preferences.insert_space_before_opening_brace_in_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, private void formatBlockOpening(IASTCompoundStatement block, String block_brace_position,
boolean insertSpaceBeforeOpeningBrace) { boolean insertSpaceBeforeOpeningBrace) {
if (!startsWithMacroExpansion(block)) { if (!startsWithMacroExpansion(block)) {
formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace); if (scribe.scanner.getCurrentPosition() <= block.getFileLocation().getNodeOffset()) {
formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace);
}
} else { } else {
scribe.startNewLine(); scribe.startNewLine();
scribe.printComment(); scribe.printComment();
@ -3641,7 +3665,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
private void formatLeftCurlyBrace(final int line, final String bracePosition) { private void formatLeftCurlyBrace(final int line, final String bracePosition) {
scribe.formatBrace = true; scribe.formatBrace = true;
try { try {
// deal with (quite unexpected) comments right before lcurly // Deal with (quite unexpected) comments right before left curly brace.
scribe.printComment(); scribe.printComment();
if (DefaultCodeFormatterConstants.NEXT_LINE_ON_WRAP.equals(bracePosition) if (DefaultCodeFormatterConstants.NEXT_LINE_ON_WRAP.equals(bracePosition)
&& (scribe.line > line || scribe.column >= preferences.page_width)) { && (scribe.line > line || scribe.column >= preferences.page_width)) {
@ -3660,7 +3684,6 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.indent(); scribe.indent();
} }
scribe.printNextToken(Token.tLBRACE, insertSpaceBeforeBrace); scribe.printNextToken(Token.tLBRACE, insertSpaceBeforeBrace);
scribe.printTrailingComment(); scribe.printTrailingComment();
} }
@ -3881,4 +3904,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
return positions; return positions;
} }
private boolean hasMemberInitializers(IASTFunctionDefinition node) {
return node instanceof ICPPASTFunctionDefinition &&
((ICPPASTFunctionDefinition) node).getMemberInitializers().length > 0;
}
} }

View file

@ -974,6 +974,21 @@ public class CodeFormatterTest extends BaseUITestCase {
assertFormatterResult(); 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 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); //int f2(int a, int b, int c, int d, int e, int f, int g);
// //