From ee055684b2e04288c0c41233ea3fd01c63e87ada Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Mon, 11 Apr 2011 00:32:13 +0000 Subject: [PATCH] Code formatter fixes. --- .../core/formatter/DefaultCodeFormatterConstants.java | 2 +- .../cdt/internal/formatter/CodeFormatterVisitor.java | 8 +++++--- .../internal/formatter/DefaultCodeFormatterOptions.java | 6 +++--- .../eclipse/cdt/internal/formatter/align/Alignment.java | 9 +++++++++ .../org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java | 9 +++++++++ 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java index 7e40759e82e..eaa5781b257 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java @@ -146,7 +146,7 @@ public class DefaultCodeFormatterConstants { * FORMATTER / Option for alignment of conditional expression * - option id: "org.eclipse.cdt.core.formatter.alignment_for_conditional_expression" * - possible values: values returned by createAlignmentValue(boolean, int, int) call - * - default: createAlignmentValue(false, WRAP_COMPACT, INDENT_ON_COLUMN) + * - default: createAlignmentValue(false, WRAP_COMPACT_FIRST_BREAK, INDENT_ON_COLUMN) * * @see #createAlignmentValue(boolean, int, int) */ 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 c1187c2037a..b75d7d98117 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 @@ -2622,8 +2622,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, } private int formatAssignment(IASTBinaryExpression node) { + Runnable tailFormatter = scribe.takeTailFormatter(); final IASTExpression op1= node.getOperand1(); - // operand 1 + // Operand 1 op1.accept(this); // In case of macros we may have already passed the equal sign position. @@ -2652,10 +2653,11 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, try { scribe.alignFragment(expressionAlignment, 0); - // operand 2 + scribe.setTailFormatter(tailFormatter); + // Operand 2 final IASTExpression op2= node.getOperand2(); op2.accept(this); - + scribe.runTailFormatter(); ok = true; } catch (AlignmentException e) { scribe.redoAlignment(e); diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/DefaultCodeFormatterOptions.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/DefaultCodeFormatterOptions.java index 5e940f6617a..ff63edb3824 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/DefaultCodeFormatterOptions.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/DefaultCodeFormatterOptions.java @@ -530,9 +530,9 @@ public class DefaultCodeFormatterOptions { try { this.alignment_for_conditional_expression = Integer.parseInt((String) alignmentForConditionalExpressionOption); } catch (NumberFormatException e) { - this.alignment_for_conditional_expression = Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN; + this.alignment_for_conditional_expression = Alignment.M_COMPACT_FIRST_BREAK_SPLIT | Alignment.M_INDENT_ON_COLUMN; } catch (ClassCastException e) { - this.alignment_for_conditional_expression = Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN; + this.alignment_for_conditional_expression = Alignment.M_COMPACT_FIRST_BREAK_SPLIT | Alignment.M_INDENT_ON_COLUMN; } } final Object alignmentForConditionalExpressionChainOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_CONDITIONAL_EXPRESSION_CHAIN); @@ -1496,7 +1496,7 @@ public class DefaultCodeFormatterOptions { this.alignment_for_base_clause_in_type_declaration = Alignment.M_NEXT_PER_LINE_SPLIT; this.alignment_for_binary_expression = Alignment.M_COMPACT_SPLIT; this.alignment_for_compact_if = Alignment.M_COMPACT_SPLIT; - this.alignment_for_conditional_expression = Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN; + this.alignment_for_conditional_expression = Alignment.M_COMPACT_FIRST_BREAK_SPLIT | Alignment.M_INDENT_ON_COLUMN; this.alignment_for_conditional_expression_chain = Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN; this.alignment_for_declarator_list = Alignment.M_COMPACT_SPLIT; this.alignment_for_enumerator_list = Alignment.M_ONE_PER_LINE_SPLIT; diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/align/Alignment.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/align/Alignment.java index c05c7964829..cee153fe123 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/align/Alignment.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/align/Alignment.java @@ -268,6 +268,15 @@ public class Alignment { */ case M_COMPACT_FIRST_BREAK_SPLIT: if (this.fragmentBreaks[0] == NONE) { + if ((this.mode & M_INDENT_ON_COLUMN) != 0) { + if (this.breakIndentationLevel <= this.alternativeBreakIndentationLevel) { + // Does not make sense to break here unless indentation is reduced. + break; + } + // Change break indentation level and erase previously created breaks. + this.breakIndentationLevel = this.alternativeBreakIndentationLevel; + eraseExistingBreaks(0); + } this.fragmentBreaks[0] = BREAK; this.fragmentIndentations[0] = this.breakIndentationLevel; return wasSplit = true; 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 788dc08c87e..c11e288a01e 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 @@ -2084,6 +2084,9 @@ public class CodeFormatterTest extends BaseUITestCase { //int variable2 = 1000000 < 2000000 ? 3000000 + 40000000 : 8000000 + 900000000; //int variable3 = 1000000 < 2000000 ? 3000000 + 4000000 + 5000000 + 6000000 + 7000000 : 8000000 + 9000000; //int variable4 = 1000000 < 2000000 ? 3000000 + 4000000 + 5000000 + 6000000 + 7000000 : 8000000 + 90000000; + //int variable5; + //variable5 = 10000000 < 2000000 ? 3000000 + 4000000 + 5000000 + 6000000 : 700000; + //variable5 = 10000000 < 2000000 ? 3000000 + 4000000 + 5000000 + 6000000 : 7000000; //} //void test() { @@ -2098,6 +2101,12 @@ public class CodeFormatterTest extends BaseUITestCase { // 1000000 < 2000000 ? // 3000000 + 4000000 + 5000000 + 6000000 + 7000000 : // 8000000 + 90000000; + // int variable5; + // variable5 = + // 10000000 < 2000000 ? 3000000 + 4000000 + 5000000 + 6000000 : 700000; + // variable5 = + // 10000000 < 2000000 ? + // 3000000 + 4000000 + 5000000 + 6000000 : 7000000; //} public void testConditionalExpression() throws Exception { fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);