From 69c315443a584d315001ecc59dd3b3a643ae2a48 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Thu, 1 Jun 2017 15:58:00 +0300 Subject: [PATCH] Rename: Disable label rename outside of reference range #KT-17128 Fixed --- ...tractReferenceSubstitutionRenameHandler.kt | 10 +++++--- .../after/test.kt | 5 ++++ .../before/test.kt | 5 ++++ .../labeledLambdaByLabelRefAfterLabel.test | 5 ++++ .../after/test.kt | 5 ++++ .../before/test.kt | 5 ++++ .../labeledLambdaByLabelRefBeforeLabel.test | 5 ++++ .../after/test.kt | 7 ++++++ .../before/test.kt | 7 ++++++ .../labeledReturnInLambdaAfterLabel.test | 5 ++++ .../after/test.kt | 7 ++++++ .../before/test.kt | 7 ++++++ .../labeledReturnInLambdaBeforeLabel.test | 5 ++++ .../refactoring/rename/AbstractRenameTest.kt | 2 +- .../rename/RenameTestGenerated.java | 24 +++++++++++++++++++ 15 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 idea/testData/refactoring/rename/labeledLambdaByLabelRefAfterLabel/after/test.kt create mode 100644 idea/testData/refactoring/rename/labeledLambdaByLabelRefAfterLabel/before/test.kt create mode 100644 idea/testData/refactoring/rename/labeledLambdaByLabelRefAfterLabel/labeledLambdaByLabelRefAfterLabel.test create mode 100644 idea/testData/refactoring/rename/labeledLambdaByLabelRefBeforeLabel/after/test.kt create mode 100644 idea/testData/refactoring/rename/labeledLambdaByLabelRefBeforeLabel/before/test.kt create mode 100644 idea/testData/refactoring/rename/labeledLambdaByLabelRefBeforeLabel/labeledLambdaByLabelRefBeforeLabel.test create mode 100644 idea/testData/refactoring/rename/labeledReturnInLambdaAfterLabel/after/test.kt create mode 100644 idea/testData/refactoring/rename/labeledReturnInLambdaAfterLabel/before/test.kt create mode 100644 idea/testData/refactoring/rename/labeledReturnInLambdaAfterLabel/labeledReturnInLambdaAfterLabel.test create mode 100644 idea/testData/refactoring/rename/labeledReturnInLambdaBeforeLabel/after/test.kt create mode 100644 idea/testData/refactoring/rename/labeledReturnInLambdaBeforeLabel/before/test.kt create mode 100644 idea/testData/refactoring/rename/labeledReturnInLambdaBeforeLabel/labeledReturnInLambdaBeforeLabel.test diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/AbstractReferenceSubstitutionRenameHandler.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/AbstractReferenceSubstitutionRenameHandler.kt index f4cd44583a3..b3c4467c84f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/AbstractReferenceSubstitutionRenameHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/AbstractReferenceSubstitutionRenameHandler.kt @@ -28,8 +28,10 @@ import com.intellij.refactoring.rename.PsiElementRenameHandler import com.intellij.refactoring.rename.RenameHandler import com.intellij.refactoring.rename.inplace.MemberInplaceRenameHandler import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils +import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtSimpleNameExpression +import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType abstract class AbstractReferenceSubstitutionRenameHandler( @@ -37,11 +39,13 @@ abstract class AbstractReferenceSubstitutionRenameHandler( ) : PsiElementRenameHandler() { companion object { fun getReferenceExpression(file: PsiFile, offset: Int): KtSimpleNameExpression? { - var elementAtCaret = file.findElementAt(offset) + var elementAtCaret = file.findElementAt(offset) ?: return null + if (elementAtCaret.node?.elementType == KtTokens.AT) return null if (elementAtCaret is PsiWhiteSpace) { - elementAtCaret = CodeInsightUtils.getElementAtOffsetIgnoreWhitespaceAfter(file, offset) + elementAtCaret = CodeInsightUtils.getElementAtOffsetIgnoreWhitespaceAfter(file, offset) ?: return null + if (offset != elementAtCaret.endOffset) return null } - return elementAtCaret?.getNonStrictParentOfType() + return elementAtCaret.getNonStrictParentOfType() } fun getReferenceExpression(dataContext: DataContext): KtSimpleNameExpression? { diff --git a/idea/testData/refactoring/rename/labeledLambdaByLabelRefAfterLabel/after/test.kt b/idea/testData/refactoring/rename/labeledLambdaByLabelRefAfterLabel/after/test.kt new file mode 100644 index 00000000000..904c31aac3f --- /dev/null +++ b/idea/testData/refactoring/rename/labeledLambdaByLabelRefAfterLabel/after/test.kt @@ -0,0 +1,5 @@ +fun foo(f: () -> R) = f() + +fun test() { + foo bar@ { return@bar false } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/labeledLambdaByLabelRefAfterLabel/before/test.kt b/idea/testData/refactoring/rename/labeledLambdaByLabelRefAfterLabel/before/test.kt new file mode 100644 index 00000000000..116fb9f29a6 --- /dev/null +++ b/idea/testData/refactoring/rename/labeledLambdaByLabelRefAfterLabel/before/test.kt @@ -0,0 +1,5 @@ +fun foo(f: () -> R) = f() + +fun test() { + foo bar@ { return@bar /*rename*/ false } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/labeledLambdaByLabelRefAfterLabel/labeledLambdaByLabelRefAfterLabel.test b/idea/testData/refactoring/rename/labeledLambdaByLabelRefAfterLabel/labeledLambdaByLabelRefAfterLabel.test new file mode 100644 index 00000000000..dae046abfe1 --- /dev/null +++ b/idea/testData/refactoring/rename/labeledLambdaByLabelRefAfterLabel/labeledLambdaByLabelRefAfterLabel.test @@ -0,0 +1,5 @@ +{ + "type": "AUTO_DETECT", + "mainFile": "test.kt", + "newName": "baz" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/labeledLambdaByLabelRefBeforeLabel/after/test.kt b/idea/testData/refactoring/rename/labeledLambdaByLabelRefBeforeLabel/after/test.kt new file mode 100644 index 00000000000..40889fd0bd0 --- /dev/null +++ b/idea/testData/refactoring/rename/labeledLambdaByLabelRefBeforeLabel/after/test.kt @@ -0,0 +1,5 @@ +fun foo(f: () -> R) = f() + +fun test() { + foo bar@ { return@bar false } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/labeledLambdaByLabelRefBeforeLabel/before/test.kt b/idea/testData/refactoring/rename/labeledLambdaByLabelRefBeforeLabel/before/test.kt new file mode 100644 index 00000000000..936cb0089b9 --- /dev/null +++ b/idea/testData/refactoring/rename/labeledLambdaByLabelRefBeforeLabel/before/test.kt @@ -0,0 +1,5 @@ +fun foo(f: () -> R) = f() + +fun test() { + foo bar@ { return/*rename*/@bar false } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/labeledLambdaByLabelRefBeforeLabel/labeledLambdaByLabelRefBeforeLabel.test b/idea/testData/refactoring/rename/labeledLambdaByLabelRefBeforeLabel/labeledLambdaByLabelRefBeforeLabel.test new file mode 100644 index 00000000000..dae046abfe1 --- /dev/null +++ b/idea/testData/refactoring/rename/labeledLambdaByLabelRefBeforeLabel/labeledLambdaByLabelRefBeforeLabel.test @@ -0,0 +1,5 @@ +{ + "type": "AUTO_DETECT", + "mainFile": "test.kt", + "newName": "baz" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/labeledReturnInLambdaAfterLabel/after/test.kt b/idea/testData/refactoring/rename/labeledReturnInLambdaAfterLabel/after/test.kt new file mode 100644 index 00000000000..4b354fe3c6b --- /dev/null +++ b/idea/testData/refactoring/rename/labeledReturnInLambdaAfterLabel/after/test.kt @@ -0,0 +1,7 @@ +fun foo(f: () -> R) = f() + +fun test() { + foo { + return@foo false + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/labeledReturnInLambdaAfterLabel/before/test.kt b/idea/testData/refactoring/rename/labeledReturnInLambdaAfterLabel/before/test.kt new file mode 100644 index 00000000000..728190ed651 --- /dev/null +++ b/idea/testData/refactoring/rename/labeledReturnInLambdaAfterLabel/before/test.kt @@ -0,0 +1,7 @@ +fun foo(f: () -> R) = f() + +fun test() { + foo { + return/*rename*/@foo false + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/labeledReturnInLambdaAfterLabel/labeledReturnInLambdaAfterLabel.test b/idea/testData/refactoring/rename/labeledReturnInLambdaAfterLabel/labeledReturnInLambdaAfterLabel.test new file mode 100644 index 00000000000..29ce6c16b62 --- /dev/null +++ b/idea/testData/refactoring/rename/labeledReturnInLambdaAfterLabel/labeledReturnInLambdaAfterLabel.test @@ -0,0 +1,5 @@ +{ + "type": "AUTO_DETECT", + "mainFile": "test.kt", + "newName": "bar" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/labeledReturnInLambdaBeforeLabel/after/test.kt b/idea/testData/refactoring/rename/labeledReturnInLambdaBeforeLabel/after/test.kt new file mode 100644 index 00000000000..4b354fe3c6b --- /dev/null +++ b/idea/testData/refactoring/rename/labeledReturnInLambdaBeforeLabel/after/test.kt @@ -0,0 +1,7 @@ +fun foo(f: () -> R) = f() + +fun test() { + foo { + return@foo false + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/labeledReturnInLambdaBeforeLabel/before/test.kt b/idea/testData/refactoring/rename/labeledReturnInLambdaBeforeLabel/before/test.kt new file mode 100644 index 00000000000..728190ed651 --- /dev/null +++ b/idea/testData/refactoring/rename/labeledReturnInLambdaBeforeLabel/before/test.kt @@ -0,0 +1,7 @@ +fun foo(f: () -> R) = f() + +fun test() { + foo { + return/*rename*/@foo false + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/labeledReturnInLambdaBeforeLabel/labeledReturnInLambdaBeforeLabel.test b/idea/testData/refactoring/rename/labeledReturnInLambdaBeforeLabel/labeledReturnInLambdaBeforeLabel.test new file mode 100644 index 00000000000..29ce6c16b62 --- /dev/null +++ b/idea/testData/refactoring/rename/labeledReturnInLambdaBeforeLabel/labeledReturnInLambdaBeforeLabel.test @@ -0,0 +1,5 @@ +{ + "type": "AUTO_DETECT", + "mainFile": "test.kt", + "newName": "bar" +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/AbstractRenameTest.kt b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/AbstractRenameTest.kt index 87e4e9538ce..2887d6ca2d5 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/AbstractRenameTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/AbstractRenameTest.kt @@ -349,7 +349,7 @@ abstract class AbstractRenameTest : KotlinLightCodeInsightFixtureTestCase() { else -> null } } - val handler = RenameHandlerRegistry.getInstance().getRenameHandler(dataContext)!! + val handler = RenameHandlerRegistry.getInstance().getRenameHandler(dataContext) ?: return@doTestCommittingDocuments Assert.assertTrue(handler.isAvailableOnDataContext(dataContext)) handler.invoke(project, editor, psiFile, dataContext) } diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java index 74556332789..572f5af5b1b 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java @@ -240,6 +240,18 @@ public class RenameTestGenerated extends AbstractRenameTest { doTest(fileName); } + @TestMetadata("labeledLambdaByLabelRefAfterLabel/labeledLambdaByLabelRefAfterLabel.test") + public void testLabeledLambdaByLabelRefAfterLabel_LabeledLambdaByLabelRefAfterLabel() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/labeledLambdaByLabelRefAfterLabel/labeledLambdaByLabelRefAfterLabel.test"); + doTest(fileName); + } + + @TestMetadata("labeledLambdaByLabelRefBeforeLabel/labeledLambdaByLabelRefBeforeLabel.test") + public void testLabeledLambdaByLabelRefBeforeLabel_LabeledLambdaByLabelRefBeforeLabel() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/labeledLambdaByLabelRefBeforeLabel/labeledLambdaByLabelRefBeforeLabel.test"); + doTest(fileName); + } + @TestMetadata("labeledLambdaByLabelWithParens/labeledLambdaByLabelWithParens.test") public void testLabeledLambdaByLabelWithParens_LabeledLambdaByLabelWithParens() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/labeledLambdaByLabelWithParens/labeledLambdaByLabelWithParens.test"); @@ -294,6 +306,18 @@ public class RenameTestGenerated extends AbstractRenameTest { doTest(fileName); } + @TestMetadata("labeledReturnInLambdaAfterLabel/labeledReturnInLambdaAfterLabel.test") + public void testLabeledReturnInLambdaAfterLabel_LabeledReturnInLambdaAfterLabel() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/labeledReturnInLambdaAfterLabel/labeledReturnInLambdaAfterLabel.test"); + doTest(fileName); + } + + @TestMetadata("labeledReturnInLambdaBeforeLabel/labeledReturnInLambdaBeforeLabel.test") + public void testLabeledReturnInLambdaBeforeLabel_LabeledReturnInLambdaBeforeLabel() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/labeledReturnInLambdaBeforeLabel/labeledReturnInLambdaBeforeLabel.test"); + doTest(fileName); + } + @TestMetadata("lambdaParameterRedeclaration/lambdaParameterRedeclaration.test") public void testLambdaParameterRedeclaration_LambdaParameterRedeclaration() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/lambdaParameterRedeclaration/lambdaParameterRedeclaration.test");