AllOpen: Add IDE integration

This commit is contained in:
Yan Zhulanow
2016-12-01 17:43:00 +03:00
committed by Yan Zhulanow
parent 4d638c2cfd
commit 6abde4223b
53 changed files with 677 additions and 175 deletions
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="kotlin-runtime" level="project" />
<orderEntry type="module" module-name="frontend" />
<orderEntry type="module" module-name="allopen-cli" />
<orderEntry type="library" name="idea-full" level="project" />
<orderEntry type="library" name="gradle-and-groovy-plugin" level="project" />
<orderEntry type="module" module-name="idea" />
<orderEntry type="module" module-name="idea-maven" />
<orderEntry type="library" name="maven" level="project" />
<orderEntry type="module" module-name="cli-common" />
<orderEntry type="module" module-name="idea-jps-common" />
<orderEntry type="module" module-name="util" />
<orderEntry type="module" module-name="annotation-based-compiler-plugins-ide-support" />
</component>
</module>
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2016 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.allopen.ide
import org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor
import org.jetbrains.kotlin.annotation.plugin.ide.AbstractGradleImportHandler
import org.jetbrains.kotlin.utils.PathUtil
class AllOpenGradleProjectImportHandler : AbstractGradleImportHandler() {
override val compilerPluginId = AllOpenCommandLineProcessor.PLUGIN_ID
override val pluginName = "allopen"
override val annotationOptionName = AllOpenCommandLineProcessor.ANNOTATION_OPTION.name
override val dataStorageTaskName = "allOpenDataStorageTask"
override val pluginJarFileFromIdea = PathUtil.getKotlinPathsForIdeaPlugin().allOpenPluginJarPath
}
@@ -0,0 +1,56 @@
/*
* Copyright 2010-2016 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.allopen.ide
import org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor
import org.jetbrains.kotlin.annotation.plugin.ide.AbstractMavenImportHandler
class AllOpenMavenProjectImportHandler : AbstractMavenImportHandler() {
private companion object {
val ANNOTATION_PARAMETER_PREFIX = "all-open:${AllOpenCommandLineProcessor.ANNOTATION_OPTION.name}="
private val SPRING_ALLOPEN_ANNOTATIONS = listOf(
"org.springframework.stereotype.Component",
"org.springframework.transaction.annotation.Transactional",
"org.springframework.scheduling.annotation.Async",
"org.springframework.cache.annotation.Cacheable"
)
}
override val compilerPluginId = AllOpenCommandLineProcessor.PLUGIN_ID
override val pluginName = "allopen"
override val annotationOptionName = AllOpenCommandLineProcessor.ANNOTATION_OPTION.name
override val mavenPluginArtifactName = "kotlin-maven-allopen"
override fun getAnnotations(enabledCompilerPlugins: List<String>, compilerPluginOptions: List<String>): List<String>? {
if ("all-open" !in enabledCompilerPlugins && "spring" !in enabledCompilerPlugins) {
return null
}
val annotations = mutableListOf<String>()
if ("spring" in enabledCompilerPlugins) {
annotations.addAll(SPRING_ALLOPEN_ANNOTATIONS)
}
annotations.addAll(compilerPluginOptions.mapNotNull { text ->
if (!text.startsWith(ANNOTATION_PARAMETER_PREFIX)) return@mapNotNull null
text.substring(ANNOTATION_PARAMETER_PREFIX.length)
})
return annotations
}
}
@@ -0,0 +1,61 @@
/*
* Copyright 2010-2016 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.allopen.ide
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootManager
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.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 java.util.*
class IdeAllOpenDeclarationAttributeAltererExtension(val project: Project) : AbstractAllOpenDeclarationAttributeAltererExtension() {
private companion object {
val ANNOTATION_OPTION_PREFIX = "plugin:$PLUGIN_ID:${ANNOTATION_OPTION.name}="
}
private val cache: CachedValue<WeakHashMap<Module, List<String>>> = cachedValue(project) {
CachedValueProvider.Result.create(WeakHashMap<Module, List<String>>(), ProjectRootModificationTracker.getInstance(project))
}
override fun getAllOpenAnnotationFqNames(modifierListOwner: KtModifierListOwner): List<String> {
val project = modifierListOwner.project
val virtualFile = modifierListOwner.containingFile?.virtualFile ?: return emptyList()
val module = ProjectRootManager.getInstance(project).fileIndex.getModuleForFile(virtualFile) ?: return emptyList()
return cache.value.getOrPut(module) {
val kotlinFacet = KotlinFacet.get(module) ?: return@getOrPut emptyList()
val commonArgs = kotlinFacet.configuration.settings.compilerInfo.commonCompilerArguments ?: return@getOrPut emptyList()
commonArgs.pluginOptions?.filter { it.startsWith(ANNOTATION_OPTION_PREFIX) }
?.map { it.substring(ANNOTATION_OPTION_PREFIX.length) }
?: emptyList()
}
}
private fun <T> cachedValue(project: Project, result: () -> CachedValueProvider.Result<T>): CachedValue<T> {
return CachedValuesManager.getManager(project).createCachedValue(result, false)
}
}