KT-15320 Live templates: please add function which returns the "outer" class name

This commit is contained in:
Toshiaki Kameyama
2017-12-26 14:00:57 +09:00
committed by Dmitry Jemerov
parent e2306ecf94
commit 79ff36592d
6 changed files with 36 additions and 1 deletions
@@ -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<Expression>, 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!!)
}
}
+5
View File
@@ -0,0 +1,5 @@
class Test() {
fun test() {
println("Test.test")<caret>
}
}
+5
View File
@@ -0,0 +1,5 @@
class Test() {
fun test() {
<caret>
}
}
@@ -0,0 +1,7 @@
class Test() {
companion object {
fun test() {
println("Test.test")<caret>
}
}
}
@@ -0,0 +1,7 @@
class Test() {
companion object {
fun test() {
<caret>
}
}
}
@@ -73,6 +73,14 @@ class LiveTemplatesTest : KotlinLightCodeInsightFixtureTestCase() {
paremeterless()
}
fun testSoutf() {
paremeterless()
}
fun testSoutf_InCompanion() {
paremeterless()
}
fun testSerr() {
paremeterless()
}