Implement "Safe Delete" refactoring for named functions
Conflicts: idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/KotlinSafeDeleteProcessor.java
This commit is contained in:
@@ -16,44 +16,99 @@
|
||||
|
||||
package org.jetbrains.jet.safeDelete;
|
||||
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.actionSystem.LangDataKeys;
|
||||
import com.intellij.codeInsight.TargetElementUtilBase;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.CharsetToolkit;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.refactoring.BaseRefactoringProcessor;
|
||||
import com.intellij.refactoring.safeDelete.SafeDeleteHandler;
|
||||
import com.intellij.testFramework.LightCodeInsightTestCase;
|
||||
import com.intellij.testFramework.LightProjectDescriptor;
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.JetClass;
|
||||
import org.jetbrains.jet.lang.psi.JetElement;
|
||||
import org.jetbrains.jet.lang.psi.JetNamedFunction;
|
||||
import org.jetbrains.jet.lang.psi.JetObjectDeclarationName;
|
||||
import org.jetbrains.jet.plugin.JetLightProjectDescriptor;
|
||||
import org.jetbrains.jet.plugin.PluginTestCaseBase;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class AbstractJetSafeDeleteTest extends LightCodeInsightTestCase {
|
||||
public abstract class AbstractJetSafeDeleteTest extends LightCodeInsightFixtureTestCase {
|
||||
@NotNull
|
||||
@Override
|
||||
protected LightProjectDescriptor getProjectDescriptor() {
|
||||
return JetLightProjectDescriptor.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
String pathBase = PluginTestCaseBase.getTestDataPathBase();
|
||||
myFixture.setTestDataPath(pathBase.substring(0, pathBase.lastIndexOf("/idea/testData")));
|
||||
}
|
||||
|
||||
public void doClassTest(@NotNull String path) throws Exception {
|
||||
doTest(path, JetClass.class);
|
||||
doTest(path, JetClass.class, false);
|
||||
}
|
||||
|
||||
public void doObjectTest(@NotNull String path) throws Exception {
|
||||
doTest(path, JetObjectDeclarationName.class);
|
||||
doTest(path, JetObjectDeclarationName.class, false);
|
||||
}
|
||||
|
||||
private <T extends JetElement> void doTest(@NotNull String path, @NotNull Class<T> elementClass) throws Exception {
|
||||
configureByFile(path);
|
||||
public void doFunctionTest(@NotNull String path) throws Exception {
|
||||
doTest(path, JetNamedFunction.class, false);
|
||||
}
|
||||
|
||||
DataContext dataContext = getCurrentEditorDataContext();
|
||||
PsiElement element = PsiTreeUtil.getParentOfType(LangDataKeys.PSI_ELEMENT.getData(dataContext), elementClass, false);
|
||||
public void doFunctionTestWithJava(@NotNull String path) throws Exception {
|
||||
doTest(path, JetNamedFunction.class, true);
|
||||
}
|
||||
|
||||
public void doJavaMethodTest(@NotNull String path) throws Exception {
|
||||
doTest(path, PsiMethod.class, true);
|
||||
}
|
||||
|
||||
private <T extends PsiElement> void doTest(
|
||||
@NotNull String path, @NotNull Class<T> elementClass, boolean withJava) throws Exception {
|
||||
String[] filePaths;
|
||||
if (withJava) {
|
||||
filePaths = new String[]{path, path.endsWith(".java") ? path.replace(".java", ".kt") : path.replace(".kt", ".java")};
|
||||
}
|
||||
else {
|
||||
filePaths = new String[]{path};
|
||||
}
|
||||
|
||||
Editor[] editors = new Editor[filePaths.length];
|
||||
int i = 0;
|
||||
for (String filePath : filePaths) {
|
||||
myFixture.configureByFile(filePath);
|
||||
editors[i++] = myFixture.getEditor();
|
||||
}
|
||||
|
||||
PsiElement elementAtCaret = null;
|
||||
for (Editor editor : editors) {
|
||||
elementAtCaret = TargetElementUtilBase.findTargetElement(
|
||||
editor, TargetElementUtilBase.REFERENCED_ELEMENT_ACCEPTED | TargetElementUtilBase.ELEMENT_NAME_ACCEPTED
|
||||
);
|
||||
if (elementAtCaret != null) break;
|
||||
}
|
||||
|
||||
assertNotNull("Couldn't find element at caret position", elementAtCaret);
|
||||
|
||||
T element = PsiTreeUtil.getParentOfType(elementAtCaret, elementClass, false);
|
||||
|
||||
try {
|
||||
new SafeDeleteHandler().invoke(getProject(), new PsiElement[] {element}, dataContext);
|
||||
checkResultByFile(path + ".after");
|
||||
SafeDeleteHandler.invoke(getProject(), new PsiElement[] {element}, null, true, null);
|
||||
for (int j = 0; j < filePaths.length; j++) {
|
||||
String expectedText = FileUtil.loadFile(new File(filePaths[j] + ".after"));
|
||||
assertEquals(StringUtil.convertLineSeparators(expectedText), editors[j].getDocument().getText());
|
||||
}
|
||||
}
|
||||
catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
|
||||
List<String> messages = new ArrayList<String>(e.getMessages());
|
||||
@@ -64,10 +119,4 @@ public abstract class AbstractJetSafeDeleteTest extends LightCodeInsightTestCase
|
||||
assertEquals(expectedMessage, StringUtil.join(messages, "\n"));
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ import org.jetbrains.jet.safeDelete.AbstractJetSafeDeleteTest;
|
||||
|
||||
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@InnerTestClasses({JetSafeDeleteTestGenerated.DeleteClass.class, JetSafeDeleteTestGenerated.DeleteObject.class})
|
||||
@InnerTestClasses({JetSafeDeleteTestGenerated.DeleteClass.class, JetSafeDeleteTestGenerated.DeleteObject.class, JetSafeDeleteTestGenerated.DeleteFunction.class, JetSafeDeleteTestGenerated.DeleteFunctionWithJavaUsages.class, JetSafeDeleteTestGenerated.DeleteJavaMethod.class})
|
||||
public class JetSafeDeleteTestGenerated extends AbstractJetSafeDeleteTest {
|
||||
@TestMetadata("idea/testData/safeDelete/deleteClass")
|
||||
public static class DeleteClass extends AbstractJetSafeDeleteTest {
|
||||
@@ -118,10 +118,142 @@ public class JetSafeDeleteTestGenerated extends AbstractJetSafeDeleteTest {
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/safeDelete/deleteFunction")
|
||||
public static class DeleteFunction extends AbstractJetSafeDeleteTest {
|
||||
public void testAllFilesPresentInDeleteFunction() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/safeDelete/deleteFunction"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("fun1.kt")
|
||||
public void testFun1() throws Exception {
|
||||
doFunctionTest("idea/testData/safeDelete/deleteFunction/fun1.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fun2.kt")
|
||||
public void testFun2() throws Exception {
|
||||
doFunctionTest("idea/testData/safeDelete/deleteFunction/fun2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("implement1.kt")
|
||||
public void testImplement1() throws Exception {
|
||||
doFunctionTest("idea/testData/safeDelete/deleteFunction/implement1.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("implement2.kt")
|
||||
public void testImplement2() throws Exception {
|
||||
doFunctionTest("idea/testData/safeDelete/deleteFunction/implement2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("noUsages.kt")
|
||||
public void testNoUsages() throws Exception {
|
||||
doFunctionTest("idea/testData/safeDelete/deleteFunction/noUsages.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("override1.kt")
|
||||
public void testOverride1() throws Exception {
|
||||
doFunctionTest("idea/testData/safeDelete/deleteFunction/override1.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("override2.kt")
|
||||
public void testOverride2() throws Exception {
|
||||
doFunctionTest("idea/testData/safeDelete/deleteFunction/override2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("overrideAndImplement1.kt")
|
||||
public void testOverrideAndImplement1() throws Exception {
|
||||
doFunctionTest("idea/testData/safeDelete/deleteFunction/overrideAndImplement1.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("overrideAndImplement2.kt")
|
||||
public void testOverrideAndImplement2() throws Exception {
|
||||
doFunctionTest("idea/testData/safeDelete/deleteFunction/overrideAndImplement2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("overrideWithUsages.kt")
|
||||
public void testOverrideWithUsages() throws Exception {
|
||||
doFunctionTest("idea/testData/safeDelete/deleteFunction/overrideWithUsages.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/safeDelete/deleteFunctionWithJavaUsages")
|
||||
public static class DeleteFunctionWithJavaUsages extends AbstractJetSafeDeleteTest {
|
||||
public void testAllFilesPresentInDeleteFunctionWithJavaUsages() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/safeDelete/deleteFunctionWithJavaUsages"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("implement1.kt")
|
||||
public void testImplement1() throws Exception {
|
||||
doFunctionTestWithJava("idea/testData/safeDelete/deleteFunctionWithJavaUsages/implement1.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("implement2.kt")
|
||||
public void testImplement2() throws Exception {
|
||||
doFunctionTestWithJava("idea/testData/safeDelete/deleteFunctionWithJavaUsages/implement2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("implement3.kt")
|
||||
public void testImplement3() throws Exception {
|
||||
doFunctionTestWithJava("idea/testData/safeDelete/deleteFunctionWithJavaUsages/implement3.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("implement4.kt")
|
||||
public void testImplement4() throws Exception {
|
||||
doFunctionTestWithJava("idea/testData/safeDelete/deleteFunctionWithJavaUsages/implement4.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("override1.kt")
|
||||
public void testOverride1() throws Exception {
|
||||
doFunctionTestWithJava("idea/testData/safeDelete/deleteFunctionWithJavaUsages/override1.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("override2.kt")
|
||||
public void testOverride2() throws Exception {
|
||||
doFunctionTestWithJava("idea/testData/safeDelete/deleteFunctionWithJavaUsages/override2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("override3.kt")
|
||||
public void testOverride3() throws Exception {
|
||||
doFunctionTestWithJava("idea/testData/safeDelete/deleteFunctionWithJavaUsages/override3.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("overrideAndImplement1.kt")
|
||||
public void testOverrideAndImplement1() throws Exception {
|
||||
doFunctionTestWithJava("idea/testData/safeDelete/deleteFunctionWithJavaUsages/overrideAndImplement1.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("overrideAndImplement2.kt")
|
||||
public void testOverrideAndImplement2() throws Exception {
|
||||
doFunctionTestWithJava("idea/testData/safeDelete/deleteFunctionWithJavaUsages/overrideAndImplement2.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/safeDelete/deleteJavaMethod")
|
||||
public static class DeleteJavaMethod extends AbstractJetSafeDeleteTest {
|
||||
public void testAllFilesPresentInDeleteJavaMethod() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/safeDelete/deleteJavaMethod"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("mixedHierarchy1.kt")
|
||||
public void testMixedHierarchy1() throws Exception {
|
||||
doJavaMethodTest("idea/testData/safeDelete/deleteJavaMethod/mixedHierarchy1.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("mixedHierarchy2.kt")
|
||||
public void testMixedHierarchy2() throws Exception {
|
||||
doJavaMethodTest("idea/testData/safeDelete/deleteJavaMethod/mixedHierarchy2.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite("JetSafeDeleteTestGenerated");
|
||||
suite.addTestSuite(DeleteClass.class);
|
||||
suite.addTestSuite(DeleteObject.class);
|
||||
suite.addTestSuite(DeleteFunction.class);
|
||||
suite.addTestSuite(DeleteFunctionWithJavaUsages.class);
|
||||
suite.addTestSuite(DeleteJavaMethod.class);
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user