Create From Usage: Quick-fix for local variables
This commit is contained in:
@@ -185,6 +185,7 @@ map.platform.class.to.kotlin.advertisement=Choose an appropriate Kotlin class
|
||||
map.platform.class.to.kotlin.family=Change to Kotlin class
|
||||
create.from.usage.family=Create from usage
|
||||
create.function.from.usage=Create function ''{0}'' from usage
|
||||
create.local.variable.from.usage=Create local variable ''{0}''
|
||||
choose.target.class.or.trait.title=Choose target class or trait
|
||||
surround.with=Surround with
|
||||
surround.with.string.template="${expr}"
|
||||
|
||||
@@ -34,38 +34,45 @@ public class ConvertToBlockBodyAction : PsiElementBaseIntentionAction() {
|
||||
}
|
||||
|
||||
override fun invoke(project: Project, editor: Editor, element: PsiElement) {
|
||||
val declaration = findDeclaration(element)!!
|
||||
convert(findDeclaration(element)!!)
|
||||
}
|
||||
|
||||
fun convert(declaration: JetDeclarationWithBody): JetDeclarationWithBody {
|
||||
val body = declaration.getBodyExpression()!!
|
||||
|
||||
fun generateBody(returnsValue: Boolean): JetExpression {
|
||||
val bodyType = expressionType(body)
|
||||
val needReturn = returnsValue &&
|
||||
(bodyType == null || (!KotlinBuiltIns.getInstance().isUnit(bodyType) && !KotlinBuiltIns.getInstance().isNothing(bodyType)))
|
||||
(bodyType == null || (!KotlinBuiltIns.getInstance().isUnit(bodyType) && !KotlinBuiltIns.getInstance().isNothing(bodyType)))
|
||||
|
||||
val oldBodyText = body.getText()!!
|
||||
val newBodyText = if (needReturn) "return ${oldBodyText}" else oldBodyText
|
||||
return JetPsiFactory(declaration).createFunctionBody(newBodyText)
|
||||
}
|
||||
|
||||
if (declaration is JetNamedFunction) {
|
||||
val returnType = functionReturnType(declaration)!!
|
||||
if (!declaration.hasDeclaredReturnType() && !KotlinBuiltIns.getInstance().isUnit(returnType)) {
|
||||
specifyTypeExplicitly(declaration, returnType)
|
||||
val newBody = when (declaration) {
|
||||
is JetNamedFunction -> {
|
||||
val returnType = functionReturnType(declaration)!!
|
||||
if (!declaration.hasDeclaredReturnType() && !KotlinBuiltIns.getInstance().isUnit(returnType)) {
|
||||
specifyTypeExplicitly(declaration, returnType)
|
||||
}
|
||||
|
||||
val newBody = generateBody(!KotlinBuiltIns.getInstance().isUnit(returnType) && !KotlinBuiltIns.getInstance().isNothing(returnType))
|
||||
|
||||
declaration.getEqualsToken()!!.delete()
|
||||
body.replace(newBody)
|
||||
}
|
||||
|
||||
val newBody = generateBody(!KotlinBuiltIns.getInstance().isUnit(returnType) && !KotlinBuiltIns.getInstance().isNothing(returnType))
|
||||
is JetPropertyAccessor -> {
|
||||
val newBody = generateBody(declaration.isGetter())
|
||||
declaration.getEqualsToken()!!.delete()
|
||||
body.replace(newBody)
|
||||
}
|
||||
|
||||
declaration.getEqualsToken()!!.delete()
|
||||
body.replace(newBody)
|
||||
}
|
||||
else if (declaration is JetPropertyAccessor) {
|
||||
val newBody = generateBody(declaration.isGetter())
|
||||
declaration.getEqualsToken()!!.delete()
|
||||
body.replace(newBody)
|
||||
}
|
||||
else {
|
||||
throw RuntimeException("Unknown declaration type: $declaration")
|
||||
else -> throw RuntimeException("Unknown declaration type: $declaration")
|
||||
}
|
||||
|
||||
return newBody.getParent() as JetDeclarationWithBody
|
||||
}
|
||||
|
||||
private fun findDeclaration(element: PsiElement): JetDeclarationWithBody? {
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.jetbrains.jet.plugin.quickfix;
|
||||
import org.jetbrains.jet.lang.psi.JetClass;
|
||||
import org.jetbrains.jet.plugin.codeInsight.ImplementMethodsHandler;
|
||||
import org.jetbrains.jet.plugin.quickfix.createFromUsage.createFunction.*;
|
||||
import org.jetbrains.jet.plugin.quickfix.createFromUsage.createVariable.*;
|
||||
|
||||
import static org.jetbrains.jet.lang.diagnostics.Errors.*;
|
||||
import static org.jetbrains.jet.lexer.JetTokens.*;
|
||||
@@ -237,6 +238,8 @@ public class QuickFixRegistrar {
|
||||
QuickFixes.factories.put(NO_VALUE_FOR_PARAMETER, CreateFunctionFromCallActionFactory.INSTANCE$);
|
||||
QuickFixes.factories.put(TOO_MANY_ARGUMENTS, CreateFunctionFromCallActionFactory.INSTANCE$);
|
||||
|
||||
QuickFixes.factories.put(UNRESOLVED_REFERENCE, CreateLocalVariableActionFactory.INSTANCE$);
|
||||
|
||||
QuickFixes.factories.put(FUNCTION_EXPECTED, CreateInvokeFunctionActionFactory.INSTANCE$);
|
||||
|
||||
QuickFixes.factories.put(TYPE_MISMATCH, new QuickFixFactoryForTypeMismatchError());
|
||||
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package org.jetbrains.jet.plugin.quickfix.createFromUsage.createVariable
|
||||
|
||||
import org.jetbrains.jet.plugin.quickfix.createFromUsage.CreateFromUsageFixBase
|
||||
import org.jetbrains.jet.plugin.JetBundle
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.jet.lang.psi.JetFile
|
||||
import org.jetbrains.jet.plugin.quickfix.JetSingleIntentionActionFactory
|
||||
import org.jetbrains.jet.lang.diagnostics.Diagnostic
|
||||
import com.intellij.codeInsight.intention.IntentionAction
|
||||
import org.jetbrains.jet.plugin.quickfix.QuickFixUtil
|
||||
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression
|
||||
import org.jetbrains.jet.lang.psi.psiUtil.getQualifiedElement
|
||||
import org.jetbrains.jet.lang.psi.JetBlockExpression
|
||||
import org.jetbrains.jet.plugin.quickfix.createFromUsage.callableBuilder.createPropertyInfo
|
||||
import org.jetbrains.jet.plugin.quickfix.createFromUsage.callableBuilder.TypeInfo
|
||||
import org.jetbrains.jet.lang.types.Variance
|
||||
import org.jetbrains.jet.plugin.quickfix.createFromUsage.callableBuilder.CallableBuilderConfiguration
|
||||
import org.jetbrains.jet.plugin.quickfix.createFromUsage.callableBuilder.createBuilder
|
||||
import org.jetbrains.jet.plugin.quickfix.createFromUsage.callableBuilder.CallablePlacement
|
||||
import com.intellij.openapi.command.CommandProcessor
|
||||
import org.jetbrains.jet.lang.psi.psiUtil.parents
|
||||
import org.jetbrains.jet.lang.psi.JetFunction
|
||||
import org.jetbrains.jet.lang.psi.JetPropertyAccessor
|
||||
import org.jetbrains.jet.lang.psi.JetElement
|
||||
import org.jetbrains.jet.plugin.intentions.ConvertToBlockBodyAction
|
||||
import org.jetbrains.jet.lang.psi.JetDeclarationWithBody
|
||||
|
||||
object CreateLocalVariableActionFactory: JetSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||
val refExpr = QuickFixUtil.getParentElementOfType(diagnostic, javaClass<JetSimpleNameExpression>()) ?: return null
|
||||
if (refExpr.getQualifiedElement() != refExpr) return null
|
||||
|
||||
val container = refExpr.parents(false)
|
||||
.filter { it is JetBlockExpression || it is JetDeclarationWithBody }
|
||||
.firstOrNull() as? JetElement ?: return null
|
||||
|
||||
val propertyInfo = createPropertyInfo(refExpr.getReferencedName(), TypeInfo.Empty, TypeInfo(refExpr, Variance.OUT_VARIANCE))
|
||||
|
||||
return object: CreateFromUsageFixBase(refExpr) {
|
||||
override fun getText(): String = JetBundle.message("create.local.variable.from.usage", propertyInfo.name)
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: JetFile?) {
|
||||
with (CallableBuilderConfiguration(propertyInfo, file!!, editor!!).createBuilder()) {
|
||||
val actualContainer = when (container) {
|
||||
is JetBlockExpression -> container
|
||||
else -> ConvertToBlockBodyAction().convert(container as JetDeclarationWithBody).getBodyExpression()!!
|
||||
}
|
||||
placement = CallablePlacement.NoReceiver(actualContainer)
|
||||
CommandProcessor.getInstance().executeCommand(project, { build() }, getText(), null)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
// "class com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFixBase" "false"
|
||||
// ACTION: Create local variable 'PrivateClass'
|
||||
// ERROR: Unresolved reference: PrivateClass
|
||||
|
||||
fun test() {
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ACTION: Create parameter 'foo'
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
class A {
|
||||
val t: Int get() {
|
||||
val foo: Int
|
||||
|
||||
return foo
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ACTION: Create parameter 'foo'
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
class A {
|
||||
val t: Int get() {
|
||||
val foo: Int
|
||||
|
||||
return foo
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ACTION: Create parameter 'foo'
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
fun test(): Int {
|
||||
val foo: Int
|
||||
|
||||
return foo
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ACTION: Create parameter 'foo'
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
fun test(): Int {
|
||||
val foo: Int
|
||||
|
||||
return foo
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
fun test(n: Int): Int {
|
||||
return when (n) {
|
||||
1 -> {
|
||||
val foo: Int
|
||||
|
||||
foo
|
||||
}
|
||||
else -> {
|
||||
n + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ACTION: Create parameter 'foo'
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
class A {
|
||||
val t: Int get() {
|
||||
return <caret>foo
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ACTION: Create parameter 'foo'
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
class A {
|
||||
val t: Int get() = <caret>foo
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Create local variable 'foo'" "false"
|
||||
// ERROR: Unresolved reference: foo
|
||||
|
||||
class A {
|
||||
val t: Int = <caret>foo
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ACTION: Create parameter 'foo'
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
fun test(): Int {
|
||||
return <caret>foo
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ACTION: Create parameter 'foo'
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
fun test(): Int = <caret>foo
|
||||
@@ -0,0 +1,13 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
fun test(n: Int): Int {
|
||||
return when (n) {
|
||||
1 -> {
|
||||
<caret>foo
|
||||
}
|
||||
else -> {
|
||||
n + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// "Create local variable 'foo'" "false"
|
||||
// ERROR: Unresolved reference: foo
|
||||
|
||||
val t: Int = <caret>foo
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Create local variable 'foo'" "false"
|
||||
// ACTION: Split property declaration
|
||||
// ERROR: Unresolved reference: foo
|
||||
|
||||
class A
|
||||
|
||||
fun test(a: A) {
|
||||
val t: Int = a.<caret>foo
|
||||
}
|
||||
@@ -218,7 +218,7 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/createFromUsage")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@InnerTestClasses({CreateFromUsage.CreateFunction.class})
|
||||
@InnerTestClasses({CreateFromUsage.CreateFunction.class, CreateFromUsage.CreateVariable.class})
|
||||
@RunWith(org.jetbrains.jet.JUnit3RunnerWithInners.class)
|
||||
public static class CreateFromUsage extends AbstractQuickFixMultiFileTest {
|
||||
public void testAllFilesPresentInCreateFromUsage() throws Exception {
|
||||
@@ -236,6 +236,17 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/createFromUsage/createVariable")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@InnerTestClasses({})
|
||||
@RunWith(org.jetbrains.jet.JUnit3RunnerWithInners.class)
|
||||
public static class CreateVariable extends AbstractQuickFixMultiFileTest {
|
||||
public void testAllFilesPresentInCreateVariable() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/createFromUsage/createVariable"), Pattern.compile("^(\\w+)\\.before\\.Main\\.kt$"), true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/modifiers")
|
||||
|
||||
@@ -618,7 +618,7 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/createFromUsage")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@InnerTestClasses({CreateFromUsage.CreateFunction.class})
|
||||
@InnerTestClasses({CreateFromUsage.CreateFunction.class, CreateFromUsage.CreateVariable.class})
|
||||
@RunWith(org.jetbrains.jet.JUnit3RunnerWithInners.class)
|
||||
public static class CreateFromUsage extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInCreateFromUsage() throws Exception {
|
||||
@@ -1162,6 +1162,75 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/createFromUsage/createVariable")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@InnerTestClasses({CreateVariable.LocalVariable.class})
|
||||
@RunWith(org.jetbrains.jet.JUnit3RunnerWithInners.class)
|
||||
public static class CreateVariable extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInCreateVariable() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/createFromUsage/createVariable"), Pattern.compile("^before(\\w+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(org.jetbrains.jet.JUnit3RunnerWithInners.class)
|
||||
public static class LocalVariable extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInLocalVariable() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/createFromUsage/createVariable/localVariable"), Pattern.compile("^before(\\w+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeInAccessor.kt")
|
||||
public void testInAccessor() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/beforeInAccessor.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeInAccessorWithExpressionBody.kt")
|
||||
public void testInAccessorWithExpressionBody() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/beforeInAccessorWithExpressionBody.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeInClass.kt")
|
||||
public void testInClass() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/beforeInClass.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeInFun.kt")
|
||||
public void testInFun() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/beforeInFun.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeInFunWithExpressionBody.kt")
|
||||
public void testInFunWithExpressionBody() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/beforeInFunWithExpressionBody.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeInWhen.kt")
|
||||
public void testInWhen() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/beforeInWhen.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeOnTopLevel.kt")
|
||||
public void testOnTopLevel() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/beforeOnTopLevel.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeQualifiedInFun.kt")
|
||||
public void testQualifiedInFun() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/beforeQualifiedInFun.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/expressions")
|
||||
|
||||
Reference in New Issue
Block a user