Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java21
-rw-r--r--org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java6
2 files changed, 26 insertions, 1 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java
index 7372b2b857..298ae13dc1 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java
@@ -12938,4 +12938,25 @@ public void testBug542625() {
"}";
formatSource(source);
}
+/**
+ * https://bugs.eclipse.org/543780 - [formatter] Compact 'if else': can't wrap before else statement
+ */
+public void testBug543780() {
+ this.formatterPrefs.keep_then_statement_on_same_line = true;
+ this.formatterPrefs.keep_else_statement_on_same_line = true;
+ this.formatterPrefs.alignment_for_compact_if = Alignment.M_ONE_PER_LINE_SPLIT + Alignment.M_FORCE;
+ this.formatterPrefs.use_tabs_only_for_leading_indentations = true;
+ String source =
+ "class Example {\n" +
+ " int foo(int argument) {\n" +
+ " if (argument == 0)\n" +
+ " return 0;\n" +
+ " if (argument == 1)\n" +
+ " return 42;\n" +
+ " else\n" +
+ " return 43;\n" +
+ " }\n" +
+ "}";
+ formatSource(source);
+}
}
diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java
index 75595ce35c..0d39d3e9ed 100644
--- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java
+++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java
@@ -763,10 +763,14 @@ public class WrapPreparator extends ASTVisitor {
int rParen = this.tm.firstIndexAfter(node.getExpression(), TokenNameRPAREN);
handleParenthesesPositions(lParen, rParen, this.options.parenthesis_positions_in_if_while_statement);
+ Statement elseStatement = node.getElseStatement();
boolean keepThenOnSameLine = this.options.keep_then_statement_on_same_line
- || (this.options.keep_simple_if_on_one_line && node.getElseStatement() == null);
+ || (this.options.keep_simple_if_on_one_line && elseStatement == null);
if (keepThenOnSameLine)
handleSimpleLoop(node.getThenStatement(), this.options.alignment_for_compact_if);
+
+ if (this.options.keep_else_statement_on_same_line && elseStatement != null)
+ handleSimpleLoop(elseStatement, this.options.alignment_for_compact_if);
return true;
}

Back to the top