From d04f88ff66086f109225abfa0be85afc8378d939 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Fri, 8 Nov 2019 13:25:54 +0900 Subject: [PATCH] Extract/Introduce variable: fix to work correctly if caret is before right parenthesis or comment #KT-28607 Fixed --- .../idea/refactoring/elementSelectionUtils.kt | 10 +++++++- idea/testData/smartSelection/beforeComment.kt | 13 ++++++++++ .../smartSelection/beforeKDocComment.kt | 13 ++++++++++ .../smartSelection/beforeRightParenthesis.kt | 13 ++++++++++ .../smartSelection/beforeRightParenthesis2.kt | 13 ++++++++++ .../smartSelection/beforeRightParenthesis3.kt | 13 ++++++++++ .../idea/SmartSelectionTestGenerated.java | 25 +++++++++++++++++++ 7 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 idea/testData/smartSelection/beforeComment.kt create mode 100644 idea/testData/smartSelection/beforeKDocComment.kt create mode 100644 idea/testData/smartSelection/beforeRightParenthesis.kt create mode 100644 idea/testData/smartSelection/beforeRightParenthesis2.kt create mode 100644 idea/testData/smartSelection/beforeRightParenthesis3.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/elementSelectionUtils.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/elementSelectionUtils.kt index 77cce999e49..1e75a8a2a32 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/elementSelectionUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/elementSelectionUtils.kt @@ -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() != null + ) return getSmartSelectSuggestions(file, offset - 1, elementKind) val elements = ArrayList() while (element != null && !(element is KtBlockExpression && element.parent !is KtFunctionLiteral) && diff --git a/idea/testData/smartSelection/beforeComment.kt b/idea/testData/smartSelection/beforeComment.kt new file mode 100644 index 00000000000..7456aa8fb1c --- /dev/null +++ b/idea/testData/smartSelection/beforeComment.kt @@ -0,0 +1,13 @@ +class A { + val list = listOf(1, 2, 3) +} + +fun foo(a: A) { + for (x in a.list/* comment */) { + println(x) + } +} + +/* +a.list +*/ \ No newline at end of file diff --git a/idea/testData/smartSelection/beforeKDocComment.kt b/idea/testData/smartSelection/beforeKDocComment.kt new file mode 100644 index 00000000000..240701931f3 --- /dev/null +++ b/idea/testData/smartSelection/beforeKDocComment.kt @@ -0,0 +1,13 @@ +class A { + val list = listOf(1, 2, 3) +} + +fun foo(a: A) { + for (x in a.list/** kdoc **/) { + println(x) + } +} + +/* +a.list +*/ \ No newline at end of file diff --git a/idea/testData/smartSelection/beforeRightParenthesis.kt b/idea/testData/smartSelection/beforeRightParenthesis.kt new file mode 100644 index 00000000000..97672adfe95 --- /dev/null +++ b/idea/testData/smartSelection/beforeRightParenthesis.kt @@ -0,0 +1,13 @@ +class A { + val list = listOf(1, 2, 3) +} + +fun foo(a: A) { + for (x in a.list) { + println(x) + } +} + +/* +a.list +*/ \ No newline at end of file diff --git a/idea/testData/smartSelection/beforeRightParenthesis2.kt b/idea/testData/smartSelection/beforeRightParenthesis2.kt new file mode 100644 index 00000000000..b8bf63da508 --- /dev/null +++ b/idea/testData/smartSelection/beforeRightParenthesis2.kt @@ -0,0 +1,13 @@ +class A { + val list = listOf(1, 2, 3) +} + +fun foo(a: A) { + for (x in a.list /*comment*/) { + println(x) + } +} + +/* +a.list +*/ \ No newline at end of file diff --git a/idea/testData/smartSelection/beforeRightParenthesis3.kt b/idea/testData/smartSelection/beforeRightParenthesis3.kt new file mode 100644 index 00000000000..44c6195d335 --- /dev/null +++ b/idea/testData/smartSelection/beforeRightParenthesis3.kt @@ -0,0 +1,13 @@ +class A { + val list = listOf(1, 2, 3) +} + +fun foo(a: A) { + for (x in a.list /** kdoc **/) { + println(x) + } +} + +/* +a.list +*/ \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/SmartSelectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/SmartSelectionTestGenerated.java index 7eec81b722d..f3d513dcf5e 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/SmartSelectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/SmartSelectionTestGenerated.java @@ -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");