diff options
author | Rory Hunter | 2020-04-28 07:11:10 +0000 |
---|---|---|
committer | Mateusz Matela | 2020-06-25 15:40:02 +0000 |
commit | e8a8430665b53e3798f10bcfcd1a1703f0cfa7e3 (patch) | |
tree | 7e79eeafe887259f8fafe10a37777c78f4d2fa94 | |
parent | 0602ab3ba9f0f9a6e47da158954a3ce747cb37ce (diff) | |
download | eclipse.jdt.core-e8a8430665b53e3798f10bcfcd1a1703f0cfa7e3.tar.gz eclipse.jdt.core-e8a8430665b53e3798f10bcfcd1a1703f0cfa7e3.tar.xz eclipse.jdt.core-e8a8430665b53e3798f10bcfcd1a1703f0cfa7e3.zip |
Bug 118641 - [formatter] Formatter does not break line on assertI20200625-1800
statement
Change-Id: I4da1278fb4d37caa752bc49122c0b9cd7bd9608a
Signed-off-by: Rory Hunter <rory@elastic.co>
6 files changed, 191 insertions, 2 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java index 5bc707d43b..73544ea312 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java @@ -15635,4 +15635,137 @@ public void testBug553155p() throws JavaModelException { formatSource(source, "record Range(int lo, int hi) { public Range { foo(); } }"); } +/** + * https://bugs.eclipse.org/118641 - [formatter] Formatter does not break line on assert statement + * + * Check that assertions are not formatted without enabling the option. + */ +public void testBug118641a() throws JavaModelException { + this.formatterPrefs.alignment_for_assertion_message = Alignment.M_NO_ALIGNMENT; + + String input = + "public class Test {\n" + + "\n" + + " public void f(int a, int b) {\n" + + " assert childElement.getElementsByTagName(FIELD_NAME).getLength() == 1 : \"XML malformed. No attribute name! Please talk to your sysadmin.\";\n" + + " return;\n" + + " }\n" + + "}"; + formatSource(input, + "public class Test {\n" + + "\n" + + " public void f(int a, int b) {\n" + + " assert childElement.getElementsByTagName(FIELD_NAME)\n" + + " .getLength() == 1 : \"XML malformed. No attribute name! Please talk to your sysadmin.\";\n" + + " return;\n" + + " }\n" + + "}"); +} +/** + * https://bugs.eclipse.org/118641 - [formatter] Formatter does not break line on assert statement + * + * + * Check that an assertion is formatted when it exceeds the page width. + */ +public void testBug118641b() throws JavaModelException { + this.formatterPrefs.wrap_before_assertion_message_operator = true; + + String input = + "public class Test {\n" + + " \n" + + " public void f(int a, int b) {\n" + + " assert childElement.getElementsByTagName( FIELD_NAME ).getLength() == 1 : \"XML malformed. No attribute name! Please talk to your sysadmin.\";\n" + + " return;\n" + + " }\n" + + "}"; + formatSource(input, + "public class Test {\n" + + "\n" + + " public void f(int a, int b) {\n" + + " assert childElement.getElementsByTagName(FIELD_NAME).getLength() == 1\n" + + " : \"XML malformed. No attribute name! Please talk to your sysadmin.\";\n" + + " return;\n" + + " }\n" + + "}"); +} +/** + * https://bugs.eclipse.org/118641 - [formatter] Formatter does not break line on assert statement + * + * + * Check that an assertion can be broken after the colon separator. + */ +public void testBug118641c() throws JavaModelException { + this.formatterPrefs.wrap_before_assertion_message_operator = false; + + String input = + "public class Test {\n" + + " \n" + + " public void f(int a, int b) {\n" + + " assert childElement.getElementsByTagName( FIELD_NAME ).getLength() == 1 : \"XML malformed. No attribute name! Please talk to your sysadmin.\";\n" + + " return;\n" + + " }\n" + + "}"; + formatSource(input, + "public class Test {\n" + + "\n" + + " public void f(int a, int b) {\n" + + " assert childElement.getElementsByTagName(FIELD_NAME).getLength() == 1 :\n" + + " \"XML malformed. No attribute name! Please talk to your sysadmin.\";\n" + + " return;\n" + + " }\n" + + "}"); +} +/** + * https://bugs.eclipse.org/118641 - [formatter] Formatter does not break line on assert statement + * + * Check that assertion messages are formatted when the assertion expression exceeds the page width. + */ +public void testBug118641d() throws JavaModelException { + this.formatterPrefs.page_width = 65; + + String input = + "public class Test {\n" + + " \n" + + " public void f(int a, int b) {\n" + + " assert childElement.getElementsByTagName( FIELD_NAME ).getLength() == 1 : \"XML malformed. No attribute name! Please talk to your sysadmin.\";\n" + + " return;\n" + + " }\n" + + "}"; + formatSource(input, + "public class Test {\n" + + "\n" + + " public void f(int a, int b) {\n" + + " assert childElement.getElementsByTagName(FIELD_NAME)\n" + + " .getLength() == 1\n" + + " : \"XML malformed. No attribute name! Please talk to your sysadmin.\";\n" + + " return;\n" + + " }\n" + + "}"); +} +/** + * https://bugs.eclipse.org/118641 - [formatter] Formatter does not break line on assert statement + * + * Check that an assertion message is wrapped when forced to do so. + */ +public void testBug118641e() throws JavaModelException { + this.formatterPrefs.alignment_for_assertion_message = Alignment.M_COMPACT_SPLIT | Alignment.M_FORCE; + + String input = + "public class Test {\n" + + " \n" + + " public void f(int a, int b) {\n" + + " assert childElement.getElementsByTagName( FIELD_NAME ).getLength() == 1 : \"XML malformed. No attribute name!\";\n" + + " return;\n" + + " }\n" + + "}"; + formatSource(input, + "public class Test {\n" + + "\n" + + " public void f(int a, int b) {\n" + + " assert childElement.getElementsByTagName(FIELD_NAME).getLength() == 1\n" + + " : \"XML malformed. No attribute name!\";\n" + + " return;\n" + + " }\n" + + "}"); +} } diff --git a/org.eclipse.jdt.core/META-INF/MANIFEST.MF b/org.eclipse.jdt.core/META-INF/MANIFEST.MF index ba284241e1..32cbf3c8b9 100644 --- a/org.eclipse.jdt.core/META-INF/MANIFEST.MF +++ b/org.eclipse.jdt.core/META-INF/MANIFEST.MF @@ -3,7 +3,7 @@ Main-Class: org.eclipse.jdt.internal.compiler.batch.Main Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.jdt.core; singleton:=true -Bundle-Version: 3.22.100.qualifier +Bundle-Version: 3.23.0.qualifier Bundle-Activator: org.eclipse.jdt.core.JavaCore Bundle-Vendor: %providerName Bundle-Localization: plugin diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java index 7f0a169627..14b018e558 100644 --- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java @@ -205,6 +205,17 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_ALIGNMENT_FOR_ASSIGNMENT = JavaCore.PLUGIN_ID + ".formatter.alignment_for_assignment"; //$NON-NLS-1$ /** * <pre> + * FORMATTER / Option for alignment of assertion message separator (:) + * - option id: "org.eclipse.jdt.core.formatter.alignment_for_assertion_message" + * - possible values: values returned by <code>createAlignmentValue(boolean, int, int)</code> call + * - default: createAlignmentValue(false, WRAP_NO_SPLIT, INDENT_DEFAULT) + * </pre> + * @see #createAlignmentValue(boolean, int, int) + * @since 3.23 + */ + public static final String FORMATTER_ALIGNMENT_FOR_ASSERTION_MESSAGE = JavaCore.PLUGIN_ID + ".formatter.alignment_for_assertion_message"; //$NON-NLS-1$ + /** + * <pre> * FORMATTER / Option for alignment of expressions with multiplicative operators (*, /, %) * - option id: "org.eclipse.jdt.core.formatter.alignment_for_multiplicative_operator" * - possible values: values returned by <code>createAlignmentValue(boolean, int, int)</code> call @@ -5112,6 +5123,18 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_WRAP_BEFORE_ADDITIVE_OPERATOR = JavaCore.PLUGIN_ID + ".formatter.wrap_before_additive_operator"; //$NON-NLS-1$ /** * <pre> + * FORMATTER / Option to wrap before the assertion message operator + * - option id: "org.eclipse.jdt.core.formatter.wrap_before_assertion_message_operator" + * - possible values: { TRUE, FALSE } + * - default: FALSE + * </pre> + * @see #TRUE + * @see #FALSE + * @since 3.23 + */ + public static final String FORMATTER_WRAP_BEFORE_ASSERTION_MESSAGE_OPERATOR = JavaCore.PLUGIN_ID + ".formatter.wrap_before_assertion_message_operator"; //$NON-NLS-1$ + /** + * <pre> * FORMATTER / Option to wrap before the string concatenation operator * - option id: "org.eclipse.jdt.core.formatter.wrap_before_string_concatenation" * - possible values: { TRUE, FALSE } diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java index 0cb87ac5f9..6eb0ed3bd6 100644 --- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java @@ -125,6 +125,7 @@ public class DefaultCodeFormatterOptions { public int alignment_for_arguments_in_explicit_constructor_call; public int alignment_for_arguments_in_method_invocation; public int alignment_for_arguments_in_qualified_allocation_expression; + public int alignment_for_assertion_message; public int alignment_for_assignment; public int alignment_for_multiplicative_operator; public int alignment_for_additive_operator; @@ -500,6 +501,7 @@ public class DefaultCodeFormatterOptions { public int text_block_indentation; public boolean wrap_before_multiplicative_operator; public boolean wrap_before_additive_operator; + public boolean wrap_before_assertion_message_operator; public boolean wrap_before_string_concatenation; public boolean wrap_before_shift_operator; public boolean wrap_before_relational_operator; @@ -556,6 +558,7 @@ public class DefaultCodeFormatterOptions { options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_EXPLICIT_CONSTRUCTOR_CALL, getAlignment(this.alignment_for_arguments_in_explicit_constructor_call)); options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION, getAlignment(this.alignment_for_arguments_in_method_invocation)); options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_QUALIFIED_ALLOCATION_EXPRESSION, getAlignment(this.alignment_for_arguments_in_qualified_allocation_expression)); + options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ASSERTION_MESSAGE, getAlignment(this.alignment_for_assertion_message)); options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ASSIGNMENT, getAlignment(this.alignment_for_assignment)); options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_MULTIPLICATIVE_OPERATOR ,getAlignment(this.alignment_for_multiplicative_operator)); options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ADDITIVE_OPERATOR ,getAlignment(this.alignment_for_additive_operator)); @@ -942,6 +945,7 @@ public class DefaultCodeFormatterOptions { options.put(DefaultCodeFormatterConstants.FORMATTER_WRAP_BEFORE_MULTIPLICATIVE_OPERATOR, this.wrap_before_multiplicative_operator ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_WRAP_BEFORE_ADDITIVE_OPERATOR, this.wrap_before_additive_operator ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); + options.put(DefaultCodeFormatterConstants.FORMATTER_WRAP_BEFORE_ASSERTION_MESSAGE_OPERATOR, this.wrap_before_assertion_message_operator ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_WRAP_BEFORE_STRING_CONCATENATION, this.wrap_before_string_concatenation ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_WRAP_BEFORE_SHIFT_OPERATOR, this.wrap_before_shift_operator ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_WRAP_BEFORE_RELATIONAL_OPERATOR, this.wrap_before_relational_operator ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); @@ -1006,6 +1010,8 @@ public class DefaultCodeFormatterOptions { this.alignment_for_arguments_in_qualified_allocation_expression = Alignment.M_COMPACT_SPLIT; } } + setInt(settings, DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ASSERTION_MESSAGE, + v -> this.alignment_for_assertion_message = v); final Object alignmentForAssignmentOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ASSIGNMENT); if (alignmentForAssignmentOption != null) { try { @@ -2491,6 +2497,8 @@ public class DefaultCodeFormatterOptions { } setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_WRAP_BEFORE_MULTIPLICATIVE_OPERATOR, DefaultCodeFormatterConstants.TRUE, v -> this.wrap_before_multiplicative_operator = v); + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_WRAP_BEFORE_ASSERTION_MESSAGE_OPERATOR, DefaultCodeFormatterConstants.TRUE, + v -> this.wrap_before_assertion_message_operator = v); setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_WRAP_BEFORE_ADDITIVE_OPERATOR, DefaultCodeFormatterConstants.TRUE, v -> this.wrap_before_additive_operator = v); setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_WRAP_BEFORE_STRING_CONCATENATION, DefaultCodeFormatterConstants.TRUE, @@ -2882,6 +2890,7 @@ public class DefaultCodeFormatterOptions { this.alignment_for_arguments_in_explicit_constructor_call = Alignment.M_COMPACT_SPLIT; this.alignment_for_arguments_in_method_invocation = Alignment.M_COMPACT_SPLIT; this.alignment_for_arguments_in_qualified_allocation_expression = Alignment.M_COMPACT_SPLIT; + this.alignment_for_assertion_message = Alignment.M_COMPACT_SPLIT; this.alignment_for_assignment = Alignment.M_NO_ALIGNMENT; this.alignment_for_multiplicative_operator = Alignment.M_COMPACT_SPLIT; this.alignment_for_additive_operator = Alignment.M_COMPACT_SPLIT; @@ -3237,6 +3246,7 @@ public class DefaultCodeFormatterOptions { this.text_block_indentation = Alignment.M_INDENT_DEFAULT; this.wrap_before_multiplicative_operator = true; this.wrap_before_additive_operator = true; + this.wrap_before_assertion_message_operator = true; this.wrap_before_string_concatenation = true; this.wrap_before_shift_operator = true; this.wrap_before_relational_operator = true; @@ -3264,6 +3274,7 @@ public class DefaultCodeFormatterOptions { this.alignment_for_arguments_in_explicit_constructor_call = Alignment.M_COMPACT_SPLIT; this.alignment_for_arguments_in_method_invocation = Alignment.M_COMPACT_SPLIT; this.alignment_for_arguments_in_qualified_allocation_expression = Alignment.M_COMPACT_SPLIT; + this.alignment_for_assertion_message = Alignment.M_COMPACT_SPLIT; this.alignment_for_assignment = Alignment.M_NO_ALIGNMENT; this.alignment_for_multiplicative_operator = Alignment.M_COMPACT_SPLIT; this.alignment_for_additive_operator = Alignment.M_COMPACT_SPLIT; @@ -3619,6 +3630,7 @@ public class DefaultCodeFormatterOptions { this.text_block_indentation = Alignment.M_INDENT_DEFAULT; this.wrap_before_multiplicative_operator = true; this.wrap_before_additive_operator = true; + this.wrap_before_assertion_message_operator = true; this.wrap_before_string_concatenation = true; this.wrap_before_shift_operator = true; this.wrap_before_relational_operator = true; 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 83ef7dc53f..558fa885fe 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 @@ -56,6 +56,7 @@ import org.eclipse.jdt.core.dom.ASTVisitor; import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration; import org.eclipse.jdt.core.dom.AnonymousClassDeclaration; import org.eclipse.jdt.core.dom.ArrayInitializer; +import org.eclipse.jdt.core.dom.AssertStatement; import org.eclipse.jdt.core.dom.Assignment; import org.eclipse.jdt.core.dom.Block; import org.eclipse.jdt.core.dom.CatchClause; @@ -1088,6 +1089,26 @@ public class WrapPreparator extends ASTVisitor { return true; } + @Override + public boolean visit(AssertStatement node) { + Expression message = node.getMessage(); + if (message != null) { + int atColon = this.tm.firstIndexBefore(message, TokenNameCOLON); + int afterColon = this.tm.firstIndexIn(message, -1); + if (this.options.wrap_before_assertion_message_operator) { + this.wrapIndexes.add(atColon); + this.secondaryWrapIndexes.add(afterColon); + } else { + this.wrapIndexes.add(afterColon); + this.secondaryWrapIndexes.add(atColon); + } + this.wrapParentIndex = this.tm.firstIndexIn(node, -1); + this.wrapGroupEnd = this.tm.lastIndexIn(node, -1); + handleWrap(this.options.alignment_for_assertion_message); + } + return true; + } + /** * Makes sure all new lines within given node will have wrap policy so that * wrap executor will fix their indentation if necessary. diff --git a/org.eclipse.jdt.core/pom.xml b/org.eclipse.jdt.core/pom.xml index 437afd0c6d..14ff99c57f 100644 --- a/org.eclipse.jdt.core/pom.xml +++ b/org.eclipse.jdt.core/pom.xml @@ -18,7 +18,7 @@ </parent> <groupId>org.eclipse.jdt</groupId> <artifactId>org.eclipse.jdt.core</artifactId> - <version>3.22.100-SNAPSHOT</version> + <version>3.23.0-SNAPSHOT</version> <packaging>eclipse-plugin</packaging> <properties> |