Create Local Variable From Usage: Support lambdas
This commit is contained in:
+21
-3
@@ -57,6 +57,7 @@ import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.jet.lexer.JetTokens
|
||||
import org.jetbrains.jet.plugin.quickfix.createFromUsage.createFunction
|
||||
import org.jetbrains.jet.plugin.util.application.runWriteAction
|
||||
import org.jetbrains.jet.plugin.refactoring.isMultiLine
|
||||
|
||||
private val TYPE_PARAMETER_LIST_VARIABLE_NAME = "typeParameterList"
|
||||
private val TEMPLATE_FROM_USAGE_FUNCTION_BODY = "New Kotlin Function Body.kt"
|
||||
@@ -256,9 +257,15 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
|
||||
}
|
||||
val newLine = psiFactory.createNewLine()
|
||||
|
||||
fun prepend(element: PsiElement, elementBeforeStart: PsiElement): PsiElement {
|
||||
fun prepend(element: PsiElement, elementBeforeStart: PsiElement, skipInitial: Boolean): PsiElement {
|
||||
val parent = elementBeforeStart.getParent()!!
|
||||
val anchor = PsiTreeUtil.skipSiblingsForward(elementBeforeStart, javaClass<PsiWhiteSpace>())
|
||||
val anchor =
|
||||
if (!skipInitial && elementBeforeStart !is PsiWhiteSpace) {
|
||||
elementBeforeStart
|
||||
}
|
||||
else {
|
||||
PsiTreeUtil.skipSiblingsForward(elementBeforeStart, javaClass<PsiWhiteSpace>())
|
||||
}
|
||||
val addedElement = parent.addBefore(element, anchor)!!
|
||||
parent.addAfter(newLine, addedElement)
|
||||
parent.addAfter(newLine, addedElement)
|
||||
@@ -296,7 +303,18 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
|
||||
?: append(declaration, classBody!!.getLastChild()!!, false)) as JetCallableDeclaration
|
||||
}
|
||||
|
||||
is JetBlockExpression -> return prepend(declaration, containingElement.getLBrace()!!) as JetCallableDeclaration
|
||||
is JetBlockExpression -> {
|
||||
val parent = containingElement.getParent()
|
||||
if (parent is JetFunctionLiteral) {
|
||||
if (!parent.isMultiLine()) {
|
||||
parent.addBefore(newLine, containingElement)
|
||||
parent.addAfter(newLine, containingElement)
|
||||
}
|
||||
|
||||
return prepend(declaration, containingElement.getFirstChild()!!, false) as JetCallableDeclaration
|
||||
}
|
||||
return prepend(declaration, containingElement.getLBrace()!!, true) as JetCallableDeclaration
|
||||
}
|
||||
|
||||
else -> throw AssertionError("Invalid containing element: ${containingElement.getText()}")
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ACTION: Create parameter 'foo'
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
fun test(n: Int) {
|
||||
val f: () -> Int = {
|
||||
val foo: Int
|
||||
|
||||
foo
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ACTION: Create parameter 'foo'
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
fun test(n: Int) {
|
||||
val f: (Int, Int) -> Int = { (a, b) ->
|
||||
val foo: Int
|
||||
|
||||
foo
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ACTION: Create parameter 'foo'
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
fun test(n: Int) {
|
||||
val f: () -> Int = {
|
||||
val foo: Int
|
||||
|
||||
foo
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ACTION: Create parameter 'foo'
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
fun test(n: Int) {
|
||||
val f: (Int, Int) -> Int = { (a, b) ->
|
||||
val foo: Int
|
||||
|
||||
foo
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ACTION: Create parameter 'foo'
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
fun test(n: Int) {
|
||||
val f: () -> Int = { <caret>foo }
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ACTION: Create parameter 'foo'
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
fun test(n: Int) {
|
||||
val f: (Int, Int) -> Int = { (a, b) -> <caret>foo }
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ACTION: Create parameter 'foo'
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
fun test(n: Int) {
|
||||
val f: () -> Int = {
|
||||
<caret>foo
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Create local variable 'foo'" "true"
|
||||
// ACTION: Create parameter 'foo'
|
||||
// ERROR: Variable 'foo' must be initialized
|
||||
|
||||
fun test(n: Int) {
|
||||
val f: (Int, Int) -> Int = { (a, b) ->
|
||||
<caret>foo
|
||||
}
|
||||
}
|
||||
@@ -1209,6 +1209,30 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeInLambdaNoParams.kt")
|
||||
public void testInLambdaNoParams() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/beforeInLambdaNoParams.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeInLambdaWithParams.kt")
|
||||
public void testInLambdaWithParams() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/beforeInLambdaWithParams.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeInMultiLineLambdaNoParams.kt")
|
||||
public void testInMultiLineLambdaNoParams() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/beforeInMultiLineLambdaNoParams.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeInMultiLineLambdaWithParams.kt")
|
||||
public void testInMultiLineLambdaWithParams() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/beforeInMultiLineLambdaWithParams.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeInWhen.kt")
|
||||
public void testInWhen() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/beforeInWhen.kt");
|
||||
|
||||
Reference in New Issue
Block a user