mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 22:52:11 +02:00
Code formatter fixes.
This commit is contained in:
parent
eacd718cb6
commit
16fdcfdd18
3 changed files with 159 additions and 74 deletions
|
@ -191,7 +191,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, ListOptions, boolean, boolean, Runnable)
|
* @see #formatList(List, ListOptions, boolean, boolean, Runnable)
|
||||||
*/
|
*/
|
||||||
class TrailingTokenFormatter implements Runnable {
|
private class TrailingTokenFormatter implements Runnable {
|
||||||
private final int tokenType;
|
private final int tokenType;
|
||||||
private final int tokenPosition;
|
private final int tokenPosition;
|
||||||
private final boolean spaceBeforeToken;
|
private final boolean spaceBeforeToken;
|
||||||
|
@ -217,10 +217,7 @@ 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) {
|
||||||
if (scribe.pendingSpace) {
|
scribe.undoSpace();
|
||||||
scribe.pendingSpace = false;
|
|
||||||
scribe.needSpace = true;
|
|
||||||
}
|
|
||||||
scribe.printNextToken(tokenType, spaceBeforeToken);
|
scribe.printNextToken(tokenType, spaceBeforeToken);
|
||||||
scribe.printTrailingComment();
|
scribe.printTrailingComment();
|
||||||
if (spaceAfterToken) {
|
if (spaceAfterToken) {
|
||||||
|
@ -234,7 +231,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
* Formats a trailing semicolon.
|
* Formats a trailing semicolon.
|
||||||
* @see #formatList(List, ListOptions, boolean, boolean, Runnable)
|
* @see #formatList(List, ListOptions, boolean, boolean, Runnable)
|
||||||
*/
|
*/
|
||||||
class TrailingSemicolonFormatter extends TrailingTokenFormatter {
|
private class TrailingSemicolonFormatter extends TrailingTokenFormatter {
|
||||||
TrailingSemicolonFormatter(IASTNode node) {
|
TrailingSemicolonFormatter(IASTNode node) {
|
||||||
super(Token.tSEMI, getLastNodeCharacterPosition(node),
|
super(Token.tSEMI, getLastNodeCharacterPosition(node),
|
||||||
fInsideFor ? preferences.insert_space_before_semicolon_in_for :
|
fInsideFor ? preferences.insert_space_before_semicolon_in_for :
|
||||||
|
@ -247,11 +244,11 @@ 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, ListOptions, boolean, boolean, Runnable)
|
* @see #formatList(List, ListOptions, boolean, boolean, Runnable)
|
||||||
*/
|
*/
|
||||||
public class CPPFunctionDeclaratorTailFormatter implements Runnable {
|
private class FunctionDeclaratorTailFormatter implements Runnable {
|
||||||
private final ICPPASTFunctionDeclarator node;
|
private final IASTFunctionDeclarator node;
|
||||||
private final Runnable continuationFormatter;
|
private final Runnable continuationFormatter;
|
||||||
|
|
||||||
public CPPFunctionDeclaratorTailFormatter(ICPPASTFunctionDeclarator node,
|
public FunctionDeclaratorTailFormatter(IASTFunctionDeclarator node,
|
||||||
Runnable tailFormatter) {
|
Runnable tailFormatter) {
|
||||||
this.node = node;
|
this.node = node;
|
||||||
this.continuationFormatter = tailFormatter;
|
this.continuationFormatter = tailFormatter;
|
||||||
|
@ -260,8 +257,39 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
public void run() {
|
public void run() {
|
||||||
boolean needSpace = skipConstVolatileRestrict();
|
boolean needSpace = skipConstVolatileRestrict();
|
||||||
int token = peekNextToken();
|
int token = peekNextToken();
|
||||||
if (token == Token.t_throw || token == Token.tIDENTIFIER)
|
if (token == Token.t_throw || token == Token.tIDENTIFIER) {
|
||||||
return;
|
if (node instanceof ICPPASTFunctionDeclarator) {
|
||||||
|
final IASTTypeId[] exceptionSpecification=
|
||||||
|
((ICPPASTFunctionDeclarator) 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 (continuationFormatter != null)
|
||||||
|
continuationFormatter.run();
|
||||||
|
skipNode(node);
|
||||||
|
ok = true;
|
||||||
|
} catch (AlignmentException e) {
|
||||||
|
scribe.redoAlignment(e);
|
||||||
|
}
|
||||||
|
} while (!ok);
|
||||||
|
scribe.exitAlignment(alignment, true);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
// Skip the rest (=0)
|
// Skip the rest (=0)
|
||||||
if (needSpace && scribe.printComment()) {
|
if (needSpace && scribe.printComment()) {
|
||||||
scribe.space();
|
scribe.space();
|
||||||
|
@ -271,8 +299,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
continuationFormatter.run();
|
continuationFormatter.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public class ClosingParensesisTailFormatter implements Runnable {
|
private class ClosingParensesisTailFormatter implements Runnable {
|
||||||
private final boolean spaceBeforeClosingParen;
|
private final boolean spaceBeforeClosingParen;
|
||||||
private final Runnable continuationFormatter;
|
private final Runnable continuationFormatter;
|
||||||
private final int parenPosition;
|
private final int parenPosition;
|
||||||
|
@ -289,10 +318,7 @@ 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);
|
||||||
if (scribe.pendingSpace) {
|
scribe.undoSpace();
|
||||||
scribe.pendingSpace = false;
|
|
||||||
scribe.needSpace = true;
|
|
||||||
}
|
|
||||||
scribe.printNextToken(Token.tRPAREN, spaceBeforeClosingParen);
|
scribe.printNextToken(Token.tRPAREN, spaceBeforeClosingParen);
|
||||||
}
|
}
|
||||||
if (continuationFormatter != null)
|
if (continuationFormatter != null)
|
||||||
|
@ -1252,15 +1278,15 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
if (needSpace) {
|
if (needSpace) {
|
||||||
scribe.space();
|
scribe.space();
|
||||||
}
|
}
|
||||||
Runnable bodyLeftBraceFormatter = null;
|
Runnable tailFormatter = null;
|
||||||
if (DefaultCodeFormatterConstants.END_OF_LINE.equals(preferences.brace_position_for_method_declaration) &&
|
if (DefaultCodeFormatterConstants.END_OF_LINE.equals(preferences.brace_position_for_method_declaration) &&
|
||||||
!hasMemberInitializers(node) && !(node instanceof ICPPASTFunctionWithTryBlock)) {
|
!hasMemberInitializers(node) && !(node instanceof ICPPASTFunctionWithTryBlock)) {
|
||||||
IASTStatement body = node.getBody();
|
IASTStatement body = node.getBody();
|
||||||
if (body instanceof IASTCompoundStatement && !startsWithMacroExpansion(body)) {
|
if (body instanceof IASTCompoundStatement && !startsWithMacroExpansion(body)) {
|
||||||
bodyLeftBraceFormatter = new TrailingTokenFormatter(Token.tLBRACE,
|
tailFormatter = new TrailingTokenFormatter(Token.tLBRACE,
|
||||||
body.getFileLocation().getNodeOffset(),
|
body.getFileLocation().getNodeOffset(),
|
||||||
preferences.insert_space_before_opening_brace_in_method_declaration, false);
|
preferences.insert_space_before_opening_brace_in_method_declaration, false);
|
||||||
scribe.setTailFormatter(bodyLeftBraceFormatter);
|
scribe.setTailFormatter(tailFormatter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
declarator.accept(this);
|
declarator.accept(this);
|
||||||
|
@ -1295,7 +1321,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bodyLeftBraceFormatter != null) {
|
if (tailFormatter != null) {
|
||||||
scribe.setTailFormatter(null);
|
scribe.setTailFormatter(null);
|
||||||
}
|
}
|
||||||
// Body
|
// Body
|
||||||
|
@ -1342,50 +1368,50 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
final ListOptions options = createListOptionsForFunctionDeclarationParameters();
|
final ListOptions options = createListOptionsForFunctionDeclarationParameters();
|
||||||
Runnable tailFormatter = scribe.takeTailFormatter();
|
Runnable tailFormatter = scribe.takeTailFormatter();
|
||||||
formatList(parameters, options, true, node.takesVarArgs(),
|
formatList(parameters, options, true, node.takesVarArgs(),
|
||||||
new CPPFunctionDeclaratorTailFormatter(node, tailFormatter));
|
new FunctionDeclaratorTailFormatter(node, tailFormatter));
|
||||||
|
|
||||||
IASTFileLocation fileLocation= node.getFileLocation();
|
// IASTFileLocation fileLocation= node.getFileLocation();
|
||||||
if (fileLocation != null &&
|
// if (fileLocation != null &&
|
||||||
scribe.scanner.getCurrentPosition() < fileLocation.getNodeOffset() + fileLocation.getNodeLength()) {
|
// scribe.scanner.getCurrentPosition() < fileLocation.getNodeOffset() + fileLocation.getNodeLength()) {
|
||||||
skipConstVolatileRestrict();
|
// skipConstVolatileRestrict();
|
||||||
|
//
|
||||||
int token = peekNextToken();
|
// int token = peekNextToken();
|
||||||
if (token == Token.t_throw || token == Token.tIDENTIFIER) {
|
// if (token == Token.t_throw || token == Token.tIDENTIFIER) {
|
||||||
final IASTTypeId[] exceptionSpecification= node.getExceptionSpecification();
|
// final IASTTypeId[] exceptionSpecification= node.getExceptionSpecification();
|
||||||
if (exceptionSpecification != null && token == Token.t_throw)
|
// if (exceptionSpecification != null && token == Token.t_throw)
|
||||||
formatExceptionSpecification(exceptionSpecification);
|
// formatExceptionSpecification(exceptionSpecification);
|
||||||
if (peekNextToken() == Token.tIDENTIFIER) {
|
// if (peekNextToken() == Token.tIDENTIFIER) {
|
||||||
Alignment alignment = scribe.createAlignment(
|
// Alignment alignment = scribe.createAlignment(
|
||||||
Alignment.TRAILING_TEXT,
|
// Alignment.TRAILING_TEXT,
|
||||||
Alignment.M_COMPACT_SPLIT,
|
// Alignment.M_COMPACT_SPLIT,
|
||||||
1,
|
// 1,
|
||||||
scribe.scanner.getCurrentPosition());
|
// scribe.scanner.getCurrentPosition());
|
||||||
|
//
|
||||||
scribe.enterAlignment(alignment);
|
// scribe.enterAlignment(alignment);
|
||||||
boolean ok = false;
|
// boolean ok = false;
|
||||||
do {
|
// do {
|
||||||
try {
|
// try {
|
||||||
scribe.alignFragment(alignment, 0);
|
// scribe.alignFragment(alignment, 0);
|
||||||
// Skip the rest of the declarator.
|
// // Skip the rest of the declarator.
|
||||||
scribe.printTrailingComment();
|
// scribe.printTrailingComment();
|
||||||
scribe.space();
|
// scribe.space();
|
||||||
if (tailFormatter != null)
|
// if (tailFormatter != null)
|
||||||
tailFormatter.run();
|
// tailFormatter.run();
|
||||||
skipNode(node);
|
// skipNode(node);
|
||||||
ok = true;
|
// ok = true;
|
||||||
} catch (AlignmentException e) {
|
// } catch (AlignmentException e) {
|
||||||
scribe.redoAlignment(e);
|
// scribe.redoAlignment(e);
|
||||||
}
|
// }
|
||||||
} while (!ok);
|
// } while (!ok);
|
||||||
scribe.exitAlignment(alignment, true);
|
// scribe.exitAlignment(alignment, true);
|
||||||
}
|
// }
|
||||||
} else {
|
// } else {
|
||||||
// Skip the rest (=0)
|
// // Skip the rest (=0)
|
||||||
scribe.printTrailingComment();
|
// scribe.printTrailingComment();
|
||||||
scribe.space();
|
// scribe.space();
|
||||||
skipNode(node);
|
// skipNode(node);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
return PROCESS_SKIP;
|
return PROCESS_SKIP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1742,6 +1742,14 @@ public class Scribe {
|
||||||
needSpace= false;
|
needSpace= false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void undoSpace() {
|
||||||
|
if (pendingSpace) {
|
||||||
|
pendingSpace = false;
|
||||||
|
needSpace = true;
|
||||||
|
column--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder buffer= new StringBuilder();
|
StringBuilder buffer= new StringBuilder();
|
||||||
|
|
|
@ -1005,7 +1005,30 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
// virtual bool function_with_a_looooooong_name(const char* parameter)
|
// virtual bool function_with_a_looooooong_name(const char* parameter)
|
||||||
// ABSTRACT;
|
// ABSTRACT;
|
||||||
//};
|
//};
|
||||||
public void testFunctionDeclarationTrailingMacro() throws Exception {
|
public void testFunctionDeclarationTrailingMacro_1() 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
//#define MACRO_WITH_ONE_PARAMETER(p)
|
||||||
|
//
|
||||||
|
//class A {
|
||||||
|
//void method1(int arguuuuuuuuuuuuuuuuuuuuument) MACRO_WITH_ONE_PARAMETER(p) {}
|
||||||
|
//void method2(int arguuuuuuuuuuuuuuuuuuuuuument) MACRO_WITH_ONE_PARAMETER(p) {}
|
||||||
|
//};
|
||||||
|
|
||||||
|
//#define MACRO_WITH_ONE_PARAMETER(p)
|
||||||
|
//
|
||||||
|
//class A {
|
||||||
|
// void method1(int arguuuuuuuuuuuuuuuuuuuuument) MACRO_WITH_ONE_PARAMETER(p) {
|
||||||
|
// }
|
||||||
|
// void method2(int arguuuuuuuuuuuuuuuuuuuuuument)
|
||||||
|
// MACRO_WITH_ONE_PARAMETER(p) {
|
||||||
|
// }
|
||||||
|
//};
|
||||||
|
public void testFunctionDeclarationTrailingMacro_2() throws Exception {
|
||||||
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
||||||
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION,
|
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION,
|
||||||
Integer.toString(Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN));
|
Integer.toString(Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN));
|
||||||
|
@ -2053,7 +2076,6 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
//
|
//
|
||||||
//class Voidifier {
|
//class Voidifier {
|
||||||
//public:
|
//public:
|
||||||
//Voidifier();
|
|
||||||
//void operator&(Stream&);
|
//void operator&(Stream&);
|
||||||
//};
|
//};
|
||||||
//
|
//
|
||||||
|
@ -2070,7 +2092,6 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
//
|
//
|
||||||
//class Voidifier {
|
//class Voidifier {
|
||||||
//public:
|
//public:
|
||||||
// Voidifier();
|
|
||||||
// void operator&(Stream&);
|
// void operator&(Stream&);
|
||||||
//};
|
//};
|
||||||
//
|
//
|
||||||
|
@ -2092,7 +2113,6 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
//
|
//
|
||||||
//class Voidifier {
|
//class Voidifier {
|
||||||
//public:
|
//public:
|
||||||
//Voidifier();
|
|
||||||
//void operator&(Stream&);
|
//void operator&(Stream&);
|
||||||
//};
|
//};
|
||||||
//
|
//
|
||||||
|
@ -2110,7 +2130,6 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
//
|
//
|
||||||
//class Voidifier {
|
//class Voidifier {
|
||||||
//public:
|
//public:
|
||||||
// Voidifier();
|
|
||||||
// void operator&(Stream&);
|
// void operator&(Stream&);
|
||||||
//};
|
//};
|
||||||
//
|
//
|
||||||
|
@ -2156,6 +2175,38 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
assertFormatterResult();
|
assertFormatterResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//class Stream {
|
||||||
|
//Stream& operator<<(const char* s);
|
||||||
|
//};
|
||||||
|
//
|
||||||
|
//Stream stream;
|
||||||
|
//int variable_with_a_long_name, another_variable_with_a_long_name;
|
||||||
|
//
|
||||||
|
//void test() {
|
||||||
|
//stream << (variable_with_a_long_name + another_variable_with_a_long_name) * variable_with_a_long_name <<
|
||||||
|
//"01234567890123456789";
|
||||||
|
//}
|
||||||
|
|
||||||
|
//class Stream {
|
||||||
|
// Stream& operator<<(const char* s);
|
||||||
|
//};
|
||||||
|
//
|
||||||
|
//Stream stream;
|
||||||
|
//int variable_with_a_long_name, another_variable_with_a_long_name;
|
||||||
|
//
|
||||||
|
//void test() {
|
||||||
|
// stream << (variable_with_a_long_name + another_variable_with_a_long_name)
|
||||||
|
// * variable_with_a_long_name
|
||||||
|
// << "01234567890123456789";
|
||||||
|
//}
|
||||||
|
// TODO(sprigogin): Enable the test when the formatter is fixed.
|
||||||
|
public void _testOverloadedLeftShiftChain_4() throws Exception {
|
||||||
|
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
||||||
|
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_OVERLOADED_LEFT_SHIFT_CHAIN,
|
||||||
|
Integer.toString(Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN));
|
||||||
|
assertFormatterResult();
|
||||||
|
}
|
||||||
|
|
||||||
//int main() {
|
//int main() {
|
||||||
// std::vector<std::vector<int>> test;
|
// std::vector<std::vector<int>> test;
|
||||||
// // some comment
|
// // some comment
|
||||||
|
|
Loading…
Add table
Reference in a new issue