MoveVariableDeclarationIntoWhenInspection should move caret to subject expression

#KT-31954 Fixed
This commit is contained in:
Dmitry Gridin
2019-06-13 19:19:37 +07:00
parent a9057e914b
commit c155bf680f
6 changed files with 11 additions and 6 deletions
@@ -15,6 +15,7 @@ import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElementVisitor import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiWhiteSpace import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.SmartPsiElementPointer import com.intellij.psi.SmartPsiElementPointer
import org.jetbrains.kotlin.idea.core.moveCaret
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isOneLiner import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isOneLiner
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.countUsages import org.jetbrains.kotlin.idea.intentions.loopToCallChain.countUsages
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.previousStatement import org.jetbrains.kotlin.idea.intentions.loopToCallChain.previousStatement
@@ -22,6 +23,7 @@ import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
import org.jetbrains.kotlin.psi.psiUtil.siblings import org.jetbrains.kotlin.psi.psiUtil.siblings
import org.jetbrains.kotlin.psi.psiUtil.startOffset
class MoveVariableDeclarationIntoWhenInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool { class MoveVariableDeclarationIntoWhenInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor = override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor =
@@ -115,7 +117,10 @@ private class VariableDeclarationIntoWhenFix(
lastChild.delete() lastChild.delete()
} }
subjectExpression.replace(newElement) val resultElement = subjectExpression.replace(newElement)
property.delete() property.delete()
val editor = resultElement.findExistingEditor() ?: return
editor.moveCaret((resultElement as? KtProperty)?.nameIdentifier?.startOffset ?: resultElement.startOffset)
} }
} }
@@ -1,7 +1,7 @@
fun test() = true fun test() = true
fun foo(): Int { fun foo(): Int {
return when (test()) { return when (<caret>test()) {
true -> 42 true -> 42
else -> null else -> null
} ?: 55 } ?: 55
@@ -1,7 +1,7 @@
fun test() = 42 fun test() = 42
fun foo() { fun foo() {
val b = when (val a = test()) { val b = when (val <caret>a = test()) {
1 -> a 1 -> a
else -> 24 else -> 24
} }
@@ -1,7 +1,7 @@
fun test() = true fun test() = true
fun foo() { fun foo() {
val b = !when (test()) { val b = !when (<caret>test()) {
true -> true true -> true
else -> false else -> false
} }
@@ -1,7 +1,7 @@
fun test() = 42 fun test() = 42
fun foo() { fun foo() {
when (val a = test()) { when (val <caret>a = test()) {
1 -> a 1 -> a
else -> 24 else -> 24
} }
@@ -1,7 +1,7 @@
fun foo() { fun foo() {
// comment // comment
when (1) { when (<caret>1) {
1 -> { 1 -> {
} }
else -> { else -> {