Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: google/google-java-format
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.16.0
Choose a base ref
...
head repository: google/google-java-format
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.17.0
Choose a head ref
  • 15 commits
  • 34 files changed
  • 4 contributors

Commits on Feb 27, 2023

  1. Update ci.yml

    cushon authored Feb 27, 2023
    Copy the full SHA
    64f98ed View commit details

Commits on Feb 28, 2023

  1. Prepare for a change to JCTree.getQualifiedIdentifier

    openjdk/jdk@a917fb3
    
    Fixes: #898
    PiperOrigin-RevId: 512759236
    cushon authored and google-java-format Team committed Feb 28, 2023
    Copy the full SHA
    a7583fb View commit details
  2. Use a standard gradle project structure.

    I'm going to add tests next, but with this weird file structure I've been using, there's no good place to put them.
    
    The only content changes here are in build.gradle.
    
    PiperOrigin-RevId: 512914783
    plumpy authored and google-java-format Team committed Feb 28, 2023
    Copy the full SHA
    9412083 View commit details

Commits on Mar 2, 2023

  1. Move the plugin to the FormattingService API.

    Also support optimizing imports, using the ImportOptimizer API.
    
    PiperOrigin-RevId: 513562756
    plumpy authored and google-java-format Team committed Mar 2, 2023
    Copy the full SHA
    84b2c9a View commit details
  2. Make the -add-exports= passed to g-j-f consistent

    PiperOrigin-RevId: 513589436
    cushon authored and google-java-format Team committed Mar 2, 2023
    Copy the full SHA
    0644d4c View commit details

Commits on Mar 3, 2023

  1. Switch (deprecated) ProjectManagerListener#onProjectOpened to Startup…

    …Activity.
    
    PiperOrigin-RevId: 513811140
    plumpy authored and google-java-format Team committed Mar 3, 2023
    Copy the full SHA
    32a6c00 View commit details
  2. Link to instructions for properly configuring the IDE's JRE.

    Pop up this warning once per-project per-launch, only when the Formatter is enabled (or at startup if the formatter is already enabled in the project).
    
    PiperOrigin-RevId: 513822794
    plumpy authored and google-java-format Team committed Mar 3, 2023
    Copy the full SHA
    4f5ffd0 View commit details

Commits on Mar 6, 2023

  1. Convert the gradle config to kotlin.

    I moved the description out of the Gradle config and into the XML. (It was just in the gradle so we could add the gjf version number, but that seems unnecessary since I changed the versioning so that the plugin version matches the gjf version.)
    
    I also updated the IntelliJ Gradle plugin to 1.13.1 because plugin publishing is broken in 1.13.0.
    
    PiperOrigin-RevId: 513888985
    plumpy authored and google-java-format Team committed Mar 6, 2023
    Copy the full SHA
    1651dea View commit details
  2. Update the --add-exports instructions.

    I don't really understand _why_ this works. I'm able to (easily) reproduce the
    problem by not including the `=` in my `idea64.options` file. But then if I
    copy/paste the command line out of `ps`, it works fine.
    
    And since it's easy enough to change this, I'm inclined to just do that?
    
    Fixes #908.
    
    PiperOrigin-RevId: 514440074
    plumpy authored and google-java-format Team committed Mar 6, 2023
    Copy the full SHA
    20527aa View commit details

Commits on Mar 7, 2023

  1. Allow blank lines inserted before comments.

    Previously, blank lines could only be emitted before comments if they were there in the input. This was irrespective of BlankLineWanted.YES hints in the OpBuilder.
    
    These hints are already being set by some paths in GJF, so now that they're respected, blank lines will be added to some existing files.
    
    Eval in unknown commit
    
    PiperOrigin-RevId: 514817708
    nreid260 authored and google-java-format Team committed Mar 7, 2023
    Copy the full SHA
    1a00656 View commit details

Commits on Mar 10, 2023

  1. Make OpsBuilder::add public final

    PiperOrigin-RevId: 515682100
    nreid260 authored and google-java-format Team committed Mar 10, 2023
    Copy the full SHA
    4c1aeff View commit details
  2. Be louder about telling users they need to modify their IDE config to…

    … use gjf.
    
    Right now if your IDE isn't set up properly and you have gjf enabled in a
    project, you get a single warning. Any uses of the code formatter after that
    will instead use the built-in formatter.
    
    This is confusing to people who don't notice the notification. They think gjf
    is formatting their code strangely. Instead, let's warn on every single
    invocation of the formatter until they either fix the configuration issue or
    disable gjf in the project.
    
    Fixes #914 #919.
    
    PiperOrigin-RevId: 515722447
    plumpy authored and google-java-format Team committed Mar 10, 2023
    Copy the full SHA
    401d5c9 View commit details

