Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8afb866
Create a build rule for compiling deps at the Java STS language level
cushon Nov 21, 2022
d9ddb94
Recognize new JSpecify package name
Stephan202 Dec 15, 2022
d24deb9
Recognize a couple `@NonNull` annotations as type-use annotations.
cpovirk Jan 6, 2023
90f9e5a
Delete dependabot.yml
cushon Jan 14, 2023
4a22aab
Return a non-zero exit code upon error
abstractlyZach Jan 16, 2023
91223aa
Fix #846 Formatter leaks threads and memory
stiemannkj1 Feb 4, 2023
0a6a85e
Refactor formatter's main to use `ExecutorCompletionService`
cushon Feb 14, 2023
8c85ac1
Follow-up to https://github.com/google/google-java-format/commit/0a6a…
cushon Feb 14, 2023
973cbff
Allow modifier reordering to be disabled
cushon Feb 24, 2023
eb7edaf
Update release.yml
cushon Feb 24, 2023
64f98ed
Update ci.yml
cushon Feb 27, 2023
a7583fb
Prepare for a change to `JCTree.getQualifiedIdentifier `
cushon Feb 28, 2023
9412083
Use a standard gradle project structure.
plumpy Feb 28, 2023
84b2c9a
Move the plugin to the FormattingService API.
plumpy Mar 2, 2023
0644d4c
Make the `-add-exports=` passed to g-j-f consistent
cushon Mar 2, 2023
32a6c00
Switch (deprecated) ProjectManagerListener#onProjectOpened to Startup…
plumpy Mar 3, 2023
4f5ffd0
Link to instructions for properly configuring the IDE's JRE.
plumpy Mar 3, 2023
1651dea
Convert the gradle config to kotlin.
plumpy Mar 3, 2023
20527aa
Update the `--add-exports` instructions.
plumpy Mar 6, 2023
1a00656
Allow blank lines inserted before comments.
nreid260 Mar 7, 2023
4c1aeff
Make OpsBuilder::add public final
nreid260 Mar 10, 2023
401d5c9
Be louder about telling users they need to modify their IDE config to…
plumpy Mar 10, 2023
25ce685
Remove AD_HOC_FORMATTING from the IntelliJ plugin.
plumpy Apr 4, 2023
0aa0d85
Merge remote-tracking branch 'upstream/master' into rebase-google-v1-…
casper-la-cour-sg Apr 18, 2023
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
6 changes: 0 additions & 6 deletions .github/dependabot.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ jobs:
- os: ubuntu-latest
java: 20-ea
experimental: true
- os: ubuntu-latest
java: 21-ea
experimental: true
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.experimental }}
steps:
Expand Down
5 changes: 2 additions & 3 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,12 @@
<link>https://docs.oracle.com/en/java/javase/11/docs/api</link>
</links>
<additionalJOptions>
<additionalJOption>--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED,com.google.googlejavaformat</additionalJOption>
<additionalJOption>--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED,com.google.googlejavaformat</additionalJOption>
<additionalJOption>--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED,com.google.googlejavaformat</additionalJOption>
<additionalJOption>--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED,com.google.googlejavaformat</additionalJOption>
<additionalJOption>--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED,com.google.googlejavaformat</additionalJOption>
<additionalJOption>--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED,com.google.googlejavaformat</additionalJOption>
<additionalJOption>--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED,com.google.googlejavaformat</additionalJOption>
<additionalJOption>--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED,com.google.googlejavaformat</additionalJOption>
<additionalJOption>--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED,com.google.googlejavaformat</additionalJOption>
</additionalJOptions>
</configuration>
<executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public BlankLineWanted merge(BlankLineWanted other) {
int depth = 0;

/** Add an {@link Op}, and record open/close ops for later validation of unclosed levels. */
private void add(Op op) {
public final void add(Op op) {
if (op instanceof OpenOp) {
depth++;
} else if (op instanceof CloseOp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,73 @@

package com.google.googlejavaformat.java;

import com.google.auto.value.AutoValue;
import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeSet;
import java.nio.file.Path;
import java.util.concurrent.Callable;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Encapsulates information about a file to be formatted, including which parts of the file to
* format.
*/
class FormatFileCallable implements Callable<String> {
class FormatFileCallable implements Callable<FormatFileCallable.Result> {

@AutoValue
abstract static class Result {
abstract @Nullable Path path();

abstract String input();

abstract @Nullable String output();

boolean changed() {
return !input().equals(output());
}

abstract @Nullable FormatterException exception();

static Result create(
@Nullable Path path,
String input,
@Nullable String output,
@Nullable FormatterException exception) {
return new AutoValue_FormatFileCallable_Result(path, input, output, exception);
}
}

private final Path path;
private final String input;
private final CommandLineOptions parameters;
private final JavaFormatterOptions options;

public FormatFileCallable(
CommandLineOptions parameters, String input, JavaFormatterOptions options) {
CommandLineOptions parameters, Path path, String input, JavaFormatterOptions options) {
this.path = path;
this.input = input;
this.parameters = parameters;
this.options = options;
}

@Override
public String call() throws FormatterException {
if (parameters.fixImportsOnly()) {
return fixImports(input);
}
public Result call() {
try {
if (parameters.fixImportsOnly()) {
return Result.create(path, input, fixImports(input), /* exception= */ null);
}

Formatter formatter = new Formatter(options);
String formatted = formatter.formatSource(input, characterRanges(input).asRanges());
formatted = fixImports(formatted);
if (parameters.reflowLongStrings()) {
formatted = StringWrapper.wrap(options.maxLineLength(), formatted, formatter);
Formatter formatter = new Formatter(options);
String formatted = formatter.formatSource(input, characterRanges(input).asRanges());
formatted = fixImports(formatted);
if (parameters.reflowLongStrings()) {
formatted = StringWrapper.wrap(options.style().maxLineLength(), formatted, formatter);
}
return Result.create(path, input, formatted, /* exception= */ null);
} catch (FormatterException e) {
return Result.create(path, input, /* output= */ null, e);
}
return formatted;
}

private String fixImports(String input) throws FormatterException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
builder.drain();
Doc doc = new DocBuilder().withOps(builder.build()).build();
doc.computeBreaks(
javaOutput.getCommentsHelper(), options.maxLineLength(), new Doc.State(+0, 0));
javaOutput.getCommentsHelper(), options.style().maxLineLength(), new Doc.State(+0, 0));
doc.write(javaOutput);
javaOutput.flush();
}
Expand Down Expand Up @@ -263,7 +263,9 @@ public ImmutableList<Replacement> getFormatReplacements(
// TODO(cushon): this is only safe because the modifier ordering doesn't affect whitespace,
// and doesn't change the replacements that are output. This is not true in general for
// 'de-linting' changes (e.g. import ordering).
javaInput = ModifierOrderer.reorderModifiers(javaInput, characterRanges);
if (options.reorderModifiers()) {
javaInput = ModifierOrderer.reorderModifiers(javaInput, characterRanges);
}

String lineSeparator = Newlines.guessLineSeparator(input);
JavaOutput javaOutput =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ private List<String> wrapLineComments(List<String> lines, int column0) {
result.add(line);
continue;
}
while (line.length() + column0 > options.maxLineLength()) {
int idx = options.maxLineLength() - column0;
while (line.length() + column0 > options.style().maxLineLength()) {
int idx = options.style().maxLineLength() - column0;
// only break on whitespace characters, and ignore the leading `// `
while (idx >= 2 && !CharMatcher.whitespace().matches(line.charAt(idx))) {
idx--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.googlejavaformat.java;

import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.Immutable;

/**
Expand All @@ -27,7 +28,8 @@
* preferences, and in fact it would work directly against our primary goals.
*/
@Immutable
public class JavaFormatterOptions {
@AutoValue
public abstract class JavaFormatterOptions {

static final int DEFAULT_MAX_LINE_LENGTH = 100;

Expand Down Expand Up @@ -57,7 +59,7 @@ int indentationMultiplier() {
return indentationMultiplier;
}

int maxLineLength() {
public int maxLineLength() {
return maxLineLength;
}

Expand All @@ -66,39 +68,24 @@ boolean alwaysStackFieldAnnotations() {
}
}

private final Style style;
private final boolean formatJavadoc;

private JavaFormatterOptions(Style style, boolean formatJavadoc) {
this.style = style;
this.formatJavadoc = formatJavadoc;
}

/** Returns the maximum formatted width */
public int maxLineLength() {
return style.maxLineLength();
}

/** Returns the multiplier for the unit of indent. */
public int indentationMultiplier() {
return style.indentationMultiplier();
return style().indentationMultiplier();
}

public boolean formatJavadoc() {
return formatJavadoc;
}
public abstract boolean formatJavadoc();

public abstract boolean reorderModifiers();

/** Returns the code style. */
public Style style() {
return style;
}
public abstract Style style();

/**
* Returns true if field annotations should be unconditionally stacked. By default, annotation
* direction is contextual.
*/
public boolean alwaysStackFieldAnnotations() {
return style.alwaysStackFieldAnnotations();
return style().alwaysStackFieldAnnotations();
}

/** Returns the default formatting options. */
Expand All @@ -108,28 +95,22 @@ public static JavaFormatterOptions defaultOptions() {

/** Returns a builder for {@link JavaFormatterOptions}. */
public static Builder builder() {
return new Builder();
return new AutoValue_JavaFormatterOptions.Builder()
.style(Style.GOOGLE)
.formatJavadoc(true)
.reorderModifiers(true);
}

/** A builder for {@link JavaFormatterOptions}. */
public static class Builder {
private Style style = Style.GOOGLE;
private boolean formatJavadoc = true;
@AutoValue.Builder
public abstract static class Builder {

private Builder() {}
public abstract Builder style(Style style);

public Builder style(Style style) {
this.style = style;
return this;
}
public abstract Builder formatJavadoc(boolean formatJavadoc);

public Builder formatJavadoc(boolean formatJavadoc) {
this.formatJavadoc = formatJavadoc;
return this;
}
public abstract Builder reorderModifiers(boolean reorderModifiers);

public JavaFormatterOptions build() {
return new JavaFormatterOptions(style, formatJavadoc);
}
public abstract JavaFormatterOptions build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,10 @@ private static ImmutableSetMultimap<String, String> typeAnnotations() {
ImmutableSetMultimap.Builder<String, String> result = ImmutableSetMultimap.builder();
for (String annotation :
ImmutableList.of(
"org.jspecify.annotations.NonNull",
"org.jspecify.annotations.Nullable",
"org.jspecify.nullness.Nullable",
"org.checkerframework.checker.nullness.qual.NonNull",
"org.checkerframework.checker.nullness.qual.Nullable")) {
String simpleName = annotation.substring(annotation.lastIndexOf('.') + 1);
result.put(simpleName, annotation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void append(String text, Range<Integer> range) {
* there's a blank line here and it's a comment.
*/
BlankLineWanted wanted = blankLines.getOrDefault(lastK, BlankLineWanted.NO);
if (isComment(text) ? sawNewlines : wanted.wanted().orElse(sawNewlines)) {
if ((sawNewlines && isComment(text)) || wanted.wanted().orElse(sawNewlines)) {
++newlinesPending;
}
}
Expand Down
Loading