Basic local val inlining support.
#KT-2637 in progress
This commit is contained in:
@@ -47,6 +47,7 @@ import org.jetbrains.jet.plugin.intentions.AbstractCodeTransformationTest;
|
||||
import org.jetbrains.jet.plugin.navigation.JetAbstractGotoSuperTest;
|
||||
import org.jetbrains.jet.plugin.quickfix.AbstractQuickFixMultiFileTest;
|
||||
import org.jetbrains.jet.plugin.quickfix.AbstractQuickFixTest;
|
||||
import org.jetbrains.jet.plugin.refactoring.inline.AbstractInlineTest;
|
||||
import org.jetbrains.jet.psi.AbstractJetPsiMatcherTest;
|
||||
import org.jetbrains.jet.resolve.AbstractResolveTest;
|
||||
import org.jetbrains.jet.safeDelete.AbstractJetSafeDeleteTest;
|
||||
@@ -373,6 +374,13 @@ public class GenerateTests {
|
||||
testModel("idea/testData/codeInsight/moveUpDown/expressions", "doTestExpression")
|
||||
);
|
||||
|
||||
generateTest(
|
||||
"idea/tests/",
|
||||
"InlineTestGenerated",
|
||||
AbstractInlineTest.class,
|
||||
testModel("idea/testData/refactoring/inline", "doTest")
|
||||
);
|
||||
|
||||
generateTest(
|
||||
"idea/tests/",
|
||||
"UnwrapRemoveTestGenerated",
|
||||
|
||||
@@ -131,6 +131,7 @@
|
||||
<refactoring.moveHandler implementation="org.jetbrains.jet.plugin.refactoring.move.JetMoveFilesOrDirectoriesHandler"/>
|
||||
<refactoring.copyHandler implementation="org.jetbrains.jet.plugin.refactoring.copy.JetCopyClassHandler"/>
|
||||
<refactoring.changeSignatureUsageProcessor implementation="org.jetbrains.jet.plugin.refactoring.changeSignature.JetChangeSignatureUsageProcessor"/>
|
||||
<inlineActionHandler implementation="org.jetbrains.jet.plugin.refactoring.inline.KotlinInlineLocalHandler"/>
|
||||
<treeStructureProvider implementation="org.jetbrains.jet.plugin.projectView.JetProjectViewProvider"/>
|
||||
|
||||
<colorSettingsPage implementation="org.jetbrains.jet.plugin.highlighter.JetColorSettingsPage"/>
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
package org.jetbrains.jet.plugin.refactoring.inline;
|
||||
|
||||
import com.intellij.codeInsight.highlighting.HighlightManager;
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.lang.refactoring.InlineActionHandler;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.colors.EditorColors;
|
||||
import com.intellij.openapi.editor.colors.EditorColorsManager;
|
||||
import com.intellij.openapi.editor.markup.TextAttributes;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.wm.StatusBar;
|
||||
import com.intellij.openapi.wm.WindowManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiReference;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.refactoring.HelpID;
|
||||
import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.util.RefactoringMessageDialog;
|
||||
import com.intellij.util.Processor;
|
||||
import com.intellij.util.Query;
|
||||
import org.jetbrains.jet.lang.psi.JetExpression;
|
||||
import org.jetbrains.jet.lang.psi.JetProperty;
|
||||
import org.jetbrains.jet.plugin.JetLanguage;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class KotlinInlineLocalHandler extends InlineActionHandler {
|
||||
@Override
|
||||
public boolean isEnabledForLanguage(Language l) {
|
||||
return l.equals(JetLanguage.INSTANCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canInlineElement(PsiElement element) {
|
||||
if (!(element instanceof JetProperty)) {
|
||||
return false;
|
||||
}
|
||||
JetProperty property = (JetProperty) element;
|
||||
return property.getInitializer() != null && !property.isVar();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void inlineElement(Project project, Editor editor, PsiElement element) {
|
||||
final JetProperty val = (JetProperty) element;
|
||||
String name = val.getName();
|
||||
|
||||
final JetExpression initializer = val.getInitializer();
|
||||
assert initializer != null : element.getText();
|
||||
|
||||
final Collection<PsiReference> references = ReferencesSearch.search(val, GlobalSearchScope.allScope(project), false).findAll();
|
||||
PsiReference[] referencesArray = references.toArray(references.toArray(new PsiReference[references.size()]));
|
||||
|
||||
if (editor != null && !ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
EditorColorsManager editorColorsManager = EditorColorsManager.getInstance();
|
||||
TextAttributes attributes = editorColorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
|
||||
HighlightManager.getInstance(project).addOccurrenceHighlights(editor, referencesArray, attributes, true, null);
|
||||
RefactoringMessageDialog dialog = new RefactoringMessageDialog(
|
||||
RefactoringBundle.message("inline.variable.title"),
|
||||
RefactoringBundle.message("inline.local.variable.prompt", name) + " " +
|
||||
RefactoringBundle.message("occurences.string", references.size()),
|
||||
HelpID.INLINE_VARIABLE,
|
||||
"OptionPane.questionIcon",
|
||||
true,
|
||||
project);
|
||||
|
||||
dialog.show();
|
||||
if (!dialog.isOK()){
|
||||
StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
|
||||
if (statusBar != null) {
|
||||
statusBar.setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
CommandProcessor.getInstance().executeCommand(project, new Runnable() {
|
||||
public void run() {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (PsiReference reference : references) {
|
||||
reference.getElement().replace(initializer.copy());
|
||||
}
|
||||
|
||||
val.delete();
|
||||
}
|
||||
});
|
||||
}
|
||||
}, RefactoringBundle.message("inline.command", name), null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun f() {
|
||||
val v = 239
|
||||
println(<caret>v)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun f() {
|
||||
println(239)
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun f() {
|
||||
val <caret>v = 239
|
||||
println(v)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun f() {
|
||||
println(239)
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun f() {
|
||||
run {
|
||||
val v = 239
|
||||
println(<caret>v)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun f() {
|
||||
run {
|
||||
println(239)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun f() {
|
||||
val m = ::println
|
||||
println(<caret>m)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun f() {
|
||||
println(::println)
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun f() {
|
||||
val v = 239
|
||||
println(v)
|
||||
println(<caret>v + v)
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun f() {
|
||||
println(239)
|
||||
println(239 + 239)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun f(val p: Int) {
|
||||
println(<caret>p)
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun f() {
|
||||
val v: Int
|
||||
println(<caret>v)
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun f() {
|
||||
var v = 239
|
||||
println(<caret>v)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.jetbrains.jet.plugin.refactoring.inline;
|
||||
|
||||
import com.intellij.codeInsight.TargetElementUtilBase;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import static com.intellij.codeInsight.TargetElementUtilBase.ELEMENT_NAME_ACCEPTED;
|
||||
import static com.intellij.codeInsight.TargetElementUtilBase.REFERENCED_ELEMENT_ACCEPTED;
|
||||
|
||||
public abstract class AbstractInlineTest extends LightCodeInsightFixtureTestCase {
|
||||
protected void doTest(@NotNull String path) throws IOException {
|
||||
File afterFile = new File(path + ".after");
|
||||
|
||||
myFixture.configureByFile(path);
|
||||
|
||||
boolean afterFileExists = afterFile.exists();
|
||||
|
||||
final PsiElement targetElement =
|
||||
TargetElementUtilBase.findTargetElement(myFixture.getEditor(), ELEMENT_NAME_ACCEPTED | REFERENCED_ELEMENT_ACCEPTED);
|
||||
final KotlinInlineLocalHandler handler = new KotlinInlineLocalHandler();
|
||||
|
||||
assertEquals(afterFileExists, handler.canInlineElement(targetElement));
|
||||
if (!afterFileExists) {
|
||||
return;
|
||||
}
|
||||
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
handler.inlineElement(myFixture.getProject(), myFixture.getEditor(), targetElement);
|
||||
}
|
||||
});
|
||||
myFixture.checkResult(FileUtil.loadFile(afterFile));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.plugin.refactoring.inline;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
import org.jetbrains.jet.JetTestUtils;
|
||||
import org.jetbrains.jet.test.InnerTestClasses;
|
||||
import org.jetbrains.jet.test.TestMetadata;
|
||||
|
||||
import org.jetbrains.jet.plugin.refactoring.inline.AbstractInlineTest;
|
||||
|
||||
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("idea/testData/refactoring/inline")
|
||||
public class InlineTestGenerated extends AbstractInlineTest {
|
||||
public void testAllFilesPresentInInline() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/refactoring/inline"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("Basic.kt")
|
||||
public void testBasic() throws Exception {
|
||||
doTest("idea/testData/refactoring/inline/Basic.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("BasicCaretOnDeclaration.kt")
|
||||
public void testBasicCaretOnDeclaration() throws Exception {
|
||||
doTest("idea/testData/refactoring/inline/BasicCaretOnDeclaration.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("InFunctionLiteral.kt")
|
||||
public void testInFunctionLiteral() throws Exception {
|
||||
doTest("idea/testData/refactoring/inline/InFunctionLiteral.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("MethodReference.kt")
|
||||
public void testMethodReference() throws Exception {
|
||||
doTest("idea/testData/refactoring/inline/MethodReference.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("MultipleUsages.kt")
|
||||
public void testMultipleUsages() throws Exception {
|
||||
doTest("idea/testData/refactoring/inline/MultipleUsages.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("Parameter.kt")
|
||||
public void testParameter() throws Exception {
|
||||
doTest("idea/testData/refactoring/inline/Parameter.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("ValWithoutInitializer.kt")
|
||||
public void testValWithoutInitializer() throws Exception {
|
||||
doTest("idea/testData/refactoring/inline/ValWithoutInitializer.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("Var.kt")
|
||||
public void testVar() throws Exception {
|
||||
doTest("idea/testData/refactoring/inline/Var.kt");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user