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

Bug 280989 - Code formatter refuses to break 'for' statement at semicolons

This commit is contained in:
Sergey Prigogin 2011-01-28 00:07:38 +00:00
parent 954f68068a
commit 514a13bad4
2 changed files with 144 additions and 46 deletions

View file

@ -1736,7 +1736,6 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} catch (AlignmentException e) { } catch (AlignmentException e) {
scribe.redoAlignment(e); scribe.redoAlignment(e);
} catch (ASTProblemException e) { } catch (ASTProblemException e) {
} }
} while (!ok); } while (!ok);
scribe.exitAlignment(listAlignment, true); scribe.exitAlignment(listAlignment, true);
@ -2519,6 +2518,19 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
final int line = scribe.line; final int line = scribe.line;
scribe.printNextToken(Token.tLPAREN, preferences.insert_space_before_opening_paren_in_for); scribe.printNextToken(Token.tLPAREN, preferences.insert_space_before_opening_paren_in_for);
fInsideFor= true; fInsideFor= true;
Alignment alignment = scribe.createAlignment(
"for", //$NON-NLS-1$
Alignment.M_COMPACT_SPLIT,
Alignment.R_OUTERMOST,
2,
scribe.scanner.getCurrentPosition(),
preferences.continuation_indentation,
false);
scribe.enterAlignment(alignment);
boolean ok = false;
do {
try {
try { try {
if (preferences.insert_space_after_opening_paren_in_for) { if (preferences.insert_space_after_opening_paren_in_for) {
scribe.space(); scribe.space();
@ -2528,6 +2540,8 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (peekNextToken() == Token.tSEMI) { if (peekNextToken() == Token.tSEMI) {
scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon_in_for); scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon_in_for);
} }
scribe.alignFragment(alignment, 0);
final IASTExpression condition = node.getConditionExpression(); final IASTExpression condition = node.getConditionExpression();
if (condition != null) { if (condition != null) {
if (preferences.insert_space_after_semicolon_in_for) { if (preferences.insert_space_after_semicolon_in_for) {
@ -2546,6 +2560,8 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (peekNextToken() == Token.tSEMI) { if (peekNextToken() == Token.tSEMI) {
scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon_in_for); scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon_in_for);
} }
scribe.alignFragment(alignment, 1);
IASTExpression iterationExpr= node.getIterationExpression(); IASTExpression iterationExpr= node.getIterationExpression();
if (iterationExpr != null) { if (iterationExpr != null) {
if (preferences.insert_space_after_semicolon_in_for) { if (preferences.insert_space_after_semicolon_in_for) {
@ -2559,8 +2575,39 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (peekNextToken() == Token.tRPAREN) { if (peekNextToken() == Token.tRPAREN) {
scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_for); scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_for);
} }
IASTStatement body = node.getBody();
if (body instanceof IASTCompoundStatement && !startsWithMacroExpansion(body)) {
formatLeftCurlyBrace(line, preferences.brace_position_for_block);
if (startNode(body)) {
try {
formatBlockOpening((IASTCompoundStatement) body,
preferences.brace_position_for_block,
preferences.insert_space_before_opening_brace_in_block);
ok = true;
scribe.exitAlignment(alignment, true);
formatOpenedBlock((IASTCompoundStatement) body,
preferences.brace_position_for_block,
preferences.indent_statements_compare_to_block);
} finally {
endOfNode(body);
}
} else {
ok = true;
scribe.exitAlignment(alignment, true);
}
} else {
ok = true;
scribe.exitAlignment(alignment, true);
formatAction(line, body, preferences.brace_position_for_block);
}
} catch (AlignmentException e) {
if (ok) {
throw e;
}
scribe.redoAlignment(e);
}
} while (!ok);
formatAction(line, node.getBody(), preferences.brace_position_for_block);
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -3200,7 +3247,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
formatLeftCurlyBrace(line, brace_position); formatLeftCurlyBrace(line, brace_position);
if (startNode(stmt)) { if (startNode(stmt)) {
try { try {
formatBlock((IASTCompoundStatement)stmt, brace_position, preferences.insert_space_before_opening_brace_in_block, preferences.indent_statements_compare_to_block); formatBlock((IASTCompoundStatement) stmt, brace_position,
preferences.insert_space_before_opening_brace_in_block,
preferences.indent_statements_compare_to_block);
} finally { } finally {
endOfNode(stmt); endOfNode(stmt);
} }
@ -3268,14 +3317,24 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
return false; return false;
} }
private void formatBlock(IASTCompoundStatement block, String block_brace_position, boolean insertSpaceBeforeOpeningBrace, boolean indentStatements) { private void formatBlock(IASTCompoundStatement block, String block_brace_position,
final boolean startsWithMacroExpansion= startsWithMacroExpansion(block); boolean insertSpaceBeforeOpeningBrace, boolean indentStatements) {
if (!startsWithMacroExpansion) { formatBlockOpening(block, block_brace_position, insertSpaceBeforeOpeningBrace);
formatOpenedBlock(block, block_brace_position, indentStatements);
}
private void formatBlockOpening(IASTCompoundStatement block, String block_brace_position,
boolean insertSpaceBeforeOpeningBrace) {
if (!startsWithMacroExpansion(block)) {
formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace); formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace);
} else { } else {
scribe.startNewLine(); scribe.startNewLine();
scribe.printComment(); scribe.printComment();
} }
}
private void formatOpenedBlock(IASTCompoundStatement block, String block_brace_position,
boolean indentStatements) {
final boolean endsWithMacroExpansion= endsWithMacroExpansion(block); final boolean endsWithMacroExpansion= endsWithMacroExpansion(block);
IASTStatement[] statements = block.getStatements(); IASTStatement[] statements = block.getStatements();
final int statementsLength = statements.length; final int statementsLength = statements.length;
@ -3300,7 +3359,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
if (!endsWithMacroExpansion) { if (!endsWithMacroExpansion) {
formatClosingBrace(block_brace_position); formatClosingBrace(block_brace_position);
} else if (!startsWithMacroExpansion) { } else if (!startsWithMacroExpansion(block)) {
if (DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED.equals(block_brace_position)) { if (DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED.equals(block_brace_position)) {
scribe.unIndent(); scribe.unIndent();
} }

View file

@ -199,6 +199,45 @@ public class CodeFormatterTest extends BaseUITestCase {
assertFormatterResult(); assertFormatterResult();
} }
//class ClassWithALongName {
//public:
//class Iterator {
//bool isDone();
//void next();
//};
//
//Iterator getIterator();
//};
//
//void test() {
//ClassWithALongName* variable_with_a_long_name;
//for (ClassWithALongName::Iterator iter_for_class_with_a_long_name = variable_with_a_long_name->getIterator(); !iter_for_class_with_a_long_name.isDone(); iter_for_class_with_a_long_name.next()) {
//}
//}
//class ClassWithALongName {
//public:
// class Iterator {
// bool isDone();
// void next();
// };
//
// Iterator getIterator();
//};
//
//void test() {
// ClassWithALongName* variable_with_a_long_name;
// for (ClassWithALongName::Iterator
// iter_for_class_with_a_long_name = variable_with_a_long_name->getIterator();
// !iter_for_class_with_a_long_name.isDone();
// iter_for_class_with_a_long_name.next()) {
// }
//}
public void testForWithEmptyExpression_Bug280989() throws Exception {
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
assertFormatterResult();
}
//#define MY private: //#define MY private:
// //
//class ClassA //class ClassA