Skip to content

Commit

Permalink
Remove SortImports from google-java-format API
Browse files Browse the repository at this point in the history
This option is only used by the CLI, so putting it in the API is
misleading.

MOE_MIGRATED_REVID=124406657
  • Loading branch information
cushon committed Jun 11, 2016
1 parent db2ac5a commit 2f86424
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 39 deletions.
Expand Up @@ -19,7 +19,7 @@
import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeSet;
import com.google.googlejavaformat.java.JavaFormatterOptions.SortImports;
import com.google.googlejavaformat.java.Main.SortImports;

import java.util.List;
import java.util.concurrent.Callable;
Expand All @@ -34,28 +34,31 @@ public class FormatFileCallable implements Callable<String> {
private final ImmutableList<Integer> offsets;
private final ImmutableList<Integer> lengths;
private final JavaFormatterOptions options;
private final SortImports sortImports;

public FormatFileCallable(
RangeSet<Integer> lineRanges,
List<Integer> offsets,
List<Integer> lengths,
String input,
JavaFormatterOptions options) {
JavaFormatterOptions options,
SortImports sortImports) {
this.input = input;
this.lineRanges = ImmutableRangeSet.copyOf(lineRanges);
this.offsets = ImmutableList.copyOf(offsets);
this.lengths = ImmutableList.copyOf(lengths);
this.options = options;
this.sortImports = sortImports;
}

@Override
public String call() throws FormatterException {

// TODO(cushon): figure out how to integrate import ordering into Formatter
String inputString = input;
if (options.sortImports() != SortImports.NO) {
if (sortImports != SortImports.NO) {
inputString = ImportOrderer.reorderImports(inputString);
if (options.sortImports() == SortImports.ONLY) {
if (sortImports == SortImports.ONLY) {
return inputString;
}
}
Expand Down
Expand Up @@ -28,7 +28,6 @@
import com.google.googlejavaformat.Op;
import com.google.googlejavaformat.OpsBuilder;
import com.google.googlejavaformat.java.JavaFormatterOptions.JavadocFormatter;
import com.google.googlejavaformat.java.JavaFormatterOptions.SortImports;
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;

import org.eclipse.jdt.core.JavaCore;
Expand Down Expand Up @@ -86,8 +85,7 @@ public final class Formatter {
* A new Formatter instance with default options.
*/
public Formatter() {
this(
new JavaFormatterOptions(JavadocFormatter.NONE, Style.GOOGLE, SortImports.NO));
this(new JavaFormatterOptions(JavadocFormatter.NONE, Style.GOOGLE));
}

public Formatter(JavaFormatterOptions options) {
Expand Down
Expand Up @@ -42,12 +42,6 @@ public class JavaFormatterOptions {

static final int DEFAULT_MAX_LINE_LENGTH = 100;

public enum SortImports {
NO,
ONLY,
ALSO
}

public enum Style {

/** The default Google Java Style configuration. */
Expand Down Expand Up @@ -92,13 +86,10 @@ public String format(JavaFormatterOptions options, String text, int column0) {

private final JavadocFormatter javadocFormatter;
private final Style style;
private final SortImports sortImports;

public JavaFormatterOptions(
JavadocFormatter javadocFormatter, Style style, SortImports sortImports) {
public JavaFormatterOptions(JavadocFormatter javadocFormatter, Style style) {
this.javadocFormatter = javadocFormatter;
this.style = style;
this.sortImports = sortImports;
}

/** Returns the Javadoc formatter. */
Expand All @@ -115,8 +106,4 @@ public int maxLineLength() {
public int indentationMultiplier() {
return style.indentationMultiplier();
}

public SortImports sortImports() {
return sortImports;
}
}
24 changes: 15 additions & 9 deletions core/src/main/java/com/google/googlejavaformat/java/Main.java
Expand Up @@ -23,7 +23,6 @@
import com.google.common.collect.TreeRangeSet;
import com.google.common.io.ByteStreams;
import com.google.googlejavaformat.java.JavaFormatterOptions.JavadocFormatter;
import com.google.googlejavaformat.java.JavaFormatterOptions.SortImports;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
Expand Down Expand Up @@ -56,6 +55,12 @@ public final class Main {
private static final Splitter COLON_SPLITTER = Splitter.on(':');
private static final String STDIN_FILENAME = "<stdin>";

enum SortImports {
NO,
ONLY,
ALSO
}

@Parameters(separators = "=")
private static final class FormatterParameters {
@Parameter(
Expand Down Expand Up @@ -213,17 +218,16 @@ public int format(String... args) throws UsageException {
JavadocFormatter.NONE,
argInfo.parameters.aospFlag
? JavaFormatterOptions.Style.AOSP
: JavaFormatterOptions.Style.GOOGLE,
sortImports);
: JavaFormatterOptions.Style.GOOGLE);

if (argInfo.parameters.stdinStdoutFlag) {
return formatStdin(argInfo, options);
return formatStdin(argInfo, options, sortImports);
} else {
return formatFiles(argInfo, options);
return formatFiles(argInfo, options, sortImports);
}
}

private int formatFiles(ArgInfo argInfo, JavaFormatterOptions options) {
private int formatFiles(ArgInfo argInfo, JavaFormatterOptions options, SortImports sortImports) {
int numThreads = Math.min(MAX_THREADS, argInfo.parameters.fileNamesFlag.size());
ExecutorService executorService = Executors.newFixedThreadPool(numThreads);

Expand Down Expand Up @@ -251,7 +255,8 @@ private int formatFiles(ArgInfo argInfo, JavaFormatterOptions options) {
argInfo.parameters.offsetFlags,
argInfo.parameters.lengthFlags,
input,
options)));
options,
sortImports)));
}

boolean allOk = true;
Expand Down Expand Up @@ -290,7 +295,7 @@ private int formatFiles(ArgInfo argInfo, JavaFormatterOptions options) {
return allOk ? 0 : 1;
}

private int formatStdin(ArgInfo argInfo, JavaFormatterOptions options) {
private int formatStdin(ArgInfo argInfo, JavaFormatterOptions options, SortImports sortImports) {
String input;
try {
input = new String(ByteStreams.toByteArray(inStream), UTF_8);
Expand All @@ -304,7 +309,8 @@ private int formatStdin(ArgInfo argInfo, JavaFormatterOptions options) {
argInfo.parameters.offsetFlags,
argInfo.parameters.lengthFlags,
input,
options)
options,
sortImports)
.call();
outWriter.write(output);
return 0;
Expand Down
Expand Up @@ -25,7 +25,6 @@
import com.google.common.collect.TreeRangeMap;
import com.google.common.collect.TreeRangeSet;
import com.google.googlejavaformat.java.JavaFormatterOptions.JavadocFormatter;
import com.google.googlejavaformat.java.JavaFormatterOptions.SortImports;
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;

import org.eclipse.jdt.core.JavaCore;
Expand Down Expand Up @@ -216,8 +215,7 @@ private static String applyReplacements(String source, RangeMap<Integer, String>
if (!fixedRanges.isEmpty()) {
try {
result =
new Formatter(
new JavaFormatterOptions(JavadocFormatter.GOOGLE, Style.GOOGLE, SortImports.NO))
new Formatter(new JavaFormatterOptions(JavadocFormatter.GOOGLE, Style.GOOGLE))
.formatSource(result, fixedRanges.asRanges());
} catch (FormatterException e) {
// javadoc reformatting is best-effort
Expand Down
Expand Up @@ -20,7 +20,6 @@
import com.google.common.base.Joiner;
import com.google.common.io.ByteStreams;
import com.google.googlejavaformat.java.JavaFormatterOptions.JavadocFormatter;
import com.google.googlejavaformat.java.JavaFormatterOptions.SortImports;
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;

import org.junit.Test;
Expand All @@ -32,8 +31,7 @@
public final class EclipseJavadocFormattingTest {

public final Formatter formatter =
new Formatter(
new JavaFormatterOptions(JavadocFormatter.ECLIPSE, Style.GOOGLE, SortImports.NO));
new Formatter(new JavaFormatterOptions(JavadocFormatter.ECLIPSE, Style.GOOGLE));

@Test
public void simple() throws Exception {
Expand Down
Expand Up @@ -20,7 +20,6 @@
import com.google.common.base.Joiner;
import com.google.common.io.ByteStreams;
import com.google.googlejavaformat.java.JavaFormatterOptions.JavadocFormatter;
import com.google.googlejavaformat.java.JavaFormatterOptions.SortImports;
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;

import org.junit.Test;
Expand All @@ -32,8 +31,7 @@
public final class GoogleJavadocFormattingTest {

private final Formatter formatter =
new Formatter(
new JavaFormatterOptions(JavadocFormatter.GOOGLE, Style.GOOGLE, SortImports.NO));
new Formatter(new JavaFormatterOptions(JavadocFormatter.GOOGLE, Style.GOOGLE));

@Test
public void notJavadoc() {
Expand Down

0 comments on commit 2f86424

Please sign in to comment.