Some Live Templates should probably be enabled also for "expressions" not only "statements" (KT-19194)
Consider if-then, if-else, do-while, while positions without block as statement position. #KT-19194 Fixed
This commit is contained in:
committed by
Nikolay Krasko
parent
543db380d2
commit
c06aaf6128
+4
-7
@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.idea.liveTemplates;
|
||||
|
||||
import com.intellij.codeInsight.template.EverywhereContextType;
|
||||
import com.intellij.codeInsight.template.TemplateContextType;
|
||||
import com.intellij.openapi.util.Condition;
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.psi.PsiComment;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
@@ -30,6 +30,7 @@ import com.intellij.psi.util.PsiUtilCore;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.KtNodeTypes;
|
||||
import org.jetbrains.kotlin.idea.KotlinLanguage;
|
||||
import org.jetbrains.kotlin.lexer.KtTokens;
|
||||
import org.jetbrains.kotlin.psi.*;
|
||||
@@ -162,12 +163,8 @@ public abstract class KotlinTemplateContextType extends TemplateContextType {
|
||||
|
||||
@Override
|
||||
protected boolean isInContext(@NotNull PsiElement element) {
|
||||
PsiElement parentStatement = PsiTreeUtil.findFirstParent(element, new Condition<PsiElement>() {
|
||||
@Override
|
||||
public boolean value(PsiElement element) {
|
||||
return element instanceof KtExpression && (element.getParent() instanceof KtBlockExpression);
|
||||
}
|
||||
});
|
||||
PsiElement parentStatement = PsiTreeUtil.findFirstParent(element, e ->
|
||||
e instanceof KtExpression && KtPsiUtil.isStatementContainer(e.getParent()));
|
||||
|
||||
if (parentStatement == null) return false;
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fun test(b: Boolean) {
|
||||
sou<caret>t
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun test(b: Boolean) {
|
||||
do sou<caret>t while (b)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun test(b: Boolean) {
|
||||
for (i in 1..10) sou<caret>t
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun test(b: Boolean) {
|
||||
if (b) println() else sou<caret>t
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun test(b: Boolean) {
|
||||
if (b) sou<caret>t else println()
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun test(b: Boolean) {
|
||||
when (b) {
|
||||
true -> sou<caret>t
|
||||
else -> println()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun test(b: Boolean) {
|
||||
while (b) sou<caret>t
|
||||
}
|
||||
+28
@@ -62,6 +62,34 @@ class LiveTemplatesContextTest : KotlinLightCodeInsightFixtureTestCase() {
|
||||
assertInContexts(Generic::class.java, Class::class.java, ObjectDeclaration::class.java)
|
||||
}
|
||||
|
||||
fun testStatementInBlock() {
|
||||
assertInContexts(Generic::class.java, Statement::class.java, Expression::class.java)
|
||||
}
|
||||
|
||||
fun testStatementInDoWhile() {
|
||||
assertInContexts(Generic::class.java, Statement::class.java, Expression::class.java)
|
||||
}
|
||||
|
||||
fun testStatementInFor() {
|
||||
assertInContexts(Generic::class.java, Statement::class.java, Expression::class.java)
|
||||
}
|
||||
|
||||
fun testStatementInIfElse() {
|
||||
assertInContexts(Generic::class.java, Statement::class.java, Expression::class.java)
|
||||
}
|
||||
|
||||
fun testStatementInIfThen() {
|
||||
assertInContexts(Generic::class.java, Statement::class.java, Expression::class.java)
|
||||
}
|
||||
|
||||
fun testStatementInWhile() {
|
||||
assertInContexts(Generic::class.java, Statement::class.java, Expression::class.java)
|
||||
}
|
||||
|
||||
fun testStatementInWhen() {
|
||||
assertInContexts(Generic::class.java, Statement::class.java, Expression::class.java)
|
||||
}
|
||||
|
||||
private fun assertInContexts(vararg expectedContexts: java.lang.Class<out KotlinTemplateContextType>) {
|
||||
myFixture.configureByFile(getTestName(false) + ".kt")
|
||||
val allContexts = TemplateContextType.EP_NAME.extensions.filter { it is KotlinTemplateContextType }
|
||||
|
||||
Reference in New Issue
Block a user