Commits on Apr 4, 2023

  1. Remove AD_HOC_FORMATTING from the IntelliJ plugin.

    I have a feeling this is causing a lot of the issues people are seeing where
    it's constantly trying to reformat and failing because of syntax errors.
    Hopefully this will make it only try to reformat when a user specifically
    requests it.
    
    PiperOrigin-RevId: 521657519
    plumpy authored and google-java-format Team committed Apr 4, 2023
    Copy the full SHA
    25ce685 View commit details

Commits on Apr 20, 2023

  1. Remove tests for handling of extra semi-colons

    javac no longer accepts these, see https://bugs.openjdk.org/browse/JDK-8027682
    
    PiperOrigin-RevId: 525775880
    cushon authored and google-java-format Team committed Apr 20, 2023
    Copy the full SHA
    18f8358 View commit details

Commits on May 1, 2023

  1. Copy the full SHA
    d6b1bcc View commit details
Showing with 953 additions and 688 deletions.
  1. +3 −0 .github/workflows/ci.yml
  2. +24 −12 README.md
  3. +3 −4 core/pom.xml
  4. +1 −1 core/src/main/java/com/google/googlejavaformat/OpsBuilder.java
  5. +1 −1 core/src/main/java/com/google/googlejavaformat/java/JavaOutput.java
  6. +13 −10 core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java
  7. +3 −4 core/src/test/java/com/google/googlejavaformat/java/MainTest.java
  8. +1 −0 core/src/test/java/com/google/googlejavaformat/java/PartialFormattingTest.java
  9. +1 −0 core/src/test/resources/com/google/googlejavaformat/java/testdata/B27246427.output
  10. +0 −7 core/src/test/resources/com/google/googlejavaformat/java/testdata/B28788559.input
  11. +0 −8 core/src/test/resources/com/google/googlejavaformat/java/testdata/B28788559.output
  12. +0 −6 core/src/test/resources/com/google/googlejavaformat/java/testdata/CL240367479.input
  13. +0 −6 core/src/test/resources/com/google/googlejavaformat/java/testdata/CL240367479.output
  14. +7 −0 core/src/test/resources/com/google/googlejavaformat/java/testdata/Fields.input
  15. +8 −0 core/src/test/resources/com/google/googlejavaformat/java/testdata/Fields.output
  16. +0 −62 idea_plugin/build.gradle
  17. +66 −0 idea_plugin/build.gradle.kts
  18. +0 −246 idea_plugin/src/com/google/googlejavaformat/intellij/CodeStyleManagerDecorator.java
  19. +0 −64 idea_plugin/src/com/google/googlejavaformat/intellij/FormatterUtil.java
  20. +0 −173 idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java
  21. +0 −57 idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatInstaller.java
  22. 0 ..._plugin/src/{ → main/java}/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.form
  23. 0 ..._plugin/src/{ → main/java}/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.java
  24. +141 −0 ..._plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingService.java
  25. +70 −0 idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java
  26. +10 −2 idea_plugin/src/{ → main/java}/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java
  27. +11 −8 ...r.java → main/java/com/google/googlejavaformat/intellij/InitialConfigurationStartupActivity.java}
  28. +104 −0 idea_plugin/src/main/java/com/google/googlejavaformat/intellij/JreConfigurationChecker.java
  29. +38 −0 idea_plugin/src/main/java/com/google/googlejavaformat/intellij/Notifications.java
  30. 0 idea_plugin/src/{ → main/java}/com/google/googlejavaformat/intellij/UiFormatterStyle.java
  31. +28 −12 idea_plugin/{ → src/main}/resources/META-INF/plugin.xml
  32. +250 −0 ...gin/src/test/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingServiceTest.java
  33. +168 −0 ...lugin/src/test/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizerTest.java
  34. +2 −5 pom.xml
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -43,6 +43,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:
36 changes: 24 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -44,14 +44,25 @@ presented when you first open a project offering to do this for you.)
To enable it by default in new projects, use `File→Other Settings→Default
Settings...`.

When enabled, it will replace the normal `Reformat Code` action, which can be
triggered from the `Code` menu or with the Ctrl-Alt-L (by default) keyboard
shortcut.
When enabled, it will replace the normal `Reformat Code` and `Optimize Imports`
actions.

