Hide Kotlin live template macros from non-Kotlin contexts

#KT-16635 Fixed
This commit is contained in:
Dmitry Jemerov
2017-05-09 10:42:30 +02:00
parent c778e0454d
commit 5d9307024a
8 changed files with 51 additions and 22 deletions
@@ -38,7 +38,7 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.utils.collectDescriptorsFiltered
class AnonymousSuperMacro : Macro() {
class AnonymousSuperMacro : KotlinMacro() {
override fun getName() = "anonymousSuper"
override fun getPresentableName() = "anonymousSuper()"
@@ -17,7 +17,10 @@
package org.jetbrains.kotlin.idea.liveTemplates.macro
import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.codeInsight.template.*
import com.intellij.codeInsight.template.Expression
import com.intellij.codeInsight.template.ExpressionContext
import com.intellij.codeInsight.template.Result
import com.intellij.codeInsight.template.TextResult
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDocumentManager
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
@@ -39,7 +42,7 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
abstract class BaseKotlinVariableMacro<TState> : Macro() {
abstract class BaseKotlinVariableMacro<TState> : KotlinMacro() {
private fun getVariables(params: Array<Expression>, context: ExpressionContext): Collection<VariableDescriptor> {
if (params.size != 0) return emptyList()
@@ -21,7 +21,7 @@ import com.intellij.psi.PsiDocumentManager
import org.jetbrains.kotlin.psi.KtFunction
import java.util.*
class FunctionParametersMacro : Macro() {
class FunctionParametersMacro : KotlinMacro() {
override fun getName() = "functionParameters"
override fun getPresentableName() = "functionParameters()"
@@ -46,7 +46,4 @@ class FunctionParametersMacro : Macro() {
}
return null
}
override fun isAcceptableInContext(context: TemplateContextType?) = context is JavaCodeContextType
}
@@ -13,15 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.liveTemplates.macro
import com.intellij.codeInsight.template.*
import org.jetbrains.kotlin.idea.liveTemplates.KotlinTemplateContextType
import com.intellij.codeInsight.template.Expression
import com.intellij.codeInsight.template.ExpressionContext
import com.intellij.codeInsight.template.Result
import com.intellij.codeInsight.template.TextResult
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
class KotlinClassNameMacro : Macro() {
class KotlinClassNameMacro : KotlinMacro() {
override fun getName() = "kotlinClassName"
override fun getPresentableName() = "kotlinClassName()"
@@ -29,6 +31,4 @@ class KotlinClassNameMacro : Macro() {
val element = context.psiElementAtStartOffset?.parentsWithSelf?.firstOrNull { it is KtClassOrObject && it.name != null } ?: return null
return TextResult((element as KtClassOrObject).name!!)
}
override fun isAcceptableInContext(context: TemplateContextType?): Boolean = context is KotlinTemplateContextType
}
@@ -16,12 +16,14 @@
package org.jetbrains.kotlin.idea.liveTemplates.macro
import com.intellij.codeInsight.template.*
import org.jetbrains.kotlin.idea.liveTemplates.KotlinTemplateContextType
import com.intellij.codeInsight.template.Expression
import com.intellij.codeInsight.template.ExpressionContext
import com.intellij.codeInsight.template.Result
import com.intellij.codeInsight.template.TextResult
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
class KotlinFunctionNameMacro : Macro() {
class KotlinFunctionNameMacro : KotlinMacro() {
override fun getName() = "kotlinFunctionName"
override fun getPresentableName() = "kotlinFunctionName()"
@@ -30,7 +32,4 @@ class KotlinFunctionNameMacro : Macro() {
val name = element?.name ?: return null
return TextResult(name)
}
override fun isAcceptableInContext(context: TemplateContextType?) = context is KotlinTemplateContextType
}
@@ -0,0 +1,27 @@
/*
* 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.idea.liveTemplates.macro
import com.intellij.codeInsight.template.Macro
import com.intellij.codeInsight.template.TemplateContextType
import org.jetbrains.kotlin.idea.liveTemplates.KotlinTemplateContextType
abstract class KotlinMacro : Macro() {
override fun isAcceptableInContext(context: TemplateContextType?): Boolean {
return context is KotlinTemplateContextType
}
}
+1 -1
View File
@@ -47,7 +47,7 @@
<orderEntry type="module" module-name="js.serializer" />
<orderEntry type="module" module-name="daemon-client" />
<orderEntry type="module" module-name="idea-repl" scope="TEST" />
<orderEntry type="module" module-name="idea-live-templates" exported="" scope="RUNTIME" />
<orderEntry type="module" module-name="idea-live-templates" exported="" />
<orderEntry type="module" module-name="tests-common" scope="TEST" />
<orderEntry type="library" scope="TEST" name="kotlin-test" level="project" />
<orderEntry type="module" module-name="lint-idea" scope="PROVIDED" />
@@ -18,7 +18,10 @@ package org.jetbrains.kotlin.idea.liveTemplates.macro
import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.codeInsight.template.*
import com.intellij.codeInsight.template.Expression
import com.intellij.codeInsight.template.ExpressionContext
import com.intellij.codeInsight.template.Result
import com.intellij.codeInsight.template.TextResult
import com.intellij.psi.PsiDocumentManager
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
@@ -34,7 +37,7 @@ import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
class SuggestVariableNameMacro : Macro() {
class SuggestVariableNameMacro : KotlinMacro() {
override fun getName() = "kotlinSuggestVariableName"
override fun getPresentableName() = "kotlinSuggestVariableName()"