Skip to content

Commit 6dcf057

Browse files
committed
Reflow long strings in the Eclipse plugin's ranges, as on the command line
Eclipse's Format of a Java editor formats the whole compilation unit with its comments over the selected ranges. It went through Formatter.getFormatReplacements, which never reflows, so a long string in the selection stayed on one line where the command line, formatDiff and IntelliJ IDEA wrap it (#219). It now goes through getFormatAndReflowReplacements, which reflows only the strings in the code that formatting the ranges rewrites. Snippets, which Eclipse formats when it inserts code, stay as they are: they are wrapped in a dummy class, and toReplacements accepts only changes to whitespace, which reflowing a string is not. The plugin had no tests; it gets JUnit and AssertJ for SnippetFormatterTest.
1 parent c799977 commit 6dcf057

3 files changed

Lines changed: 77 additions & 1 deletion

File tree

‎open-java-format-eclipse-plugin/build.gradle‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ dependencies {
2929

3030
implementation project(':open-java-format')
3131
implementation libs.jspecify
32+
33+
testImplementation libs.junit.jupiter
34+
testImplementation libs.assertj
3235
}
3336

3437
// The dependencies embedded in the plugin jar. Named once, so the manifest can list exactly what

‎open-java-format-eclipse-plugin/src/main/java/com/palantir/javaformat/java/SnippetFormatter.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ public List<Replacement> format(
101101
if (kind != SnippetKind.COMPILATION_UNIT) {
102102
throw new IllegalArgumentException("comment formatting is only supported for compilation units");
103103
}
104-
return formatter.getFormatReplacements(source, ranges);
104+
// A whole file, as the editor formats it: the ranges reflow their long strings, as on the command line.
105+
return formatter.getFormatAndReflowReplacements(source, rangeSet);
105106
}
106107
SnippetWrapper wrapper = snippetWrapper(kind, source, initialIndent);
107108
ranges = offsetRanges(ranges, wrapper.offset);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.palantir.javaformat.java;
16+
17+
import static org.assertj.core.api.Assertions.assertThat;
18+
19+
import com.google.common.collect.Range;
20+
import com.palantir.javaformat.Utils;
21+
import com.palantir.javaformat.java.SnippetFormatter.SnippetKind;
22+
import java.util.List;
23+
import org.junit.jupiter.api.Test;
24+
25+
final class SnippetFormatterTest {
26+
private static final String LONG_STRINGS = """
27+
class S {
28+
String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed.";
29+
30+
String changed = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.";
31+
}
32+
""";
33+
34+
// The editor's Format: a compilation unit with its comments.
35+
@Test
36+
void reflowsTheLongStringOfTheRangeOnly() throws FormatterException {
37+
int start = LONG_STRINGS.indexOf(" String changed");
38+
Range<Integer> changed = Range.closedOpen(start, LONG_STRINGS.indexOf('\n', start));
39+
40+
List<Replacement> replacements = new SnippetFormatter()
41+
.format(SnippetKind.COMPILATION_UNIT, LONG_STRINGS, List.of(changed), 0, /* includeComments= */ true);
42+
43+
assertThat(Utils.applyReplacements(LONG_STRINGS, replacements)).isEqualTo("""
44+
class S {
45+
String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed.";
46+
47+
String changed =
48+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et"
49+
+ " dolore magna.";
50+
}
51+
""");
52+
}
53+
54+
// Snippets, which Eclipse formats when it inserts code, are wrapped in a class and may only change whitespace.
55+
@Test
56+
void leavesTheLongStringOfAStatementSnippet() throws FormatterException {
57+
String literal = "\"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt"
58+
+ " ut labore et dolore magna.\"";
59+
String statement = "String s = " + literal + ";";
60+
61+
// Two levels: the statement sits in a block of the dummy class, as in a method's body.
62+
List<Replacement> replacements = new SnippetFormatter()
63+
.format(
64+
SnippetKind.STATEMENTS,
65+
statement,
66+
List.of(Range.closedOpen(0, statement.length())),
67+
2,
68+
/* includeComments= */ false);
69+
70+
assertThat(Utils.applyReplacements(statement, replacements)).contains(literal);
71+
}
72+
}

0 commit comments

Comments
 (0)