Skip to content

Commit dda16c2

Browse files
committed
Merge upstream master into fix/1369-markdown-link-wrap
2 parents 11e27bc + f10be21 commit dda16c2

20 files changed

Lines changed: 731 additions & 90 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ jobs:
6666
release: ${{ matrix.java }}
6767
- name: "Set up JDK ${{ matrix.java }} from Zulu"
6868
if: ${{ matrix.java != 'EA' && matrix.java != 'GraalVM' }}
69-
uses: actions/setup-java@b6effb05e454b25005698d916606bdc6ffcbf961 # v5.7.0
69+
uses: actions/setup-java@b843ebf9b913146b38da2eaa7aeddf76074ae1ad # v6.1.0
7070
with:
7171
java-version: ${{ matrix.java }}
7272
distribution: "zulu"
@@ -95,7 +95,7 @@ jobs:
9595
with:
9696
persist-credentials: false
9797
- name: "Set up GraalVM ${{ matrix.java }}"
98-
uses: graalvm/setup-graalvm@5298d94fb55a4f185c602eeac5de1b553882abe2 # v1.6.4
98+
uses: graalvm/setup-graalvm@27b360dbb857524eaf0b4c4cef364d38e5b4f173 # v1.6.7
9999
with:
100100
java-version: "25"
101101
distribution: "graalvm-community"
@@ -121,7 +121,7 @@ jobs:
121121
with:
122122
persist-credentials: false
123123
- name: "Set up JDK 21"
124-
uses: actions/setup-java@b6effb05e454b25005698d916606bdc6ffcbf961 # v5.7.0
124+
uses: actions/setup-java@b843ebf9b913146b38da2eaa7aeddf76074ae1ad # v6.1.0
125125
with:
126126
java-version: 21
127127
distribution: "zulu"

‎.github/workflows/release.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
persist-credentials: false
3434

3535
- name: Set up JDK
36-
uses: actions/setup-java@b6effb05e454b25005698d916606bdc6ffcbf961 # v5.7.0
36+
uses: actions/setup-java@b843ebf9b913146b38da2eaa7aeddf76074ae1ad # v6.1.0
3737
with:
3838
java-version: 21
3939
distribution: "zulu"
@@ -76,7 +76,7 @@ jobs:
7676
GITHUB_EVENT_INPUTS_VERSION: ${{ github.event.inputs.version }}
7777

7878
- name: Add Artifacts to Release Entry
79-
uses: softprops/action-gh-release@3d0d9888cb7fd7b750713d6e236d1fcb99157228 # v3.0.2
79+
uses: softprops/action-gh-release@efb35369e0ad2afab669f228072c1b0d510eae64 # v3.0.3
8080
with:
8181
draft: true
8282
name: ${{ github.event.input.version }}
@@ -111,7 +111,7 @@ jobs:
111111
with:
112112
persist-credentials: false
113113
- name: "Set up GraalVM"
114-
uses: graalvm/setup-graalvm@5298d94fb55a4f185c602eeac5de1b553882abe2 # v1.6.4
114+
uses: graalvm/setup-graalvm@27b360dbb857524eaf0b4c4cef364d38e5b4f173 # v1.6.7
115115
with:
116116
java-version: "25"
117117
distribution: "graalvm-community"

‎README.md‎

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ offsets (`--offset`), passing through to standard-out (default) or altered
2929
in-place (`--replace`).
3030

