From 79ff36592d785115c1777ee3415b9da0a30cfe10 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Tue, 26 Dec 2017 14:00:57 +0900 Subject: [PATCH] KT-15320 Live templates: please add function which returns the "outer" class name --- .../idea/liveTemplates/macro/KotlinClassNameMacro.kt | 5 ++++- idea/idea-live-templates/testData/soutf.exp.kt | 5 +++++ idea/idea-live-templates/testData/soutf.kt | 5 +++++ .../idea-live-templates/testData/soutf_InCompanion.exp.kt | 7 +++++++ idea/idea-live-templates/testData/soutf_InCompanion.kt | 7 +++++++ .../kotlin/idea/liveTemplates/LiveTemplatesTest.kt | 8 ++++++++ 6 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 idea/idea-live-templates/testData/soutf.exp.kt create mode 100644 idea/idea-live-templates/testData/soutf.kt create mode 100644 idea/idea-live-templates/testData/soutf_InCompanion.exp.kt create mode 100644 idea/idea-live-templates/testData/soutf_InCompanion.kt diff --git a/idea/idea-live-templates/src/org/jetbrains/kotlin/idea/liveTemplates/macro/KotlinClassNameMacro.kt b/idea/idea-live-templates/src/org/jetbrains/kotlin/idea/liveTemplates/macro/KotlinClassNameMacro.kt index 7c121120397..7b687638910 100644 --- a/idea/idea-live-templates/src/org/jetbrains/kotlin/idea/liveTemplates/macro/KotlinClassNameMacro.kt +++ b/idea/idea-live-templates/src/org/jetbrains/kotlin/idea/liveTemplates/macro/KotlinClassNameMacro.kt @@ -20,6 +20,7 @@ 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.lexer.KtTokens import org.jetbrains.kotlin.psi.KtClassOrObject import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf @@ -28,7 +29,9 @@ class KotlinClassNameMacro : KotlinMacro() { override fun getPresentableName() = "kotlinClassName()" override fun calculateResult(params: Array, context: ExpressionContext): Result? { - val element = context.psiElementAtStartOffset?.parentsWithSelf?.firstOrNull { it is KtClassOrObject && it.name != null } ?: return null + val element = context.psiElementAtStartOffset?.parentsWithSelf?.firstOrNull { + it is KtClassOrObject && it.name != null && !it.hasModifier(KtTokens.COMPANION_KEYWORD) + } ?: return null return TextResult((element as KtClassOrObject).name!!) } } \ No newline at end of file diff --git a/idea/idea-live-templates/testData/soutf.exp.kt b/idea/idea-live-templates/testData/soutf.exp.kt new file mode 100644 index 00000000000..218cdc11813 --- /dev/null +++ b/idea/idea-live-templates/testData/soutf.exp.kt @@ -0,0 +1,5 @@ +class Test() { + fun test() { + println("Test.test") + } +} \ No newline at end of file diff --git a/idea/idea-live-templates/testData/soutf.kt b/idea/idea-live-templates/testData/soutf.kt new file mode 100644 index 00000000000..5176558b767 --- /dev/null +++ b/idea/idea-live-templates/testData/soutf.kt @@ -0,0 +1,5 @@ +class Test() { + fun test() { + + } +} \ No newline at end of file diff --git a/idea/idea-live-templates/testData/soutf_InCompanion.exp.kt b/idea/idea-live-templates/testData/soutf_InCompanion.exp.kt new file mode 100644 index 00000000000..fd41b7a1a4b --- /dev/null +++ b/idea/idea-live-templates/testData/soutf_InCompanion.exp.kt @@ -0,0 +1,7 @@ +class Test() { + companion object { + fun test() { + println("Test.test") + } + } +} \ No newline at end of file diff --git a/idea/idea-live-templates/testData/soutf_InCompanion.kt b/idea/idea-live-templates/testData/soutf_InCompanion.kt new file mode 100644 index 00000000000..f42b8adde8d --- /dev/null +++ b/idea/idea-live-templates/testData/soutf_InCompanion.kt @@ -0,0 +1,7 @@ +class Test() { + companion object { + fun test() { + + } + } +} \ No newline at end of file diff --git a/idea/idea-live-templates/tests/org/jetbrains/kotlin/idea/liveTemplates/LiveTemplatesTest.kt b/idea/idea-live-templates/tests/org/jetbrains/kotlin/idea/liveTemplates/LiveTemplatesTest.kt index df802db22e0..dff7f946253 100644 --- a/idea/idea-live-templates/tests/org/jetbrains/kotlin/idea/liveTemplates/LiveTemplatesTest.kt +++ b/idea/idea-live-templates/tests/org/jetbrains/kotlin/idea/liveTemplates/LiveTemplatesTest.kt @@ -73,6 +73,14 @@ class LiveTemplatesTest : KotlinLightCodeInsightFixtureTestCase() { paremeterless() } + fun testSoutf() { + paremeterless() + } + + fun testSoutf_InCompanion() { + paremeterless() + } + fun testSerr() { paremeterless() }