Skip to content

Commit a24b04f

Browse files
plumpycpovirk
authored andcommitted
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
1 parent 16b56a3 commit a24b04f

File tree

4 files changed

+34
-60
lines changed

4 files changed

+34
-60
lines changed

idea_plugin/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
plugins {
18-
id "org.jetbrains.intellij" version "0.4.11"
18+
id "org.jetbrains.intellij" version "0.4.18"
1919
}
2020

2121
repositories {
@@ -31,14 +31,14 @@ apply plugin: 'java'
3131

3232
intellij {
3333
pluginName = "google-java-format"
34-
version = "193.4932.9-EAP-SNAPSHOT"
34+
version = "2020.1"
3535
}
3636

3737
patchPluginXml {
3838
pluginDescription = "Formats source code using the google-java-format tool. This version of " +
3939
"the plugin uses version ${googleJavaFormatVersion} of the tool."
40-
version = "${googleJavaFormatVersion}.0.3"
41-
sinceBuild = '173'
40+
version = "${googleJavaFormatVersion}.0.5"
41+
sinceBuild = '201'
4242
untilBuild = ''
4343
}
4444

idea_plugin/resources/META-INF/plugin.xml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
<change-notes><![CDATA[
1414
<dl>
15+
<dt>1.7.0.5</dt>
16+
<dd>Added a version for 2020.1+ IDEs.</dd>
17+
<dt>1.7.0.4</dt>
18+
<dd>Marked the plugin as being incompatible with 2020.1+ IDEs.</dd>
1519
<dt>1.7.0.3</dt>
1620
<dd>Fixed the plugin on 2019.3 IDEs.</dd>
1721
<dt>1.7.0.2</dt>
@@ -23,19 +27,12 @@
2327
</dl>
2428
]]></change-notes>
2529

26-
<project-components>
27-
<component>
28-
<implementation-class>
29-
com.google.googlejavaformat.intellij.GoogleJavaFormatInstaller
30-
</implementation-class>
31-
<loadForDefaultProject/>
32-
</component>
33-
<component>
34-
<implementation-class>
35-
com.google.googlejavaformat.intellij.InitialConfigurationComponent
36-
</implementation-class>
37-
</component>
38-
</project-components>
30+
<applicationListeners>
31+
<listener class="com.google.googlejavaformat.intellij.InitialConfigurationProjectManagerListener"
32+
topic="com.intellij.openapi.project.ProjectManagerListener"/>
33+
<listener class="com.google.googlejavaformat.intellij.GoogleJavaFormatInstaller"
34+
topic="com.intellij.openapi.project.ProjectManagerListener"/>
35+
</applicationListeners>
3936

4037
<extensions defaultExtensionNs="com.intellij">
4138
<projectConfigurable instance="com.google.googlejavaformat.intellij.GoogleJavaFormatConfigurable"

idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatInstaller.java

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,25 @@
1616

1717
package com.google.googlejavaformat.intellij;
1818

19+
import static com.google.common.base.Preconditions.checkState;
20+
1921
import com.intellij.ide.plugins.IdeaPluginDescriptor;
20-
import com.intellij.ide.plugins.PluginManager;
21-
import com.intellij.openapi.application.ApplicationInfo;
22-
import com.intellij.openapi.application.impl.ApplicationInfoImpl;
23-
import com.intellij.openapi.components.ProjectComponent;
22+
import com.intellij.ide.plugins.PluginManagerCore;
2423
import com.intellij.openapi.extensions.PluginId;
2524
import com.intellij.openapi.project.Project;
25+
import com.intellij.openapi.project.ProjectManagerListener;
2626
import com.intellij.psi.codeStyle.CodeStyleManager;
27-
import com.intellij.serviceContainer.PlatformComponentManagerImpl;
28-
import org.picocontainer.MutablePicoContainer;
27+
import com.intellij.serviceContainer.ComponentManagerImpl;
28+
import org.jetbrains.annotations.NotNull;
2929

3030
/**
3131
* A component that replaces the default IntelliJ {@link CodeStyleManager} with one that formats via
3232
* google-java-format.
3333
*/
34-
final class GoogleJavaFormatInstaller implements ProjectComponent {
35-
36-
private static final String CODE_STYLE_MANAGER_KEY = CodeStyleManager.class.getName();
37-
38-
private final Project project;
39-
40-
private GoogleJavaFormatInstaller(Project project) {
41-
this.project = project;
42-
}
34+
final class GoogleJavaFormatInstaller implements ProjectManagerListener {
4335

4436
@Override
45-
public void projectOpened() {
37+
public void projectOpened(@NotNull Project project) {
4638
installFormatter(project);
4739
}
4840

@@ -57,20 +49,9 @@ private static void installFormatter(Project project) {
5749
}
5850

5951
private static void setManager(Project project, CodeStyleManager newManager) {
60-
if (useNewServicesApi()) {
61-
PlatformComponentManagerImpl platformComponentManager =
62-
(PlatformComponentManagerImpl) project;
63-
IdeaPluginDescriptor plugin = PluginManager.getPlugin(PluginId.getId("google-java-format"));
64-
platformComponentManager.registerServiceInstance(CodeStyleManager.class, newManager, plugin);
65-
} else {
66-
MutablePicoContainer container = (MutablePicoContainer) project.getPicoContainer();
67-
container.unregisterComponent(CODE_STYLE_MANAGER_KEY);
68-
container.registerComponentInstance(CODE_STYLE_MANAGER_KEY, newManager);
69-
}
70-
}
71-
72-
private static boolean useNewServicesApi() {
73-
ApplicationInfo appInfo = ApplicationInfoImpl.getInstance();
74-
return appInfo.getBuild().getBaselineVersion() >= 193;
52+
ComponentManagerImpl platformComponentManager = (ComponentManagerImpl) project;
53+
IdeaPluginDescriptor plugin = PluginManagerCore.getPlugin(PluginId.getId("google-java-format"));
54+
checkState(plugin != null, "Couldn't locate our own PluginDescriptor.");
55+
platformComponentManager.registerServiceInstance(CodeStyleManager.class, newManager, plugin);
7556
}
7657
}

idea_plugin/src/com/google/googlejavaformat/intellij/InitialConfigurationComponent.java renamed to idea_plugin/src/com/google/googlejavaformat/intellij/InitialConfigurationProjectManagerListener.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,28 @@
2020
import com.intellij.notification.NotificationDisplayType;
2121
import com.intellij.notification.NotificationGroup;
2222
import com.intellij.notification.NotificationType;
23-
import com.intellij.openapi.components.ProjectComponent;
2423
import com.intellij.openapi.project.Project;
24+
import com.intellij.openapi.project.ProjectManagerListener;
25+
import org.jetbrains.annotations.NotNull;
2526

26-
final class InitialConfigurationComponent implements ProjectComponent {
27+
final class InitialConfigurationProjectManagerListener implements ProjectManagerListener {
2728

2829
private static final String NOTIFICATION_TITLE = "Enable google-java-format";
2930
private static final NotificationGroup NOTIFICATION_GROUP =
3031
new NotificationGroup(NOTIFICATION_TITLE, NotificationDisplayType.STICKY_BALLOON, true);
3132

32-
private final Project project;
33-
private final GoogleJavaFormatSettings settings;
33+
@Override
34+
public void projectOpened(@NotNull Project project) {
3435

35-
public InitialConfigurationComponent(Project project, GoogleJavaFormatSettings settings) {
36-
this.project = project;
37-
this.settings = settings;
38-
}
36+
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
3937

40-
@Override
41-
public void projectOpened() {
4238
if (settings.isUninitialized()) {
4339
settings.setEnabled(false);
44-
displayNewUserNotification();
40+
displayNewUserNotification(project, settings);
4541
}
4642
}
4743

48-
private void displayNewUserNotification() {
44+
private void displayNewUserNotification(Project project, GoogleJavaFormatSettings settings) {
4945
Notification notification =
5046
new Notification(
5147
NOTIFICATION_GROUP.getDisplayId(),

0 commit comments

Comments
 (0)