diff options
author | Mateusz Matela | 2019-01-28 20:47:16 +0000 |
---|---|---|
committer | Mateusz Matela | 2019-01-28 20:47:46 +0000 |
commit | ab5b1aa805358dc4dc51ab38d117658c23ba22e0 (patch) | |
tree | 4f83e9231064092eae2cc9cbdc942088f4a3efc2 | |
parent | ec7b27d00e7457096b173be6b860b3d9fd92f3e5 (diff) | |
download | eclipse.jdt.core-ab5b1aa805358dc4dc51ab38d117658c23ba22e0.tar.gz eclipse.jdt.core-ab5b1aa805358dc4dc51ab38d117658c23ba22e0.tar.xz eclipse.jdt.core-ab5b1aa805358dc4dc51ab38d117658c23ba22e0.zip |
Bug 543780 - [formatter] Compact 'if else': can't wrap before elseI20190129-1800I20190128-1800
statement
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; } |