Merge pull request #2773 from t-kameyama/KT-28607

KT-28607 Extract/Introduce variable fails if caret is just after expression
This commit is contained in:
igoriakovlev
2019-11-12 19:13:56 +03:00
committed by GitHub
7 changed files with 99 additions and 1 deletions
@@ -22,6 +22,7 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.ui.popup.JBPopupAdapter
import com.intellij.openapi.ui.popup.JBPopupFactory
import com.intellij.openapi.ui.popup.LightweightWindowEvent
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiWhiteSpace
@@ -31,10 +32,13 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.util.CodeInsightUtils
import org.jetbrains.kotlin.idea.refactoring.introduce.findExpressionOrStringFragment
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespaceAndComments
import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypeAndBranch
import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespaceAndComments
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import java.awt.Component
import java.util.*
@@ -103,7 +107,11 @@ fun getSmartSelectSuggestions(
var element: PsiElement? = file.findElementAt(offset) ?: return emptyList()
if (element is PsiWhiteSpace) return getSmartSelectSuggestions(file, offset - 1, elementKind)
if (element is PsiWhiteSpace
|| element?.node?.elementType == KtTokens.RPAR
|| element is PsiComment
|| element?.getStrictParentOfType<KDoc>() != null
) return getSmartSelectSuggestions(file, offset - 1, elementKind)
val elements = ArrayList<KtElement>()
while (element != null && !(element is KtBlockExpression && element.parent !is KtFunctionLiteral) &&
+13
View File
@@ -0,0 +1,13 @@
class A {
val list = listOf(1, 2, 3)
}
fun foo(a: A) {
for (x in a.list<caret>/* comment */) {
println(x)
}
}
/*
a.list
*/
+13
View File
@@ -0,0 +1,13 @@
class A {
val list = listOf(1, 2, 3)
}
fun foo(a: A) {
for (x in a.list<caret>/** kdoc **/) {
println(x)
}
}
/*
a.list
*/
+13
View File
@@ -0,0 +1,13 @@
class A {
val list = listOf(1, 2, 3)
}
fun foo(a: A) {
for (x in a.list<caret>) {
println(x)
}
}
/*
a.list
*/
+13
View File
@@ -0,0 +1,13 @@
class A {
val list = listOf(1, 2, 3)
}
fun foo(a: A) {
for (x in a.list /*comment*/<caret>) {
println(x)
}
}
/*
a.list
*/
+13
View File
@@ -0,0 +1,13 @@
class A {
val list = listOf(1, 2, 3)
}
fun foo(a: A) {
for (x in a.list /** kdoc **/<caret>) {
println(x)
}
}
/*
a.list
*/
@@ -28,6 +28,31 @@ public class SmartSelectionTestGenerated extends AbstractSmartSelectionTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/smartSelection"), Pattern.compile("^([^.]+)\\.kt$"), true);
}
@TestMetadata("beforeComment.kt")
public void testBeforeComment() throws Exception {
runTest("idea/testData/smartSelection/beforeComment.kt");
}
@TestMetadata("beforeKDocComment.kt")
public void testBeforeKDocComment() throws Exception {
runTest("idea/testData/smartSelection/beforeKDocComment.kt");
}
@TestMetadata("beforeRightParenthesis.kt")
public void testBeforeRightParenthesis() throws Exception {
runTest("idea/testData/smartSelection/beforeRightParenthesis.kt");
}
@TestMetadata("beforeRightParenthesis2.kt")
public void testBeforeRightParenthesis2() throws Exception {
runTest("idea/testData/smartSelection/beforeRightParenthesis2.kt");
}
@TestMetadata("beforeRightParenthesis3.kt")
public void testBeforeRightParenthesis3() throws Exception {
runTest("idea/testData/smartSelection/beforeRightParenthesis3.kt");
}
@TestMetadata("commentsAndExtraSpaces.kt")
public void testCommentsAndExtraSpaces() throws Exception {
runTest("idea/testData/smartSelection/commentsAndExtraSpaces.kt");