From d7e79e7f28bfb1ec7d131f7fbc2cb327af214558 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Wed, 2 Sep 2015 17:59:40 +0300 Subject: [PATCH] Introduce Variable: Forbid inside of type references and 'super' references #KT-8324 Fixed --- .../KotlinIntroduceVariableHandler.java | 7 +++ .../ConstructorDelegationCall.kt | 5 ++ .../ConstructorDelegationCall.kt.conflicts | 1 + .../DelegatorByExpressionInDelegate.kt | 7 +++ .../DelegatorByExpressionInDelegate.kt.after | 8 ++++ .../DelegatorByExpressionInType.kt | 7 +++ .../DelegatorByExpressionInType.kt.conflicts | 1 + .../DelegatorToSuperCallInArgument.kt | 5 ++ .../DelegatorToSuperCallInArgument.kt.after | 6 +++ .../DelegatorToSuperCallInType.kt | 3 ++ .../DelegatorToSuperCallInType.kt.conflicts | 1 + .../DelegatorToSuperClass.kt | 5 ++ .../DelegatorToSuperClass.kt.conflicts | 1 + .../introduceVariable/SuperReference.kt | 11 +++++ .../SuperReference.kt.conflicts | 1 + .../introduceVariable/ThisReference.kt | 9 ++++ .../introduceVariable/ThisReference.kt.after | 10 ++++ .../introduce/JetExtractionTestGenerated.java | 48 +++++++++++++++++++ 18 files changed, 136 insertions(+) create mode 100644 idea/testData/refactoring/introduceVariable/ConstructorDelegationCall.kt create mode 100644 idea/testData/refactoring/introduceVariable/ConstructorDelegationCall.kt.conflicts create mode 100644 idea/testData/refactoring/introduceVariable/DelegatorByExpressionInDelegate.kt create mode 100644 idea/testData/refactoring/introduceVariable/DelegatorByExpressionInDelegate.kt.after create mode 100644 idea/testData/refactoring/introduceVariable/DelegatorByExpressionInType.kt create mode 100644 idea/testData/refactoring/introduceVariable/DelegatorByExpressionInType.kt.conflicts create mode 100644 idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInArgument.kt create mode 100644 idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInArgument.kt.after create mode 100644 idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInType.kt create mode 100644 idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInType.kt.conflicts create mode 100644 idea/testData/refactoring/introduceVariable/DelegatorToSuperClass.kt create mode 100644 idea/testData/refactoring/introduceVariable/DelegatorToSuperClass.kt.conflicts create mode 100644 idea/testData/refactoring/introduceVariable/SuperReference.kt create mode 100644 idea/testData/refactoring/introduceVariable/SuperReference.kt.conflicts create mode 100644 idea/testData/refactoring/introduceVariable/ThisReference.kt create mode 100644 idea/testData/refactoring/introduceVariable/ThisReference.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.java b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.java index 44767692efa..74fd2e00b7c 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.java +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.java @@ -130,6 +130,13 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase { } } + //noinspection unchecked + if (PsiTreeUtil.getNonStrictParentOfType(expression, + JetTypeReference.class, JetConstructorCalleeExpression.class, JetSuperExpression.class) != null) { + showErrorHint(project, editor, JetRefactoringBundle.message("cannot.refactor.no.container")); + return; + } + AnalysisResult analysisResult = ResolvePackage.analyzeAndGetResult(expression); final BindingContext bindingContext = analysisResult.getBindingContext(); final JetType expressionType = bindingContext.getType(expression); //can be null or error type diff --git a/idea/testData/refactoring/introduceVariable/ConstructorDelegationCall.kt b/idea/testData/refactoring/introduceVariable/ConstructorDelegationCall.kt new file mode 100644 index 00000000000..b2dc183f4da --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/ConstructorDelegationCall.kt @@ -0,0 +1,5 @@ +open class A(n: Int) + +class B { + constructor(x: Int) : A(x + 1) +} \ No newline at end of file diff --git a/idea/testData/refactoring/introduceVariable/ConstructorDelegationCall.kt.conflicts b/idea/testData/refactoring/introduceVariable/ConstructorDelegationCall.kt.conflicts new file mode 100644 index 00000000000..1568c6aea62 --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/ConstructorDelegationCall.kt.conflicts @@ -0,0 +1 @@ +Cannot refactor in this place diff --git a/idea/testData/refactoring/introduceVariable/DelegatorByExpressionInDelegate.kt b/idea/testData/refactoring/introduceVariable/DelegatorByExpressionInDelegate.kt new file mode 100644 index 00000000000..77b63a1bf99 --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/DelegatorByExpressionInDelegate.kt @@ -0,0 +1,7 @@ +interface T + +object O : T + +fun foo() { + val x = object : T by O {} +} \ No newline at end of file diff --git a/idea/testData/refactoring/introduceVariable/DelegatorByExpressionInDelegate.kt.after b/idea/testData/refactoring/introduceVariable/DelegatorByExpressionInDelegate.kt.after new file mode 100644 index 00000000000..3266675e4e9 --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/DelegatorByExpressionInDelegate.kt.after @@ -0,0 +1,8 @@ +interface T + +object O : T + +fun foo() { + val o = O + val x = object : T by o {} +} \ No newline at end of file diff --git a/idea/testData/refactoring/introduceVariable/DelegatorByExpressionInType.kt b/idea/testData/refactoring/introduceVariable/DelegatorByExpressionInType.kt new file mode 100644 index 00000000000..664c81a96b8 --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/DelegatorByExpressionInType.kt @@ -0,0 +1,7 @@ +interface T + +object O : T + +fun foo() { + val x = object : T by O {} +} \ No newline at end of file diff --git a/idea/testData/refactoring/introduceVariable/DelegatorByExpressionInType.kt.conflicts b/idea/testData/refactoring/introduceVariable/DelegatorByExpressionInType.kt.conflicts new file mode 100644 index 00000000000..1568c6aea62 --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/DelegatorByExpressionInType.kt.conflicts @@ -0,0 +1 @@ +Cannot refactor in this place diff --git a/idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInArgument.kt b/idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInArgument.kt new file mode 100644 index 00000000000..0ae3e56e57c --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInArgument.kt @@ -0,0 +1,5 @@ +open class A(n: Int) + +fun foo() { + val x = object : A(1) {} +} \ No newline at end of file diff --git a/idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInArgument.kt.after b/idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInArgument.kt.after new file mode 100644 index 00000000000..1f40cbc1b5a --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInArgument.kt.after @@ -0,0 +1,6 @@ +open class A(n: Int) + +fun foo() { + val i = 1 + val x = object : A(i) {} +} \ No newline at end of file diff --git a/idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInType.kt b/idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInType.kt new file mode 100644 index 00000000000..31465d54d35 --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInType.kt @@ -0,0 +1,3 @@ +fun foo() { + val x = object : Any() {} +} \ No newline at end of file diff --git a/idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInType.kt.conflicts b/idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInType.kt.conflicts new file mode 100644 index 00000000000..1568c6aea62 --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInType.kt.conflicts @@ -0,0 +1 @@ +Cannot refactor in this place diff --git a/idea/testData/refactoring/introduceVariable/DelegatorToSuperClass.kt b/idea/testData/refactoring/introduceVariable/DelegatorToSuperClass.kt new file mode 100644 index 00000000000..b6a6b14ac31 --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/DelegatorToSuperClass.kt @@ -0,0 +1,5 @@ +interface T + +fun foo() { + val x = object : T {} +} \ No newline at end of file diff --git a/idea/testData/refactoring/introduceVariable/DelegatorToSuperClass.kt.conflicts b/idea/testData/refactoring/introduceVariable/DelegatorToSuperClass.kt.conflicts new file mode 100644 index 00000000000..1568c6aea62 --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/DelegatorToSuperClass.kt.conflicts @@ -0,0 +1 @@ +Cannot refactor in this place diff --git a/idea/testData/refactoring/introduceVariable/SuperReference.kt b/idea/testData/refactoring/introduceVariable/SuperReference.kt new file mode 100644 index 00000000000..7777e5db89b --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/SuperReference.kt @@ -0,0 +1,11 @@ +open class A { + open fun foo() { + + } +} + +class B : A() { + override fun foo() { + super.foo() + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/introduceVariable/SuperReference.kt.conflicts b/idea/testData/refactoring/introduceVariable/SuperReference.kt.conflicts new file mode 100644 index 00000000000..1568c6aea62 --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/SuperReference.kt.conflicts @@ -0,0 +1 @@ +Cannot refactor in this place diff --git a/idea/testData/refactoring/introduceVariable/ThisReference.kt b/idea/testData/refactoring/introduceVariable/ThisReference.kt new file mode 100644 index 00000000000..eda00010cbf --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/ThisReference.kt @@ -0,0 +1,9 @@ +class A { + fun bar() { + + } + + fun foo() { + this.foo() + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/introduceVariable/ThisReference.kt.after b/idea/testData/refactoring/introduceVariable/ThisReference.kt.after new file mode 100644 index 00000000000..14c40f42c3f --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/ThisReference.kt.after @@ -0,0 +1,10 @@ +class A { + fun bar() { + + } + + fun foo() { + val a = this + a.foo() + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/JetExtractionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/JetExtractionTestGenerated.java index 507243e2705..8ec7c71a9b0 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/JetExtractionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/JetExtractionTestGenerated.java @@ -61,6 +61,42 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest { doIntroduceVariableTest(fileName); } + @TestMetadata("ConstructorDelegationCall.kt") + public void testConstructorDelegationCall() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/ConstructorDelegationCall.kt"); + doIntroduceVariableTest(fileName); + } + + @TestMetadata("DelegatorByExpressionInDelegate.kt") + public void testDelegatorByExpressionInDelegate() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/DelegatorByExpressionInDelegate.kt"); + doIntroduceVariableTest(fileName); + } + + @TestMetadata("DelegatorByExpressionInType.kt") + public void testDelegatorByExpressionInType() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/DelegatorByExpressionInType.kt"); + doIntroduceVariableTest(fileName); + } + + @TestMetadata("DelegatorToSuperCallInArgument.kt") + public void testDelegatorToSuperCallInArgument() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInArgument.kt"); + doIntroduceVariableTest(fileName); + } + + @TestMetadata("DelegatorToSuperCallInType.kt") + public void testDelegatorToSuperCallInType() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/DelegatorToSuperCallInType.kt"); + doIntroduceVariableTest(fileName); + } + + @TestMetadata("DelegatorToSuperClass.kt") + public void testDelegatorToSuperClass() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/DelegatorToSuperClass.kt"); + doIntroduceVariableTest(fileName); + } + @TestMetadata("DoWhileAddBlock.kt") public void testDoWhileAddBlock() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/DoWhileAddBlock.kt"); @@ -295,6 +331,18 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest { doIntroduceVariableTest(fileName); } + @TestMetadata("SuperReference.kt") + public void testSuperReference() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/SuperReference.kt"); + doIntroduceVariableTest(fileName); + } + + @TestMetadata("ThisReference.kt") + public void testThisReference() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/ThisReference.kt"); + doIntroduceVariableTest(fileName); + } + @TestMetadata("TwoExplicitReceivers.kt") public void testTwoExplicitReceivers() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/TwoExplicitReceivers.kt");