Skip to content

Commit 7e2e9c5

Browse files
jpd236cushon
authored andcommitted
Add --skip-sorting-imports flag to skip sorting imports. (#109)
Unused imports will still be removed.
1 parent 0193c9d commit 7e2e9c5

File tree

8 files changed

+73
-3
lines changed

8 files changed

+73
-3
lines changed

core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ final class CommandLineOptions {
3636
private final boolean stdin;
3737
private final boolean fixImportsOnly;
3838
private final boolean removeJavadocOnlyImports;
39+
private final boolean sortImports;
3940

4041
CommandLineOptions(
4142
ImmutableList<String> files,
@@ -48,7 +49,8 @@ final class CommandLineOptions {
4849
boolean help,
4950
boolean stdin,
5051
boolean fixImportsOnly,
51-
boolean removeJavadocOnlyImports) {
52+
boolean removeJavadocOnlyImports,
53+
boolean sortImports) {
5254
this.files = files;
5355
this.inPlace = inPlace;
5456
this.lines = lines;
@@ -60,6 +62,7 @@ final class CommandLineOptions {
6062
this.stdin = stdin;
6163
this.fixImportsOnly = fixImportsOnly;
6264
this.removeJavadocOnlyImports = removeJavadocOnlyImports;
65+
this.sortImports = sortImports;
6366
}
6467

6568
/** The files to format. */
@@ -120,6 +123,11 @@ boolean removeJavadocOnlyImports() {
120123
return removeJavadocOnlyImports;
121124
}
122125

126+
/** Sort imports. */
127+
boolean sortImports() {
128+
return sortImports;
129+
}
130+
123131
/** Returns true if partial formatting was selected. */
124132
boolean isSelection() {
125133
return !lines().isEmpty() || !offsets().isEmpty() || !lengths().isEmpty();
@@ -142,6 +150,7 @@ static class Builder {
142150
private Boolean stdin = false;
143151
private Boolean fixImportsOnly = false;
144152
private Boolean removeJavadocOnlyImports = false;
153+
private Boolean sortImports = true;
145154

146155
ImmutableList.Builder<String> filesBuilder() {
147156
return files;
@@ -196,6 +205,11 @@ Builder removeJavadocOnlyImports(boolean removeJavadocOnlyImports) {
196205
return this;
197206
}
198207

208+
Builder sortImports(boolean sortImports) {
209+
this.sortImports = sortImports;
210+
return this;
211+
}
212+
199213
CommandLineOptions build() {
200214
return new CommandLineOptions(
201215
this.files.build(),
@@ -208,7 +222,8 @@ CommandLineOptions build() {
208222
this.help,
209223
this.stdin,
210224
this.fixImportsOnly,
211-
this.removeJavadocOnlyImports);
225+
this.removeJavadocOnlyImports,
226+
this.sortImports);
212227
}
213228
}
214229
}

core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ static CommandLineOptions parse(Iterable<String> options) {
8989
case "--experimental-remove-javadoc-only-imports":
9090
optionsBuilder.removeJavadocOnlyImports(true);
9191
break;
92+
case "--skip-sorting-imports":
93+
optionsBuilder.sortImports(false);
94+
break;
9295
case "-":
9396
optionsBuilder.stdin(true);
9497
break;

core/src/main/java/com/google/googlejavaformat/java/FormatFileCallable.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ private String fixImports(String input) throws FormatterException {
5555
parameters.removeJavadocOnlyImports()
5656
? JavadocOnlyImports.REMOVE
5757
: JavadocOnlyImports.KEEP);
58-
input = ImportOrderer.reorderImports(input);
58+
if (parameters.sortImports()) {
59+
input = ImportOrderer.reorderImports(input);
60+
}
5961
return input;
6062
}
6163

core/src/main/java/com/google/googlejavaformat/java/UsageException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public final class UsageException extends Exception {
4040
" Use AOSP style instead of Google Style (4-space indentation)",
4141
" --fix-imports-only",
4242
" Fix import order and remove any unused imports, but do no other formatting.",
43+
" --skip-sorting-imports",
44+
" Do not fix the import order. Unused imports will still be removed.",
4345
" --length, -length",
4446
" Character length to format.",
4547
" --lines, -lines, --line, -line",

core/src/test/java/com/google/googlejavaformat/java/CommandLineOptionsParserTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public void defaults() {
4040
assertThat(options.offsets()).isEmpty();
4141
assertThat(options.inPlace()).isFalse();
4242
assertThat(options.version()).isFalse();
43+
assertThat(options.sortImports()).isTrue();
4344
}
4445

4546
@Test
@@ -108,6 +109,14 @@ public void version() {
108109
assertThat(CommandLineOptionsParser.parse(Arrays.asList("-v")).version()).isTrue();
109110
}
110111

112+
@Test
113+
public void skipSortingImports() {
114+
assertThat(
115+
CommandLineOptionsParser.parse(Arrays.asList("--skip-sorting-imports"))
116+
.sortImports())
117+
.isFalse();
118+
}
119+
111120
// TODO(cushon): consider handling this in the parser and reporting a more detailed error
112121
@Test
113122
public void illegalLines() {

core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ public void importOrderingAndFormatting() throws IOException, UsageException {
259259
importOrdering(null, "com/google/googlejavaformat/java/testimports/A.imports-and-formatting");
260260
}
261261

262+
@Test
263+
public void formattingWithoutImportOrdering() throws IOException, UsageException {
264+
importOrdering(
265+
"--skip-sorting-imports",
266+
"com/google/googlejavaformat/java/testimports/A.formatting-only");
267+
}
268+
262269
private void importOrdering(String sortArg, String outputResourceName)
263270
throws IOException, UsageException {
264271
Path tmpdir = testFolder.newFolder().toPath();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.google.example;
2+
3+
import com.google.common.base.Preconditions;
4+
5+
import org.junit.runner.RunWith;
6+
import org.junit.runners.JUnit4;
7+
8+
import java.util.List;
9+
10+
import javax.annotations.Nullable;
11+
12+
import static org.junit.Assert.fail;
13+
import static com.google.truth.Truth.assertThat;
14+
15+
@RunWith(JUnit4.class)
16+
public class SomeTest {
17+
18+
<T> void check(@Nullable List<T> x) {
19+
Preconditions.checkNodeNull(x);
20+
}
21+
22+
void f() {
23+
List<String> xs = null;
24+
assertThat(xs).isNull();
25+
try {
26+
check(xs);
27+
fail();
28+
} catch (NullPointerException e) {
29+
}
30+
}
31+
}

core/src/test/resources/com/google/googlejavaformat/java/testimports/A.input

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.junit.runner.RunWith;
66
import org.junit.runners.JUnit4;
77

88
import java.util.List;
9+
import java.util.Set;
910

1011
import javax.annotations.Nullable;
1112

0 commit comments

Comments
 (0)