Skip to content

Automated rollback of commit 4c91240f69bc4982c5c3908087dfccdbcb93e5aa. #1242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2025
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 @@ -57,8 +57,7 @@ record CommandLineOptions(
boolean setExitIfChanged,
Optional<String> assumeFilename,
boolean reflowLongStrings,
boolean formatJavadoc,
Optional<String> profile) {
boolean formatJavadoc) {

/** Returns true if partial formatting was selected. */
boolean isSelection() {
Expand Down Expand Up @@ -130,8 +129,6 @@ default Builder addLength(Integer length) {

Builder formatJavadoc(boolean formatJavadoc);

Builder profile(String profile);

CommandLineOptions build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.googlejavaformat.java;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
Expand All @@ -25,6 +26,7 @@
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -62,67 +64,72 @@ static CommandLineOptions parse(Iterable<String> options) {
flag = option;
value = null;
}
flag = flag.startsWith("--") ? flag.substring(1) : flag;
// NOTE: update usage information in UsageException when new flags are added
switch (flag) {
case "-i":
case "-r":
case "-replace":
case "--replace":
optionsBuilder.inPlace(true);
break;
case "--lines":
case "-lines":
case "--line":
case "-line":
parseRangeSet(linesBuilder, getValue(flag, it, value));
break;
case "--offset":
case "-offset":
optionsBuilder.addOffset(parseInteger(it, flag, value));
break;
case "--length":
case "-length":
optionsBuilder.addLength(parseInteger(it, flag, value));
break;
case "--aosp":
case "-aosp":
case "-a":
optionsBuilder.aosp(true);
break;
case "--version":
case "-version":
case "-v":
optionsBuilder.version(true);
break;
case "--help":
case "-help":
case "-h":
optionsBuilder.help(true);
break;
case "-fix-imports-only":
case "--fix-imports-only":
optionsBuilder.fixImportsOnly(true);
break;
case "-skip-sorting-imports":
case "--skip-sorting-imports":
optionsBuilder.sortImports(false);
break;
case "-skip-removing-unused-imports":
case "--skip-removing-unused-imports":
optionsBuilder.removeUnusedImports(false);
break;
case "-skip-reflowing-long-strings":
case "--skip-reflowing-long-strings":
optionsBuilder.reflowLongStrings(false);
break;
case "-skip-javadoc-formatting":
case "--skip-javadoc-formatting":
optionsBuilder.formatJavadoc(false);
break;
case "-":
optionsBuilder.stdin(true);
break;
case "-n":
case "-dry-run":
case "--dry-run":
optionsBuilder.dryRun(true);
break;
case "-set-exit-if-changed":
case "--set-exit-if-changed":
optionsBuilder.setExitIfChanged(true);
break;
case "-assume-filename":
case "--assume-filename":
optionsBuilder.assumeFilename(getValue(flag, it, value));
break;
case "-profile":
optionsBuilder.profile(getValue(flag, it, value));
break;
default:
throw new IllegalArgumentException("unexpected flag: " + flag);
}
Expand Down Expand Up @@ -195,16 +202,14 @@ private static void expandParamsFiles(Iterable<String> args, List<String> expand
} else if (arg.startsWith("@@")) {
expanded.add(arg.substring(1));
} else {
Path path = Path.of(arg.substring(1));
Path path = Paths.get(arg.substring(1));
try {
String sequence = Files.readString(path);
String sequence = new String(Files.readAllBytes(path), UTF_8);
expandParamsFiles(ARG_SPLITTER.split(sequence), expanded);
} catch (IOException e) {
throw new UncheckedIOException(path + ": could not read file: " + e.getMessage(), e);
}
}
}
}

private CommandLineOptionsParser() {}
}
11 changes: 4 additions & 7 deletions core/src/main/java/com/google/googlejavaformat/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ static int main(InputStream in, PrintWriter out, PrintWriter err, String... args
*/
public int format(String... args) throws UsageException {
CommandLineOptions parameters = processArgs(args);
return format(parameters);
}

int format(CommandLineOptions parameters) {
if (parameters.version()) {
errWriter.println(versionString());
return 0;
}
if (parameters.help()) {
throw new UsageException();
}

JavaFormatterOptions options =
JavaFormatterOptions.builder()
.style(parameters.aosp() ? Style.AOSP : Style.GOOGLE)
Expand Down Expand Up @@ -278,9 +278,6 @@ public static CommandLineOptions processArgs(String... args) throws UsageExcepti
if (parameters.dryRun() && parameters.inPlace()) {
throw new UsageException("cannot use --dry-run and --in-place at the same time");
}
if (parameters.help()) {
throw new UsageException();
}
return parameters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,7 @@ public void numberOfOffsetsMustMatchNumberOfLengths() throws UsageException {
public void noFilesToFormatRequiresEitherHelpOrVersion() throws UsageException {
Main.processArgs("-version");

try {
Main.processArgs("-help");
fail();
} catch (UsageException e) {
// expected
}
Main.processArgs("-help");

try {
Main.processArgs();
Expand Down