Skip to content

Commit d48962f

Browse files
committed
Improve extract variable refactoring: support replacing of all expression occurrences
1 parent f7fa5b7 commit d48962f

File tree

7 files changed

+330
-21
lines changed

7 files changed

+330
-21
lines changed

kotlin-bundled-compiler/META-INF/MANIFEST.MF

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ Export-Package: com.google.common.base,
191191
jline.internal;x-internal:=true,
192192
kotlin,
193193
kotlin.collections,
194-
kotlin.sequences,
195194
kotlin.concurrent,
196195
kotlin.internal;x-internal:=true,
197196
kotlin.io,
@@ -201,6 +200,7 @@ Export-Package: com.google.common.base,
201200
kotlin.jvm.internal.unsafe;x-internal:=true,
202201
kotlin.properties,
203202
kotlin.reflect,
203+
kotlin.sequences,
204204
kotlin.text,
205205
messages,
206206
misc,
@@ -223,6 +223,7 @@ Export-Package: com.google.common.base,
223223
org.jetbrains.kotlin.backend.common.bridges,
224224
org.jetbrains.kotlin.backend.common.output,
225225
org.jetbrains.kotlin.builtins,
226+
org.jetbrains.kotlin.caches.resolve,
226227
org.jetbrains.kotlin.cfg,
227228
org.jetbrains.kotlin.cfg.pseudocode,
228229
org.jetbrains.kotlin.cfg.pseudocode.instructions,
@@ -270,6 +271,7 @@ Export-Package: com.google.common.base,
270271
org.jetbrains.kotlin.idea.core.quickfix,
271272
org.jetbrains.kotlin.idea.resolve,
272273
org.jetbrains.kotlin.idea.util,
274+
org.jetbrains.kotlin.idea.util.psi.patternMatching,
273275
org.jetbrains.kotlin.incremental.components,
274276
org.jetbrains.kotlin.j2k,
275277
org.jetbrains.kotlin.js,

kotlin-eclipse-aspects/META-INF/MANIFEST.MF

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ Import-Package: com.intellij.navigation,
4141
org.jetbrains.kotlin.ui.editors,
4242
org.jetbrains.kotlin.ui.navigation,
4343
org.jetbrains.kotlin.ui.refactorings.rename,
44-
org.jetbrains.kotlin.utils,
45-
org.eclipse.jdt.internal.ui.javaeditor
44+
org.jetbrains.kotlin.utils
4645
Eclipse-SupplementBundle:
4746
org.eclipse.jdt.debug.ui,
4847
org.eclipse.jdt.launching,