3131
Option `--help` will print full usage details; including built-in documentation
32-
about other flags, such as `--aosp`, `--fix-imports-only`,
32+
about other flags, such as `--style`, `--max-line-length`, `--fix-imports-only`,
3333
`--skip-sorting-imports`, `--skip-removing-unused-import`,
3434
`--skip-reflowing-long-strings`, `--skip-javadoc-formatting`, or the `--dry-run`
3535
and `--set-exit-if-changed`.
@@ -40,9 +40,10 @@ arguments.
4040
To reformat changed lines in a specific patch, use
4141
[`google-java-format-diff.py`](https://github.com/google/google-java-format/blob/master/scripts/google-java-format-diff.py).
4242

43-
***Note:*** *There is no configurability as to the formatter's algorithm for
44-
formatting. This is a deliberate design decision to unify our code formatting on
45-
a single format.*
43+
***Note:*** *There is almost no configurability as to the formatter's algorithm
44+
for formatting. This is a deliberate design decision to unify our code
45+
formatting on a single format. See [Configuration](#configuration) for the few
46+
options that are available.*
4647

4748
### IntelliJ, Android Studio, and other JetBrains IDEs
4849

@@ -192,6 +193,46 @@ new Formatter().formatSource(source, output);
192193
Your starting point should be the instance methods of
193194
`com.google.googlejavaformat.java.Formatter`.
194195

196+
## Configuration
197+
198+
`google-java-format` is an opinionated formatter with very little configuration.
199+
Its core formatting decisions, like where lines break and how much continuation
200+
lines are indented, are deliberately not configurable. See also
201+
[this FAQ entry](https://github.com/google/google-java-format/wiki/FAQ#i-just-need-to-configure-it-a-bit-differently-how)
202+
for more details.
203+
204+
There are a few configuration options for projects that need them:
205+
206+
* `--style=google|aosp` selects the code style. The default, `google`, follows
207+
[Google Java Style][]. `aosp` follows the
208+
[AOSP Java code style](https://source.android.com/docs/setup/contribute/code-style):
209+
it indents blocks by 4 spaces and continuation lines by 8, instead of 2
210+
and 4. Line breaking is otherwise the same as Google style, so
211+
`--style=aosp` is also an option for projects that want 4-space indentation
212+
without otherwise following AOSP conventions. (`--google-style` and `--aosp`
213+
are equivalent shorthands.)
214+
* `--max-line-length=N` sets the column limit. The default is 100.
215+
216+
Besides layout, the formatter makes some changes that aren't just whitespace,
217+
and each of them can be turned off:
218+
219+
* `--cleanup=false` disables small cleanups such as removing redundant
220+
semicolons, empty annotation parentheses (`@Foo()` to `@Foo`), and
221+
parentheses around a single lambda parameter (`(x) -> ...` to `x -> ...`).
222+
Cleanups are enabled by default, and only apply to the lines being
223+
formatted.
224+
* `--skip-sorting-imports` and `--skip-removing-unused-imports` disable import
225+
sorting and unused import removal (`--fix-imports-only` does only those, and
226+
no other formatting).
227+
* `--skip-reordering-modifiers` keeps modifiers in their existing order.
228+
* `--skip-reflowing-long-strings` disables splitting string literals that
229+
exceed the column limit.
230+
* `--skip-javadoc-formatting` leaves Javadoc comments unformatted.
231+
232+
Run `google-java-format --help` for the full command-line reference. When using
233+
the formatter as a library, `JavaFormatterOptions.builder()` and
234+
`JavaFormatterOptions.Style.builder()` provide the corresponding options.
235+
195236
## Building from source
196237

197238
```

‎core/src/main/java/com/google/googlejavaformat/java/ImportOrderer.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private ImmutableList<Replacement> findReplacements() throws FormatterException
9494
// Make sure there are no more imports before the next class (etc) definition.
9595
Optional<Integer> maybeLaterImport = findIdentifier(afterLastImport, IMPORT_OR_CLASS_START);
9696
if (maybeLaterImport.isPresent() && tokenAt(maybeLaterImport.get()).equals("import")) {
97-
throw new FormatterException("Imports not contiguous (perhaps a comment separates them?)");
97+
return ImmutableList.of();
9898
}
9999

100100
int start = toks.get(unindentedFirstImportStart).getPosition();

‎core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,18 @@ private String preserveIndentation(List<String> lines, int column0) {
112112
private String indentLineComments(Tok tok, List<String> lines, int column0) {
113113
lines = wrapLineComments(tok, lines, column0);
114114
StringBuilder builder = new StringBuilder();
115-
builder.append(lines.get(0).trim());
116115
String indentString = options.indentString(column0);
117-
for (int i = 1; i < lines.size(); ++i) {
118-
builder.append(lineSeparator).append(indentString).append(lines.get(i).trim());
116+
for (int i = 0; i < lines.size(); ++i) {
117+
if (i > 0) {
118+
builder.append(lineSeparator).append(indentString);
119+
}
120+
builder.append(CharMatcher.whitespace().trimFrom(lines.get(i)));
119121
}
120122
return builder.toString();
121123
}
122124

123-
private static final Pattern LINE_COMMENT_MISSING_SPACE_PREFIX = Pattern.compile("^(//+)[^\\s/]");
125+
private static final Pattern LINE_COMMENT_MISSING_SPACE_PREFIX =
126+
Pattern.compile("^(//+)[^\\s/]", Pattern.UNICODE_CHARACTER_CLASS);
124127

125128
// Preserve special `//noinspection` and `//$NON-NLS-x$` comments used by IDEs, which cannot
126129
// contain leading spaces.
@@ -160,8 +163,8 @@ private List<String> wrapLineComments(Tok tok, List<String> lines, int column0)
160163
int prefixLength = lineCommentPrefix.length();
161164
while (line.length() + column0 > options.maxLineLength()) {
162165
int idx = options.maxLineLength() - column0;
163-
// only break on whitespace characters, and ignore the leading comment marker
164-
while (idx >= prefixLength && !CharMatcher.whitespace().matches(line.charAt(idx))) {
166+
// only break on breaking whitespace characters, and ignore the leading comment marker
167+
while (idx >= prefixLength && !CharMatcher.breakingWhitespace().matches(line.charAt(idx))) {
165168
idx--;
166169
}
167170
if (idx <= prefixLength) {

0 commit comments

Comments
 (0)