diff options
author | Mateusz Matela | 2019-07-14 19:26:22 +0000 |
---|---|---|
committer | Mateusz Matela | 2019-07-29 20:58:52 +0000 |
commit | 1e052a8f67b9f7c8dc2c3f8bb4f7cafc00966e06 (patch) | |
tree | ac183347713acb9d8e786763da9afd118ace972c | |
parent | de94bf3e02a331d5abf69c0fbc4855e04f919774 (diff) | |
download | eclipse.jdt.core-1e052a8f67b9f7c8dc2c3f8bb4f7cafc00966e06.tar.gz eclipse.jdt.core-1e052a8f67b9f7c8dc2c3f8bb4f7cafc00966e06.tar.xz eclipse.jdt.core-1e052a8f67b9f7c8dc2c3f8bb4f7cafc00966e06.zip |
Bug 549249 - [formatter] Extend Blank Lines settings to remove excessI20190729-1800
lines
Change-Id: I93681309ba635155aab55cdc722ab6fdb306f0cc
Signed-off-by: Mateusz Matela <mateusz.matela@gmail.com>
20 files changed, 1570 insertions, 68 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 298ae13dc1..91ba60c9c7 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 @@ -10630,7 +10630,7 @@ public void testBug356851() throws Exception { * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=437639" */ public void testBug437639() throws Exception { - this.formatterPrefs.blank_lines_between_import_groups = 0; + this.formatterPrefs.blank_lines_between_import_groups = ~0; String source = "package com.test;\n" + "\n" + 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 678c648075..c76e787a2b 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 @@ -14912,4 +14912,112 @@ public void testBug543818g() throws JavaModelException { String input = getCompilationUnit("Formatter", "", "test543818", "in.java").getSource(); formatSource(input, getCompilationUnit("Formatter", "", "test543818", "G_out.java").getSource()); } +/** + * https://bugs.eclipse.org/549249 - [formatter] Extend Blank Lines settings to remove excess lines + */ +public void testBug549249a() throws JavaModelException { + this.formatterPrefs.number_of_empty_lines_to_preserve = 4; + this.formatterPrefs.blank_lines_after_imports = ~2; + String input = getCompilationUnit("Formatter", "", "test549249", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test549249", "A_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/549249 - [formatter] Extend Blank Lines settings to remove excess lines + */ +public void testBug549249b() throws JavaModelException { + this.formatterPrefs.number_of_empty_lines_to_preserve = 4; + this.formatterPrefs.blank_lines_after_package = ~1; + String input = getCompilationUnit("Formatter", "", "test549249", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test549249", "B_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/549249 - [formatter] Extend Blank Lines settings to remove excess lines + */ +public void testBug549249c() throws JavaModelException { + this.formatterPrefs.number_of_empty_lines_to_preserve = 4; + this.formatterPrefs.blank_lines_at_beginning_of_method_body = ~0; + String input = getCompilationUnit("Formatter", "", "test549249", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test549249", "C_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/549249 - [formatter] Extend Blank Lines settings to remove excess lines + */ +public void testBug549249d() throws JavaModelException { + this.formatterPrefs.number_of_empty_lines_to_preserve = 4; + this.formatterPrefs.blank_lines_before_field = ~3; + String input = getCompilationUnit("Formatter", "", "test549249", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test549249", "D_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/549249 - [formatter] Extend Blank Lines settings to remove excess lines + */ +public void testBug549249e() throws JavaModelException { + this.formatterPrefs.number_of_empty_lines_to_preserve = 4; + this.formatterPrefs.blank_lines_before_first_class_body_declaration = ~2; + String input = getCompilationUnit("Formatter", "", "test549249", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test549249", "E_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/549249 - [formatter] Extend Blank Lines settings to remove excess lines + */ +public void testBug549249f() throws JavaModelException { + this.formatterPrefs.number_of_empty_lines_to_preserve = 4; + this.formatterPrefs.blank_lines_before_imports = ~1; + String input = getCompilationUnit("Formatter", "", "test549249", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test549249", "F_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/549249 - [formatter] Extend Blank Lines settings to remove excess lines + */ +public void testBug549249g() throws JavaModelException { + this.formatterPrefs.number_of_empty_lines_to_preserve = 4; + this.formatterPrefs.blank_lines_before_member_type = ~0; + String input = getCompilationUnit("Formatter", "", "test549249", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test549249", "G_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/549249 - [formatter] Extend Blank Lines settings to remove excess lines + */ +public void testBug549249h() throws JavaModelException { + this.formatterPrefs.number_of_empty_lines_to_preserve = 4; + this.formatterPrefs.blank_lines_before_method = ~3; + String input = getCompilationUnit("Formatter", "", "test549249", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test549249", "H_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/549249 - [formatter] Extend Blank Lines settings to remove excess lines + */ +public void testBug549249i() throws JavaModelException { + this.formatterPrefs.number_of_empty_lines_to_preserve = 4; + this.formatterPrefs.blank_lines_before_new_chunk = ~2; + String input = getCompilationUnit("Formatter", "", "test549249", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test549249", "I_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/549249 - [formatter] Extend Blank Lines settings to remove excess lines + */ +public void testBug549249j() throws JavaModelException { + this.formatterPrefs.number_of_empty_lines_to_preserve = 4; + this.formatterPrefs.blank_lines_before_package = ~1; + String input = getCompilationUnit("Formatter", "", "test549249", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test549249", "J_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/549249 - [formatter] Extend Blank Lines settings to remove excess lines + */ +public void testBug549249k() throws JavaModelException { + this.formatterPrefs.number_of_empty_lines_to_preserve = 4; + this.formatterPrefs.blank_lines_between_import_groups = ~1; + String input = getCompilationUnit("Formatter", "", "test549249", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test549249", "K_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/549249 - [formatter] Extend Blank Lines settings to remove excess lines + */ +public void testBug549249l() throws JavaModelException { + this.formatterPrefs.number_of_empty_lines_to_preserve = 4; + this.formatterPrefs.blank_lines_between_type_declarations = ~3; + String input = getCompilationUnit("Formatter", "", "test549249", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test549249", "L_out.java").getSource()); +} } diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/A_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/A_out.java new file mode 100644 index 0000000000..99cf9d61ca --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/A_out.java @@ -0,0 +1,105 @@ + + + + +package foo.bar.baz; + + + + +import java.util.List; +import java.util.Vector; + + + + +import org.eclipse.jdt.core.dom.ASTParser; + + + + +import java.net.Socket; + + +public class Another { +} + + + + +public class Example { + + + + + public class Nested { + } + + + + + public static class Pair { + + + + + public String first; + + + + + public String second; +// Between here... + + + + +// ...and here are 10 blank lines + }; + + + + + private LinkedList fList; + + + + + public int counter; + + + + + public Example(LinkedList list) { + + + + + fList = list; + counter = 0; + } + + + + + public void push(Pair p) { + + + + + fList.add(p); + ++counter; + } + + + + + public Object pop() { + + + + + --counter; + return (Pair) fList.getLast(); + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/B_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/B_out.java new file mode 100644 index 0000000000..d85ddc4c42 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/B_out.java @@ -0,0 +1,104 @@ + + + + +package foo.bar.baz; + +import java.util.List; +import java.util.Vector; + + + + +import org.eclipse.jdt.core.dom.ASTParser; + + + + +import java.net.Socket; + + + + +public class Another { +} + + + + +public class Example { + + + + + public class Nested { + } + + + + + public static class Pair { + + + + + public String first; + + + + + public String second; +// Between here... + + + + +// ...and here are 10 blank lines + }; + + + + + private LinkedList fList; + + + + + public int counter; + + + + + public Example(LinkedList list) { + + + + + fList = list; + counter = 0; + } + + + + + public void push(Pair p) { + + + + + fList.add(p); + ++counter; + } + + + + + public Object pop() { + + + + + --counter; + return (Pair) fList.getLast(); + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/C_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/C_out.java new file mode 100644 index 0000000000..858e42a777 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/C_out.java @@ -0,0 +1,95 @@ + + + + +package foo.bar.baz; + + + + +import java.util.List; +import java.util.Vector; + + + + +import org.eclipse.jdt.core.dom.ASTParser; + + + + +import java.net.Socket; + + + + +public class Another { +} + + + + +public class Example { + + + + + public class Nested { + } + + + + + public static class Pair { + + + + + public String first; + + + + + public String second; +// Between here... + + + + +// ...and here are 10 blank lines + }; + + + + + private LinkedList fList; + + + + + public int counter; + + + + + public Example(LinkedList list) { + fList = list; + counter = 0; + } + + + + + public void push(Pair p) { + fList.add(p); + ++counter; + } + + + + + public Object pop() { + --counter; + return (Pair) fList.getLast(); + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/D_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/D_out.java new file mode 100644 index 0000000000..8b1a8552e5 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/D_out.java @@ -0,0 +1,104 @@ + + + + +package foo.bar.baz; + + + + +import java.util.List; +import java.util.Vector; + + + + +import org.eclipse.jdt.core.dom.ASTParser; + + + + +import java.net.Socket; + + + + +public class Another { +} + + + + +public class Example { + + + + + public class Nested { + } + + + + + public static class Pair { + + + + + public String first; + + + + public String second; +// Between here... + + + + +// ...and here are 10 blank lines + }; + + + + private LinkedList fList; + + + + public int counter; + + + + + public Example(LinkedList list) { + + + + + fList = list; + counter = 0; + } + + + + + public void push(Pair p) { + + + + + fList.add(p); + ++counter; + } + + + + + public Object pop() { + + + + + --counter; + return (Pair) fList.getLast(); + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/E_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/E_out.java new file mode 100644 index 0000000000..64c13caaaf --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/E_out.java @@ -0,0 +1,103 @@ + + + + +package foo.bar.baz; + + + + +import java.util.List; +import java.util.Vector; + + + + +import org.eclipse.jdt.core.dom.ASTParser; + + + + +import java.net.Socket; + + + + +public class Another { +} + + + + +public class Example { + + + public class Nested { + } + + + + + public static class Pair { + + + public String first; + + + + + public String second; +// Between here... + + + + +// ...and here are 10 blank lines + }; + + + + + private LinkedList fList; + + + + + public int counter; + + + + + public Example(LinkedList list) { + + + + + fList = list; + counter = 0; + } + + + + + public void push(Pair p) { + + + + + fList.add(p); + ++counter; + } + + + + + public Object pop() { + + + + + --counter; + return (Pair) fList.getLast(); + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/F_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/F_out.java new file mode 100644 index 0000000000..d85ddc4c42 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/F_out.java @@ -0,0 +1,104 @@ + + + + +package foo.bar.baz; + +import java.util.List; +import java.util.Vector; + + + + +import org.eclipse.jdt.core.dom.ASTParser; + + + + +import java.net.Socket; + + + + +public class Another { +} + + + + +public class Example { + + + + + public class Nested { + } + + + + + public static class Pair { + + + + + public String first; + + + + + public String second; +// Between here... + + + + +// ...and here are 10 blank lines + }; + + + + + private LinkedList fList; + + + + + public int counter; + + + + + public Example(LinkedList list) { + + + + + fList = list; + counter = 0; + } + + + + + public void push(Pair p) { + + + + + fList.add(p); + ++counter; + } + + + + + public Object pop() { + + + + + --counter; + return (Pair) fList.getLast(); + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/G_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/G_out.java new file mode 100644 index 0000000000..3d0b9fe149 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/G_out.java @@ -0,0 +1,103 @@ + + + + +package foo.bar.baz; + + + + +import java.util.List; +import java.util.Vector; + + + + +import org.eclipse.jdt.core.dom.ASTParser; + + + + +import java.net.Socket; + + + + +public class Another { +} + + + + +public class Example { + + + + + public class Nested { + } + public static class Pair { + + + + + public String first; + + + + + public String second; +// Between here... + + + + +// ...and here are 10 blank lines + }; + + + + + private LinkedList fList; + + + + + public int counter; + + + + + public Example(LinkedList list) { + + + + + fList = list; + counter = 0; + } + + + + + public void push(Pair p) { + + + + + fList.add(p); + ++counter; + } + + + + + public Object pop() { + + + + + --counter; + return (Pair) fList.getLast(); + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/H_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/H_out.java new file mode 100644 index 0000000000..7ccb51dd20 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/H_out.java @@ -0,0 +1,104 @@ + + + + +package foo.bar.baz; + + + + +import java.util.List; +import java.util.Vector; + + + + +import org.eclipse.jdt.core.dom.ASTParser; + + + + +import java.net.Socket; + + + + +public class Another { +} + + + + +public class Example { + + + + + public class Nested { + } + + + + + public static class Pair { + + + + + public String first; + + + + + public String second; +// Between here... + + + + +// ...and here are 10 blank lines + }; + + + + + private LinkedList fList; + + + + + public int counter; + + + + public Example(LinkedList list) { + + + + + fList = list; + counter = 0; + } + + + + public void push(Pair p) { + + + + + fList.add(p); + ++counter; + } + + + + public Object pop() { + + + + + --counter; + return (Pair) fList.getLast(); + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/I_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/I_out.java new file mode 100644 index 0000000000..2313207dc3 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/I_out.java @@ -0,0 +1,103 @@ + + + + +package foo.bar.baz; + + + + +import java.util.List; +import java.util.Vector; + + + + +import org.eclipse.jdt.core.dom.ASTParser; + + + + +import java.net.Socket; + + + + +public class Another { +} + + + + +public class Example { + + + + + public class Nested { + } + + + + + public static class Pair { + + + + + public String first; + + + + + public String second; +// Between here... + + + + +// ...and here are 10 blank lines + }; + + + private LinkedList fList; + + + + + public int counter; + + + public Example(LinkedList list) { + + + + + fList = list; + counter = 0; + } + + + + + public void push(Pair p) { + + + + + fList.add(p); + ++counter; + } + + + + + public Object pop() { + + + + + --counter; + return (Pair) fList.getLast(); + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/J_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/J_out.java new file mode 100644 index 0000000000..3e8b152e8a --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/J_out.java @@ -0,0 +1,104 @@ + +package foo.bar.baz; + + + + +import java.util.List; +import java.util.Vector; + + + + +import org.eclipse.jdt.core.dom.ASTParser; + + + + +import java.net.Socket; + + + + +public class Another { +} + + + + +public class Example { + + + + + public class Nested { + } + + + + + public static class Pair { + + + + + public String first; + + + + + public String second; +// Between here... + + + + +// ...and here are 10 blank lines + }; + + + + + private LinkedList fList; + + + + + public int counter; + + + + + public Example(LinkedList list) { + + + + + fList = list; + counter = 0; + } + + + + + public void push(Pair p) { + + + + + fList.add(p); + ++counter; + } + + + + + public Object pop() { + + + + + --counter; + return (Pair) fList.getLast(); + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/K_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/K_out.java new file mode 100644 index 0000000000..8d9484ebc8 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/K_out.java @@ -0,0 +1,101 @@ + + + + +package foo.bar.baz; + + + + +import java.util.List; +import java.util.Vector; + +import org.eclipse.jdt.core.dom.ASTParser; + +import java.net.Socket; + + + + +public class Another { +} + + + + +public class Example { + + + + + public class Nested { + } + + + + + public static class Pair { + + + + + public String first; + + + + + public String second; +// Between here... + + + + +// ...and here are 10 blank lines + }; + + + + + private LinkedList fList; + + + + + public int counter; + + + + + public Example(LinkedList list) { + + + + + fList = list; + counter = 0; + } + + + + + public void push(Pair p) { + + + + + fList.add(p); + ++counter; + } + + + + + public Object pop() { + + + + + --counter; + return (Pair) fList.getLast(); + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/L_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/L_out.java new file mode 100644 index 0000000000..70aed405b0 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/L_out.java @@ -0,0 +1,106 @@ + + + + +package foo.bar.baz; + + + + +import java.util.List; +import java.util.Vector; + + + + +import org.eclipse.jdt.core.dom.ASTParser; + + + + +import java.net.Socket; + + + + +public class Another { +} + + + +public class Example { + + + + + public class Nested { + } + + + + + public static class Pair { + + + + + public String first; + + + + + public String second; +// Between here... + + + + +// ...and here are 10 blank lines + }; + + + + + private LinkedList fList; + + + + + public int counter; + + + + + public Example(LinkedList list) { + + + + + fList = list; + counter = 0; + } + + + + + public void push(Pair p) { + + + + + fList.add(p); + ++counter; + } + + + + + public Object pop() { + + + + + --counter; + return (Pair) fList.getLast(); + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/in.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/in.java new file mode 100644 index 0000000000..c9e4c06d29 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test549249/in.java @@ -0,0 +1,107 @@ + + + + +package foo.bar.baz; + + + + +import java.util.List; +import java.util.Vector; + + + + +import org.eclipse.jdt.core.dom.ASTParser; + + + + +import java.net.Socket; + + + + +public class Another { +} + + + + +public class Example { + + + + + public class Nested { + } + + + + + public static class Pair { + + + + + public String first; + + + + + public String second; +// Between here... + + + + +// ...and here are 10 blank lines + }; + + + + + private LinkedList fList; + + + + + public int counter; + + + + + public Example(LinkedList list) { + + + + + fList = list; + counter = 0; + } + + + + + public void push(Pair p) { + + + + + fList.add(p); + ++counter; + } + + + + + public Object pop() { + + + + + --counter; + return (Pair) fList.getLast(); + } +} 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 b3d3900fe3..1216958d70 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 @@ -553,9 +553,11 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_ALIGNMENT_FOR_UNION_TYPE_IN_MULTICATCH = JavaCore.PLUGIN_ID + ".formatter.alignment_for_union_type_in_multicatch"; //$NON-NLS-1$ /** * <pre> - * FORMATTER / Option to add blank lines after the imports declaration + * FORMATTER / Option to add or remove blank lines after the imports declaration * - option id: "org.eclipse.jdt.core.formatter.blank_lines_after_imports" - * - possible values: "<n>", where n is zero or a positive integer + * - possible values: "<n>", where n is an integer. If n is negative, the actual number of + * blank lines is ~n and any excess blank lines are deleted, overriding the + * {@link #FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE} option * - default: "0" * </pre> * @since 3.0 @@ -563,9 +565,11 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_BLANK_LINES_AFTER_IMPORTS = JavaCore.PLUGIN_ID + ".formatter.blank_lines_after_imports"; //$NON-NLS-1$ /** * <pre> - * FORMATTER / Option to add blank lines after the package declaration + * FORMATTER / Option to add or remove blank lines after the package declaration * - option id: "org.eclipse.jdt.core.formatter.blank_lines_after_package" - * - possible values: "<n>", where n is zero or a positive integer + * - possible values: "<n>", where n is an integer. If n is negative, the actual number of + * blank lines is ~n and any excess blank lines are deleted, overriding the + * {@link #FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE} option * - default: "0" * </pre> * @since 3.0 @@ -573,9 +577,11 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_BLANK_LINES_AFTER_PACKAGE = JavaCore.PLUGIN_ID + ".formatter.blank_lines_after_package"; //$NON-NLS-1$ /** * <pre> - * FORMATTER / Option to add blank lines at the beginning of the method body + * FORMATTER / Option to add or remove blank lines at the beginning of the method body * - option id: "org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_method_body" - * - possible values: "<n>", where n is zero or a positive integer + * - possible values: "<n>", where n is an integer. If n is negative, the actual number of + * blank lines is ~n and any excess blank lines are deleted, overriding the + * {@link #FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE} option * - default: "0" * </pre> * @since 3.0 @@ -583,9 +589,11 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_BLANK_LINES_AT_BEGINNING_OF_METHOD_BODY = JavaCore.PLUGIN_ID + ".formatter.number_of_blank_lines_at_beginning_of_method_body"; //$NON-NLS-1$ /** * <pre> - * FORMATTER / Option to add blank lines before a field declaration + * FORMATTER / Option to add or remove blank lines before a field declaration * - option id: "org.eclipse.jdt.core.formatter.blank_lines_before_field" - * - possible values: "<n>", where n is zero or a positive integer + * - possible values: "<n>", where n is an integer. If n is negative, the actual number of + * blank lines is ~n and any excess blank lines are deleted, overriding the + * {@link #FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE} option * - default: "0" * </pre> * @since 3.0 @@ -593,9 +601,11 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_BLANK_LINES_BEFORE_FIELD = JavaCore.PLUGIN_ID + ".formatter.blank_lines_before_field"; //$NON-NLS-1$ /** * <pre> - * FORMATTER / Option to add blank lines before the first class body declaration + * FORMATTER / Option to add or remove blank lines before the first class body declaration * - option id: "org.eclipse.jdt.core.formatter.blank_lines_before_first_class_body_declaration" - * - possible values: "<n>", where n is zero or a positive integer + * - possible values: "<n>", where n is an integer. If n is negative, the actual number of + * blank lines is ~n and any excess blank lines are deleted, overriding the + * {@link #FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE} option * - default: "0" * </pre> * @since 3.0 @@ -603,9 +613,11 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_BLANK_LINES_BEFORE_FIRST_CLASS_BODY_DECLARATION = JavaCore.PLUGIN_ID + ".formatter.blank_lines_before_first_class_body_declaration"; //$NON-NLS-1$ /** * <pre> - * FORMATTER / Option to add blank lines before the imports declaration + * FORMATTER / Option to add or remove blank lines before the imports declaration * - option id: "org.eclipse.jdt.core.formatter.blank_lines_before_imports" - * - possible values: "<n>", where n is zero or a positive integer + * - possible values: "<n>", where n is an integer. If n is negative, the actual number of + * blank lines is ~n and any excess blank lines are deleted, overriding the + * {@link #FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE} option * - default: "0" * </pre> * @since 3.0 @@ -613,9 +625,11 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_BLANK_LINES_BEFORE_IMPORTS = JavaCore.PLUGIN_ID + ".formatter.blank_lines_before_imports"; //$NON-NLS-1$ /** * <pre> - * FORMATTER / Option to add blank lines before a member type declaration + * FORMATTER / Option to add or remove blank lines before a member type declaration * - option id: "org.eclipse.jdt.core.formatter.blank_lines_before_member_type" - * - possible values: "<n>", where n is zero or a positive integer + * - possible values: "<n>", where n is an integer. If n is negative, the actual number of + * blank lines is ~n and any excess blank lines are deleted, overriding the + * {@link #FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE} option * - default: "0" * </pre> * @since 3.0 @@ -623,9 +637,11 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_BLANK_LINES_BEFORE_MEMBER_TYPE = JavaCore.PLUGIN_ID + ".formatter.blank_lines_before_member_type"; //$NON-NLS-1$ /** * <pre> - * FORMATTER / Option to add blank lines before a method declaration + * FORMATTER / Option to add or remove blank lines before a method declaration * - option id: "org.eclipse.jdt.core.formatter.blank_lines_before_method" - * - possible values: "<n>", where n is zero or a positive integer + * - possible values: "<n>", where n is an integer. If n is negative, the actual number of + * blank lines is ~n and any excess blank lines are deleted, overriding the + * {@link #FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE} option * - default: "0" * </pre> * @since 3.0 @@ -633,9 +649,11 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_BLANK_LINES_BEFORE_METHOD = JavaCore.PLUGIN_ID + ".formatter.blank_lines_before_method"; //$NON-NLS-1$ /** * <pre> - * FORMATTER / Option to add blank lines before a new chunk + * FORMATTER / Option to add or remove blank lines before a new chunk * - option id: "org.eclipse.jdt.core.formatter.blank_lines_before_new_chunk" - * - possible values: "<n>", where n is zero or a positive integer + * - possible values: "<n>", where n is an integer. If n is negative, the actual number of + * blank lines is ~n and any excess blank lines are deleted, overriding the + * {@link #FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE} option * - default: "0" * </pre> * @since 3.0 @@ -643,9 +661,11 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_BLANK_LINES_BEFORE_NEW_CHUNK = JavaCore.PLUGIN_ID + ".formatter.blank_lines_before_new_chunk"; //$NON-NLS-1$ /** * <pre> - * FORMATTER / Option to add blank lines before the package declaration + * FORMATTER / Option to add or remove blank lines before the package declaration * - option id: "org.eclipse.jdt.core.formatter.blank_lines_before_package" - * - possible values: "<n>", where n is zero or a positive integer + * - possible values: "<n>", where n is an integer. If n is negative, the actual number of + * blank lines is ~n and any excess blank lines are deleted, overriding the + * {@link #FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE} option * - default: "0" * </pre> * @since 3.0 @@ -653,9 +673,11 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_BLANK_LINES_BEFORE_PACKAGE = JavaCore.PLUGIN_ID + ".formatter.blank_lines_before_package"; //$NON-NLS-1$ /** * <pre> - * FORMATTER / Option to add blank lines between import groups + * FORMATTER / Option to add or remove blank lines between import groups * - option id: "org.eclipse.jdt.core.formatter.blank_lines_between_import_groups" - * - possible values: "<n>", where n is zero or a positive integer + * - possible values: "<n>", where n is an integer. If n is negative, the actual number of + * blank lines is ~n and any excess blank lines are deleted, overriding the + * {@link #FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE} option * - default: "1" * </pre> * Note: Import groups are defined once "Organize Import" operation has been executed. The code formatter itself @@ -666,9 +688,11 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_BLANK_LINES_BETWEEN_IMPORT_GROUPS = JavaCore.PLUGIN_ID + ".formatter.blank_lines_between_import_groups"; //$NON-NLS-1$ /** * <pre> - * FORMATTER / Option to add blank lines between type declarations + * FORMATTER / Option to add or remove blank lines between type declarations * - option id: "org.eclipse.jdt.core.formatter.blank_lines_between_type_declarations" - * - possible values: "<n>", where n is zero or a positive integer + * - possible values: "<n>", where n is an integer. If n is negative, the actual number of + * blank lines is ~n and any excess blank lines are deleted, overriding the + * {@link #FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE} option * - default: "0" * </pre> * @since 3.0 diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/LineBreaksPreparator.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/LineBreaksPreparator.java index 1150f1e7fe..abb714cb62 100644 --- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/LineBreaksPreparator.java +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/LineBreaksPreparator.java @@ -24,7 +24,6 @@ import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameR import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameSEMICOLON; import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameelse; import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNamefinally; -import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNamepackage; import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNamewhile; import java.util.List; @@ -96,10 +95,18 @@ public class LineBreaksPreparator extends ASTVisitor { @Override public boolean visit(CompilationUnit node) { List<ImportDeclaration> imports = node.imports(); - if (!imports.isEmpty()) { - int index = this.tm.firstIndexIn(imports.get(0), -1); - if (index > 0) - this.tm.get(index).putLineBreaksBefore(this.options.blank_lines_before_imports + 1); + if (!imports.isEmpty() && this.tm.firstIndexIn(imports.get(0), -1) > 0) + putBlankLinesBefore(imports.get(0), this.options.blank_lines_before_imports); + + for (int i = 1; i < imports.size(); i++) { + int from = this.tm.lastIndexIn(imports.get(i - 1), -1); + int to = this.tm.firstIndexIn(imports.get(i), -1); + for (int j = from; j < to; j++) { + Token token1 = this.tm.get(j); + Token token2 = this.tm.get(j + 1); + if (this.tm.countLineBreaksBetween(token1, token2) > 1) + putBlankLinesAfter(token1, this.options.blank_lines_between_import_groups); + } } List<AnnotationTypeDeclaration> types = node.types(); @@ -114,14 +121,13 @@ public class LineBreaksPreparator extends ASTVisitor { @Override public boolean visit(PackageDeclaration node) { - int blanks = this.options.blank_lines_before_package; - if (blanks > 0) { - List<Annotation> annotations = node.annotations(); - int firstTokenIndex = annotations.isEmpty() ? this.tm.firstIndexBefore(node.getName(), TokenNamepackage) - : this.tm.firstIndexIn(annotations.get(0), -1); - this.tm.get(firstTokenIndex).putLineBreaksBefore(blanks + 1); + if (node.getJavadoc() == null) { + putBlankLinesBefore(node, this.options.blank_lines_before_package); + } else { + putBlankLinesAfter(this.tm.lastTokenIn(node.getJavadoc(), -1), this.options.blank_lines_before_package); } - this.tm.lastTokenIn(node, TokenNameSEMICOLON).putLineBreaksAfter(this.options.blank_lines_after_package + 1); + + putBlankLinesAfter(this.tm.lastTokenIn(node, -1), this.options.blank_lines_after_package); this.declarationModifierVisited = false; return true; } @@ -164,10 +170,9 @@ public class LineBreaksPreparator extends ASTVisitor { blankLines = this.options.blank_lines_before_method; } - if (!sameChunk(previous, bodyDeclaration)) - blankLines = Math.max(blankLines, this.options.blank_lines_before_new_chunk); - putBlankLinesBefore(bodyDeclaration, blankLines); + if (!sameChunk(previous, bodyDeclaration)) + putBlankLinesBefore(bodyDeclaration, this.options.blank_lines_before_new_chunk); } previous = bodyDeclaration; } @@ -188,7 +193,21 @@ public class LineBreaksPreparator extends ASTVisitor { int index = this.tm.firstIndexIn(node, -1); while (index > 0 && this.tm.get(index - 1).tokenType == TokenNameCOMMENT_JAVADOC) index--; - this.tm.get(index).putLineBreaksBefore(linesCount + 1); + if (linesCount >= 0) { + this.tm.get(index).putLineBreaksBefore(linesCount + 1); + } else { + this.tm.get(index).putLineBreaksBefore(~linesCount + 1); + this.tm.get(index).setPreserveLineBreaksBefore(false); + } + } + + private void putBlankLinesAfter(Token token, int linesCount) { + if (linesCount >= 0) { + token.putLineBreaksAfter(linesCount + 1); + } else { + token.putLineBreaksAfter(~linesCount + 1); + token.setPreserveLineBreaksAfter(false); + } } @Override @@ -260,7 +279,7 @@ public class LineBreaksPreparator extends ASTVisitor { handleBracedCode(node.getBody(), null, bracePosition, this.options.indent_statements_compare_to_body); Token openBrace = this.tm.firstTokenIn(node.getBody(), TokenNameLBRACE); if (openBrace.getLineBreaksAfter() > 0) // if not, these are empty braces - openBrace.putLineBreaksAfter(this.options.blank_lines_at_beginning_of_method_body + 1); + putBlankLinesAfter(openBrace, this.options.blank_lines_at_beginning_of_method_body); return true; } diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Token.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Token.java index 18ee94d9b7..b2b7144267 100644 --- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Token.java +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Token.java @@ -97,6 +97,7 @@ public class Token { public final int tokenType; private boolean spaceBefore, spaceAfter; private int lineBreaksBefore, lineBreaksAfter; + private boolean preserveLineBreaksBefore = true, preserveLineBreaksAfter = true; private boolean wrapped; private int indent; private int emptyLineIndentAdjustment; @@ -129,6 +130,8 @@ public class Token { this.spaceAfter = tokenToCopy.spaceAfter; this.lineBreaksBefore = tokenToCopy.lineBreaksBefore; this.lineBreaksAfter = tokenToCopy.lineBreaksAfter; + this.preserveLineBreaksBefore = tokenToCopy.preserveLineBreaksBefore; + this.preserveLineBreaksAfter = tokenToCopy.preserveLineBreaksAfter; this.indent = tokenToCopy.indent; this.nextLineOnWrap = tokenToCopy.nextLineOnWrap; this.wrapPolicy = tokenToCopy.wrapPolicy; @@ -217,6 +220,22 @@ public class Token { this.lineBreaksAfter = 0; } + public void setPreserveLineBreaksBefore(boolean preserveLineBreaksBefore) { + this.preserveLineBreaksBefore = preserveLineBreaksBefore; + } + + public boolean isPreserveLineBreaksBefore() { + return this.preserveLineBreaksBefore; + } + + public void setPreserveLineBreaksAfter(boolean preserveLineBreaksAfter) { + this.preserveLineBreaksAfter = preserveLineBreaksAfter; + } + + public boolean isPreserveLineBreaksAfter() { + return this.preserveLineBreaksAfter; + } + /** Increases this token's indentation by one position */ public void indent() { this.indent++; diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/Aligner.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/Aligner.java index 011565e0a0..79962ab42f 100644 --- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/Aligner.java +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/Aligner.java @@ -204,17 +204,21 @@ public class Aligner { private boolean isNewGroup(ASTNode node, ASTNode previousNode) { if (previousNode == null) return true; - int lineBreaks = 0; + int totalLineBreaks = 0; int from = this.tm.lastIndexIn(previousNode, -1); int to = this.tm.firstIndexIn(node, -1); Token previousToken = this.tm.get(from); for (int i = from + 1; i <= to; i++) { Token token = this.tm.get(i); - lineBreaks += Math.min(this.tm.countLineBreaksBetween(previousToken, token), - this.options.number_of_empty_lines_to_preserve + 1); + int lineBreaks = Math.max(previousToken.getLineBreaksAfter(), token.getLineBreaksBefore()); + if (previousToken.isPreserveLineBreaksAfter() && token.isPreserveLineBreaksBefore()) { + lineBreaks = Math.max(lineBreaks, Math.min(this.tm.countLineBreaksBetween(previousToken, token), + this.options.number_of_empty_lines_to_preserve + 1)); + } + totalLineBreaks += lineBreaks; previousToken = token; } - return lineBreaks > this.options.align_fields_grouping_blank_lines; + return totalLineBreaks > this.options.align_fields_grouping_blank_lines; } private <N extends ASTNode> void alignNodes(List<N> alignGroup, AlignIndexFinder<N> tokenFinder) { 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 b5e8822ba3..8d4fcaf236 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 @@ -60,7 +60,6 @@ import org.eclipse.jdt.core.dom.Assignment; import org.eclipse.jdt.core.dom.Block; import org.eclipse.jdt.core.dom.CatchClause; import org.eclipse.jdt.core.dom.ClassInstanceCreation; -import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.ConditionalExpression; import org.eclipse.jdt.core.dom.ConstructorInvocation; import org.eclipse.jdt.core.dom.CreationReference; @@ -75,7 +74,6 @@ import org.eclipse.jdt.core.dom.FieldAccess; import org.eclipse.jdt.core.dom.FieldDeclaration; import org.eclipse.jdt.core.dom.ForStatement; import org.eclipse.jdt.core.dom.IfStatement; -import org.eclipse.jdt.core.dom.ImportDeclaration; import org.eclipse.jdt.core.dom.InfixExpression; import org.eclipse.jdt.core.dom.InfixExpression.Operator; import org.eclipse.jdt.core.dom.LambdaExpression; @@ -225,8 +223,6 @@ public class WrapPreparator extends ASTVisitor { final Aligner aligner; - int importsStart = -1, importsEnd = -1; - /* * temporary values used when calling {@link #handleWrap(int)} to avoid ArrayList initialization and long lists of * parameters @@ -268,16 +264,6 @@ public class WrapPreparator extends ASTVisitor { } @Override - public boolean visit(CompilationUnit node) { - List<ImportDeclaration> imports = node.imports(); - if (!imports.isEmpty()) { - this.importsStart = this.tm.firstIndexIn(imports.get(0), -1); - this.importsEnd = this.tm.lastIndexIn(imports.get(imports.size() - 1), -1); - } - return true; - } - - @Override public boolean visit(NormalAnnotation node) { int lParen = this.tm.firstIndexAfter(node.getTypeName(), TokenNameLPAREN); int rParen = this.tm.lastIndexIn(node, TokenNameRPAREN); @@ -1318,9 +1304,7 @@ public class WrapPreparator extends ASTVisitor { @Override protected boolean token(Token token, int index) { - boolean isBetweenImports = index > WrapPreparator.this.importsStart - && index < WrapPreparator.this.importsEnd; - int lineBreaks = getLineBreaksToPreserve(getPrevious(), token, isBetweenImports); + int lineBreaks = getLineBreaksToPreserve(getPrevious(), token); if (lineBreaks > 1 || (!this.join_wrapped_lines && token.isWrappable()) || index == 0) token.putLineBreaksBefore(lineBreaks); return true; @@ -1330,7 +1314,7 @@ public class WrapPreparator extends ASTVisitor { Token last = this.tm.get(this.tm.size() - 1); last.clearLineBreaksAfter(); - int endingBreaks = getLineBreaksToPreserve(last, null, false); + int endingBreaks = getLineBreaksToPreserve(last, null); if (endingBreaks > 0) { last.putLineBreaksAfter(endingBreaks); } else if ((this.kind & (CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.K_MODULE_INFO)) != 0 @@ -1339,7 +1323,11 @@ public class WrapPreparator extends ASTVisitor { } } - int getLineBreaksToPreserve(Token token1, Token token2, boolean isBetweenImports) { + int getLineBreaksToPreserve(Token token1, Token token2) { + if ((token1 != null && !token1.isPreserveLineBreaksAfter()) + || (token2 != null && !token2.isPreserveLineBreaksBefore())) { + return 0; + } if (token1 != null) { List<Token> structure = token1.getInternalStructure(); if (structure != null && !structure.isEmpty()) @@ -1351,9 +1339,6 @@ public class WrapPreparator extends ASTVisitor { token2 = structure.get(0); } int lineBreaks = WrapPreparator.this.tm.countLineBreaksBetween(token1, token2); - if (isBetweenImports) - return lineBreaks > 1 ? (this.options.blank_lines_between_import_groups + 1) : 0; - int toPreserve = this.options.number_of_empty_lines_to_preserve; if (token1 != null && token2 != null) toPreserve++; // n empty lines = n+1 line breaks, except for file start and end |