Create From Usage: Place local variable declaration right before its usage

#KT-6704 Fixed
This commit is contained in:
Alexey Sedunov
2015-03-11 17:09:50 +03:00
parent 8a0808b133
commit fe9b08f93d
4 changed files with 27 additions and 5 deletions
@@ -56,9 +56,7 @@ import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl
import java.util.LinkedHashMap
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createClass.ClassKind
import org.jetbrains.kotlin.utils.addToStdlib.singletonOrEmptyList
import org.jetbrains.kotlin.psi.psiUtil.siblings
import com.intellij.openapi.editor.ScrollType
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor
import com.intellij.psi.PsiClass
import com.intellij.codeInsight.daemon.impl.quickfix.CreateFromUsageUtils
@@ -81,6 +79,7 @@ import com.intellij.openapi.ui.*
import com.intellij.codeInsight.template.impl.*
import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils
import org.jetbrains.kotlin.idea.util.application.*
import org.jetbrains.kotlin.psi.psiUtil.*
private val TYPE_PARAMETER_LIST_VARIABLE_NAME = "typeParameterList"
private val TEMPLATE_FROM_USAGE_FUNCTION_BODY = "New Kotlin Function Body.kt"
@@ -559,10 +558,10 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
parent.addBefore(newLine, containingElement)
parent.addAfter(newLine, containingElement)
}
containingElement.addBefore(declaration, containingElement.getFirstChild()!!) as JetNamedDeclaration
}
else containingElement.addAfter(declaration, containingElement.getLBrace()!!) as JetNamedDeclaration
val sibling = config.originalElement.parents().first { it.getParent() == containingElement }
containingElement.addBefore(declaration, sibling) as JetNamedDeclaration
}
else -> throw AssertionError("Invalid containing element: ${containingElement.getText()}")
@@ -0,0 +1,9 @@
// "Create local variable 'foo'" "true"
fun test(n: Int) {
val i = 1
test(i)
test(i + 1)
val foo = 0
test(foo)
}
@@ -0,0 +1,8 @@
// "Create local variable 'foo'" "true"
fun test(n: Int) {
val i = 1
test(i)
test(i + 1)
test(<caret>foo)
}
@@ -2309,6 +2309,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("beforePositionNextToUsage.kt")
public void testPositionNextToUsage() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/beforePositionNextToUsage.kt");
doTest(fileName);
}
@TestMetadata("beforeQualifiedInFun.kt")
public void testQualifiedInFun() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/beforeQualifiedInFun.kt");