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,48 +2518,96 @@ 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;
try { Alignment alignment = scribe.createAlignment(
if (preferences.insert_space_after_opening_paren_in_for) { "for", //$NON-NLS-1$
scribe.space(); Alignment.M_COMPACT_SPLIT,
} Alignment.R_OUTERMOST,
IASTStatement initializerStmt= node.getInitializerStatement(); 2,
initializerStmt.accept(this); scribe.scanner.getCurrentPosition(),
if (peekNextToken() == Token.tSEMI) { preferences.continuation_indentation,
scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon_in_for); false);
} scribe.enterAlignment(alignment);
final IASTExpression condition = node.getConditionExpression();
if (condition != null) { boolean ok = false;
if (preferences.insert_space_after_semicolon_in_for) { do {
scribe.space(); try {
} try {
condition.accept(this); if (preferences.insert_space_after_opening_paren_in_for) {
} else if (node instanceof ICPPASTForStatement) {
final IASTDeclaration conditionDecl = ((ICPPASTForStatement) node).getConditionDeclaration();
if (conditionDecl != null) {
if (preferences.insert_space_after_semicolon_in_for) {
scribe.space(); scribe.space();
} }
conditionDecl.accept(this); IASTStatement initializerStmt= node.getInitializerStatement();
} initializerStmt.accept(this);
} 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); }
}
IASTExpression iterationExpr= node.getIterationExpression(); scribe.alignFragment(alignment, 0);
if (iterationExpr != null) { final IASTExpression condition = node.getConditionExpression();
if (preferences.insert_space_after_semicolon_in_for) { if (condition != null) {
scribe.space(); if (preferences.insert_space_after_semicolon_in_for) {
} scribe.space();
iterationExpr.accept(this); }
} condition.accept(this);
} finally { } else if (node instanceof ICPPASTForStatement) {
fInsideFor= false; final IASTDeclaration conditionDecl = ((ICPPASTForStatement) node).getConditionDeclaration();
} if (conditionDecl != null) {
if (peekNextToken() == Token.tRPAREN) { if (preferences.insert_space_after_semicolon_in_for) {
scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_for); scribe.space();
} }
conditionDecl.accept(this);
}
}
if (peekNextToken() == Token.tSEMI) {
scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon_in_for);
}
scribe.alignFragment(alignment, 1);
IASTExpression iterationExpr= node.getIterationExpression();
if (iterationExpr != null) {
if (preferences.insert_space_after_semicolon_in_for) {
scribe.space();
}
iterationExpr.accept(this);
}
} finally {
fInsideFor= false;
}
if (peekNextToken() == Token.tRPAREN) {
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();
} }
@ -3359,7 +3418,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
final boolean statementIsNullStmt= statement instanceof IASTNullStatement; final boolean statementIsNullStmt= statement instanceof IASTNullStatement;
if ((previousStatementIsNullStmt && !statementIsNullStmt) if ((previousStatementIsNullStmt && !statementIsNullStmt)
|| (!previousStatementIsNullStmt && !statementIsNullStmt)) { || (!previousStatementIsNullStmt && !statementIsNullStmt)) {
scribe.startNewLine(); scribe.startNewLine();
} }
try { try {

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