Skip to content

Commit db2713a

Browse files
cushongoogle-java-format Team
authored and
google-java-format Team
committed
Migrate to CommandLineOptions to a record and AutoBuilder
This allows the CheckReturnValue analysis to tell that it's a builder and ignore the results of builder methods. PiperOrigin-RevId: 745341492
1 parent 8633826 commit db2713a

File tree

2 files changed

+83
-247
lines changed

2 files changed

+83
-247
lines changed

core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java

Lines changed: 76 additions & 246 deletions
Original file line numberDiff line numberDiff line change
@@ -14,291 +14,121 @@
1414

1515
package com.google.googlejavaformat.java;
1616

17+
import com.google.auto.value.AutoBuilder;
1718
import com.google.common.collect.ImmutableList;
1819
import com.google.common.collect.ImmutableRangeSet;
19-
import com.google.common.collect.RangeSet;
20-
import com.google.common.collect.TreeRangeSet;
20+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2121
import java.util.Optional;
2222

2323
/**
2424
* Command line options for google-java-format.
2525
*
26-
* <p>google-java-format doesn't depend on AutoValue, to allow AutoValue to depend on
27-
* google-java-format.
26+
* @param files The files to format.
27+
* @param inPlace Format files in place.
28+
* @param lines Line ranges to format.
29+
* @param offsets Character offsets for partial formatting, paired with {@code lengths}.
30+
* @param lengths Partial formatting region lengths, paired with {@code offsets}.
31+
* @param aosp Use AOSP style instead of Google Style (4-space indentation).
32+
* @param version Print the version.
33+
* @param help Print usage information.
34+
* @param stdin Format input from stdin.
35+
* @param fixImportsOnly Fix imports, but do no formatting.
36+
* @param sortImports Sort imports.
37+
* @param removeUnusedImports Remove unused imports.
38+
* @param dryRun Print the paths of the files whose contents would change if the formatter were run
39+
* normally.
40+
* @param setExitIfChanged Return exit code 1 if there are any formatting changes.
41+
* @param assumeFilename Return the name to use for diagnostics when formatting standard input.
2842
*/
29-
final class CommandLineOptions {
30-
31-
private final ImmutableList<String> files;
32-
private final boolean inPlace;
33-
private final ImmutableRangeSet<Integer> lines;
34-
private final ImmutableList<Integer> offsets;
35-
private final ImmutableList<Integer> lengths;
36-
private final boolean aosp;
37-
private final boolean version;
38-
private final boolean help;
39-
private final boolean stdin;
40-
private final boolean fixImportsOnly;
41-
private final boolean sortImports;
42-
private final boolean removeUnusedImports;
43-
private final boolean dryRun;
44-
private final boolean setExitIfChanged;
45-
private final Optional<String> assumeFilename;
46-
private final boolean reflowLongStrings;
47-
private final boolean formatJavadoc;
48-
49-
CommandLineOptions(
50-
ImmutableList<String> files,
51-
boolean inPlace,
52-
ImmutableRangeSet<Integer> lines,
53-
ImmutableList<Integer> offsets,
54-
ImmutableList<Integer> lengths,
55-
boolean aosp,
56-
boolean version,
57-
boolean help,
58-
boolean stdin,
59-
boolean fixImportsOnly,
60-
boolean sortImports,
61-
boolean removeUnusedImports,
62-
boolean dryRun,
63-
boolean setExitIfChanged,
64-
Optional<String> assumeFilename,
65-
boolean reflowLongStrings,
66-
boolean formatJavadoc) {
67-
this.files = files;
68-
this.inPlace = inPlace;
69-
this.lines = lines;
70-
this.offsets = offsets;
71-
this.lengths = lengths;
72-
this.aosp = aosp;
73-
this.version = version;
74-
this.help = help;
75-
this.stdin = stdin;
76-
this.fixImportsOnly = fixImportsOnly;
77-
this.sortImports = sortImports;
78-
this.removeUnusedImports = removeUnusedImports;
79-
this.dryRun = dryRun;
80-
this.setExitIfChanged = setExitIfChanged;
81-
this.assumeFilename = assumeFilename;
82-
this.reflowLongStrings = reflowLongStrings;
83-
this.formatJavadoc = formatJavadoc;
84-
}
85-
86-
/** The files to format. */
87-
ImmutableList<String> files() {
88-
return files;
89-
}
90-
91-
/** Format files in place. */
92-
boolean inPlace() {
93-
return inPlace;
94-
}
95-
96-
/** Line ranges to format. */
97-
ImmutableRangeSet<Integer> lines() {
98-
return lines;
99-
}
100-
101-
/** Character offsets for partial formatting, paired with {@code lengths}. */
102-
ImmutableList<Integer> offsets() {
103-
return offsets;
104-
}
105-
106-
/** Partial formatting region lengths, paired with {@code offsets}. */
107-
ImmutableList<Integer> lengths() {
108-
return lengths;
109-
}
110-
111-
/** Use AOSP style instead of Google Style (4-space indentation). */
112-
boolean aosp() {
113-
return aosp;
114-
}
115-
116-
/** Print the version. */
117-
boolean version() {
118-
return version;
119-
}
120-
121-
/** Print usage information. */
122-
boolean help() {
123-
return help;
124-
}
125-
126-
/** Format input from stdin. */
127-
boolean stdin() {
128-
return stdin;
129-
}
130-
131-
/** Fix imports, but do no formatting. */
132-
boolean fixImportsOnly() {
133-
return fixImportsOnly;
134-
}
135-
136-
/** Sort imports. */
137-
boolean sortImports() {
138-
return sortImports;
139-
}
140-
141-
/** Remove unused imports. */
142-
boolean removeUnusedImports() {
143-
return removeUnusedImports;
144-
}
145-
146-
/**
147-
* Print the paths of the files whose contents would change if the formatter were run normally.
148-
*/
149-
boolean dryRun() {
150-
return dryRun;
151-
}
152-
153-
/** Return exit code 1 if there are any formatting changes. */
154-
boolean setExitIfChanged() {
155-
return setExitIfChanged;
156-
}
157-
158-
/** Return the name to use for diagnostics when formatting standard input. */
159-
Optional<String> assumeFilename() {
160-
return assumeFilename;
161-
}
162-
163-
boolean reflowLongStrings() {
164-
return reflowLongStrings;
165-
}
43+
record CommandLineOptions(
44+
ImmutableList<String> files,
45+
boolean inPlace,
46+
ImmutableRangeSet<Integer> lines,
47+
ImmutableList<Integer> offsets,
48+
ImmutableList<Integer> lengths,
49+
boolean aosp,
50+
boolean version,
51+
boolean help,
52+
boolean stdin,
53+
boolean fixImportsOnly,
54+
boolean sortImports,
55+
boolean removeUnusedImports,
56+
boolean dryRun,
57+
boolean setExitIfChanged,
58+
Optional<String> assumeFilename,
59+
boolean reflowLongStrings,
60+
boolean formatJavadoc) {
16661

16762
/** Returns true if partial formatting was selected. */
16863
boolean isSelection() {
16964
return !lines().isEmpty() || !offsets().isEmpty() || !lengths().isEmpty();
17065
}
17166

172-
boolean formatJavadoc() {
173-
return formatJavadoc;
174-
}
175-
17667
static Builder builder() {
177-
return new Builder();
68+
return new AutoBuilder_CommandLineOptions_Builder()
69+
.sortImports(true)
70+
.removeUnusedImports(true)
71+
.reflowLongStrings(true)
72+
.formatJavadoc(true)
73+
.aosp(false)
74+
.version(false)
75+
.help(false)
76+
.stdin(false)
77+
.fixImportsOnly(false)
78+
.dryRun(false)
79+
.setExitIfChanged(false)
80+
.inPlace(false);
17881
}
17982

180-
static class Builder {
83+
@AutoBuilder
84+
interface Builder {
18185

182-
private final ImmutableList.Builder<String> files = ImmutableList.builder();
183-
private final RangeSet<Integer> lines = TreeRangeSet.create();
184-
private final ImmutableList.Builder<Integer> offsets = ImmutableList.builder();
185-
private final ImmutableList.Builder<Integer> lengths = ImmutableList.builder();
186-
private boolean inPlace = false;
187-
private boolean aosp = false;
188-
private boolean version = false;
189-
private boolean help = false;
190-
private boolean stdin = false;
191-
private boolean fixImportsOnly = false;
192-
private boolean sortImports = true;
193-
private boolean removeUnusedImports = true;
194-
private boolean dryRun = false;
195-
private boolean setExitIfChanged = false;
196-
private Optional<String> assumeFilename = Optional.empty();
197-
private boolean reflowLongStrings = true;
198-
private boolean formatJavadoc = true;
86+
ImmutableList.Builder<String> filesBuilder();
19987

200-
ImmutableList.Builder<String> filesBuilder() {
201-
return files;
202-
}
88+
Builder inPlace(boolean inPlace);
20389

204-
Builder inPlace(boolean inPlace) {
205-
this.inPlace = inPlace;
206-
return this;
207-
}
90+
Builder lines(ImmutableRangeSet<Integer> lines);
20891

209-
RangeSet<Integer> linesBuilder() {
210-
return lines;
211-
}
92+
ImmutableList.Builder<Integer> offsetsBuilder();
21293

213-
Builder addOffset(Integer offset) {
214-
offsets.add(offset);
94+
@CanIgnoreReturnValue
95+
default Builder addOffset(Integer offset) {
96+
offsetsBuilder().add(offset);
21597
return this;
21698
}
21799

218-
Builder addLength(Integer length) {
219-
lengths.add(length);
220-
return this;
221-
}
100+
ImmutableList.Builder<Integer> lengthsBuilder();
222101

223-
Builder aosp(boolean aosp) {
224-
this.aosp = aosp;
102+
@CanIgnoreReturnValue
103+
default Builder addLength(Integer length) {
104+
lengthsBuilder().add(length);
225105
return this;
226106
}
227107

228-
Builder version(boolean version) {
229-
this.version = version;
230-
return this;
231-
}
108+
Builder aosp(boolean aosp);
232109

233-
Builder help(boolean help) {
234-
this.help = help;
235-
return this;
236-
}
110+
Builder version(boolean version);
237111

238-
Builder stdin(boolean stdin) {
239-
this.stdin = stdin;
240-
return this;
241-
}
112+
Builder help(boolean help);
242113

243-
Builder fixImportsOnly(boolean fixImportsOnly) {
244-
this.fixImportsOnly = fixImportsOnly;
245-
return this;
246-
}
114+
Builder stdin(boolean stdin);
247115

248-
Builder sortImports(boolean sortImports) {
249-
this.sortImports = sortImports;
250-
return this;
251-
}
116+
Builder fixImportsOnly(boolean fixImportsOnly);
252117

253-
Builder removeUnusedImports(boolean removeUnusedImports) {
254-
this.removeUnusedImports = removeUnusedImports;
255-
return this;
256-
}
118+
Builder sortImports(boolean sortImports);
257119

258-
Builder dryRun(boolean dryRun) {
259-
this.dryRun = dryRun;
260-
return this;
261-
}
120+
Builder removeUnusedImports(boolean removeUnusedImports);
262121

263-
Builder setExitIfChanged(boolean setExitIfChanged) {
264-
this.setExitIfChanged = setExitIfChanged;
265-
return this;
266-
}
122+
Builder dryRun(boolean dryRun);
267123

268-
Builder assumeFilename(String assumeFilename) {
269-
this.assumeFilename = Optional.of(assumeFilename);
270-
return this;
271-
}
124+
Builder setExitIfChanged(boolean setExitIfChanged);
272125

273-
Builder reflowLongStrings(boolean reflowLongStrings) {
274-
this.reflowLongStrings = reflowLongStrings;
275-
return this;
276-
}
126+
Builder assumeFilename(String assumeFilename);
277127

278-
Builder formatJavadoc(boolean formatJavadoc) {
279-
this.formatJavadoc = formatJavadoc;
280-
return this;
281-
}
128+
Builder reflowLongStrings(boolean reflowLongStrings);
282129

283-
CommandLineOptions build() {
284-
return new CommandLineOptions(
285-
files.build(),
286-
inPlace,
287-
ImmutableRangeSet.copyOf(lines),
288-
offsets.build(),
289-
lengths.build(),
290-
aosp,
291-
version,
292-
help,
293-
stdin,
294-
fixImportsOnly,
295-
sortImports,
296-
removeUnusedImports,
297-
dryRun,
298-
setExitIfChanged,
299-
assumeFilename,
300-
reflowLongStrings,
301-
formatJavadoc);
302-
}
130+
Builder formatJavadoc(boolean formatJavadoc);
131+
132+
CommandLineOptions build();
303133
}
304134
}

0 commit comments

Comments
 (0)