From 63d292003cb1ee40b8a7a244b62be26a89750795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Librecz?= Date: Mon, 25 Jul 2022 21:10:55 +0200 Subject: [PATCH] Refactor installer --- CHANGELOG.md | 8 +++++--- gradle.properties | 2 +- .../plugin/SwiftFormatInstaller.kt | 19 +++++++++++++++---- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50a70b7..109e08d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,12 @@ # intellij-swift-format Changelog ## [Unreleased] +### Changed +- Refactor installer ## [1.0.0] -### Added -- Formatter based on Keith Lazuka's [intellij-elm](https://github.com/klazuka/intellij-elm) and Google - Inc.'s [google-java-format](https://github.com/google/google-java-format) plugins. +### Added +- Formatter based on Keith Lazuka's [intellij-elm](https://github.com/klazuka/intellij-elm) and Google + Inc.'s [google-java-format](https://github.com/google/google-java-format) plugins. - Settings diff --git a/gradle.properties b/gradle.properties index e86b37d..f988f9a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ pluginGroup=org.swiftformat.plugin pluginName=swift-format # SemVer format -> https://semver.org -pluginVersion=1.0.0 +pluginVersion=1.0.1 # See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html # for insight into build numbers and IntelliJ Platform versions. pluginSinceBuild=221 diff --git a/src/main/kotlin/org/swiftformat/plugin/SwiftFormatInstaller.kt b/src/main/kotlin/org/swiftformat/plugin/SwiftFormatInstaller.kt index 8809b38..d6723c5 100644 --- a/src/main/kotlin/org/swiftformat/plugin/SwiftFormatInstaller.kt +++ b/src/main/kotlin/org/swiftformat/plugin/SwiftFormatInstaller.kt @@ -19,11 +19,12 @@ package org.swiftformat.plugin import com.google.common.base.Preconditions import com.intellij.ide.plugins.PluginManagerCore +import com.intellij.openapi.extensions.PluginDescriptor 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.ComponentManagerImpl +import kotlin.reflect.jvm.kotlinFunction /** * A component that replaces the default IntelliJ [CodeStyleManager] with one that formats via @@ -45,11 +46,21 @@ internal class SwiftFormatInstaller : ProjectManagerListener { } private fun setManager(project: Project, newManager: CodeStyleManager) { - val platformComponentManager = project as ComponentManagerImpl + val componentManagerImplClass = + (Class.forName("com.intellij.serviceContainer.ComponentManagerImpl")) + val registerServiceInstanceMethod = + componentManagerImplClass + .getMethod( + "registerServiceInstance", + Class::class.java, + Object::class.java, + PluginDescriptor::class.java) + .kotlinFunction + val platformComponentManager = componentManagerImplClass.cast(project) val plugin = PluginManagerCore.getPlugin(PluginId.getId("org.swiftformat.plugin")) Preconditions.checkState(plugin != null, "Couldn't locate our own PluginDescriptor.") - platformComponentManager.registerServiceInstance( - CodeStyleManager::class.java, newManager, plugin!!) + registerServiceInstanceMethod!!.call( + platformComponentManager, CodeStyleManager::class.java, newManager, plugin!!) } } }