kotlin-eclipse-core/src/org/jetbrains/kotlin/core/model/KotlinEnvironment.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
import org.jetbrains.annotations.Nullable;
3131
import org.jetbrains.kotlin.asJava.KtLightClassForFacade;
3232
import org.jetbrains.kotlin.asJava.LightClassGenerationSupport;
33+
import org.jetbrains.kotlin.caches.resolve.KotlinCacheService;
3334
import org.jetbrains.kotlin.cli.common.CliModuleVisibilityManagerImpl;
3435
import org.jetbrains.kotlin.cli.jvm.compiler.CliLightClassGenerationSupport;
3536
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
3637
import org.jetbrains.kotlin.codegen.extensions.ExpressionCodegenExtension;
3738
import org.jetbrains.kotlin.core.filesystem.KotlinLightClassManager;
3839
import org.jetbrains.kotlin.core.log.KotlinLogger;
3940
import org.jetbrains.kotlin.core.resolve.BuiltInsReferenceResolver;
41+
import org.jetbrains.kotlin.core.resolve.KotlinCacheServiceImpl;
4042
import org.jetbrains.kotlin.core.resolve.KotlinSourceIndex;
4143
import org.jetbrains.kotlin.core.resolve.lang.kotlin.EclipseVirtualFileFinder;
4244
import org.jetbrains.kotlin.core.utils.ProjectUtils;
@@ -122,6 +124,7 @@ protected void preregisterServices() {
122124
project.registerService(CodeStyleManager.class, new DummyCodeStyleManager());
123125
project.registerService(BuiltInsReferenceResolver.class, new BuiltInsReferenceResolver(project));
124126
project.registerService(KotlinSourceIndex.class, new KotlinSourceIndex());
127+
project.registerService(KotlinCacheService.class, new KotlinCacheServiceImpl());
125128

126129
configureClasspath();
127130

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*******************************************************************************
2+
* Copyright 2000-2016 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*******************************************************************************/
17+
package org.jetbrains.kotlin.core.resolve
18+
19+
import org.jetbrains.kotlin.caches.resolve.KotlinCacheService
20+
import org.jetbrains.kotlin.psi.KtElement
21+
import org.jetbrains.kotlin.idea.resolve.ResolutionFacade
22+
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
23+
import com.intellij.openapi.project.Project
24+
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
25+
import org.jetbrains.kotlin.resolve.BindingContext
26+
import org.jetbrains.kotlin.analyzer.AnalysisResult
27+
import com.intellij.psi.PsiElement
28+
import org.jetbrains.kotlin.psi.KtDeclaration
29+
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
30+
import org.jetbrains.kotlin.core.builder.KotlinPsiManager
31+
import org.eclipse.jdt.core.JavaCore
32+
import org.jetbrains.kotlin.core.log.KotlinLogger
33+
import org.jetbrains.kotlin.core.model.KotlinAnalysisFileCache
34+
import org.jetbrains.kotlin.resolve.diagnostics.KotlinSuppressCache
35+
36+
public class KotlinCacheServiceImpl : KotlinCacheService {
37+
override fun getSuppressionCache(): KotlinSuppressCache {
38+
throw UnsupportedOperationException()
39+
}
40+
41+
override fun getResolutionFacade(elements: List<KtElement>): ResolutionFacade {
42+
return KotlinSimpleResolutionFacade()
43+
}
44+
}
45+
46+
class KotlinSimpleResolutionFacade : ResolutionFacade {
47+
override val moduleDescriptor: ModuleDescriptor
48+
get() = throw UnsupportedOperationException()
49+
50+
override val project: Project
51+
get() = throw UnsupportedOperationException()
52+
53+
override fun analyze(element: KtElement, bodyResolveMode: BodyResolveMode): BindingContext {
54+
val ktFile = element.getContainingKtFile()
55+
val javaProject = KotlinPsiManager.getJavaProject(element)
56+
if (javaProject == null) {
57+
KotlinLogger.logWarning("JavaProject for $element (in $ktFile) is null")
58+
return BindingContext.EMPTY
59+
}
60+
61+
return KotlinAnalysisFileCache.getAnalysisResult(ktFile, javaProject).analysisResult.bindingContext
62+
}
63+
64+
override fun analyzeFullyAndGetResult(elements: Collection<KtElement>): AnalysisResult {
65+
throw UnsupportedOperationException()
66+
}
67+
68+
override fun <T : Any> getFrontendService(element: PsiElement, serviceClass: Class<T>): T {
69+
throw UnsupportedOperationException()
70+
}
71+
72+
override fun <T : Any> getFrontendService(serviceClass: Class<T>): T {
73+
throw UnsupportedOperationException()
74+
}
75+
76+
override fun <T : Any> getFrontendService(moduleDescriptor: ModuleDescriptor, serviceClass: Class<T>): T {
77+
throw UnsupportedOperationException()
78+
}
79+
80+
override fun <T : Any> getIdeService(serviceClass: Class<T>): T {
81+
throw UnsupportedOperationException()
82+
}
83+
84+
override fun resolveToDescriptor(declaration: KtDeclaration): DeclarationDescriptor {
85+
throw UnsupportedOperationException()
86+
}
87+
}

kotlin-eclipse-ui-test/src/org/jetbrains/kotlin/ui/tests/refactoring/extract/KotlinExtractVariableTest.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jetbrains.kotlin.ui.tests.refactoring.extract;
22

3+
import org.junit.Ignore;
34
import org.junit.Test;
45

56
public class KotlinExtractVariableTest extends KotlinExtractVariableTestCase {
@@ -13,6 +14,17 @@ public void testArrayAccess() {
1314
doTest("common_testData/ide/refactoring/introduceVariable/ArrayAccessExpr.kt");
1415
}
1516

17+
@Test
18+
public void testCallUnderSmartCast() {
19+
doTest("common_testData/ide/refactoring/introduceVariable/callUnderSmartCast.kt", "foo");
20+
}
21+
22+
@Ignore("Ignore because of formatter issues")
23+
@Test
24+
public void testComplexCallee() {
25+
doTest("common_testData/ide/refactoring/introduceVariable/ComplexCallee.kt", "function");
26+
}
27+
1628
@Test
1729
public void testDelegatorByExpressionInDelegate() {
1830
doTest("common_testData/ide/refactoring/introduceVariable/DelegatorByExpressionInDelegate.kt", "o");
@@ -33,6 +45,23 @@ public void testDoWhileAddBlockInner() {
3345
doTest("common_testData/ide/refactoring/introduceVariable/DoWhileAddBlockInner.kt");
3446
}
3547

48+
@Test
49+
public void testFewOccurrences() {
50+
doTest("common_testData/ide/refactoring/introduceVariable/FewOccurrences.kt");
51+
}
52+
53+
@Ignore("Ignore because of formatter issues")
54+
@Test
55+
public void testFunctionAddBlock() {
56+
doTest("common_testData/ide/refactoring/introduceVariable/FunctionAddBlock.kt");
57+
}
58+
59+
@Ignore("Ignore because of formatter issues")
60+
@Test
61+
public void testFunctionAddBlockInner() {
62+
doTest("common_testData/ide/refactoring/introduceVariable/FunctionAddBlockInner.kt");
63+
}
64+
3665
@Test
3766
public void testIfElseAddBlock() {
3867
doTest("common_testData/ide/refactoring/introduceVariable/IfElseAddBlock.kt");
@@ -43,6 +72,11 @@ public void testIfThenAddBlock() {
4372
doTest("common_testData/ide/refactoring/introduceVariable/IfCondition.kt", "b");
4473
}
4574

75+
@Test
76+
public void testIfThenAddBlockInner() {
77+
doTest("common_testData/ide/refactoring/introduceVariable/IfThenAddBlockInner.kt");
78+
}
79+
4680
@Test
4781
public void testIfThenValuedAddBlock() {
4882
doTest("common_testData/ide/refactoring/introduceVariable/IfThenValuedAddBlock.kt");
@@ -53,11 +87,54 @@ public void testIntroduceAndCreateBlock() {
5387
doTest("common_testData/ide/refactoring/introduceVariable/IntroduceAndCreateBlock.kt");
5488
}
5589

90+
@Test
91+
public void testManyInnerOccurences() {
92+
doTest("common_testData/ide/refactoring/introduceVariable/ManyInnerOccurences.kt");
93+
}
94+
95+
@Test
96+
public void testManyOccurrences() {
97+
doTest("common_testData/ide/refactoring/introduceVariable/ManyOccurrences.kt");
98+
}
99+
56100
@Test
57101
public void testNoNewLinesInBetween() {
58102
doTest("common_testData/ide/refactoring/introduceVariable/NoNewLinesInBetween.kt", "bar");
59103
}
60104

105+
@Ignore("Ignore because of formatter issues")
106+
@Test
107+
public void testNoNewLinesInBetweenNoBraces() {
108+
doTest("common_testData/ide/refactoring/introduceVariable/NoNewLinesInBetweenNoBraces.kt", "bar");
109+
}
110+
111+
@Test
112+
public void testNotNullAssertion() {
113+
doTest("common_testData/ide/refactoring/introduceVariable/notNullAssertion.kt", "length");
114+
}
115+
116+
@Test
117+
public void testOccurrencesInStringTemplate() {
118+
doTest("common_testData/ide/refactoring/introduceVariable/OccurrencesInStringTemplate.kt");
119+
}
120+
121+
@Test
122+
public void testOneExplicitReceiver() {
123+
doTest("common_testData/ide/refactoring/introduceVariable/OneExplicitReceiver.kt", "prop");
124+
}
125+
126+
@Ignore("Ignore because of formatter issues")
127+
@Test
128+
public void testPropertyAccessorAddBlock() {
129+
doTest("common_testData/ide/refactoring/introduceVariable/PropertyAccessorAddBlock.kt");
130+
}
131+
132+
@Ignore("Ignore because of formatter issues")
133+
@Test
134+
public void testPropertyAccessorAddBlockInner() {
135+
doTest("common_testData/ide/refactoring/introduceVariable/PropertyAccessorAddBlockInner.kt");
136+
}
137+
61138
@Test
62139
public void testReplaceOccurence() {
63140
doTest("common_testData/ide/refactoring/introduceVariable/ReplaceOccurence.kt", "x");
@@ -83,6 +160,11 @@ public void testThisReference() {
83160
doTest("common_testData/ide/refactoring/introduceVariable/ThisReference.kt", "a");
84161
}
85162

163+
@Test
164+
public void testTwoExplicitReceivers() {
165+
doTest("common_testData/ide/refactoring/introduceVariable/TwoExplicitReceivers.kt", "f1");
166+
}
167+
86168
@Test
87169
public void testWhenAddBlock() {
88170
doTest("common_testData/ide/refactoring/introduceVariable/WhenAddBlock.kt");

0 commit comments

Comments
 (0)