diff --git a/compiler/plugin-api/src/org/jetbrains/kotlin/compiler/plugin/CompilerPluginRegistrar.kt b/compiler/plugin-api/src/org/jetbrains/kotlin/compiler/plugin/CompilerPluginRegistrar.kt index 60dc8ae1113..0537417f3c0 100644 --- a/compiler/plugin-api/src/org/jetbrains/kotlin/compiler/plugin/CompilerPluginRegistrar.kt +++ b/compiler/plugin-api/src/org/jetbrains/kotlin/compiler/plugin/CompilerPluginRegistrar.kt @@ -6,6 +6,7 @@ package org.jetbrains.kotlin.compiler.plugin import com.intellij.openapi.project.Project +import com.intellij.openapi.util.Disposer import org.jetbrains.annotations.TestOnly import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.config.CompilerConfigurationKey @@ -25,9 +26,24 @@ abstract class CompilerPluginRegistrar { val registeredExtensions: Map, List> get() = _registeredExtensions + private val _disposables = mutableListOf() + val disposables: List + get() = _disposables + fun ProjectExtensionDescriptor.registerExtension(extension: T) { _registeredExtensions.getOrPut(this, ::mutableListOf).add(extension) } + + /** + * Passed [disposable] will be called at the end of compilation + */ + fun registerDisposable(disposable: PluginDisposable) { + _disposables += disposable + } + } + + fun interface PluginDisposable { + fun dispose() } abstract val supportsK2: Boolean @@ -47,6 +63,9 @@ fun CompilerPluginRegistrar.ExtensionStorage.registerInProject( } } } + for (disposable in disposables) { + Disposer.register(project) { disposable.dispose() } + } } private fun ProjectExtensionDescriptor.registerExtensionUnsafe(project: Project, extension: Any) {