Introduce var/val postfix templates

#KT-4710 In Progress
This commit is contained in:
Denis Zharkov
2016-07-11 11:17:58 +03:00
committed by Nikolay Krasko
parent 7ba914f92a
commit b702886f0d
9 changed files with 60 additions and 1 deletions
@@ -0,0 +1,4 @@
fun bar() = 1
fun foo() {
val bar = bar()
}
@@ -0,0 +1,4 @@
fun bar() = 1
fun foo() {
<spot>bar()</spot>$key
}
@@ -0,0 +1,5 @@
<html>
<body>
Creates variable from given expression.
</body>
</html>
@@ -24,6 +24,7 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.intentions.negate
import org.jetbrains.kotlin.idea.refactoring.introduce.introduceVariable.KotlinIntroduceVariableHandler
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
@@ -41,7 +42,9 @@ class KtPostfixTemplateProvider : PostfixTemplateProvider {
KtNotNullPostfixTemplate("nn"),
KtIsNullPostfixTemplate,
KtWhenExpressionPostfixTemplate,
KtTryPostfixTemplate
KtTryPostfixTemplate,
KtIntroduceVariablePostfixTemplate("val"),
KtIntroduceVariablePostfixTemplate("var")
)
override fun isTerminalSymbol(currentChar: Char) = currentChar == '.' || currentChar == '!'
@@ -60,6 +63,19 @@ private object KtNotPostfixTemplate : NotPostfixTemplate(
createExpressionSelector { it.isBoolean() }
)
private class KtIntroduceVariablePostfixTemplate(
val kind: String
) : PostfixTemplateWithExpressionSelector(kind, "$kind name = expression", createExpressionSelector()) {
override fun expandForChooseExpression(expression: PsiElement, editor: Editor) {
KotlinIntroduceVariableHandler.doRefactoring(
expression.project, editor, expression as KtExpression,
isVar = kind == "var",
occurrencesToReplace = null,
onNonInteractiveFinish = null
)
}
}
internal object KtPostfixTemplatePsiInfo : PostfixTemplatePsiInfo() {
override fun createExpression(context: PsiElement, prefix: String, suffix: String) =
KtPsiFactory(context.project).createExpression(prefix + context.text + suffix)
+4
View File
@@ -0,0 +1,4 @@
fun bar() = 1
fun foo() {
val x = bar().val<caret>
}
+5
View File
@@ -0,0 +1,5 @@
fun bar() = 1
fun foo() {
val <caret>bar = bar()
val x = bar
}
+4
View File
@@ -0,0 +1,4 @@
fun bar() = 1
fun foo() {
val x = bar().var<caret>
}
+5
View File
@@ -0,0 +1,5 @@
fun bar() = 1
fun foo() {
var bar = bar()
val x = bar
}
@@ -77,6 +77,18 @@ public class PostfixTemplateProviderTestGenerated extends AbstractPostfixTemplat
doTest(fileName);
}
@TestMetadata("val.kt")
public void testVal() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/val.kt");
doTest(fileName);
}
@TestMetadata("var.kt")
public void testVar() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/var.kt");
doTest(fileName);
}
@TestMetadata("when.kt")
public void testWhen() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/when.kt");