diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 00000000..daec3189 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: "maven" + directory: "/" + schedule: + interval: "daily" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7d3dbc75..016b8e77 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,7 +29,7 @@ jobs: fail-fast: false matrix: os: [ ubuntu-latest ] - java: [ 17, 14, 11 ] + java: [ 17, 11 ] experimental: [ false ] include: # Only test on macos and windows with a single recent JDK to avoid a @@ -47,7 +47,7 @@ jobs: continue-on-error: ${{ matrix.experimental }} steps: - name: Cancel previous - uses: styfle/cancel-workflow-action@0.8.0 + uses: styfle/cancel-workflow-action@0.9.1 with: access_token: ${{ github.token }} - name: 'Check out repository' @@ -73,10 +73,10 @@ jobs: steps: - name: 'Check out repository' uses: actions/checkout@v2 - - name: 'Set up JDK 15' + - name: 'Set up JDK 17' uses: actions/setup-java@v2 with: - java-version: 15 + java-version: 17 distribution: 'zulu' cache: 'maven' server-id: sonatype-nexus-snapshots @@ -86,4 +86,4 @@ jobs: env: CI_DEPLOY_USERNAME: ${{ secrets.CI_DEPLOY_USERNAME }} CI_DEPLOY_PASSWORD: ${{ secrets.CI_DEPLOY_PASSWORD }} - run: mvn source:jar deploy -B -DskipTests=true -Dinvoker.skip=true -Dmaven.javadoc.skip=true + run: mvn -pl '!eclipse_plugin' source:jar deploy -B -DskipTests=true -Dinvoker.skip=true -Dmaven.javadoc.skip=true diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000..a3f066df --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,74 @@ +name: Release google-java-format + +on: + workflow_dispatch: + inputs: + version: + description: "version number for this release." + required: true + +jobs: + build-maven-jars: + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Setup Signing Key + run: | + gpg-agent --daemon --default-cache-ttl 7200 + echo -e "${{ secrets.GPG_SIGNING_KEY }}" | gpg --batch --import --no-tty + echo "hello world" > temp.txt + gpg --detach-sig --yes -v --output=/dev/null --pinentry-mode loopback --passphrase "${{ secrets.GPG_PASSPHRASE }}" temp.txt + rm temp.txt + gpg --list-secret-keys --keyid-format LONG + + - name: Checkout + uses: actions/checkout@v2.4.0 + + - name: Set up JDK + uses: actions/setup-java@v2.5.0 + with: + java-version: 17 + distribution: 'zulu' + cache: 'maven' + server-id: sonatype-nexus-staging + server-username: CI_DEPLOY_USERNAME + server-password: CI_DEPLOY_PASSWORD + + - name: Bump Version Number + run: | + mvn --no-transfer-progress versions:set versions:commit -DnewVersion="${{ github.event.inputs.version }}" + mvn tycho-versions:update-eclipse-metadata -pl eclipse_plugin + git ls-files | grep 'pom.xml$' | xargs git add + git config --global user.email "${{ github.actor }}@users.noreply.github.com" + git config --global user.name "${{ github.actor }}" + git commit -m "Release google-java-format ${{ github.event.inputs.version }}" + git tag "v${{ github.event.inputs.version }}" + echo "TARGET_COMMITISH=$(git rev-parse HEAD)" >> $GITHUB_ENV + git remote set-url origin https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/google/google-java-format.git + + - name: Deploy to Sonatype staging + env: + CI_DEPLOY_USERNAME: ${{ secrets.CI_DEPLOY_USERNAME }} + CI_DEPLOY_PASSWORD: ${{ secrets.CI_DEPLOY_PASSWORD }} + run: + mvn --no-transfer-progress -pl '!eclipse_plugin' -P sonatype-oss-release clean deploy -Dgpg.passphrase="${{ secrets.GPG_PASSPHRASE }}" + + - name: Build Eclipse plugin + run: + mvn --no-transfer-progress -pl 'eclipse_plugin' verify gpg:sign -DskipTests=true -Dgpg.passphrase="${{ secrets.GPG_PASSPHRASE }}" + + - name: Push tag + run: | + git push origin "v${{ github.event.inputs.version }}" + + - name: Add Jars to Release Entry + uses: softprops/action-gh-release@v0.1.14 + with: + draft: true + name: ${{ github.event.input.version }} + tag_name: "v${{ github.event.inputs.version }}" + target_commitish: ${{ env.TARGET_COMMITISH }} + files: | + core/target/google-java-format-*.jar + eclipse_plugin/target/google-java-format-eclipse-plugin-*.jar diff --git a/.mvn/jvm.config b/.mvn/jvm.config new file mode 100644 index 00000000..504456f9 --- /dev/null +++ b/.mvn/jvm.config @@ -0,0 +1,10 @@ +--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED +--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED +--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED +--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED +--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED +--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED +--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED +--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED +--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED +--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED diff --git a/README.md b/README.md index 3b79cd0b..5d1d7c7d 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ and run it with: ``` -java -jar /path/to/google-java-format-1.12.0-all-deps.jar [files...] +java -jar /path/to/google-java-format-${GJF_VERSION?}-all-deps.jar [files...] ``` The formatter can act on whole files, on limited lines (`--lines`), on specific @@ -39,7 +39,7 @@ java \ --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \ --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \ --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \ - -jar google-java-format-1.12.0-all-deps.jar [files...] + -jar google-java-format-${GJF_VERSION?}-all-deps.jar [files...] ``` ### IntelliJ, Android Studio, and other JetBrains IDEs @@ -70,9 +70,9 @@ and import it into File→Settings→Editor→Code Style. ### Eclipse -Version 1.11 of the -[google-java-format Eclipse plugin](https://github.com/google/google-java-format/releases/download/v1.12.0/google-java-format-eclipse-plugin-1.12.0.jar) -can be downloaded from the releases page. Drop it into the Eclipse +The latest version of the `google-java-format` Eclipse plugin can be downloaded +from the [releases page](https://github.com/google/google-java-format/releases). +Drop it into the Eclipse [drop-ins folder](http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fmisc%2Fp2_dropins_format.html) to activate the plugin. @@ -113,7 +113,7 @@ configuration. com.google.googlejavaformat google-java-format - 1.12.0 + ${google-java-format.version} ``` @@ -121,7 +121,7 @@ configuration. ```groovy dependencies { - implementation 'com.google.googlejavaformat:google-java-format:1.12.0' + implementation 'com.google.googlejavaformat:google-java-format:$googleJavaFormatVersion' } ``` diff --git a/core/pom.xml b/core/pom.xml index 5ebdb591..fefaeb25 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -22,7 +22,7 @@ com.google.googlejavaformat google-java-format-parent - HEAD-SNAPSHOT + 1.14.0 google-java-format @@ -75,10 +75,15 @@ com.google.truth truth + + com.google.truth.extensions + truth-java8-extension + test + com.google.testing.compile compile-testing - 0.15 + 0.19 test @@ -119,7 +124,7 @@ org.apache.maven.plugins maven-shade-plugin - 2.4.3 + 3.2.4 shade-all-deps @@ -191,7 +196,7 @@ org.codehaus.mojo build-helper-maven-plugin - 3.0.0 + 3.3.0 add-source @@ -244,5 +249,45 @@ + + native + + + + org.graalvm.buildtools + native-maven-plugin + 0.9.9 + true + + + build-native + + build + + package + + + test-native + + test + + test + + + + google-java-format + + ${project.build.directory}/${project.artifactId}-${project.version}-all-deps.jar + + + -H:IncludeResourceBundles=com.sun.tools.javac.resources.compiler + --no-fallback + --initialize-at-build-time=com.sun.tools.javac.file.Locations + + + + + + diff --git a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java index 3e973958..aac829dd 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java +++ b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java @@ -14,8 +14,6 @@ package com.google.googlejavaformat.java; -import static com.google.common.base.StandardSystemProperty.JAVA_CLASS_VERSION; -import static com.google.common.base.StandardSystemProperty.JAVA_SPECIFICATION_VERSION; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.collect.ImmutableList; @@ -42,7 +40,6 @@ import com.sun.tools.javac.util.Options; import java.io.IOError; import java.io.IOException; -import java.lang.reflect.Method; import java.net.URI; import java.util.ArrayList; import java.util.Collection; @@ -154,7 +151,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept OpsBuilder builder = new OpsBuilder(javaInput, javaOutput); // Output the compilation unit. JavaInputAstVisitor visitor; - if (getMajor() >= 14) { + if (Runtime.version().feature() >= 14) { try { visitor = Class.forName("com.google.googlejavaformat.java.java14.Java14InputAstVisitor") @@ -176,23 +173,6 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept javaOutput.flush(); } - // Runtime.Version was added in JDK 9, so use reflection to access it to preserve source - // compatibility with Java 8. - private static int getMajor() { - try { - Method versionMethod = Runtime.class.getMethod("version"); - Object version = versionMethod.invoke(null); - return (int) version.getClass().getMethod("major").invoke(version); - } catch (Exception e) { - // continue below - } - int version = (int) Double.parseDouble(JAVA_CLASS_VERSION.value()); - if (49 <= version && version <= 52) { - return version - (49 - 5); - } - throw new IllegalStateException("Unknown Java version: " + JAVA_SPECIFICATION_VERSION.value()); - } - static boolean errorDiagnostic(Diagnostic input) { if (input.getKind() != Diagnostic.Kind.ERROR) { return false; diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java index e671ad01..165bdebb 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java @@ -311,7 +311,7 @@ private static ImmutableMap makePositionToColumnMap(List for (Tok tok : toks) { builder.put(tok.getPosition(), tok.getColumn()); } - return builder.build(); + return builder.buildOrThrow(); } /** diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java index 8da71956..daed2502 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -2294,7 +2294,7 @@ private ImmutableList visitModifiers( ? forceBreakList(declarationAnnotationBreak) : breakList(declarationAnnotationBreak)); } - formatAnnotationOrModifier(declarationModifiers.removeFirst()); + formatAnnotationOrModifier(declarationModifiers); first = false; lastWasAnnotation = true; } @@ -2317,7 +2317,7 @@ private ImmutableList visitModifiers( if (!first) { builder.addAll(breakFillList(Optional.empty())); } - formatAnnotationOrModifier(declarationModifiers.removeFirst()); + formatAnnotationOrModifier(declarationModifiers); first = false; } builder.close(); @@ -2452,10 +2452,15 @@ DeclarationModifiersAndTypeAnnotations splitModifiers( modifiers.subList(0, idx + 1), typeAnnotations.build().reverse()); } - private void formatAnnotationOrModifier(AnnotationOrModifier modifier) { + private void formatAnnotationOrModifier(Deque modifiers) { + AnnotationOrModifier modifier = modifiers.removeFirst(); switch (modifier.getKind()) { case MODIFIER: token(modifier.modifier().getText()); + if (modifier.modifier().getText().equals("non")) { + token(modifiers.removeFirst().modifier().getText()); + token(modifiers.removeFirst().modifier().getText()); + } break; case ANNOTATION: scan(modifier.annotation(), null); @@ -2471,10 +2476,6 @@ boolean isTypeAnnotation(AnnotationTree annotationTree) { return typeAnnotationSimpleNames.contains(((IdentifierTree) annotationType).getName()); } - boolean nextIsModifier() { - return isModifier(builder.peekToken().get()); - } - private static boolean isModifier(String token) { switch (token) { case "public": @@ -2490,7 +2491,8 @@ private static boolean isModifier(String token) { case "strictfp": case "default": case "sealed": - case "non-sealed": + case "non": + case "-": return true; default: return false; diff --git a/core/src/main/java/com/google/googlejavaformat/java/TypeNameClassifier.java b/core/src/main/java/com/google/googlejavaformat/java/TypeNameClassifier.java index 4e871a67..21fae5f0 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/TypeNameClassifier.java +++ b/core/src/main/java/com/google/googlejavaformat/java/TypeNameClassifier.java @@ -164,7 +164,7 @@ static JavaCaseFormat from(String name) { hasLowercase |= Character.isLowerCase(c); } if (firstUppercase) { - return hasLowercase ? UPPER_CAMEL : UPPERCASE; + return (hasLowercase || name.length() == 1) ? UPPER_CAMEL : UPPERCASE; } else { return hasUppercase ? LOWER_CAMEL : LOWERCASE; } diff --git a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocFormatter.java b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocFormatter.java index 5addc677..03938a67 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocFormatter.java +++ b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocFormatter.java @@ -166,15 +166,30 @@ private static Token standardize(Token token, Token standardToken) { * fits on one line. */ private static String makeSingleLineIfPossible(int blockIndent, String input) { - int oneLinerContentLength = MAX_LINE_LENGTH - "/** */".length() - blockIndent; Matcher matcher = ONE_CONTENT_LINE_PATTERN.matcher(input); - if (matcher.matches() && matcher.group(1).isEmpty()) { - return "/** */"; - } else if (matcher.matches() && matcher.group(1).length() <= oneLinerContentLength) { - return "/** " + matcher.group(1) + " */"; + if (matcher.matches()) { + String line = matcher.group(1); + if (line.isEmpty()) { + return "/** */"; + } else if (oneLineJavadoc(line, blockIndent)) { + return "/** " + line + " */"; + } } return input; } + private static boolean oneLineJavadoc(String line, int blockIndent) { + int oneLinerContentLength = MAX_LINE_LENGTH - "/** */".length() - blockIndent; + if (line.length() > oneLinerContentLength) { + return false; + } + // If the javadoc contains only a tag, use multiple lines to encourage writing a summary + // fragment, unless it's /* @hide */. + if (line.startsWith("@") && !line.equals("@hide")) { + return false; + } + return true; + } + private JavadocFormatter() {} } diff --git a/core/src/main/resources/META-INF/native-image/reflect-config.json b/core/src/main/resources/META-INF/native-image/reflect-config.json new file mode 100644 index 00000000..2c658034 --- /dev/null +++ b/core/src/main/resources/META-INF/native-image/reflect-config.json @@ -0,0 +1,6 @@ +[ + { + "name": "com.sun.tools.javac.parser.UnicodeReader", + "allDeclaredMethods": true + } +] diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java index 22202e98..61a43468 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java @@ -51,7 +51,7 @@ public class FormatterIntegrationTest { .putAll(14, "I477", "Records", "RSLs", "Var", "ExpressionSwitch", "I574", "I594") .putAll(15, "I603") .putAll(16, "I588") - .putAll(17, "I683", "I684") + .putAll(17, "I683", "I684", "I696") .build(); @Parameters(name = "{index}: {0}") diff --git a/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java b/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java index f5103d9b..6849c01f 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java @@ -937,7 +937,9 @@ public void onlyParams() { "class Test {}", }; String[] expected = { - "/** @param this is a param */", // + "/**", // + " * @param this is a param", + " */", "class Test {}", }; doFormatTest(input, expected); @@ -1415,4 +1417,33 @@ public void u2028LineSeparator() { }; doFormatTest(input, expected); } + + @Test + public void missingSummaryFragment() { + String[] input = { + "public class Foo {", + " /**", + " * @return something.", + " */", + " public void setSomething() {}", + "", + " /**", + " * @hide", + " */", + " public void setSomething() {}", + "}", + }; + String[] expected = { + "public class Foo {", + " /**", + " * @return something.", + " */", + " public void setSomething() {}", + "", + " /** @hide */", + " public void setSomething() {}", + "}", + }; + doFormatTest(input, expected); + } } diff --git a/core/src/test/java/com/google/googlejavaformat/java/TypeNameClassifierTest.java b/core/src/test/java/com/google/googlejavaformat/java/TypeNameClassifierTest.java index 9d1e00a1..3270bc64 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/TypeNameClassifierTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/TypeNameClassifierTest.java @@ -43,6 +43,7 @@ public void caseFormat() throws Exception { assertThat(JavaCaseFormat.from("a_$")).isEqualTo(JavaCaseFormat.LOWERCASE); assertThat(JavaCaseFormat.from("_")).isEqualTo(JavaCaseFormat.LOWERCASE); assertThat(JavaCaseFormat.from("_A")).isEqualTo(JavaCaseFormat.UPPERCASE); + assertThat(JavaCaseFormat.from("A")).isEqualTo(JavaCaseFormat.UPPER_CAMEL); } private static Optional getPrefix(String qualifiedName) { @@ -62,6 +63,7 @@ public void typePrefixLength() { assertThat(getPrefix("ClassName.CONST")).hasValue(1); assertThat(getPrefix("ClassName.varName")).hasValue(1); assertThat(getPrefix("ClassName.Inner.varName")).hasValue(2); + assertThat(getPrefix("com.R.foo")).hasValue(2); } @Test diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I696.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I696.input new file mode 100644 index 00000000..156e6efc --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I696.input @@ -0,0 +1,11 @@ +public abstract non-sealed class A extends SealedClass { +} + +non-sealed class B extends SealedClass { +} + +non-sealed @A class B extends SealedClass { +} + +@A non-sealed class B extends SealedClass { +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I696.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I696.output new file mode 100644 index 00000000..14721c37 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I696.output @@ -0,0 +1,8 @@ +public abstract non-sealed class A extends SealedClass {} + +non-sealed class B extends SealedClass {} + +non-sealed @A class B extends SealedClass {} + +@A +non-sealed class B extends SealedClass {} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/b26306390.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/b26306390.input new file mode 100644 index 00000000..da6c01b0 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/b26306390.input @@ -0,0 +1,3 @@ +class B26306390 { + int resourceId = com.some.extremely.verbose.pkg.name.R.string.some_extremely_long_resource_identifier_that_exceeds_the_column_limit; +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/b26306390.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/b26306390.output new file mode 100644 index 00000000..a21772f8 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/b26306390.output @@ -0,0 +1,5 @@ +class B26306390 { + int resourceId = + com.some.extremely.verbose.pkg.name.R.string + .some_extremely_long_resource_identifier_that_exceeds_the_column_limit; +} diff --git a/eclipse_plugin/META-INF/MANIFEST.MF b/eclipse_plugin/META-INF/MANIFEST.MF index 34b1e8c8..91324539 100644 --- a/eclipse_plugin/META-INF/MANIFEST.MF +++ b/eclipse_plugin/META-INF/MANIFEST.MF @@ -3,7 +3,7 @@ Bundle-ManifestVersion: 2 Bundle-Name: google-java-format Bundle-SymbolicName: google-java-format-eclipse-plugin;singleton:=true Bundle-Vendor: Google -Bundle-Version: 1.11.0 +Bundle-Version: 1.13.0 Bundle-RequiredExecutionEnvironment: JavaSE-11 Require-Bundle: org.eclipse.jdt.core;bundle-version="3.10.0", org.eclipse.jface, diff --git a/eclipse_plugin/pom.xml b/eclipse_plugin/pom.xml index 6b49c901..534e685f 100644 --- a/eclipse_plugin/pom.xml +++ b/eclipse_plugin/pom.xml @@ -22,7 +22,7 @@ com.google.googlejavaformat google-java-format-eclipse-plugin eclipse-plugin - 1.11.0 + 1.13.0 Google Java Format Plugin for Eclipse 4.5+ @@ -32,7 +32,7 @@ UTF-8 - 1.7.0 + 2.6.0 @@ -56,7 +56,7 @@ org.apache.maven.plugins maven-dependency-plugin - 3.1.2 + 3.2.0 copy-dependencies diff --git a/idea_plugin/.gitignore b/idea_plugin/.gitignore new file mode 100644 index 00000000..16bc65a5 --- /dev/null +++ b/idea_plugin/.gitignore @@ -0,0 +1,5 @@ +build +.gradle +gradle +gradlew +gradlew.bat \ No newline at end of file diff --git a/idea_plugin/build.gradle b/idea_plugin/build.gradle index 3d807af0..d9f769d4 100644 --- a/idea_plugin/build.gradle +++ b/idea_plugin/build.gradle @@ -15,7 +15,7 @@ */ plugins { - id "org.jetbrains.intellij" version "0.7.2" + id "org.jetbrains.intellij" version "1.3.1" } repositories { @@ -23,23 +23,27 @@ repositories { } ext { - googleJavaFormatVersion = '1.10.0' + googleJavaFormatVersion = "1.13.0" } -apply plugin: 'org.jetbrains.intellij' -apply plugin: 'java' +apply plugin: "org.jetbrains.intellij" +apply plugin: "java" + +sourceCompatibility = JavaVersion.VERSION_11 +targetCompatibility = JavaVersion.VERSION_11 intellij { pluginName = "google-java-format" - version = "211.6693.65-EAP-SNAPSHOT" + plugins = ["java"] + version = "221.3427-EAP-CANDIDATE-SNAPSHOT" } patchPluginXml { pluginDescription = "Formats source code using the google-java-format tool. This version of " + "the plugin uses version ${googleJavaFormatVersion} of the tool." - version = "${googleJavaFormatVersion}.0" - sinceBuild = '201' - untilBuild = '' + version.set("${googleJavaFormatVersion}.0") + sinceBuild = "203" + untilBuild = "" } publishPlugin { @@ -48,11 +52,11 @@ publishPlugin { sourceSets { main { - java.srcDir 'src' - resources.srcDir 'resources' + java.srcDir "src" + resources.srcDir "resources" } } dependencies { - compile "com.google.googlejavaformat:google-java-format:${googleJavaFormatVersion}" + implementation "com.google.googlejavaformat:google-java-format:${googleJavaFormatVersion}" } diff --git a/idea_plugin/resources/META-INF/plugin.xml b/idea_plugin/resources/META-INF/plugin.xml index b089ad60..f10cde5a 100644 --- a/idea_plugin/resources/META-INF/plugin.xml +++ b/idea_plugin/resources/META-INF/plugin.xml @@ -14,7 +14,8 @@ limitations under the License. --> - + google-java-format google-java-format @@ -24,10 +25,16 @@ - com.intellij.modules.lang + com.intellij.java +
1.13.0.0
+
Updated to use google-java-format 1.13.
+
1.12.0.0
+
Updated to use google-java-format 1.12.
+
1.11.0.0
+
Updated to use google-java-format 1.11.
1.10.0.0
Updated to use google-java-format 1.10.
1.9.0.0
@@ -52,17 +59,22 @@ ]]>
- + + topic="com.intellij.openapi.project.ProjectManagerListener"/> - - + + +
diff --git a/idea_plugin/src/com/google/googlejavaformat/intellij/CodeStyleManagerDecorator.java b/idea_plugin/src/com/google/googlejavaformat/intellij/CodeStyleManagerDecorator.java index fc335ad6..af5da957 100644 --- a/idea_plugin/src/com/google/googlejavaformat/intellij/CodeStyleManagerDecorator.java +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/CodeStyleManagerDecorator.java @@ -34,10 +34,11 @@ import com.intellij.util.ThrowableRunnable; import java.util.Collection; import org.checkerframework.checker.nullness.qual.Nullable; +import org.jetbrains.annotations.NotNull; /** * Decorates the {@link CodeStyleManager} abstract class by delegating to a concrete implementation - * instance (likely IJ's default instance). + * instance (likely IntelliJ's default instance). */ @SuppressWarnings("deprecation") class CodeStyleManagerDecorator extends CodeStyleManager @@ -54,98 +55,102 @@ CodeStyleManager getDelegate() { } @Override - public Project getProject() { + public @NotNull Project getProject() { return delegate.getProject(); } @Override - public PsiElement reformat(PsiElement element) throws IncorrectOperationException { + public @NotNull PsiElement reformat(@NotNull PsiElement element) + throws IncorrectOperationException { return delegate.reformat(element); } @Override - public PsiElement reformat(PsiElement element, boolean canChangeWhiteSpacesOnly) + public @NotNull PsiElement reformat(@NotNull PsiElement element, boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException { return delegate.reformat(element, canChangeWhiteSpacesOnly); } @Override - public PsiElement reformatRange(PsiElement element, int startOffset, int endOffset) + public PsiElement reformatRange(@NotNull PsiElement element, int startOffset, int endOffset) throws IncorrectOperationException { return delegate.reformatRange(element, startOffset, endOffset); } @Override public PsiElement reformatRange( - PsiElement element, int startOffset, int endOffset, boolean canChangeWhiteSpacesOnly) + @NotNull PsiElement element, int startOffset, int endOffset, boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException { return delegate.reformatRange(element, startOffset, endOffset, canChangeWhiteSpacesOnly); } @Override - public void reformatText(PsiFile file, int startOffset, int endOffset) + public void reformatText(@NotNull PsiFile file, int startOffset, int endOffset) throws IncorrectOperationException { delegate.reformatText(file, startOffset, endOffset); } @Override - public void reformatText(PsiFile file, Collection ranges) + public void reformatText(@NotNull PsiFile file, @NotNull Collection ranges) throws IncorrectOperationException { delegate.reformatText(file, ranges); } @Override - public void reformatTextWithContext(PsiFile psiFile, ChangedRangesInfo changedRangesInfo) + public void reformatTextWithContext( + @NotNull PsiFile psiFile, @NotNull ChangedRangesInfo changedRangesInfo) throws IncorrectOperationException { delegate.reformatTextWithContext(psiFile, changedRangesInfo); } @Override - public void reformatTextWithContext(PsiFile file, Collection ranges) + public void reformatTextWithContext( + @NotNull PsiFile file, @NotNull Collection ranges) throws IncorrectOperationException { delegate.reformatTextWithContext(file, ranges); } @Override - public void adjustLineIndent(PsiFile file, TextRange rangeToAdjust) + public void adjustLineIndent(@NotNull PsiFile file, TextRange rangeToAdjust) throws IncorrectOperationException { delegate.adjustLineIndent(file, rangeToAdjust); } @Override - public int adjustLineIndent(PsiFile file, int offset) throws IncorrectOperationException { + public int adjustLineIndent(@NotNull PsiFile file, int offset) + throws IncorrectOperationException { return delegate.adjustLineIndent(file, offset); } @Override - public int adjustLineIndent(Document document, int offset) { + public int adjustLineIndent(@NotNull Document document, int offset) { return delegate.adjustLineIndent(document, offset); } - public void scheduleIndentAdjustment(Document document, int offset) { + public void scheduleIndentAdjustment(@NotNull Document document, int offset) { delegate.scheduleIndentAdjustment(document, offset); } @Override - public boolean isLineToBeIndented(PsiFile file, int offset) { + public boolean isLineToBeIndented(@NotNull PsiFile file, int offset) { return delegate.isLineToBeIndented(file, offset); } @Override @Nullable - public String getLineIndent(PsiFile file, int offset) { + public String getLineIndent(@NotNull PsiFile file, int offset) { return delegate.getLineIndent(file, offset); } @Override @Nullable - public String getLineIndent(PsiFile file, int offset, FormattingMode mode) { + public String getLineIndent(@NotNull PsiFile file, int offset, FormattingMode mode) { return delegate.getLineIndent(file, offset, mode); } @Override @Nullable - public String getLineIndent(Document document, int offset) { + public String getLineIndent(@NotNull Document document, int offset) { return delegate.getLineIndent(document, offset); } @@ -165,7 +170,7 @@ public Indent zeroIndent() { } @Override - public void reformatNewlyAddedElement(ASTNode block, ASTNode addedElement) + public void reformatNewlyAddedElement(@NotNull ASTNode block, @NotNull ASTNode addedElement) throws IncorrectOperationException { delegate.reformatNewlyAddedElement(block, addedElement); } @@ -192,22 +197,23 @@ public T performActionWithFormatterDisabled(Computable r) { } @Override - public int getSpacing(PsiFile file, int offset) { + public int getSpacing(@NotNull PsiFile file, int offset) { return delegate.getSpacing(file, offset); } @Override - public int getMinLineFeeds(PsiFile file, int offset) { + public int getMinLineFeeds(@NotNull PsiFile file, int offset) { return delegate.getMinLineFeeds(file, offset); } @Override - public void runWithDocCommentFormattingDisabled(PsiFile file, Runnable runnable) { + public void runWithDocCommentFormattingDisabled( + @NotNull PsiFile file, @NotNull Runnable runnable) { delegate.runWithDocCommentFormattingDisabled(file, runnable); } @Override - public DocCommentSettings getDocCommentSettings(PsiFile file) { + public @NotNull DocCommentSettings getDocCommentSettings(@NotNull PsiFile file) { return delegate.getDocCommentSettings(file); } @@ -223,7 +229,8 @@ public FormattingMode getCurrentFormattingMode() { } @Override - public int adjustLineIndent(final Document document, final int offset, FormattingMode mode) + public int adjustLineIndent( + final @NotNull Document document, final int offset, FormattingMode mode) throws IncorrectOperationException { if (delegate instanceof FormattingModeAwareIndentAdjuster) { return ((FormattingModeAwareIndentAdjuster) delegate) @@ -233,7 +240,7 @@ public int adjustLineIndent(final Document document, final int offset, Formattin } @Override - public void scheduleReformatWhenSettingsComputed(PsiFile file) { + public void scheduleReformatWhenSettingsComputed(@NotNull PsiFile file) { delegate.scheduleReformatWhenSettingsComputed(file); } } diff --git a/idea_plugin/src/com/google/googlejavaformat/intellij/FormatterUtil.java b/idea_plugin/src/com/google/googlejavaformat/intellij/FormatterUtil.java index 9939bbad..a5e69c93 100644 --- a/idea_plugin/src/com/google/googlejavaformat/intellij/FormatterUtil.java +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/FormatterUtil.java @@ -50,8 +50,7 @@ static Map getReplacements( } private static Collection> toRanges(Collection textRanges) { - return textRanges - .stream() + return textRanges.stream() .map(textRange -> Range.closedOpen(textRange.getStartOffset(), textRange.getEndOffset())) .collect(Collectors.toList()); } diff --git a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java index 550d0a93..3d56743e 100644 --- a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java @@ -22,10 +22,10 @@ import com.google.googlejavaformat.java.Formatter; import com.google.googlejavaformat.java.JavaFormatterOptions; import com.google.googlejavaformat.java.JavaFormatterOptions.Style; +import com.intellij.ide.highlighter.JavaFileType; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.editor.Document; -import com.intellij.openapi.fileTypes.StdFileTypes; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; @@ -44,7 +44,7 @@ /** * A {@link CodeStyleManager} implementation which formats .java files with google-java-format. - * Formatting of all other types of files is delegated to IJ's default implementation. + * Formatting of all other types of files is delegated to IntelliJ's default implementation. */ class GoogleJavaFormatCodeStyleManager extends CodeStyleManagerDecorator { @@ -53,7 +53,7 @@ public GoogleJavaFormatCodeStyleManager(@NotNull CodeStyleManager original) { } @Override - public void reformatText(PsiFile file, int startOffset, int endOffset) + public void reformatText(@NotNull PsiFile file, int startOffset, int endOffset) throws IncorrectOperationException { if (overrideFormatterForFile(file)) { formatInternal(file, ImmutableList.of(new TextRange(startOffset, endOffset))); @@ -63,7 +63,7 @@ public void reformatText(PsiFile file, int startOffset, int endOffset) } @Override - public void reformatText(PsiFile file, Collection ranges) + public void reformatText(@NotNull PsiFile file, @NotNull Collection ranges) throws IncorrectOperationException { if (overrideFormatterForFile(file)) { formatInternal(file, ranges); @@ -73,7 +73,7 @@ public void reformatText(PsiFile file, Collection ranges) } @Override - public void reformatTextWithContext(PsiFile file, ChangedRangesInfo info) + public void reformatTextWithContext(@NotNull PsiFile file, @NotNull ChangedRangesInfo info) throws IncorrectOperationException { List ranges = new ArrayList<>(); if (info.insertedRanges != null) { @@ -84,7 +84,8 @@ public void reformatTextWithContext(PsiFile file, ChangedRangesInfo info) } @Override - public void reformatTextWithContext(PsiFile file, Collection ranges) { + public void reformatTextWithContext( + @NotNull PsiFile file, @NotNull Collection ranges) { if (overrideFormatterForFile(file)) { formatInternal(file, ranges); } else { @@ -94,7 +95,10 @@ public void reformatTextWithContext(PsiFile file, Collection ranges * Format the ranges of the given document. * *

Overriding methods will need to modify the document with the result of the external - * formatter (usually using {@link #performReplacements(Document, Map)}. + * formatter (usually using {@link #performReplacements(Document, Map)}). */ private void format(Document document, Collection ranges) { Style style = GoogleJavaFormatSettings.getInstance(getProject()).getStyle(); diff --git a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java index f6d9b5ff..1e92a4bd 100644 --- a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java @@ -23,6 +23,7 @@ import com.intellij.openapi.components.Storage; import com.intellij.openapi.project.Project; import org.checkerframework.checker.nullness.qual.Nullable; +import org.jetbrains.annotations.NotNull; @State( name = "GoogleJavaFormatSettings", @@ -42,7 +43,7 @@ public State getState() { } @Override - public void loadState(State state) { + public void loadState(@NotNull State state) { this.state = state; } @@ -73,7 +74,7 @@ void setStyle(JavaFormatterOptions.Style style) { enum EnabledState { UNKNOWN, ENABLED, - DISABLED; + DISABLED } static class State { @@ -85,7 +86,7 @@ static class State { public void setEnabled(@Nullable String enabledStr) { if (enabledStr == null) { enabled = EnabledState.UNKNOWN; - } else if (Boolean.valueOf(enabledStr)) { + } else if (Boolean.parseBoolean(enabledStr)) { enabled = EnabledState.ENABLED; } else { enabled = EnabledState.DISABLED; diff --git a/idea_plugin/src/com/google/googlejavaformat/intellij/InitialConfigurationProjectManagerListener.java b/idea_plugin/src/com/google/googlejavaformat/intellij/InitialConfigurationProjectManagerListener.java index da02310c..1906347f 100644 --- a/idea_plugin/src/com/google/googlejavaformat/intellij/InitialConfigurationProjectManagerListener.java +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/InitialConfigurationProjectManagerListener.java @@ -17,8 +17,8 @@ package com.google.googlejavaformat.intellij; import com.intellij.notification.Notification; -import com.intellij.notification.NotificationDisplayType; import com.intellij.notification.NotificationGroup; +import com.intellij.notification.NotificationGroupManager; import com.intellij.notification.NotificationType; import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectManagerListener; @@ -28,11 +28,10 @@ final class InitialConfigurationProjectManagerListener implements ProjectManager private static final String NOTIFICATION_TITLE = "Enable google-java-format"; private static final NotificationGroup NOTIFICATION_GROUP = - new NotificationGroup(NOTIFICATION_TITLE, NotificationDisplayType.STICKY_BALLOON, true); + NotificationGroupManager.getInstance().getNotificationGroup(NOTIFICATION_TITLE); @Override public void projectOpened(@NotNull Project project) { - GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project); if (settings.isUninitialized()) { diff --git a/pom.xml b/pom.xml index 75a18293..3a3e76b9 100644 --- a/pom.xml +++ b/pom.xml @@ -23,14 +23,11 @@ com.google.googlejavaformat google-java-format-parent pom - HEAD-SNAPSHOT + 1.14.0 core - Google Java Format Parent @@ -90,13 +87,13 @@ UTF-8 1.8 - 30.1.1-jre - 1.0 - 3.6.1 - 2.7.1 - 1.8.2 - 1.0 - 3.1.0 + 31.0.1-jre + 1.1.3 + 3.21.2 + 2.11.0 + 1.9 + 1.0.1 + 3.3.1 3.2.1 @@ -135,7 +132,7 @@ junit junit - 4.13.1 + 4.13.2 test @@ -150,6 +147,12 @@ ${truth.version} test + + com.google.truth.extensions + truth-java8-extension + ${truth.version} + test + @@ -158,28 +161,28 @@ maven-compiler-plugin - 3.7.0 + 3.9.0 maven-jar-plugin - 3.0.2 + 3.2.2 maven-source-plugin - 2.1.2 + 3.2.1 maven-javadoc-plugin - 3.2.0 + 3.3.1 maven-gpg-plugin - 1.4 + 3.0.1 org.apache.felix maven-bundle-plugin - 2.4.0 + 5.1.4 @@ -191,19 +194,7 @@ ${java.version} ${java.version} UTF-8 - true - - -J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED - -J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED - -J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED - -J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED - -J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED - -J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED - -J--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED - -J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED - -J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED - -J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED -XDcompilePolicy=simple -Xplugin:ErrorProne @@ -260,7 +251,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 3.2.0 + 3.3.1 none @@ -276,7 +267,7 @@ org.apache.maven.plugins maven-surefire-plugin - 2.18 + 2.22.2 @@ -344,7 +335,7 @@ org.apache.maven.plugins maven-gpg-plugin - 1.6 + 3.0.1 sign-artifacts