[kotlin] extract function: keep selection range as narrow as possible

^KTIJ-29938 fixed

GitOrigin-RevId: 5302707448930515842bf6e17b6e2f10f5e92fb0
This commit is contained in:
Anna Kozlova
2024-05-14 08:00:45 +00:00
committed by intellij-monorepo-bot
parent 7f21671ba2
commit 730f0bdcab
4 changed files with 18 additions and 1 deletions
@@ -0,0 +1,8 @@
fun testFun(x: Any): Unit {
println(x)
extracted<caret>()
}
private fun extracted() {
println("that's something else")
}
@@ -0,0 +1,4 @@
fun testFun(x: Any): Unit {
println(x)
<selection>println("that's something else")</selection>
}
@@ -11,6 +11,7 @@ import com.intellij.openapi.keymap.KeymapUtil
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElement
import com.intellij.psi.SmartPointerManager
import com.intellij.psi.util.PsiTreeUtil
@@ -61,7 +62,7 @@ interface AbstractInplaceExtractionHelper<KotlinType,
val first = elements.first()
val callTextRange =
editor.document.createRangeMarker(
max(rangeOf(PsiTreeUtil.skipWhitespacesAndCommentsBackward(first) ?: first).startOffset - 1, 0),
max(rangeOf(PsiTreeUtil.skipSiblingsBackward(first, PsiComment::class.java) ?: first).startOffset - 1, 0),
rangeOf(elements.last()).endOffset
).apply {
isGreedyToLeft = true
@@ -50,6 +50,10 @@ class FirInplaceExtractFunctionTest : KotlinLightCodeInsightFixtureTestCase() {
doTest()
}
fun testDontSkipWhitespacesBefore() {
doTest()
}
fun testStringTemplateWithNameConflict() {
doTest(changedName = "substring")
}