Skip to content

Commit 1e28093

Browse files
committed
Improve compatibility with recent IDEA releases
Update `SpringFormatComponent` to use an alternative plugin registration technique for recent versions of IDEA. Closes gh-159
1 parent 7951067 commit 1e28093

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

Diff for: spring-javaformat-intellij/spring-javaformat-intellij-plugin/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@
6262
<version>${project.version}</version>
6363
<scope>provided</scope>
6464
</dependency>
65+
<dependency>
66+
<groupId>io.spring.javaformat.intellij</groupId>
67+
<artifactId>platform-service-container</artifactId>
68+
<version>${project.version}</version>
69+
<scope>provided</scope>
70+
</dependency>
6571
<dependency>
6672
<groupId>io.spring.javaformat.intellij</groupId>
6773
<artifactId>util</artifactId>

Diff for: spring-javaformat-intellij/spring-javaformat-intellij-plugin/src/main/java/io/spring/format/formatter/intellij/SpringFormatComponent.java

+18-6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@
1919
import java.util.concurrent.locks.Lock;
2020
import java.util.concurrent.locks.ReentrantLock;
2121

22+
import com.intellij.ide.plugins.IdeaPluginDescriptor;
23+
import com.intellij.ide.plugins.PluginManagerCore;
2224
import com.intellij.ide.util.PropertiesComponent;
25+
import com.intellij.openapi.application.ApplicationInfo;
2326
import com.intellij.openapi.application.ApplicationManager;
2427
import com.intellij.openapi.components.ProjectComponent;
2528
import com.intellij.openapi.diagnostic.Logger;
29+
import com.intellij.openapi.extensions.PluginId;
2630
import com.intellij.openapi.project.Project;
2731
import com.intellij.psi.codeStyle.CodeStyleManager;
32+
import com.intellij.serviceContainer.PlatformComponentManagerImpl;
2833
import org.picocontainer.MutablePicoContainer;
2934

3035
import io.spring.format.formatter.intellij.codestyle.SpringCodeStyleManager;
@@ -90,12 +95,12 @@ private void update(State state) {
9095
}
9196
if (state == State.ACTIVE && !(manager instanceof SpringCodeStyleManager)) {
9297
logger.debug("Enabling SpringCodeStyleManager");
93-
reregisterComponent(new SpringCodeStyleManager(manager));
98+
registerCodeStyleManager(new SpringCodeStyleManager(manager));
9499
this.properties.setValue(ACTIVE_PROPERTY, true);
95100
}
96101
if (state == State.NOT_ACTIVE && (manager instanceof SpringCodeStyleManager)) {
97102
logger.debug("Disabling SpringCodeStyleManager");
98-
reregisterComponent(((SpringCodeStyleManager) manager).getDelegate());
103+
registerCodeStyleManager(((SpringCodeStyleManager) manager).getDelegate());
99104
this.properties.setValue(ACTIVE_PROPERTY, false);
100105
}
101106
ApplicationManager.getApplication().invokeLater(() -> this.statusIndicator.update(state));
@@ -105,10 +110,17 @@ private void update(State state) {
105110
}
106111
}
107112

108-
private void reregisterComponent(CodeStyleManager manager) {
109-
MutablePicoContainer container = (MutablePicoContainer) this.project.getPicoContainer();
110-
container.unregisterComponent(CODE_STYLE_MANAGER_KEY);
111-
container.registerComponentInstance(CODE_STYLE_MANAGER_KEY, manager);
113+
private void registerCodeStyleManager(CodeStyleManager manager) {
114+
if (ApplicationInfo.getInstance().getBuild().getBaselineVersion() >= 193) {
115+
PlatformComponentManagerImpl platformComponentManager = (PlatformComponentManagerImpl) this.project;
116+
IdeaPluginDescriptor plugin = PluginManagerCore.getPlugin(PluginId.getId("spring-javaformat"));
117+
platformComponentManager.registerServiceInstance(CodeStyleManager.class, manager, plugin);
118+
}
119+
else {
120+
MutablePicoContainer container = (MutablePicoContainer) this.project.getPicoContainer();
121+
container.unregisterComponent(CODE_STYLE_MANAGER_KEY);
122+
container.registerComponentInstance(CODE_STYLE_MANAGER_KEY, manager);
123+
}
112124
}
113125

114126
}

Diff for: spring-javaformat-intellij/spring-javaformat-intellij-runtime/pom.xml

+22
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@
9090
name="*/platform/util/ui/src/**" />
9191
<include
9292
name="*/platform/extensions/src/**" />
93+
<include
94+
name="*/platform/service-container/src/**" />
9395
<include
9496
name="*/plugins/maven/src/**" />
9597
<include
@@ -116,6 +118,9 @@
116118
<zip
117119
destfile="${project.build.directory}/intellij-source/extensions-sources.zip"
118120
basedir="${project.build.directory}/intellij-source/platform/extensions/src" />
121+
<zip
122+
destfile="${project.build.directory}/intellij-source/platform-service-container-sources.zip"
123+
basedir="${project.build.directory}/intellij-source/platform/service-container/src" />
119124
<zip
120125
destfile="${project.build.directory}/intellij-source/maven-sources.zip"
121126
basedir="${project.build.directory}/intellij-source/plugins/maven/src" />
@@ -222,6 +227,23 @@
222227
<generatePom>true</generatePom>
223228
</configuration>
224229
</execution>
230+
<execution>
231+
<id>install-intellij-platform-service-container</id>
232+
<phase>install</phase>
233+
<inherited>false</inherited>
234+
<goals>
235+
<goal>install-file</goal>
236+
</goals>
237+
<configuration>
238+
<file>${project.build.directory}/intellij/platform-serviceContainer.jar</file>
239+
<sources>${project.build.directory}/intellij-source/platform-service-container-sources.zip</sources>
240+
<groupId>io.spring.javaformat.intellij</groupId>
241+
<artifactId>platform-service-container</artifactId>
242+
<version>${project.version}</version>
243+
<packaging>jar</packaging>
244+
<generatePom>true</generatePom>
245+
</configuration>
246+
</execution>
225247
<execution>
226248
<id>install-intellij-idea</id>
227249
<phase>install</phase>

0 commit comments

Comments
 (0)