From c4dedd508aa61c5733ae404bfbbf6a02c4f474a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Mon, 30 Mar 2026 10:45:58 -0700 Subject: [PATCH] Update `UsageException` to use text blocks. PiperOrigin-RevId: 891795727 --- .../googlejavaformat/java/UsageException.java | 122 ++++++++---------- 1 file changed, 57 insertions(+), 65 deletions(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/UsageException.java b/core/src/main/java/com/google/googlejavaformat/java/UsageException.java index 50d55d4d4..2cdbd92c7 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/UsageException.java +++ b/core/src/main/java/com/google/googlejavaformat/java/UsageException.java @@ -16,66 +16,62 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.base.Joiner; - /** Checked exception class for formatter command-line usage errors. */ final class UsageException extends Exception { - private static final Joiner NEWLINE_JOINER = Joiner.on(System.lineSeparator()); + private static final String DOCS_LINK = + "https://github.com/google/google-java-format"; + + private static final String USAGE = +""" + +Usage: google-java-format [options] file(s) - private static final String[] DOCS_LINK = { - "https://github.com/google/google-java-format", - }; +Options: + -i, -r, -replace, --replace + Send formatted output back to files, not stdout. + - + Format stdin -> stdout + --assume-filename, -assume-filename + File name to use for diagnostics when formatting standard input (default is ). + --aosp, -aosp, -a + 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. + --skip-removing-unused-imports + Do not remove unused imports. Imports will still be sorted. + --skip-reflowing-long-strings + Do not reflow string literals that exceed the column limit. + --skip-javadoc-formatting + Do not reformat javadoc. + --dry-run, -n + Prints the paths of the files whose contents would change if the formatter were run normally. + --set-exit-if-changed + Return exit code 1 if there are any formatting changes. + --lines, -lines, --line, -line + Line range(s) to format, e.g. the first 5 lines are 1:5 (1-based; default is all). + --offset, -offset + Character offset to format (0-based; default is all). + --length, -length + Character length to format. + --help, -help, -h + Print this usage statement. + --version, -version, -v + Print the version. + @ + Read options and filenames from file. - private static final String[] USAGE = { - "", - "Usage: google-java-format [options] file(s)", - "", - "Options:", - " -i, -r, -replace, --replace", - " Send formatted output back to files, not stdout.", - " -", - " Format stdin -> stdout", - " --assume-filename, -assume-filename", - " File name to use for diagnostics when formatting standard input (default is ).", - " --aosp, -aosp, -a", - " 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.", - " --skip-removing-unused-imports", - " Do not remove unused imports. Imports will still be sorted.", - " --skip-reflowing-long-strings", - " Do not reflow string literals that exceed the column limit.", - " --skip-javadoc-formatting", - " Do not reformat javadoc.", - " --dry-run, -n", - " Prints the paths of the files whose contents would change if the formatter were run" - + " normally.", - " --set-exit-if-changed", - " Return exit code 1 if there are any formatting changes.", - " --lines, -lines, --line, -line", - " Line range(s) to format, e.g. the first 5 lines are 1:5 (1-based; default is all).", - " --offset, -offset", - " Character offset to format (0-based; default is all).", - " --length, -length", - " Character length to format.", - " --help, -help, -h", - " Print this usage statement.", - " --version, -version, -v", - " Print the version.", - " @", - " Read options and filenames from file.", - "", - }; +"""; - private static final String[] ADDITIONAL_USAGE = { - "If -i is given with -, the result is sent to stdout.", - "The --lines, --offset, and --length flags may be given more than once.", - "The --offset and --length flags must be given an equal number of times.", - "If --lines, --offset, or --length are given, only one file (or -) may be given." - }; + private static final String ADDITIONAL_USAGE = +""" +If -i is given with -, the result is sent to stdout. +The --lines, --offset, and --length flags may be given more than once. +The --offset and --length flags must be given an equal number of times. +If --lines, --offset, or --length are given, only one file (or -) may be given. +"""; UsageException() { super(buildMessage(null)); @@ -90,19 +86,15 @@ private static String buildMessage(String message) { if (message != null) { builder.append(message).append('\n'); } - appendLines(builder, USAGE); - appendLines(builder, ADDITIONAL_USAGE); - appendLines(builder, new String[] {""}); - appendLine(builder, Main.versionString()); - appendLines(builder, DOCS_LINK); + appendText(builder, USAGE); + appendText(builder, ADDITIONAL_USAGE); + appendText(builder, "\n"); + appendText(builder, Main.versionString()); + appendText(builder, DOCS_LINK); return builder.toString(); } - private static void appendLine(StringBuilder builder, String line) { - builder.append(line).append(System.lineSeparator()); - } - - private static void appendLines(StringBuilder builder, String[] lines) { - NEWLINE_JOINER.appendTo(builder, lines).append(System.lineSeparator()); + private static void appendText(StringBuilder builder, String text) { + builder.append(text.replace("\n", System.lineSeparator())); } }