Skip to content

Commit

Permalink
Update the open-source google-java-format plugin for 2020.1.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
plumpy authored and cpovirk committed Apr 27, 2020
1 parent 16b56a3 commit a24b04f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 60 deletions.
8 changes: 4 additions & 4 deletions idea_plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

plugins {
id "org.jetbrains.intellij" version "0.4.11"
id "org.jetbrains.intellij" version "0.4.18"
}

repositories {
Expand All @@ -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 = ''
}

Expand Down
23 changes: 10 additions & 13 deletions idea_plugin/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

<change-notes><![CDATA[
<dl>
<dt>1.7.0.5</dt>
<dd>Added a version for 2020.1+ IDEs.</dd>
<dt>1.7.0.4</dt>
<dd>Marked the plugin as being incompatible with 2020.1+ IDEs.</dd>
<dt>1.7.0.3</dt>
<dd>Fixed the plugin on 2019.3 IDEs.</dd>
<dt>1.7.0.2</dt>
Expand All @@ -23,19 +27,12 @@
</dl>
]]></change-notes>

<project-components>
<component>
<implementation-class>
com.google.googlejavaformat.intellij.GoogleJavaFormatInstaller
</implementation-class>
<loadForDefaultProject/>
</component>
<component>
<implementation-class>
com.google.googlejavaformat.intellij.InitialConfigurationComponent
</implementation-class>
</component>
</project-components>
<applicationListeners>
<listener class="com.google.googlejavaformat.intellij.InitialConfigurationProjectManagerListener"
topic="com.intellij.openapi.project.ProjectManagerListener"/>
<listener class="com.google.googlejavaformat.intellij.GoogleJavaFormatInstaller"
topic="com.intellij.openapi.project.ProjectManagerListener"/>
</applicationListeners>

<extensions defaultExtensionNs="com.intellij">
<projectConfigurable instance="com.google.googlejavaformat.intellij.GoogleJavaFormatConfigurable"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,25 @@

package com.google.googlejavaformat.intellij;

import static com.google.common.base.Preconditions.checkState;

import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.PluginManager;
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.application.impl.ApplicationInfoImpl;
import com.intellij.openapi.components.ProjectComponent;
import com.intellij.ide.plugins.PluginManagerCore;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManagerListener;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.serviceContainer.PlatformComponentManagerImpl;
import org.picocontainer.MutablePicoContainer;
import com.intellij.serviceContainer.ComponentManagerImpl;
import org.jetbrains.annotations.NotNull;

/**
* A component that replaces the default IntelliJ {@link CodeStyleManager} with one that formats via
* google-java-format.
*/
final class GoogleJavaFormatInstaller implements ProjectComponent {

private static final String CODE_STYLE_MANAGER_KEY = CodeStyleManager.class.getName();

private final Project project;

private GoogleJavaFormatInstaller(Project project) {
this.project = project;
}
final class GoogleJavaFormatInstaller implements ProjectManagerListener {

@Override
public void projectOpened() {
public void projectOpened(@NotNull Project project) {
installFormatter(project);
}

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

private static void setManager(Project project, CodeStyleManager newManager) {
if (useNewServicesApi()) {
PlatformComponentManagerImpl platformComponentManager =
(PlatformComponentManagerImpl) project;
IdeaPluginDescriptor plugin = PluginManager.getPlugin(PluginId.getId("google-java-format"));
platformComponentManager.registerServiceInstance(CodeStyleManager.class, newManager, plugin);
} else {
MutablePicoContainer container = (MutablePicoContainer) project.getPicoContainer();
container.unregisterComponent(CODE_STYLE_MANAGER_KEY);
container.registerComponentInstance(CODE_STYLE_MANAGER_KEY, newManager);
}
}

private static boolean useNewServicesApi() {
ApplicationInfo appInfo = ApplicationInfoImpl.getInstance();
return appInfo.getBuild().getBaselineVersion() >= 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit a24b04f

Please sign in to comment.