SamWithReceiver: Add general-purpose plugin and Gradle/Maven integrations
This commit is contained in:
committed by
Yan Zhulanow
parent
db3172a750
commit
d07fd52c43
@@ -25,10 +25,10 @@ import com.intellij.psi.util.CachedValue
|
||||
import com.intellij.psi.util.CachedValueProvider
|
||||
import com.intellij.psi.util.CachedValuesManager
|
||||
import org.jetbrains.kotlin.allopen.AbstractAllOpenDeclarationAttributeAltererExtension
|
||||
import org.jetbrains.kotlin.idea.facet.KotlinFacet
|
||||
import org.jetbrains.kotlin.psi.KtModifierListOwner
|
||||
import org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor.Companion.PLUGIN_ID
|
||||
import org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor.Companion.ANNOTATION_OPTION
|
||||
import org.jetbrains.kotlin.annotation.plugin.ide.getSpecialAnnotations
|
||||
import java.util.*
|
||||
|
||||
class IdeAllOpenDeclarationAttributeAltererExtension(val project: Project) : AbstractAllOpenDeclarationAttributeAltererExtension() {
|
||||
@@ -48,14 +48,7 @@ class IdeAllOpenDeclarationAttributeAltererExtension(val project: Project) : Abs
|
||||
if (modifierListOwner == null) return emptyList()
|
||||
val module = ModuleUtilCore.findModuleForPsiElement(modifierListOwner) ?: return emptyList()
|
||||
|
||||
return cache.value.getOrPut(module) {
|
||||
val kotlinFacet = KotlinFacet.get(module) ?: return@getOrPut emptyList()
|
||||
val commonArgs = kotlinFacet.configuration.settings.compilerArguments ?: return@getOrPut emptyList()
|
||||
|
||||
commonArgs.pluginOptions?.filter { it.startsWith(ANNOTATION_OPTION_PREFIX) }
|
||||
?.map { it.substring(ANNOTATION_OPTION_PREFIX.length) }
|
||||
?: emptyList()
|
||||
}
|
||||
return cache.value.getOrPut(module) { module.getSpecialAnnotations(ANNOTATION_OPTION_PREFIX) }
|
||||
}
|
||||
|
||||
private fun <T> cachedValue(project: Project, result: () -> CachedValueProvider.Result<T>): CachedValue<T> {
|
||||
|
||||
@@ -16,10 +16,21 @@
|
||||
|
||||
package org.jetbrains.kotlin.annotation.plugin.ide
|
||||
|
||||
import com.intellij.openapi.module.Module
|
||||
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
|
||||
import org.jetbrains.kotlin.idea.facet.KotlinFacet
|
||||
import java.io.File
|
||||
|
||||
fun Module.getSpecialAnnotations(prefix: String): List<String> {
|
||||
val kotlinFacet = org.jetbrains.kotlin.idea.facet.KotlinFacet.get(this) ?: return emptyList()
|
||||
val commonArgs = kotlinFacet.configuration.settings.compilerArguments ?: return emptyList()
|
||||
|
||||
return commonArgs.pluginOptions
|
||||
?.filter { it.startsWith(prefix) }
|
||||
?.map { it.substring(prefix.length) }
|
||||
?: emptyList()
|
||||
}
|
||||
|
||||
internal class AnnotationBasedCompilerPluginSetup(val annotationFqNames: List<String>, val classpath: List<String>)
|
||||
|
||||
internal fun modifyCompilerArgumentsForPlugin(
|
||||
|
||||
@@ -23,7 +23,7 @@ import com.intellij.openapi.roots.ProjectRootModificationTracker
|
||||
import com.intellij.psi.util.CachedValue
|
||||
import com.intellij.psi.util.CachedValueProvider
|
||||
import com.intellij.psi.util.CachedValuesManager
|
||||
import org.jetbrains.kotlin.idea.facet.KotlinFacet
|
||||
import org.jetbrains.kotlin.annotation.plugin.ide.getSpecialAnnotations
|
||||
import org.jetbrains.kotlin.noarg.NoArgCommandLineProcessor.Companion.ANNOTATION_OPTION
|
||||
import org.jetbrains.kotlin.noarg.NoArgCommandLineProcessor.Companion.PLUGIN_ID
|
||||
import org.jetbrains.kotlin.noarg.diagnostic.AbstractNoArgDeclarationChecker
|
||||
@@ -43,15 +43,7 @@ class IdeNoArgDeclarationChecker(val project: Project) : AbstractNoArgDeclaratio
|
||||
if (modifierListOwner == null) return emptyList()
|
||||
val module = ModuleUtilCore.findModuleForPsiElement(modifierListOwner) ?: return emptyList()
|
||||
|
||||
return cache.value.getOrPut(module) {
|
||||
val kotlinFacet = KotlinFacet.get(module) ?: return@getOrPut emptyList()
|
||||
val commonArgs = kotlinFacet.configuration.settings.compilerArguments ?: return@getOrPut emptyList()
|
||||
|
||||
commonArgs.pluginOptions
|
||||
?.filter { it.startsWith(ANNOTATION_OPTION_PREFIX) }
|
||||
?.map { it.substring(ANNOTATION_OPTION_PREFIX.length) }
|
||||
?: emptyList()
|
||||
}
|
||||
return cache.value.getOrPut(module) { module.getSpecialAnnotations(ANNOTATION_OPTION_PREFIX) }
|
||||
}
|
||||
|
||||
private fun <T> cachedValue(project: Project, result: () -> CachedValueProvider.Result<T>): CachedValue<T> {
|
||||
|
||||
@@ -29,17 +29,26 @@ import org.jetbrains.kotlin.container.get
|
||||
import org.jetbrains.kotlin.extensions.StorageComponentContainerContributor
|
||||
import org.jetbrains.kotlin.load.java.sam.SamWithReceiverResolver
|
||||
import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverConfigurationKeys.ANNOTATION
|
||||
import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverConfigurationKeys.PRESET
|
||||
import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverCommandLineProcessor.Companion.SUPPORTED_PRESETS
|
||||
|
||||
object SamWithReceiverConfigurationKeys {
|
||||
val ANNOTATION: CompilerConfigurationKey<List<String>> =
|
||||
CompilerConfigurationKey.create("annotation qualified name")
|
||||
|
||||
val PRESET: CompilerConfigurationKey<List<String>> = CompilerConfigurationKey.create("annotation preset")
|
||||
}
|
||||
|
||||
class SamWithReceiverCommandLineProcessor : CommandLineProcessor {
|
||||
companion object {
|
||||
val SUPPORTED_PRESETS = emptyMap<String, List<String>>()
|
||||
|
||||
val ANNOTATION_OPTION = CliOption("annotation", "<fqname>", "Annotation qualified names",
|
||||
required = false, allowMultipleOccurrences = true)
|
||||
|
||||
val PRESET_OPTION = CliOption("preset", "<name>", "Preset name (${SUPPORTED_PRESETS.keys.joinToString()})",
|
||||
required = false, allowMultipleOccurrences = true)
|
||||
|
||||
val PLUGIN_ID = "org.jetbrains.kotlin.samWithReceiver"
|
||||
}
|
||||
|
||||
@@ -48,13 +57,17 @@ class SamWithReceiverCommandLineProcessor : CommandLineProcessor {
|
||||
|
||||
override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) = when (option) {
|
||||
ANNOTATION_OPTION -> configuration.appendList(ANNOTATION, value)
|
||||
PRESET_OPTION -> configuration.appendList(PRESET, value)
|
||||
else -> throw CliOptionProcessingException("Unknown option: ${option.name}")
|
||||
}
|
||||
}
|
||||
|
||||
class SamWithReceiverComponentRegistrar : ComponentRegistrar {
|
||||
override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
|
||||
val annotations = configuration.get(SamWithReceiverConfigurationKeys.ANNOTATION) ?: return
|
||||
val annotations = configuration.get(ANNOTATION)?.toMutableList() ?: mutableListOf()
|
||||
configuration.get(PRESET)?.forEach { preset ->
|
||||
SUPPORTED_PRESETS[preset]?.let { annotations += it }
|
||||
}
|
||||
if (annotations.isEmpty()) return
|
||||
|
||||
StorageComponentContainerContributor.registerExtension(project, CliSamWithReceiverComponentContributor(annotations))
|
||||
|
||||
@@ -8,9 +8,14 @@
|
||||
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="kotlin-runtime" level="project" />
|
||||
<orderEntry type="module" module-name="plugin-api" />
|
||||
<orderEntry type="module" module-name="sam-with-receiver-cli" />
|
||||
<orderEntry type="module" module-name="frontend" />
|
||||
<orderEntry type="module" module-name="idea" />
|
||||
<orderEntry type="module" module-name="idea-maven" />
|
||||
<orderEntry type="module" module-name="idea-analysis" />
|
||||
<orderEntry type="module" module-name="frontend.java" />
|
||||
<orderEntry type="module" module-name="annotation-based-compiler-plugins-ide-support" />
|
||||
<orderEntry type="module" module-name="util" />
|
||||
</component>
|
||||
</module>
|
||||
+24
-1
@@ -16,20 +16,43 @@
|
||||
|
||||
package org.jetbrains.kotlin.samWithReceiver.ide
|
||||
|
||||
import com.intellij.openapi.module.Module
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.roots.ProjectRootModificationTracker
|
||||
import com.intellij.psi.util.CachedValueProvider.*
|
||||
import com.intellij.psi.util.CachedValuesManager
|
||||
import org.jetbrains.kotlin.analyzer.ModuleInfo
|
||||
import org.jetbrains.kotlin.annotation.plugin.ide.getSpecialAnnotations
|
||||
import org.jetbrains.kotlin.container.ComponentProvider
|
||||
import org.jetbrains.kotlin.container.get
|
||||
import org.jetbrains.kotlin.extensions.StorageComponentContainerContributor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.ModuleProductionSourceInfo
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.ScriptDependenciesModuleInfo
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.ScriptModuleInfo
|
||||
import org.jetbrains.kotlin.load.java.sam.SamWithReceiverResolver
|
||||
import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverResolverExtension
|
||||
import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverCommandLineProcessor.Companion.PLUGIN_ID
|
||||
import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverCommandLineProcessor.Companion.ANNOTATION_OPTION
|
||||
import java.util.*
|
||||
|
||||
class IdeSamWithReceiverComponentContributor(val project: Project) : StorageComponentContainerContributor {
|
||||
private companion object {
|
||||
val ANNOTATION_OPTION_PREFIX = "plugin:$PLUGIN_ID:${ANNOTATION_OPTION.name}="
|
||||
}
|
||||
|
||||
private val cache = CachedValuesManager.getManager(project).createCachedValue({
|
||||
Result.create(WeakHashMap<Module, List<String>>(), ProjectRootModificationTracker.getInstance(project))
|
||||
}, /* trackValue = */ false)
|
||||
|
||||
private fun getAnnotationsForModule(module: Module): List<String> {
|
||||
return cache.value.getOrPut(module) { module.getSpecialAnnotations(ANNOTATION_OPTION_PREFIX) }
|
||||
}
|
||||
|
||||
class IdeSamWithReceiverComponentContributor : StorageComponentContainerContributor {
|
||||
override fun onContainerComposed(container: ComponentProvider, moduleInfo: ModuleInfo?) {
|
||||
val annotations = when (moduleInfo) {
|
||||
is ScriptModuleInfo -> moduleInfo.scriptDefinition.annotationsForSamWithReceivers
|
||||
is ScriptDependenciesModuleInfo -> moduleInfo.scriptModuleInfo?.scriptDefinition?.annotationsForSamWithReceivers
|
||||
is ModuleProductionSourceInfo -> getAnnotationsForModule(moduleInfo.module)
|
||||
else -> null
|
||||
} ?: return
|
||||
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.samWithReceiver.ide
|
||||
|
||||
import org.jetbrains.kotlin.annotation.plugin.ide.AbstractGradleImportHandler
|
||||
import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverCommandLineProcessor
|
||||
import org.jetbrains.kotlin.utils.PathUtil
|
||||
|
||||
class SamWithReceiverGradleProjectImportHandler : AbstractGradleImportHandler() {
|
||||
override val compilerPluginId = SamWithReceiverCommandLineProcessor.PLUGIN_ID
|
||||
override val pluginName = "sam-with-receiver"
|
||||
override val annotationOptionName = SamWithReceiverCommandLineProcessor.ANNOTATION_OPTION.name
|
||||
override val dataStorageTaskName = "samWithReceiverDataStorageTask"
|
||||
override val pluginJarFileFromIdea = PathUtil.getKotlinPathsForIdeaPlugin().allOpenPluginJarPath
|
||||
|
||||
override fun getAnnotationsForPreset(presetName: String): List<String> {
|
||||
for ((name, annotations) in SamWithReceiverCommandLineProcessor.SUPPORTED_PRESETS.entries) {
|
||||
if (presetName == name) {
|
||||
return annotations
|
||||
}
|
||||
}
|
||||
|
||||
return super.getAnnotationsForPreset(presetName)
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.samWithReceiver.ide
|
||||
|
||||
import org.jetbrains.kotlin.annotation.plugin.ide.AbstractMavenImportHandler
|
||||
import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverCommandLineProcessor
|
||||
import org.jetbrains.kotlin.utils.PathUtil
|
||||
|
||||
class SamWithReceiverMavenProjectImportHandler : AbstractMavenImportHandler() {
|
||||
private companion object {
|
||||
val ANNOTATION_PARAMETER_PREFIX = "sam-with-receiver:${SamWithReceiverCommandLineProcessor.ANNOTATION_OPTION.name}="
|
||||
}
|
||||
|
||||
override val compilerPluginId = SamWithReceiverCommandLineProcessor.PLUGIN_ID
|
||||
override val pluginName = "samWithReceiver"
|
||||
override val annotationOptionName = SamWithReceiverCommandLineProcessor.ANNOTATION_OPTION.name
|
||||
override val mavenPluginArtifactName = "kotlin-maven-sam-with-receiver"
|
||||
override val pluginJarFileFromIdea = PathUtil.getKotlinPathsForIdeaPlugin().allOpenPluginJarPath
|
||||
|
||||
override fun getAnnotations(enabledCompilerPlugins: List<String>, compilerPluginOptions: List<String>): List<String>? {
|
||||
if ("sam-with-receiver" !in enabledCompilerPlugins) {
|
||||
return null
|
||||
}
|
||||
|
||||
val annotations = mutableListOf<String>()
|
||||
|
||||
for ((presetName, presetAnnotations) in SamWithReceiverCommandLineProcessor.SUPPORTED_PRESETS) {
|
||||
if (presetName in enabledCompilerPlugins) {
|
||||
annotations.addAll(presetAnnotations)
|
||||
}
|
||||
}
|
||||
|
||||
annotations.addAll(compilerPluginOptions.mapNotNull { text ->
|
||||
if (!text.startsWith(ANNOTATION_PARAMETER_PREFIX)) return@mapNotNull null
|
||||
text.substring(ANNOTATION_PARAMETER_PREFIX.length)
|
||||
})
|
||||
|
||||
return annotations
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user