Implement if/else/null/notnull/when/try postfix templates
#KT-4710 In Progress
This commit is contained in:
committed by
Nikolay Krasko
parent
09266b222b
commit
4d28199cc2
@@ -0,0 +1,5 @@
|
||||
fun foo(x: Boolean) {
|
||||
if (!x) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Boolean) {
|
||||
<spot>x</spot>$key
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Checks boolean expression to be false.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo(x: Boolean) {
|
||||
if (x) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Boolean) {
|
||||
<spot>x</spot>$key
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Checks boolean expression to be true.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo(x: Any?) {
|
||||
if (x == null) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Any?) {
|
||||
<spot>x</spot>$key
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Checks expression to be null.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo(x: Any?) {
|
||||
if (x != null) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Any?) {
|
||||
<spot>x</spot>$key
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Checks expression to be not null.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo(x: Any?) {
|
||||
try {
|
||||
x
|
||||
} catch (e: Exception) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Any?) {
|
||||
<spot>x</spot>$key
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Surrounds expression with try-catch block.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo(x: String) {
|
||||
when (x) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: String) {
|
||||
<spot>x</spot>$key
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Wrap expression with 'when' statement
|
||||
</body>
|
||||
</html>
|
||||
+11
-2
@@ -33,7 +33,16 @@ import org.jetbrains.kotlin.types.typeUtil.isBoolean
|
||||
|
||||
|
||||
class KtPostfixTemplateProvider : PostfixTemplateProvider {
|
||||
override fun getTemplates() = setOf<PostfixTemplate>(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)
|
||||
|
||||
|
||||
+73
@@ -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()
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Boolean) {
|
||||
x.else<caret>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo(x: Boolean) {
|
||||
if (!x) {
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Boolean) {
|
||||
x.if<caret>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo(x: Boolean) {
|
||||
if (x) {
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Any?) {
|
||||
x.notnull<caret>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo(x: Any?) {
|
||||
if (x != null) {
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Any?) {
|
||||
x.null<caret>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo(x: Any?) {
|
||||
if (x == null) {
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Boolean) {
|
||||
x.try<caret>
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo(x: Boolean) {
|
||||
try {
|
||||
x
|
||||
} catch(e: <caret>Exception) {
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: String) {
|
||||
x.when<caret>
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun foo(x: String) {
|
||||
when (x) {
|
||||
<caret> -> {
|
||||
}
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
+36
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user