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
9 changes: 5 additions & 4 deletions core/src/main/java/com/google/googlejavaformat/Newlines.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,16 @@ public static String guessLineSeparator(String text) {
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
switch (c) {
case '\r':
case '\r' -> {
if (i + 1 < text.length() && text.charAt(i + 1) == '\n') {
return "\r\n";
}
return "\r";
case '\n':
}
case '\n' -> {
return "\n";
default:
break;
}
default -> {}
}
}
return "\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,72 +66,25 @@ static CommandLineOptions parse(Iterable<String> options) {
}
// 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":
optionsBuilder.fixImportsOnly(true);
break;
case "--skip-sorting-imports":
optionsBuilder.sortImports(false);
break;
case "--skip-removing-unused-imports":
optionsBuilder.removeUnusedImports(false);
break;
case "--skip-reflowing-long-strings":
optionsBuilder.reflowLongStrings(false);
break;
case "--skip-javadoc-formatting":
optionsBuilder.formatJavadoc(false);
break;
case "-":
optionsBuilder.stdin(true);
break;
case "-n":
case "--dry-run":
optionsBuilder.dryRun(true);
break;
case "--set-exit-if-changed":
optionsBuilder.setExitIfChanged(true);
break;
case "-assume-filename":
case "--assume-filename":
optionsBuilder.assumeFilename(getValue(flag, it, value));
break;
default:
throw new IllegalArgumentException("unexpected flag: " + flag);
case "-i", "-r", "-replace", "--replace" -> optionsBuilder.inPlace(true);
case "--lines", "-lines", "--line", "-line" ->
parseRangeSet(linesBuilder, getValue(flag, it, value));
case "--offset", "-offset" -> optionsBuilder.addOffset(parseInteger(it, flag, value));
case "--length", "-length" -> optionsBuilder.addLength(parseInteger(it, flag, value));
case "--aosp", "-aosp", "-a" -> optionsBuilder.aosp(true);
case "--version", "-version", "-v" -> optionsBuilder.version(true);
case "--help", "-help", "-h" -> optionsBuilder.help(true);
case "--fix-imports-only" -> optionsBuilder.fixImportsOnly(true);
case "--skip-sorting-imports" -> optionsBuilder.sortImports(false);
case "--skip-removing-unused-imports" -> optionsBuilder.removeUnusedImports(false);
case "--skip-reflowing-long-strings" -> optionsBuilder.reflowLongStrings(false);
case "--skip-javadoc-formatting" -> optionsBuilder.formatJavadoc(false);
case "-" -> optionsBuilder.stdin(true);
case "-n", "--dry-run" -> optionsBuilder.dryRun(true);
case "--set-exit-if-changed" -> optionsBuilder.setExitIfChanged(true);
case "-assume-filename", "--assume-filename" ->
optionsBuilder.assumeFilename(getValue(flag, it, value));
default -> throw new IllegalArgumentException("unexpected flag: " + flag);
}
}
optionsBuilder.lines(ImmutableRangeSet.copyOf(linesBuilder));
Expand Down Expand Up @@ -175,17 +128,18 @@ private static void parseRangeSet(RangeSet<Integer> result, String ranges) {
*/
private static Range<Integer> parseRange(String arg) {
List<String> args = COLON_SPLITTER.splitToList(arg);
switch (args.size()) {
case 1:
return switch (args.size()) {
case 1 -> {
int line = Integer.parseInt(args.get(0)) - 1;
return Range.closedOpen(line, line + 1);
case 2:
yield Range.closedOpen(line, line + 1);
}
case 2 -> {
int line0 = Integer.parseInt(args.get(0)) - 1;
int line1 = Integer.parseInt(args.get(1)) - 1;
return Range.closedOpen(line0, line1 + 1);
default:
throw new IllegalArgumentException(arg);
}
yield Range.closedOpen(line0, line1 + 1);
}
default -> throw new IllegalArgumentException(arg);
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,18 @@ private static Iterable<List<AnnotationTree>> reorderBySourcePosition(
* int}.
*/
private static Tree extractDims(Deque<List<AnnotationTree>> dims, Tree node) {
switch (node.getKind()) {
case ARRAY_TYPE:
return extractDims(dims, ((ArrayTypeTree) node).getType());
case ANNOTATED_TYPE:
return switch (node.getKind()) {
case ARRAY_TYPE -> extractDims(dims, ((ArrayTypeTree) node).getType());
case ANNOTATED_TYPE -> {
AnnotatedTypeTree annotatedTypeTree = (AnnotatedTypeTree) node;
if (annotatedTypeTree.getUnderlyingType().getKind() != Tree.Kind.ARRAY_TYPE) {
return node;
yield node;
}
node = extractDims(dims, annotatedTypeTree.getUnderlyingType());
dims.addFirst(ImmutableList.copyOf(annotatedTypeTree.getAnnotations()));
return node;
default:
return node;
}
yield node;
}
default -> node;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,9 @@ static boolean errorDiagnostic(Diagnostic<?> input) {
if (input.getKind() != Diagnostic.Kind.ERROR) {
return false;
}
switch (input.getCode()) {
case "compiler.err.invalid.meth.decl.ret.type.req":
// accept constructor-like method declarations that don't match the name of their
// enclosing class
return false;
default:
break;
}
return true;
// accept constructor-like method declarations that don't match the name of their
// enclosing class
return !input.getCode().equals("compiler.err.invalid.meth.decl.ret.type.req");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,10 @@ boolean isAndroid() {

/** True if this is a Java import per AOSP style. */
boolean isJava() {
switch (topLevel()) {
case "java":
case "javax":
return true;
default:
return false;
}
return switch (topLevel()) {
case "java", "javax" -> true;
default -> false;
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,20 +561,18 @@ private static ImmutableList<Token> buildTokens(List<Tok> toks) {
// TODO(cushon): find a better strategy.
if (toks.get(k).isSlashStarComment()) {
switch (tok.getText()) {
case "(":
case "<":
case ".":
case "(", "<", "." -> {
break OUTER;
default:
break;
}
default -> {}
}
}
if (toks.get(k).isJavadocComment()) {
switch (tok.getText()) {
case ";":
case ";" -> {
break OUTER;
default:
break;
}
default -> {}
}
}
if (isParamComment(toks.get(k))) {
Expand Down
Loading
Loading