The import ordering is not handled by this plugin, unfortunately. To fix the
import order, download the
[IntelliJ Java Google Style file](https://github.com/raw/google/styleguide/gh-pages/intellij-java-google-style.xml)
and import it into File→Settings→Editor→Code Style.
#### IntelliJ JRE Config

The google-java-format plugin uses some internal classes that aren't available
without extra configuration. To use the plugin, go to `Help→Edit Custom VM
Options...` and paste in these lines:

```
--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
--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
```

Once you've done that, restart the IDE.

### Eclipse

@@ -94,11 +105,12 @@ following JVM flags are required when running on JDK 16 and newer, due to
[JEP 396: Strongly Encapsulate JDK Internals by Default](https://openjdk.java.net/jeps/396):

```
--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.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
--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
--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
```

#### Maven
7 changes: 3 additions & 4 deletions core/pom.xml
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
<parent>
<groupId>com.google.googlejavaformat</groupId>
<artifactId>google-java-format-parent</artifactId>
<version>HEAD-SNAPSHOT</version>
<version>1.17.0</version>
</parent>

<artifactId>google-java-format</artifactId>
@@ -102,13 +102,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>
Original file line number Diff line number Diff line change
@@ -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) {
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -49,7 +49,6 @@
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
import com.sun.tools.javac.tree.JCTree.JCIdent;
import com.sun.tools.javac.tree.JCTree.JCImport;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Log;
@@ -293,9 +292,7 @@ private static RangeMap<Integer, String> buildReplacements(
}

private static String getSimpleName(JCImport importTree) {
return importTree.getQualifiedIdentifier() instanceof JCIdent
? ((JCIdent) importTree.getQualifiedIdentifier()).getName().toString()
: ((JCFieldAccess) importTree.getQualifiedIdentifier()).getIdentifier().toString();
return getQualifiedIdentifier(importTree).getIdentifier().toString();
}

private static boolean isUnused(
@@ -304,18 +301,15 @@ private static boolean isUnused(
Multimap<String, Range<Integer>> usedInJavadoc,
JCImport importTree,
String simpleName) {
String qualifier =
((JCFieldAccess) importTree.getQualifiedIdentifier()).getExpression().toString();
JCFieldAccess qualifiedIdentifier = getQualifiedIdentifier(importTree);
String qualifier = qualifiedIdentifier.getExpression().toString();
if (qualifier.equals("java.lang")) {
return true;
}
if (unit.getPackageName() != null && unit.getPackageName().toString().equals(qualifier)) {
return true;
}
if (importTree.getQualifiedIdentifier() instanceof JCFieldAccess
&& ((JCFieldAccess) importTree.getQualifiedIdentifier())
.getIdentifier()
.contentEquals("*")) {
if (qualifiedIdentifier.getIdentifier().contentEquals("*")) {
return false;
}

@@ -328,6 +322,15 @@ private static boolean isUnused(
return true;
}

private static JCFieldAccess getQualifiedIdentifier(JCImport importTree) {
// Use reflection because the return type is JCTree in some versions and JCFieldAccess in others
try {
return (JCFieldAccess) JCImport.class.getMethod("getQualifiedIdentifier").invoke(importTree);
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
}

/** Applies the replacements to the given source, and re-format any edited javadoc. */
private static String applyReplacements(String source, RangeMap<Integer, String> replacements) {
// save non-empty fixed ranges for reformatting after fixes are applied
Original file line number Diff line number Diff line change
@@ -54,13 +54,12 @@ public class MainTest {

private static final ImmutableList<String> ADD_EXPORTS =
ImmutableList.of(
"--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
"--add-exports=jdk.compiler/com.sun.tools.javac.code=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.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",
"--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
"--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED");
"--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED");

@Test
public void testUsageOutput() {
Original file line number Diff line number Diff line change
@@ -679,6 +679,7 @@ public void noTokensOnLine() throws Exception {
String input =
lines(
" package com.google.googlejavaformat.java;",
"",
"/*",
" * Copyright 2015 Google Inc.",
" *",
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ enum TrailingComment {

/** a */
Object a;

/** b */
Object b;
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
class Fields {

int a = 1;
int b = 1;

int c = 1;
/** Javadoc */
int d = 1;

int x = 1;

int y = 1;
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
class Fields {

int a = 1;
int b = 1;

int c = 1;

/** Javadoc */
int d = 1;

int x = 1;

int y = 1;
62 changes: 0 additions & 62 deletions idea_plugin/build.gradle

This file was deleted.

66 changes: 66 additions & 0 deletions idea_plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins { id("org.jetbrains.intellij") version "1.13.3" }

apply(plugin = "org.jetbrains.intellij")

apply(plugin = "java")

repositories { mavenCentral() }

val googleJavaFormatVersion = "1.16.0"

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

intellij {
pluginName.set("google-java-format")
plugins.set(listOf("java"))
version.set("2021.3")
}

tasks {
patchPluginXml {
version.set("${googleJavaFormatVersion}.2")
sinceBuild.set("213")
untilBuild.set("")
}

publishPlugin {
val jetbrainsPluginRepoToken: String by project
token.set(jetbrainsPluginRepoToken)
}

withType<Test>().configureEach {
jvmArgs(
"--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
"--add-exports jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
"--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
"--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",
)
}
}

dependencies {
implementation("com.google.googlejavaformat:google-java-format:${googleJavaFormatVersion}")
testImplementation("junit:junit:4.13.2")
testImplementation("com.google.truth:truth:1.1.3")
}
Loading