Skip to content

Commit

Permalink
Refactor installer
Browse files Browse the repository at this point in the history
  • Loading branch information
kuglee committed Jul 25, 2022
1 parent 7c61346 commit 63d2920
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 15 additions & 4 deletions src/main/kotlin/org/swiftformat/plugin/SwiftFormatInstaller.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!!)
}
}
}

0 comments on commit 63d2920

Please sign in to comment.