Skip to content

Commit

Permalink
Support IDEA 2020.1
Browse files Browse the repository at this point in the history
Closes gh-180
  • Loading branch information
philwebb committed Apr 10, 2020
1 parent e8039de commit 4b04f33
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.spring.format.formatter.intellij;

import java.lang.reflect.Method;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

Expand All @@ -26,10 +27,11 @@
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.PluginDescriptor;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.openapi.project.Project;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.serviceContainer.PlatformComponentManagerImpl;
import com.intellij.serviceContainer.ComponentManagerImpl;
import org.picocontainer.MutablePicoContainer;

import io.spring.format.formatter.intellij.codestyle.SpringCodeStyleManager;
Expand Down Expand Up @@ -112,9 +114,14 @@ private void update(State state) {

private void registerCodeStyleManager(CodeStyleManager manager) {
if (ApplicationInfo.getInstance().getBuild().getBaselineVersion() >= 193) {
PlatformComponentManagerImpl platformComponentManager = (PlatformComponentManagerImpl) this.project;
IdeaPluginDescriptor plugin = PluginManagerCore.getPlugin(PluginId.getId("spring-javaformat"));
platformComponentManager.registerServiceInstance(CodeStyleManager.class, manager, plugin);
try {
((ComponentManagerImpl) this.project).registerServiceInstance(CodeStyleManager.class, manager, plugin);
}
catch (NoSuchMethodError ex) {
Method method = findRegisterServiceInstanceMethod(this.project.getClass());
invokeRegisterServiceInstanceMethod(manager, plugin, method);
}
}
else {
MutablePicoContainer container = (MutablePicoContainer) this.project.getPicoContainer();
Expand All @@ -123,4 +130,32 @@ private void registerCodeStyleManager(CodeStyleManager manager) {
}
}

private Method findRegisterServiceInstanceMethod(Class<?> projectClass) {
if (projectClass != null) {
Method[] methods = projectClass.getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals("registerServiceInstance") && method.getParameterCount() == 3) {
if (PluginDescriptor.class.isAssignableFrom(method.getParameterTypes()[2])) {
return method;
}
}
}
return findRegisterServiceInstanceMethod(projectClass.getSuperclass());
}
return null;
}

private void invokeRegisterServiceInstanceMethod(CodeStyleManager manager, IdeaPluginDescriptor plugin,
Method method) {
if (method == null) {
throw new IllegalStateException("Unsupported IntelliJ version");
}
method.setAccessible(true);
try {
method.invoke(this.project, manager, plugin);
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<name>Spring JavaFormat IntelliJ Runtime</name>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
<intellij.binary>https://download.jetbrains.com/idea/ideaIC-2019.3.1.tar.gz</intellij.binary>
<intellij.source>https://github.com/JetBrains/intellij-community/archive/idea/193.5662.53.zip</intellij.source>
<intellij.root>idea-IC-193.5662.53</intellij.root>
<intellij.binary>https://download.jetbrains.com/idea/ideaIC-2020.1.tar.gz</intellij.binary>
<intellij.source>https://github.com/JetBrains/intellij-community/archive/idea/201.6668.121.zip</intellij.source>
<intellij.root>idea-IC-201.6668.121</intellij.root>
</properties>
<build>
<plugins>
Expand Down

0 comments on commit 4b04f33

Please sign in to comment.