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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ final class CommandLineOptions {
private final boolean stdin;
private final boolean fixImportsOnly;
private final boolean removeJavadocOnlyImports;
private final boolean sortImports;

CommandLineOptions(
ImmutableList<String> files,
Expand All @@ -48,7 +49,8 @@ final class CommandLineOptions {
boolean help,
boolean stdin,
boolean fixImportsOnly,
boolean removeJavadocOnlyImports) {
boolean removeJavadocOnlyImports,
boolean sortImports) {
this.files = files;
this.inPlace = inPlace;
this.lines = lines;
Expand All @@ -60,6 +62,7 @@ final class CommandLineOptions {
this.stdin = stdin;
this.fixImportsOnly = fixImportsOnly;
this.removeJavadocOnlyImports = removeJavadocOnlyImports;
this.sortImports = sortImports;
}

/** The files to format. */
Expand Down Expand Up @@ -120,6 +123,11 @@ boolean removeJavadocOnlyImports() {
return removeJavadocOnlyImports;
}

/** Sort imports. */
boolean sortImports() {
return sortImports;
}

/** Returns true if partial formatting was selected. */
boolean isSelection() {
return !lines().isEmpty() || !offsets().isEmpty() || !lengths().isEmpty();
Expand All @@ -142,6 +150,7 @@ static class Builder {
private Boolean stdin = false;
private Boolean fixImportsOnly = false;
private Boolean removeJavadocOnlyImports = false;
private Boolean sortImports = true;

ImmutableList.Builder<String> filesBuilder() {
return files;
Expand Down Expand Up @@ -196,6 +205,11 @@ Builder removeJavadocOnlyImports(boolean removeJavadocOnlyImports) {
return this;
}

Builder sortImports(boolean sortImports) {
this.sortImports = sortImports;
return this;
}

CommandLineOptions build() {
return new CommandLineOptions(
this.files.build(),
Expand All @@ -208,7 +222,8 @@ CommandLineOptions build() {
this.help,
this.stdin,
this.fixImportsOnly,
this.removeJavadocOnlyImports);
this.removeJavadocOnlyImports,
this.sortImports);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ static CommandLineOptions parse(Iterable<String> options) {
case "--experimental-remove-javadoc-only-imports":
optionsBuilder.removeJavadocOnlyImports(true);
break;
case "--skip-sorting-imports":
optionsBuilder.sortImports(false);
break;
case "-":
optionsBuilder.stdin(true);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ private String fixImports(String input) throws FormatterException {
parameters.removeJavadocOnlyImports()
? JavadocOnlyImports.REMOVE
: JavadocOnlyImports.KEEP);
input = ImportOrderer.reorderImports(input);
if (parameters.sortImports()) {
input = ImportOrderer.reorderImports(input);
}
return input;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public final class UsageException extends Exception {
" Use AOSP style instead of Google Style (4-space indentation)",
" --fix-imports-only",
" Fix import order and remove any unused imports, but do no other formatting.",
" --skip-sorting-imports",
" Do not fix the import order. Unused imports will still be removed.",
" --length, -length",
" Character length to format.",
" --lines, -lines, --line, -line",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void defaults() {
assertThat(options.offsets()).isEmpty();
assertThat(options.inPlace()).isFalse();
assertThat(options.version()).isFalse();
assertThat(options.sortImports()).isTrue();
}

@Test
Expand Down Expand Up @@ -108,6 +109,14 @@ public void version() {
assertThat(CommandLineOptionsParser.parse(Arrays.asList("-v")).version()).isTrue();
}

@Test
public void skipSortingImports() {
assertThat(
CommandLineOptionsParser.parse(Arrays.asList("--skip-sorting-imports"))
.sortImports())
.isFalse();
}

// TODO(cushon): consider handling this in the parser and reporting a more detailed error
@Test
public void illegalLines() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ public void importOrderingAndFormatting() throws IOException, UsageException {
importOrdering(null, "com/google/googlejavaformat/java/testimports/A.imports-and-formatting");
}

@Test
public void formattingWithoutImportOrdering() throws IOException, UsageException {
importOrdering(
"--skip-sorting-imports",
"com/google/googlejavaformat/java/testimports/A.formatting-only");
}

private void importOrdering(String sortArg, String outputResourceName)
throws IOException, UsageException {
Path tmpdir = testFolder.newFolder().toPath();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.google.example;

import com.google.common.base.Preconditions;

import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.List;

import javax.annotations.Nullable;

import static org.junit.Assert.fail;
import static com.google.truth.Truth.assertThat;

@RunWith(JUnit4.class)
public class SomeTest {

<T> void check(@Nullable List<T> x) {
Preconditions.checkNodeNull(x);
}

void f() {
List<String> xs = null;
assertThat(xs).isNull();
try {
check(xs);
fail();
} catch (NullPointerException e) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.List;
import java.util.Set;

import javax.annotations.Nullable;

Expand Down