Skip to content

Commit 80049d7

Browse files
committed
Revert the changes of this branch
Reverts edce6f5, 68e5bdf, b445927 and b06ccee: the order of fixing imports in formatSourceAndFixImports, reflowing strings only in the ranges, the in-process service reflowing strings, and the module import test that followed from the first.
1 parent b06ccee commit 80049d7

11 files changed

Lines changed: 54 additions & 490 deletions

File tree

‎gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/FormatDiffTest.java‎

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ void parsing_git_diff_output_works() throws IOException {
6969
@MethodSource("getFormatters")
7070
void reformat_a_subpath_of_a_git_directory_for_only_changed_lines(FormatterService formatterService)
7171
throws IOException, InterruptedException {
72-
initRepo();
72+
runCommandInRepo("git", "init");
73+
runCommandInRepo("git", "config", "user.name", "Test User");
74+
runCommandInRepo("git", "config", "user.email", "test-user@palantir.com");
75+
runCommandInRepo("git", "config", "commit.gpgsign", "false");
76+
runCommandInRepo("git", "commit", "--allow-empty", "-m", "Init");
7377

7478
Path subdir = repo.resolve("subdir");
7579
Files.createDirectories(subdir);
@@ -88,52 +92,6 @@ void reformat_a_subpath_of_a_git_directory_for_only_changed_lines(FormatterServi
8892
assertThat(dontTouchMe).hasContent(" class DontTouchMe {}");
8993
}
9094

91-
@ParameterizedTest
92-
@MethodSource("getFormatters")
93-
void reflows_the_long_strings_of_changed_lines_only(FormatterService formatterService)
94-
throws IOException, InterruptedException {
95-
initRepo();
96-
String outside = " String outside = \"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris"
97-
+ " nisi ut aliquip ex ea commodo consequat sed.\";";
98-
Path longStrings = repo.resolve("LongStrings.java");
99-
Files.write(longStrings, List.of("class LongStrings {", outside, "}"), UTF_8);
100-
runCommandInRepo("git", "add", ".");
101-
runCommandInRepo("git", "commit", "-m", "A long string");
102-
103-
Files.write(
104-
longStrings,
105-
List.of(
106-
"class LongStrings {",
107-
outside,
108-
"",
109-
" String changed = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod"
110-
+ " tempor incididunt ut labore et dolore magna.\";",
111-
"}"),
112-
UTF_8);
113-
114-
FormatDiff.formatDiff(repo, formatterService);
115-
116-
assertThat(longStrings)
117-
.hasContent(String.join(
118-
"\n",
119-
"class LongStrings {",
120-
outside,
121-
"",
122-
" String changed =",
123-
" \"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor"
124-
+ " incididunt ut labore et\"",
125-
" + \" dolore magna.\";",
126-
"}"));
127-
}
128-
129-
private void initRepo() throws IOException, InterruptedException {
130-
runCommandInRepo("git", "init");
131-
runCommandInRepo("git", "config", "user.name", "Test User");
132-
runCommandInRepo("git", "config", "user.email", "test-user@palantir.com");
133-
runCommandInRepo("git", "config", "commit.gpgsign", "false");
134-
runCommandInRepo("git", "commit", "--allow-empty", "-m", "Init");
135-
}
136-
13795
private void runCommandInRepo(String... args) throws IOException, InterruptedException {
13896
Process process =
13997
new ProcessBuilder().command(args).directory(repo.toFile()).start();
@@ -149,9 +107,6 @@ private void runCommandInRepo(String... args) throws IOException, InterruptedExc
149107

150108
private static Stream<FormatterService> getFormatters() throws IOException {
151109
return Stream.of(
152-
// In this JVM, as formatDiff runs it without the native binary.
153-
FormatterServiceLoader.load(
154-
getClasspath().stream().map(Path::toFile).toList()),
155110
new BootstrappingFormatterService(
156111
javaBinPath(), Runtime.version().feature(), getClasspath()),
157112
new NativeImageFormatterService(

‎open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/FormatterServicesTest.java‎

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.palantir.javaformat.Utils;
2525
import com.palantir.javaformat.java.FormatterException;
2626
import com.palantir.javaformat.java.FormatterService;
27-
import com.palantir.javaformat.java.FormatterServiceImpl;
2827
import com.palantir.javaformat.java.Replacement;
2928
import java.io.File;
3029
import java.net.URI;
@@ -97,47 +96,13 @@ public void main( String aaaa ) {
9796
.isEmpty();
9897
}
9998

100-
@ParameterizedTest
101-
@MethodSource("getFormattersAndTheInProcessOne")
102-
void reflows_the_long_strings_of_the_ranges_only(FormatterService formatterService) throws FormatterException {
103-
String input = """
104-
class S {
105-
String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed.";
106-
107-
String changed = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.";
108-
}
109-
""";
110-
String expectedOutput = """
111-
class S {
112-
String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed.";
113-
114-
String changed =
115-
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et"
116-
+ " dolore magna.";
117-
}
118-
""";
119-
int start = input.indexOf(" String changed");
120-
int end = input.indexOf('\n', start);
121-
122-
List<Replacement> replacements =
123-
formatterService.getFormatReplacements(input, List.of(Range.closedOpen(start, end)));
124-
125-
assertThat(Utils.applyReplacements(input, replacements)).isEqualTo(expectedOutput);
126-
}
127-
12899
private static Stream<FormatterService> getFormatters() {
129100
return Stream.of(
130101
new BootstrappingFormatterService(
131102
javaBinPath(), Runtime.version().feature(), getClasspath()),
132103
new NativeImageFormatterService(Path.of(System.getenv("NATIVE_IMAGE_CLASSPATH"))));
133104
}
134105

135-
// With the one that formats in this JVM, as formatDiff does without the native binary. On input that does not parse
136-
// it throws, where the other two return no replacements, so it is not among getFormatters.
137-
private static Stream<FormatterService> getFormattersAndTheInProcessOne() {
138-
return Stream.concat(Stream.of(new FormatterServiceImpl()), getFormatters());
139-
}
140-
141106
private String getTestResourceContent(String resourceName) {
142107
try {
143108
URI resource = getClass().getClassLoader().getResource(resourceName).toURI();

‎open-java-format-spi/src/main/java/com/palantir/javaformat/java/FormatterService.java‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ public interface FormatterService {
2828
/**
2929
* Emit a list of {@link Replacement}s to convert from input to formatted output.
3030
*
31-
* <p>The ranges are formatted, and the long strings in the code that formatting them rewrites are reflowed; the
32-
* imports, and the strings elsewhere, stay as they are.
33-
*
3431
* @param input the input compilation unit
3532
* @param ranges the character ranges to reformat
3633
* @return a list of {@link Replacement}s, sorted from low index to high index, without overlaps

‎open-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.palantir.javaformat.Utils;
2525
import java.io.UncheckedIOException;
2626
import java.util.List;
27+
import java.util.Set;
2728
import java.util.concurrent.Callable;
2829

2930
/** Encapsulates information about a file to be formatted, including which parts of the file to format. */
@@ -50,10 +51,23 @@ public String call() throws FormatterException {
5051
Formatter formatter = Formatter.createFormatter(options);
5152
if (parameters.outputReplacements()) {
5253
RangeSet<Integer> characterRanges = characterRanges(input);
53-
return writeFormatReplacements(
54-
parameters.reflowLongStrings()
55-
? formatter.getFormatAndReflowReplacements(input, characterRanges)
56-
: formatter.getFormatReplacements(input, characterRanges.asRanges()));
54+
Set<Range<Integer>> rangesToChange = characterRanges.asRanges();
55+
List<Replacement> replacements = formatter.getFormatReplacements(input, rangesToChange);
56+
if (parameters.reflowLongStrings()) {
57+
String formattedText = Utils.applyReplacements(input, replacements);
58+
// avoid trying to wrap the input unless the lines needing wrapping are part of the "characterRanges"
59+
if (!StringWrapper.linesNeedWrapping(options.maxLineLength(), formattedText, characterRanges)) {
60+
return writeFormatReplacements(replacements);
61+
}
62+
String wrappedFormattedText = StringWrapper.wrap(options.maxLineLength(), formattedText, formatter);
63+
// if no wrapping change happened, return the initial replacements
64+
if (wrappedFormattedText.equals(formattedText)) {
65+
return writeFormatReplacements(replacements);
66+
}
67+
return writeFormatReplacements(List.of(Replacement.create(0, input.length(), wrappedFormattedText)));
68+
} else {
69+
return writeFormatReplacements(replacements);
70+
}
5771
}
5872
return formatFile(formatter);
5973
}

‎open-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java‎

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,7 @@ public String formatSource(String input) throws FormatterException {
296296
/**
297297
* Formats an input string (a Java compilation unit) and fixes imports.
298298
*
299-
* <p>Fixing imports includes ordering, spacing, and removal of unused import statements. Long strings are reflowed
300-
* too, and the result is what {@code open-java-format -} prints with no flags.
299+
* <p>Fixing imports includes ordering, spacing, and removal of unused import statements.
301300
*
302301
* @param input the input string
303302
* @return the output string
@@ -306,10 +305,9 @@ public String formatSource(String input) throws FormatterException {
306305
* Style Guide - 3.3.3 Import ordering and spacing</a>
307306
*/
308307
public String formatSourceAndFixImports(String input) throws FormatterException {
309-
// The imports are fixed after formatting, as the command line fixes them: it has to format first, because the
310-
// ranges it formats are offsets into the input. In the other order the comment of a removed import ends up
311-
// elsewhere, and an extraneous semicolon between imports is dropped where the command line fails the file.
312-
String formatted = fixImports(formatSource(input));
308+
input = ImportOrderer.reorderImports(input, options.style());
309+
input = RemoveUnusedImports.removeUnusedImports(input);
310+
String formatted = formatSource(input);
313311
return StringWrapper.wrap(options.maxLineLength(), formatted, this);
314312
}
315313

@@ -366,27 +364,4 @@ public List<Replacement> getFormatReplacements(String input, Collection<Range<In
366364
RangeSet<Integer> tokenRangeSet = javaInput.characterRangesToTokenRanges(characterRanges);
367365
return javaOutput.getFormatReplacements(tokenRangeSet);
368366
}
369-
370-
/**
371-
* Emit the {@link Replacement}s that format the given character ranges and reflow the long strings in the code
372-
* that formatting them rewrites; the imports, and the strings elsewhere, stay as they are. The command line prints
373-
* these for {@code --output-replacements}, and {@link FormatterServiceImpl#getFormatReplacements} returns them,
374-
* so that {@code formatDiff} and IntelliJ IDEA format the same on the JVM and with the native binary.
375-
*/
376-
List<Replacement> getFormatAndReflowReplacements(String input, RangeSet<Integer> characterRanges)
377-
throws FormatterException {
378-
List<Replacement> replacements = getFormatReplacements(input, characterRanges.asRanges());
379-
String formatted = Utils.applyReplacements(input, replacements);
380-
RangeSet<Integer> formattedRanges = StringWrapper.moveRanges(characterRanges, replacements);
381-
// avoid trying to wrap the input unless the lines needing wrapping are part of the ranges
382-
if (!StringWrapper.linesNeedWrapping(options.maxLineLength(), formatted, formattedRanges)) {
383-
return replacements;
384-
}
385-
String wrapped = StringWrapper.wrap(options.maxLineLength(), formatted, formattedRanges, this);
386-
// if no wrapping change happened, return the initial replacements
387-
if (wrapped.equals(formatted)) {
388-
return replacements;
389-
}
390-
return List.of(Replacement.create(0, input.length(), wrapped));
391-
}
392367
}

‎open-java-format/src/main/java/com/palantir/javaformat/java/FormatterServiceImpl.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.google.auto.service.AutoService;
1919
import com.google.common.collect.ImmutableList;
2020
import com.google.common.collect.Range;
21-
import com.google.common.collect.TreeRangeSet;
2221
import com.palantir.javaformat.java.JavaFormatterOptions.Style;
2322
import java.util.Collection;
2423

@@ -37,8 +36,7 @@ public FormatterServiceImpl() {
3736
@SuppressForbidden(reason = "FormatterService, kept in the SPI for compatibility, returns ImmutableList")
3837
public ImmutableList<Replacement> getFormatReplacements(String text, Collection<Range<Integer>> toRanges)
3938
throws FormatterException {
40-
// What the command line prints for --output-replacements, which the bootstrapping and native services run.
41-
return ImmutableList.copyOf(formatter.getFormatAndReflowReplacements(text, TreeRangeSet.create(toRanges)));
39+
return ImmutableList.copyOf(formatter.getFormatReplacements(text, toRanges));
4240
}
4341

4442
@Override

0 commit comments

Comments
 (0)