Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions open-java-format-eclipse-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ dependencies {

implementation project(':open-java-format')
implementation libs.jspecify

testImplementation libs.junit.jupiter
testImplementation libs.assertj
}

// The dependencies embedded in the plugin jar. Named once, so the manifest can list exactly what
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public List<Replacement> format(
if (kind != SnippetKind.COMPILATION_UNIT) {
throw new IllegalArgumentException("comment formatting is only supported for compilation units");
}
return formatter.getFormatReplacements(source, ranges);
// A whole file, as the editor formats it: the ranges reflow their long strings, as on the command line.
return formatter.getFormatAndReflowReplacements(source, rangeSet);
}
SnippetWrapper wrapper = snippetWrapper(kind, source, initialIndent);
ranges = offsetRanges(ranges, wrapper.offset);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.javaformat.java;

import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.Range;
import com.palantir.javaformat.Utils;
import com.palantir.javaformat.java.SnippetFormatter.SnippetKind;
import java.util.List;
import org.junit.jupiter.api.Test;

final class SnippetFormatterTest {
private static final String LONG_STRINGS = """
class S {
String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed.";
String changed = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.";
}
""";

// The editor's Format: a compilation unit with its comments.
@Test
void reflowsTheLongStringOfTheRangeOnly() throws FormatterException {
int start = LONG_STRINGS.indexOf(" String changed");
Range<Integer> changed = Range.closedOpen(start, LONG_STRINGS.indexOf('\n', start));

List<Replacement> replacements = new SnippetFormatter()
.format(SnippetKind.COMPILATION_UNIT, LONG_STRINGS, List.of(changed), 0, /* includeComments= */ true);

assertThat(Utils.applyReplacements(LONG_STRINGS, replacements)).isEqualTo("""
class S {
String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed.";
String changed =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et"
+ " dolore magna.";
}
""");
}

// Snippets, which Eclipse formats when it inserts code, are wrapped in a class and may only change whitespace.
@Test
void leavesTheLongStringOfAStatementSnippet() throws FormatterException {
String literal = "\"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt"
+ " ut labore et dolore magna.\"";
String statement = "String s = " + literal + ";";

// Two levels: the statement sits in a block of the dummy class, as in a method's body.
List<Replacement> replacements = new SnippetFormatter()
.format(
SnippetKind.STATEMENTS,
statement,
List.of(Range.closedOpen(0, statement.length())),
2,
/* includeComments= */ false);

assertThat(Utils.applyReplacements(statement, replacements)).contains(literal);
}
}
Loading