Allow if/when/try postfix templates in expression position

#KT-14078 Fixed
This commit is contained in:
Denis Zharkov
2016-09-28 20:24:34 +03:00
parent bff9dd4ef5
commit 18146fafdf
4 changed files with 23 additions and 6 deletions
@@ -29,14 +29,14 @@ import org.jetbrains.kotlin.types.typeUtil.isBoolean
internal object KtIfExpressionPostfixTemplate : SurroundPostfixTemplateBase(
"if", "if (expr)",
KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = true) { it.isBoolean() }
KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = false) { it.isBoolean() }
) {
override fun getSurrounder() = KotlinWithIfExpressionSurrounder(withElse = false)
}
internal object KtElseExpressionPostfixTemplate : SurroundPostfixTemplateBase(
"else", "if (!expr)",
KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = true) { it.isBoolean() }
KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = false) { it.isBoolean() }
) {
override fun getSurrounder() = KotlinWithIfExpressionSurrounder(withElse = false)
override fun getWrappedExpression(expression: PsiElement?) = (expression as KtExpression).negate()
@@ -44,7 +44,7 @@ internal object KtElseExpressionPostfixTemplate : SurroundPostfixTemplateBase(
internal class KtNotNullPostfixTemplate(val name: String) : SurroundPostfixTemplateBase(
name, "if (expr != null)",
KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = true, predicate = TypeUtils::isNullableType)
KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = false, predicate = TypeUtils::isNullableType)
) {
override fun getSurrounder() = KotlinWithIfExpressionSurrounder(withElse = false)
override fun getTail() = "!= null"
@@ -52,7 +52,7 @@ internal class KtNotNullPostfixTemplate(val name: String) : SurroundPostfixTempl
internal object KtIsNullPostfixTemplate : SurroundPostfixTemplateBase(
"null", "if (expr == null)",
KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = true, predicate = TypeUtils::isNullableType)
KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = false, predicate = TypeUtils::isNullableType)
) {
override fun getSurrounder() = KotlinWithIfExpressionSurrounder(withElse = false)
override fun getTail() = "== null"
@@ -60,14 +60,14 @@ internal object KtIsNullPostfixTemplate : SurroundPostfixTemplateBase(
internal object KtWhenExpressionPostfixTemplate : SurroundPostfixTemplateBase(
"when", "when (expr)",
KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = true)
KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = false)
) {
override fun getSurrounder() = KotlinWhenSurrounder()
}
internal object KtTryPostfixTemplate : SurroundPostfixTemplateBase(
"try", "try { code } catch (e: Exception) { }",
KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = true)
KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = false)
) {
override fun getSurrounder() = KotlinTryCatchSurrounder()
}
+3
View File
@@ -0,0 +1,3 @@
fun foo(x: String) {
val y = x.when<caret>
}
@@ -0,0 +1,8 @@
fun foo(x: String) {
val y = when (x) {
-> {
}
else -> {
}
}
}
@@ -161,6 +161,12 @@ public class PostfixTemplateProviderTestGenerated extends AbstractPostfixTemplat
doTest(fileName);
}
@TestMetadata("whenExpression.kt")
public void testWhenExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/whenExpression.kt");
doTest(fileName);
}
@TestMetadata("while.kt")
public void testWhile() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/while.kt");