From a24b04f09b51b2b33f39c357be3eaf3f3017ac63 Mon Sep 17 00:00:00 2001 From: plumpy Date: Mon, 27 Apr 2020 10:24:00 -0700 Subject: [PATCH 1/2] Update the open-source google-java-format plugin for 2020.1. I couldn't find any good way to make this backwards compatible. Oh well. FYI, It looks like in 2020.1, we can probably actually use the ExternalFormatProcessor extension point instead of doing all this hacky nonsense... they added a #format(PsiFile, TextRange) method you can override. But I'll leave that for later because 2020.1 is out now and people are mad. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=308647308 --- idea_plugin/build.gradle | 8 ++-- idea_plugin/resources/META-INF/plugin.xml | 23 +++++----- .../intellij/GoogleJavaFormatInstaller.java | 43 ++++++------------- ...lConfigurationProjectManagerListener.java} | 20 ++++----- 4 files changed, 34 insertions(+), 60 deletions(-) rename idea_plugin/src/com/google/googlejavaformat/intellij/{InitialConfigurationComponent.java => InitialConfigurationProjectManagerListener.java} (77%) diff --git a/idea_plugin/build.gradle b/idea_plugin/build.gradle index 23691f62a..7b2f389d9 100644 --- a/idea_plugin/build.gradle +++ b/idea_plugin/build.gradle @@ -15,7 +15,7 @@ */ plugins { - id "org.jetbrains.intellij" version "0.4.11" + id "org.jetbrains.intellij" version "0.4.18" } repositories { @@ -31,14 +31,14 @@ apply plugin: 'java' intellij { pluginName = "google-java-format" - version = "193.4932.9-EAP-SNAPSHOT" + version = "2020.1" } 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.3" - sinceBuild = '173' + version = "${googleJavaFormatVersion}.0.5" + sinceBuild = '201' untilBuild = '' } diff --git a/idea_plugin/resources/META-INF/plugin.xml b/idea_plugin/resources/META-INF/plugin.xml index 8b4bb1f70..d633b4045 100644 --- a/idea_plugin/resources/META-INF/plugin.xml +++ b/idea_plugin/resources/META-INF/plugin.xml @@ -12,6 +12,10 @@ +
1.7.0.5
+
Added a version for 2020.1+ IDEs.
+
1.7.0.4
+
Marked the plugin as being incompatible with 2020.1+ IDEs.
1.7.0.3
Fixed the plugin on 2019.3 IDEs.
1.7.0.2
@@ -23,19 +27,12 @@ ]]>
- - - - com.google.googlejavaformat.intellij.GoogleJavaFormatInstaller - - - - - - com.google.googlejavaformat.intellij.InitialConfigurationComponent - - - + + + + = 193; + ComponentManagerImpl platformComponentManager = (ComponentManagerImpl) project; + IdeaPluginDescriptor plugin = PluginManagerCore.getPlugin(PluginId.getId("google-java-format")); + checkState(plugin != null, "Couldn't locate our own PluginDescriptor."); + platformComponentManager.registerServiceInstance(CodeStyleManager.class, newManager, plugin); } } diff --git a/idea_plugin/src/com/google/googlejavaformat/intellij/InitialConfigurationComponent.java b/idea_plugin/src/com/google/googlejavaformat/intellij/InitialConfigurationProjectManagerListener.java similarity index 77% rename from idea_plugin/src/com/google/googlejavaformat/intellij/InitialConfigurationComponent.java rename to idea_plugin/src/com/google/googlejavaformat/intellij/InitialConfigurationProjectManagerListener.java index 39bde1b29..da02310c7 100644 --- a/idea_plugin/src/com/google/googlejavaformat/intellij/InitialConfigurationComponent.java +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/InitialConfigurationProjectManagerListener.java @@ -20,32 +20,28 @@ import com.intellij.notification.NotificationDisplayType; import com.intellij.notification.NotificationGroup; import com.intellij.notification.NotificationType; -import com.intellij.openapi.components.ProjectComponent; import com.intellij.openapi.project.Project; +import com.intellij.openapi.project.ProjectManagerListener; +import org.jetbrains.annotations.NotNull; -final class InitialConfigurationComponent implements ProjectComponent { +final class InitialConfigurationProjectManagerListener implements ProjectManagerListener { private static final String NOTIFICATION_TITLE = "Enable google-java-format"; private static final NotificationGroup NOTIFICATION_GROUP = new NotificationGroup(NOTIFICATION_TITLE, NotificationDisplayType.STICKY_BALLOON, true); - private final Project project; - private final GoogleJavaFormatSettings settings; + @Override + public void projectOpened(@NotNull Project project) { - public InitialConfigurationComponent(Project project, GoogleJavaFormatSettings settings) { - this.project = project; - this.settings = settings; - } + GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project); - @Override - public void projectOpened() { if (settings.isUninitialized()) { settings.setEnabled(false); - displayNewUserNotification(); + displayNewUserNotification(project, settings); } } - private void displayNewUserNotification() { + private void displayNewUserNotification(Project project, GoogleJavaFormatSettings settings) { Notification notification = new Notification( NOTIFICATION_GROUP.getDisplayId(), From a399e821dab2ad6286d5f5ad4ff8299cbc8b41fb Mon Sep 17 00:00:00 2001 From: cushon Date: Mon, 27 Apr 2020 11:13:08 -0700 Subject: [PATCH 2/2] Fix formatting of records without an explicit constructor Fixes https://github.com/google/google-java-format/issues/460 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=308658958 --- .../java/JavaInputAstVisitor.java | 5 ++- .../java/java14/Java14InputAstVisitor.java | 38 +++++++++++++------ .../java/testdata/java14.input | 10 ++++- .../java/testdata/java14.output | 8 ++++ 4 files changed, 47 insertions(+), 14 deletions(-) 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 7370b668c..ee211b500 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -1332,9 +1332,12 @@ public Void visitAnnotatedType(AnnotatedTypeTree node, Void unused) { return null; } - // TODO(cushon): Use Flags.COMPACT_RECORD_CONSTRUCTOR once if/when we drop support for Java 11 + // TODO(cushon): Use Flags if/when we drop support for Java 11 + protected static final long COMPACT_RECORD_CONSTRUCTOR = 1L << 51; + protected static final long RECORD = 1L << 61; + @Override public Void visitMethod(MethodTree node, Void unused) { sync(node); diff --git a/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java index f3dee6b67..28a110395 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java @@ -15,9 +15,10 @@ package com.google.googlejavaformat.java.java14; import static com.google.common.collect.ImmutableList.toImmutableList; -import static com.google.common.collect.MoreCollectors.onlyElement; +import static com.google.common.collect.MoreCollectors.toOptional; import com.google.common.base.Verify; +import com.google.common.collect.ImmutableList; import com.google.googlejavaformat.Op; import com.google.googlejavaformat.OpsBuilder; import com.google.googlejavaformat.java.JavaInputAstVisitor; @@ -26,13 +27,13 @@ import com.sun.source.tree.ClassTree; import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.InstanceOfTree; -import com.sun.source.tree.MethodTree; import com.sun.source.tree.SwitchExpressionTree; import com.sun.source.tree.Tree; import com.sun.source.tree.YieldTree; import com.sun.tools.javac.code.Flags; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCMethodDecl; +import com.sun.tools.javac.tree.JCTree.JCVariableDecl; import com.sun.tools.javac.tree.TreeInfo; import java.util.List; import java.util.Optional; @@ -112,20 +113,17 @@ public void visitRecordDeclaration(ClassTree node) { if (!node.getTypeParameters().isEmpty()) { typeParametersRest(node.getTypeParameters(), hasSuperInterfaceTypes ? plusFour : ZERO); } - MethodTree constructor = - node.getMembers().stream() - .filter(JCMethodDecl.class::isInstance) - .map(JCMethodDecl.class::cast) - .filter( - m -> (m.mods.flags & COMPACT_RECORD_CONSTRUCTOR) == COMPACT_RECORD_CONSTRUCTOR) - .collect(onlyElement()); + ImmutableList parameters = + compactRecordConstructor(node) + .map(m -> ImmutableList.copyOf(m.getParameters())) + .orElseGet(() -> recordVariables(node)); token("("); - if (!constructor.getParameters().isEmpty() || constructor.getReceiverParameter() != null) { + if (!parameters.isEmpty()) { // Break before args. builder.breakToFill(""); } - visitFormals( - Optional.ofNullable(constructor.getReceiverParameter()), constructor.getParameters()); + // record headers can't declare receiver parameters + visitFormals(/* receiver= */ Optional.empty(), parameters); token(")"); if (hasSuperInterfaceTypes) { builder.breakToFill(" "); @@ -157,6 +155,22 @@ public void visitRecordDeclaration(ClassTree node) { dropEmptyDeclarations(); } + private static Optional compactRecordConstructor(ClassTree node) { + return node.getMembers().stream() + .filter(JCMethodDecl.class::isInstance) + .map(JCMethodDecl.class::cast) + .filter(m -> (m.mods.flags & COMPACT_RECORD_CONSTRUCTOR) == COMPACT_RECORD_CONSTRUCTOR) + .collect(toOptional()); + } + + private static ImmutableList recordVariables(ClassTree node) { + return node.getMembers().stream() + .filter(JCVariableDecl.class::isInstance) + .map(JCVariableDecl.class::cast) + .filter(m -> (m.mods.flags & RECORD) == RECORD) + .collect(toImmutableList()); + } + @Override public Void visitInstanceOf(InstanceOfTree node, Void unused) { sync(node); diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/java14.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/java14.input index ce9c1257c..ecbea285b 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/java14.input +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/java14.input @@ -19,6 +19,14 @@ class Java14 { } } + record Foo(int id) {} + + record Rcv(int id) { + public Rcv(Rcv this, int id) { + this.id = id; + } + } + void g() { var block = """ hello @@ -35,4 +43,4 @@ class Java14 { case WEDNESDAY -> 9; }; } -} \ No newline at end of file +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/java14.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/java14.output index bddf08da7..c8c435bbd 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/java14.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/java14.output @@ -19,6 +19,14 @@ class Java14 { } } + record Foo(int id) {} + + record Rcv(int id) { + public Rcv(Rcv this, int id) { + this.id = id; + } + } + void g() { var block = """ hello