diff options
| author | Markus Keller | 2015-02-16 17:41:34 +0000 |
|---|---|---|
| committer | Markus Keller | 2015-02-16 17:41:34 +0000 |
| commit | 51017f7d8c9ef0baaf841def4e614f01aa0f81d6 (patch) | |
| tree | 3006bff004d88aa6c6aedef9ca96719856fd0c4b | |
| parent | 0b6f06d77f121375ae3494c8c329aad2cfc0a1a4 (diff) | |
| download | eclipse.jdt.core-51017f7d8c9ef0baaf841def4e614f01aa0f81d6.tar.gz eclipse.jdt.core-51017f7d8c9ef0baaf841def4e614f01aa0f81d6.tar.xz eclipse.jdt.core-51017f7d8c9ef0baaf841def4e614f01aa0f81d6.zip | |
Bug 460008: [formatter] Inserts wrong line breaks on ASTRewrite (Extract Constant, Extract Local Variable)
3 files changed, 50 insertions, 9 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 dd55b344ec..2ae7192584 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 @@ -10617,4 +10617,26 @@ public void testBug437639() throws Exception { "}\n" ); } +/** + * @bug 460008: [formatter] Inserts wrong line breaks on ASTRewrite (Extract Constant, Extract Local Variable) + * @test test line break is not added at end of expression + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=460008" + */ +public void testBug460008() throws Exception { + this.formatterPrefs.insert_new_line_at_end_of_file_if_missing = true; + String source = "name"; + + formatSource(source, source, CodeFormatter.K_EXPRESSION); + formatSource(source, source, CodeFormatter.K_UNKNOWN); + + source = "public int field = 1;"; + formatSource(source, source, CodeFormatter.K_STATEMENTS | CodeFormatter.F_INCLUDE_COMMENTS); + + source = "/**Javadoc*/public int field=1;"; + String result = "/** Javadoc */\n" + + "public int field = 1;"; + formatSource(source, result, CodeFormatter.K_CLASS_BODY_DECLARATIONS | CodeFormatter.F_INCLUDE_COMMENTS); + + // K_COMPILATION_UNIT is tested by FormatterRegressionTests#test512() and #test643() +} } diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java index b4c224523e..7cceb885df 100644 --- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java @@ -192,7 +192,7 @@ public class DefaultCodeFormatter extends CodeFormatter { prepareSpaces(); prepareLineBreaks(); prepareComments(); - prepareWraps(); + prepareWraps(kind); this.tokenManager.applyFormatOff(); @@ -366,8 +366,8 @@ public class DefaultCodeFormatter extends CodeFormatter { commentsPreparator.finishUp(); } - private void prepareWraps() { - WrapPreparator wrapPreparator = new WrapPreparator(this.tokenManager, this.workingOptions); + private void prepareWraps(int kind) { + WrapPreparator wrapPreparator = new WrapPreparator(this.tokenManager, this.workingOptions, kind); this.astRoot.accept(wrapPreparator); wrapPreparator.finishUp(); } 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 00b1dc6060..a49ce78ede 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 @@ -10,7 +10,22 @@ *******************************************************************************/ package org.eclipse.jdt.internal.formatter.linewrap; -import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.*; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameCOLON; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameCOMMENT_BLOCK; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameCOMMENT_JAVADOC; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameCOMMENT_LINE; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameDOT; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameEQUAL; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameLBRACE; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameLPAREN; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameOR; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameQUESTION; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameRPAREN; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameStringLiteral; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameextends; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameimplements; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNamenew; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNamethrows; import java.util.ArrayList; import java.util.Collections; @@ -52,12 +67,13 @@ import org.eclipse.jdt.core.dom.UnionType; import org.eclipse.jdt.core.dom.VariableDeclaration; import org.eclipse.jdt.core.dom.VariableDeclarationFragment; import org.eclipse.jdt.core.dom.VariableDeclarationStatement; +import org.eclipse.jdt.core.formatter.CodeFormatter; import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions; +import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions.Alignment; import org.eclipse.jdt.internal.formatter.Token; +import org.eclipse.jdt.internal.formatter.Token.WrapPolicy; import org.eclipse.jdt.internal.formatter.TokenManager; import org.eclipse.jdt.internal.formatter.TokenTraverser; -import org.eclipse.jdt.internal.formatter.Token.WrapPolicy; -import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions.Alignment; public class WrapPreparator extends ASTVisitor { @@ -85,7 +101,8 @@ public class WrapPreparator extends ASTVisitor { final TokenManager tm; final DefaultCodeFormatterOptions options; - + final int kind; + FieldAligner fieldAligner; int importsStart = -1, importsEnd = -1; @@ -101,9 +118,10 @@ public class WrapPreparator extends ASTVisitor { private int currentDepth = 0; - public WrapPreparator(TokenManager tokenManager, DefaultCodeFormatterOptions options) { + public WrapPreparator(TokenManager tokenManager, DefaultCodeFormatterOptions options, int kind) { this.tm = tokenManager; this.options = options; + this.kind = kind; } @Override @@ -722,7 +740,8 @@ public class WrapPreparator extends ASTVisitor { endingBreaks = Math.min(endingBreaks, this.options.number_of_empty_lines_to_preserve); if (endingBreaks > 0) { last.putLineBreaksAfter(endingBreaks); - } else if (this.options.insert_new_line_at_end_of_file_if_missing) { + } else if ((this.kind & CodeFormatter.K_COMPILATION_UNIT) != 0 + && this.options.insert_new_line_at_end_of_file_if_missing) { last.breakAfter(); } } |
