diff --git a/idea/resources/postfixTemplates/KtElseExpressionPostfixTemplate/after.kt.template b/idea/resources/postfixTemplates/KtElseExpressionPostfixTemplate/after.kt.template new file mode 100644 index 00000000000..e32ca5fcbe3 --- /dev/null +++ b/idea/resources/postfixTemplates/KtElseExpressionPostfixTemplate/after.kt.template @@ -0,0 +1,5 @@ +fun foo(x: Boolean) { + if (!x) { + + } +} diff --git a/idea/resources/postfixTemplates/KtElseExpressionPostfixTemplate/before.kt.template b/idea/resources/postfixTemplates/KtElseExpressionPostfixTemplate/before.kt.template new file mode 100644 index 00000000000..854b83b9867 --- /dev/null +++ b/idea/resources/postfixTemplates/KtElseExpressionPostfixTemplate/before.kt.template @@ -0,0 +1,3 @@ +fun foo(x: Boolean) { + x$key +} diff --git a/idea/resources/postfixTemplates/KtElseExpressionPostfixTemplate/description.html b/idea/resources/postfixTemplates/KtElseExpressionPostfixTemplate/description.html new file mode 100644 index 00000000000..a87db3d9c05 --- /dev/null +++ b/idea/resources/postfixTemplates/KtElseExpressionPostfixTemplate/description.html @@ -0,0 +1,5 @@ + + +Checks boolean expression to be false. + + diff --git a/idea/resources/postfixTemplates/KtIfExpressionPostfixTemplate/after.kt.template b/idea/resources/postfixTemplates/KtIfExpressionPostfixTemplate/after.kt.template new file mode 100644 index 00000000000..d05a9f2a0cc --- /dev/null +++ b/idea/resources/postfixTemplates/KtIfExpressionPostfixTemplate/after.kt.template @@ -0,0 +1,5 @@ +fun foo(x: Boolean) { + if (x) { + + } +} diff --git a/idea/resources/postfixTemplates/KtIfExpressionPostfixTemplate/before.kt.template b/idea/resources/postfixTemplates/KtIfExpressionPostfixTemplate/before.kt.template new file mode 100644 index 00000000000..854b83b9867 --- /dev/null +++ b/idea/resources/postfixTemplates/KtIfExpressionPostfixTemplate/before.kt.template @@ -0,0 +1,3 @@ +fun foo(x: Boolean) { + x$key +} diff --git a/idea/resources/postfixTemplates/KtIfExpressionPostfixTemplate/description.html b/idea/resources/postfixTemplates/KtIfExpressionPostfixTemplate/description.html new file mode 100644 index 00000000000..37b9bad7d2d --- /dev/null +++ b/idea/resources/postfixTemplates/KtIfExpressionPostfixTemplate/description.html @@ -0,0 +1,5 @@ + + +Checks boolean expression to be true. + + diff --git a/idea/resources/postfixTemplates/KtIsNullPostfixTemplate/after.kt.template b/idea/resources/postfixTemplates/KtIsNullPostfixTemplate/after.kt.template new file mode 100644 index 00000000000..f076a3a65c2 --- /dev/null +++ b/idea/resources/postfixTemplates/KtIsNullPostfixTemplate/after.kt.template @@ -0,0 +1,5 @@ +fun foo(x: Any?) { + if (x == null) { + + } +} diff --git a/idea/resources/postfixTemplates/KtIsNullPostfixTemplate/before.kt.template b/idea/resources/postfixTemplates/KtIsNullPostfixTemplate/before.kt.template new file mode 100644 index 00000000000..3feec7caa29 --- /dev/null +++ b/idea/resources/postfixTemplates/KtIsNullPostfixTemplate/before.kt.template @@ -0,0 +1,3 @@ +fun foo(x: Any?) { + x$key +} diff --git a/idea/resources/postfixTemplates/KtIsNullPostfixTemplate/description.html b/idea/resources/postfixTemplates/KtIsNullPostfixTemplate/description.html new file mode 100644 index 00000000000..eb854ae563a --- /dev/null +++ b/idea/resources/postfixTemplates/KtIsNullPostfixTemplate/description.html @@ -0,0 +1,5 @@ + + +Checks expression to be null. + + diff --git a/idea/resources/postfixTemplates/KtNotNullPostfixTemplate/after.kt.template b/idea/resources/postfixTemplates/KtNotNullPostfixTemplate/after.kt.template new file mode 100644 index 00000000000..cf8b720fd4b --- /dev/null +++ b/idea/resources/postfixTemplates/KtNotNullPostfixTemplate/after.kt.template @@ -0,0 +1,5 @@ +fun foo(x: Any?) { + if (x != null) { + + } +} diff --git a/idea/resources/postfixTemplates/KtNotNullPostfixTemplate/before.kt.template b/idea/resources/postfixTemplates/KtNotNullPostfixTemplate/before.kt.template new file mode 100644 index 00000000000..3feec7caa29 --- /dev/null +++ b/idea/resources/postfixTemplates/KtNotNullPostfixTemplate/before.kt.template @@ -0,0 +1,3 @@ +fun foo(x: Any?) { + x$key +} diff --git a/idea/resources/postfixTemplates/KtNotNullPostfixTemplate/description.html b/idea/resources/postfixTemplates/KtNotNullPostfixTemplate/description.html new file mode 100644 index 00000000000..d6989ac12d8 --- /dev/null +++ b/idea/resources/postfixTemplates/KtNotNullPostfixTemplate/description.html @@ -0,0 +1,5 @@ + + +Checks expression to be not null. + + diff --git a/idea/resources/postfixTemplates/KtTryPostfixTemplate/after.kt.template b/idea/resources/postfixTemplates/KtTryPostfixTemplate/after.kt.template new file mode 100644 index 00000000000..8a652d1d5b5 --- /dev/null +++ b/idea/resources/postfixTemplates/KtTryPostfixTemplate/after.kt.template @@ -0,0 +1,7 @@ +fun foo(x: Any?) { + try { + x + } catch (e: Exception) { + + } +} diff --git a/idea/resources/postfixTemplates/KtTryPostfixTemplate/before.kt.template b/idea/resources/postfixTemplates/KtTryPostfixTemplate/before.kt.template new file mode 100644 index 00000000000..3feec7caa29 --- /dev/null +++ b/idea/resources/postfixTemplates/KtTryPostfixTemplate/before.kt.template @@ -0,0 +1,3 @@ +fun foo(x: Any?) { + x$key +} diff --git a/idea/resources/postfixTemplates/KtTryPostfixTemplate/description.html b/idea/resources/postfixTemplates/KtTryPostfixTemplate/description.html new file mode 100644 index 00000000000..c4e83938a56 --- /dev/null +++ b/idea/resources/postfixTemplates/KtTryPostfixTemplate/description.html @@ -0,0 +1,5 @@ + + +Surrounds expression with try-catch block. + + diff --git a/idea/resources/postfixTemplates/KtWhenExpressionPostfixTemplate/after.kt.template b/idea/resources/postfixTemplates/KtWhenExpressionPostfixTemplate/after.kt.template new file mode 100644 index 00000000000..706fa6308c4 --- /dev/null +++ b/idea/resources/postfixTemplates/KtWhenExpressionPostfixTemplate/after.kt.template @@ -0,0 +1,5 @@ +fun foo(x: String) { + when (x) { + + } +} diff --git a/idea/resources/postfixTemplates/KtWhenExpressionPostfixTemplate/before.kt.template b/idea/resources/postfixTemplates/KtWhenExpressionPostfixTemplate/before.kt.template new file mode 100644 index 00000000000..531dd72ee26 --- /dev/null +++ b/idea/resources/postfixTemplates/KtWhenExpressionPostfixTemplate/before.kt.template @@ -0,0 +1,3 @@ +fun foo(x: String) { + x$key +} diff --git a/idea/resources/postfixTemplates/KtWhenExpressionPostfixTemplate/description.html b/idea/resources/postfixTemplates/KtWhenExpressionPostfixTemplate/description.html new file mode 100644 index 00000000000..76139626e1c --- /dev/null +++ b/idea/resources/postfixTemplates/KtWhenExpressionPostfixTemplate/description.html @@ -0,0 +1,5 @@ + + +Wrap expression with 'when' statement + + diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/postfix/KtPostfixTemplateProvider.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/postfix/KtPostfixTemplateProvider.kt index 6d7725a191b..f5e161901a2 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/postfix/KtPostfixTemplateProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/postfix/KtPostfixTemplateProvider.kt @@ -33,7 +33,16 @@ import org.jetbrains.kotlin.types.typeUtil.isBoolean class KtPostfixTemplateProvider : PostfixTemplateProvider { - override fun getTemplates() = setOf(KtNotPostfixTemplate) + override fun getTemplates() = setOf( + KtNotPostfixTemplate, + KtIfExpressionPostfixTemplate, + KtElseExpressionPostfixTemplate, + KtNotNullPostfixTemplate("notnull"), + KtNotNullPostfixTemplate("nn"), + KtIsNullPostfixTemplate, + KtWhenExpressionPostfixTemplate, + KtTryPostfixTemplate + ) override fun isTerminalSymbol(currentChar: Char) = currentChar == '.' || currentChar == '!' @@ -51,7 +60,7 @@ private object KtNotPostfixTemplate : NotPostfixTemplate( createExpressionSelector { it.isBoolean() } ) -private object KtPostfixTemplatePsiInfo : PostfixTemplatePsiInfo() { +internal object KtPostfixTemplatePsiInfo : PostfixTemplatePsiInfo() { override fun createExpression(context: PsiElement, prefix: String, suffix: String) = KtPsiFactory(context.project).createExpression(prefix + context.text + suffix) diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/postfix/surrounderBasedPostfixTemplates.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/postfix/surrounderBasedPostfixTemplates.kt new file mode 100644 index 00000000000..e506f067041 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/postfix/surrounderBasedPostfixTemplates.kt @@ -0,0 +1,73 @@ +/* + * Copyright 2010-2016 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.codeInsight.postfix + +import com.intellij.codeInsight.template.postfix.templates.SurroundPostfixTemplateBase +import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.idea.codeInsight.surroundWith.expression.KotlinWhenSurrounder +import org.jetbrains.kotlin.idea.codeInsight.surroundWith.expression.KotlinWithIfExpressionSurrounder +import org.jetbrains.kotlin.idea.codeInsight.surroundWith.statement.KotlinTryCatchSurrounder +import org.jetbrains.kotlin.idea.intentions.negate +import org.jetbrains.kotlin.psi.KtExpression +import org.jetbrains.kotlin.types.TypeUtils +import org.jetbrains.kotlin.types.typeUtil.isBoolean + + +internal object KtIfExpressionPostfixTemplate : SurroundPostfixTemplateBase( + "if", "if (expr)", + KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = true) { it.isBoolean() } +) { + override fun getSurrounder() = KotlinWithIfExpressionSurrounder(withElse = false) +} + +internal object KtElseExpressionPostfixTemplate : SurroundPostfixTemplateBase( + "else", "if (!expr)", + KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = true) { it.isBoolean() } +) { + override fun getSurrounder() = KotlinWithIfExpressionSurrounder(withElse = false) + override fun getWrappedExpression(expression: PsiElement?) = (expression as KtExpression).negate() +} + +internal class KtNotNullPostfixTemplate(val name: String) : SurroundPostfixTemplateBase( + name, "if (expr != null)", + KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = true, predicate = TypeUtils::isNullableType) +) { + override fun getSurrounder() = KotlinWithIfExpressionSurrounder(withElse = false) + override fun getTail() = "!= null" +} + +internal object KtIsNullPostfixTemplate : SurroundPostfixTemplateBase( + "null", "if (expr == null)", + KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = true, predicate = TypeUtils::isNullableType) +) { + override fun getSurrounder() = KotlinWithIfExpressionSurrounder(withElse = false) + override fun getTail() = "== null" +} + +internal object KtWhenExpressionPostfixTemplate : SurroundPostfixTemplateBase( + "when", "when (expr)", + KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = true) +) { + override fun getSurrounder() = KotlinWhenSurrounder() +} + +internal object KtTryPostfixTemplate : SurroundPostfixTemplateBase( + "try", "try { code } catch (e: Exception) { }", + KtPostfixTemplatePsiInfo, createExpressionSelector(statementsOnly = true) +) { + override fun getSurrounder() = KotlinTryCatchSurrounder() +} diff --git a/idea/testData/codeInsight/postfix/else.kt b/idea/testData/codeInsight/postfix/else.kt new file mode 100644 index 00000000000..9fb5b908aa5 --- /dev/null +++ b/idea/testData/codeInsight/postfix/else.kt @@ -0,0 +1,3 @@ +fun foo(x: Boolean) { + x.else +} diff --git a/idea/testData/codeInsight/postfix/else.kt.after b/idea/testData/codeInsight/postfix/else.kt.after new file mode 100644 index 00000000000..62eed7c9e11 --- /dev/null +++ b/idea/testData/codeInsight/postfix/else.kt.after @@ -0,0 +1,5 @@ +fun foo(x: Boolean) { + if (!x) { + + } +} diff --git a/idea/testData/codeInsight/postfix/if.kt b/idea/testData/codeInsight/postfix/if.kt new file mode 100644 index 00000000000..a81181b5699 --- /dev/null +++ b/idea/testData/codeInsight/postfix/if.kt @@ -0,0 +1,3 @@ +fun foo(x: Boolean) { + x.if +} diff --git a/idea/testData/codeInsight/postfix/if.kt.after b/idea/testData/codeInsight/postfix/if.kt.after new file mode 100644 index 00000000000..3f90f798716 --- /dev/null +++ b/idea/testData/codeInsight/postfix/if.kt.after @@ -0,0 +1,5 @@ +fun foo(x: Boolean) { + if (x) { + + } +} diff --git a/idea/testData/codeInsight/postfix/notnull.kt b/idea/testData/codeInsight/postfix/notnull.kt new file mode 100644 index 00000000000..95f1be9343e --- /dev/null +++ b/idea/testData/codeInsight/postfix/notnull.kt @@ -0,0 +1,3 @@ +fun foo(x: Any?) { + x.notnull +} diff --git a/idea/testData/codeInsight/postfix/notnull.kt.after b/idea/testData/codeInsight/postfix/notnull.kt.after new file mode 100644 index 00000000000..a73f0e8401e --- /dev/null +++ b/idea/testData/codeInsight/postfix/notnull.kt.after @@ -0,0 +1,5 @@ +fun foo(x: Any?) { + if (x != null) { + + } +} diff --git a/idea/testData/codeInsight/postfix/null.kt b/idea/testData/codeInsight/postfix/null.kt new file mode 100644 index 00000000000..c705723726d --- /dev/null +++ b/idea/testData/codeInsight/postfix/null.kt @@ -0,0 +1,3 @@ +fun foo(x: Any?) { + x.null +} diff --git a/idea/testData/codeInsight/postfix/null.kt.after b/idea/testData/codeInsight/postfix/null.kt.after new file mode 100644 index 00000000000..9d32efb67b0 --- /dev/null +++ b/idea/testData/codeInsight/postfix/null.kt.after @@ -0,0 +1,5 @@ +fun foo(x: Any?) { + if (x == null) { + + } +} diff --git a/idea/testData/codeInsight/postfix/try.kt b/idea/testData/codeInsight/postfix/try.kt new file mode 100644 index 00000000000..c8db2ae4474 --- /dev/null +++ b/idea/testData/codeInsight/postfix/try.kt @@ -0,0 +1,3 @@ +fun foo(x: Boolean) { + x.try +} diff --git a/idea/testData/codeInsight/postfix/try.kt.after b/idea/testData/codeInsight/postfix/try.kt.after new file mode 100644 index 00000000000..273b004b150 --- /dev/null +++ b/idea/testData/codeInsight/postfix/try.kt.after @@ -0,0 +1,6 @@ +fun foo(x: Boolean) { + try { + x + } catch(e: Exception) { + } +} diff --git a/idea/testData/codeInsight/postfix/when.kt b/idea/testData/codeInsight/postfix/when.kt new file mode 100644 index 00000000000..d7ac08c5037 --- /dev/null +++ b/idea/testData/codeInsight/postfix/when.kt @@ -0,0 +1,3 @@ +fun foo(x: String) { + x.when +} diff --git a/idea/testData/codeInsight/postfix/when.kt.after b/idea/testData/codeInsight/postfix/when.kt.after new file mode 100644 index 00000000000..55dc1a3d407 --- /dev/null +++ b/idea/testData/codeInsight/postfix/when.kt.after @@ -0,0 +1,8 @@ +fun foo(x: String) { + when (x) { + -> { + } + else -> { + } + } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/PostfixTemplateProviderTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/PostfixTemplateProviderTestGenerated.java index b8516781991..89229d51853 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/PostfixTemplateProviderTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/PostfixTemplateProviderTestGenerated.java @@ -35,6 +35,18 @@ public class PostfixTemplateProviderTestGenerated extends AbstractPostfixTemplat KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/postfix"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("else.kt") + public void testElse() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/else.kt"); + doTest(fileName); + } + + @TestMetadata("if.kt") + public void testIf() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/if.kt"); + doTest(fileName); + } + @TestMetadata("notBoolean.kt") public void testNotBoolean() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/notBoolean.kt"); @@ -46,4 +58,28 @@ public class PostfixTemplateProviderTestGenerated extends AbstractPostfixTemplat String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/notString.kt"); doTest(fileName); } + + @TestMetadata("notnull.kt") + public void testNotnull() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/notnull.kt"); + doTest(fileName); + } + + @TestMetadata("null.kt") + public void testNull() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/null.kt"); + doTest(fileName); + } + + @TestMetadata("try.kt") + public void testTry() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/try.kt"); + doTest(fileName); + } + + @TestMetadata("when.kt") + public void testWhen() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/when.kt"); + doTest(fileName); + } }