Implement "Safe Delete" refactoring for classes and objects

This commit is contained in:
Alexey Sedunov
2013-06-27 16:39:34 +04:00
parent bcad49246b
commit f2dd8a6b60
35 changed files with 619 additions and 2 deletions
@@ -49,6 +49,7 @@ import org.jetbrains.jet.plugin.quickfix.AbstractQuickFixMultiFileTest;
import org.jetbrains.jet.plugin.quickfix.AbstractQuickFixTest;
import org.jetbrains.jet.psi.AbstractJetPsiMatcherTest;
import org.jetbrains.jet.resolve.AbstractResolveTest;
import org.jetbrains.jet.safeDelete.AbstractJetSafeDeleteTest;
import org.jetbrains.jet.test.generator.SimpleTestClassModel;
import org.jetbrains.jet.test.generator.TestClassModel;
import org.jetbrains.jet.test.generator.TestGenerator;
@@ -395,6 +396,14 @@ public class GenerateTests {
AbstractJetQuickDocProviderTest.class,
testModel("idea/testData/editor/quickDoc", "doTest")
);
generateTest(
"idea/tests/",
"JetSafeDeleteTestGenerated",
AbstractJetSafeDeleteTest.class,
testModel("idea/testData/safeDelete/deleteClass", "doClassTest"),
testModel("idea/testData/safeDelete/deleteObject", "doObjectTest")
);
}
private static SimpleTestClassModel testModel(@NotNull String rootPath) {
+1
View File
@@ -227,6 +227,7 @@
<elementDescriptionProvider implementation="org.jetbrains.jet.plugin.findUsages.JetElementDescriptionProvider"/>
<findUsagesHandlerFactory implementation="org.jetbrains.jet.plugin.findUsages.KotlinFindUsagesHandlerFactory"/>
<usageTypeProvider implementation="org.jetbrains.jet.plugin.findUsages.JetUsageTypeProvider"/>
<refactoring.safeDeleteProcessor implementation="org.jetbrains.jet.plugin.refactoring.safeDelete.KotlinSafeDeleteProcessor" id="kotlinProcessor"/>
<debugger.positionManagerFactory implementation="org.jetbrains.jet.plugin.debugger.JetPositionManagerFactory"/>
<codeInsight.implementMethod language="jet" implementationClass="org.jetbrains.jet.plugin.codeInsight.ImplementMethodsHandler"/>
<codeInsight.overrideMethod language="jet" implementationClass="org.jetbrains.jet.plugin.codeInsight.OverrideMethodsHandler"/>
@@ -21,16 +21,21 @@ import com.intellij.psi.PsiElement;
import com.intellij.refactoring.RefactoringActionHandler;
import com.intellij.refactoring.changeSignature.ChangeSignatureHandler;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFunction;
import org.jetbrains.jet.lang.psi.JetProperty;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.plugin.refactoring.changeSignature.JetChangeSignatureHandler;
import org.jetbrains.jet.plugin.refactoring.introduceVariable.JetIntroduceVariableHandler;
import org.jetbrains.jet.plugin.refactoring.safeDelete.KotlinSafeDeleteProcessor;
/**
* User: Alefas
* Date: 25.01.12
*/
public class JetRefactoringSupportProvider extends RefactoringSupportProvider {
@Override
public boolean isSafeDeleteAvailable(PsiElement element) {
return KotlinSafeDeleteProcessor.checkElement(element);
}
@Override
public RefactoringActionHandler getIntroduceVariableHandler() {
return new JetIntroduceVariableHandler();
@@ -0,0 +1,125 @@
/*
* 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.safeDelete;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.safeDelete.JavaSafeDeleteProcessor;
import com.intellij.refactoring.safeDelete.NonCodeUsageSearchInfo;
import com.intellij.refactoring.safeDelete.SafeDeleteProcessor;
import com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceSimpleDeleteUsageInfo;
import com.intellij.usageView.UsageInfo;
import com.intellij.util.Processor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetClassOrObject;
import org.jetbrains.jet.lang.psi.JetImportDirective;
import org.jetbrains.jet.lang.psi.JetObjectDeclaration;
import org.jetbrains.jet.lang.psi.JetObjectDeclarationName;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor {
public static boolean checkElement(PsiElement element) {
return element instanceof JetClassOrObject
|| element instanceof JetObjectDeclarationName;
}
@Override
public boolean handlesElement(PsiElement element) {
return checkElement(element);
}
protected static NonCodeUsageSearchInfo getDefaultNonCodeUsageSearchInfo(
@NotNull PsiElement element, @NotNull PsiElement[] allElementsToDelete
) {
return new NonCodeUsageSearchInfo(SafeDeleteProcessor.getDefaultInsideDeletedCondition(allElementsToDelete), element);
}
@Nullable
@Override
public NonCodeUsageSearchInfo findUsages(PsiElement element, PsiElement[] allElementsToDelete, List<UsageInfo> result) {
if (element instanceof JetClassOrObject) {
return findClassOrObjectUsages(element, (JetClassOrObject) element, allElementsToDelete, result);
}
if (element instanceof JetObjectDeclarationName && element.getParent() instanceof JetObjectDeclaration) {
return findClassOrObjectUsages(element, (JetObjectDeclaration) element.getParent(), allElementsToDelete, result);
}
return getDefaultNonCodeUsageSearchInfo(element, allElementsToDelete);
}
@SuppressWarnings("MethodOverridesPrivateMethodOfSuperclass")
protected static boolean isInside(PsiElement place, PsiElement[] ancestors) {
return isInside(place, Arrays.asList(ancestors));
}
@SuppressWarnings("MethodOverridesPrivateMethodOfSuperclass")
protected static boolean isInside(PsiElement place, Collection<? extends PsiElement> ancestors) {
for (PsiElement element : ancestors) {
if (isInside(place, element)) return true;
}
return false;
}
protected static NonCodeUsageSearchInfo findClassOrObjectUsages(
PsiElement referencedElement,
final JetClassOrObject classOrObject,
final PsiElement[] allElementsToDelete,
final List<UsageInfo> result
) {
ReferencesSearch.search(referencedElement).forEach(new Processor<PsiReference>() {
@Override
public boolean process(PsiReference reference) {
PsiElement element = reference.getElement();
if (!isInside(element, allElementsToDelete)) {
JetImportDirective importDirective = PsiTreeUtil.getParentOfType(element, JetImportDirective.class, false);
if (importDirective != null) {
result.add(new SafeDeleteImportDirectiveUsageInfo(importDirective, classOrObject));
return true;
}
result.add(new SafeDeleteReferenceSimpleDeleteUsageInfo(element, classOrObject, false));
}
return true;
}
});
return getDefaultNonCodeUsageSearchInfo(referencedElement, allElementsToDelete);
}
@Nullable
@Override
public UsageInfo[] preprocessUsages(Project project, UsageInfo[] usages) {
return usages;
}
@Override
public Collection<PsiElement> getAdditionalElementsToDelete(
PsiElement element, Collection<PsiElement> allElementsToDelete, boolean askUser
) {
if (element instanceof JetObjectDeclarationName && element.getParent() instanceof JetObjectDeclaration) {
return Arrays.asList(element.getParent());
}
return super.getAdditionalElementsToDelete(element, allElementsToDelete, askUser);
}
}
@@ -0,0 +1,57 @@
/*
* 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.safeDelete;
import com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceSimpleDeleteUsageInfo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
public class SafeDeleteImportDirectiveUsageInfo extends SafeDeleteReferenceSimpleDeleteUsageInfo {
public SafeDeleteImportDirectiveUsageInfo(@NotNull JetImportDirective importDirective, @NotNull JetDeclaration declaration) {
super(importDirective, declaration, isSafeToDelete(declaration, importDirective));
}
private static boolean isSafeToDelete(@NotNull JetDeclaration declaration, @NotNull JetImportDirective importDirective) {
JetExpression importExpr = importDirective.getImportedReference();
JetReferenceExpression importReference = null;
if (importExpr instanceof JetSimpleNameExpression) {
importReference = (JetReferenceExpression) importExpr;
}
else if (importExpr instanceof JetDotQualifiedExpression) {
JetExpression selector = ((JetDotQualifiedExpression) importExpr).getSelectorExpression();
if (selector instanceof JetSimpleNameExpression) {
importReference = (JetReferenceExpression) selector;
}
}
if (importReference == null) return false;
BindingContext bindingContext =
AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) declaration.getContainingFile()).getBindingContext();
DeclarationDescriptor referenceDescriptor = bindingContext.get(BindingContext.REFERENCE_TARGET, importReference);
DeclarationDescriptor declarationDescriptor = declaration instanceof JetObjectDeclaration
? bindingContext.get(BindingContext.OBJECT_DECLARATION, ((JetObjectDeclaration) declaration).getNameAsDeclaration())
: bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, declaration);
return referenceDescriptor == declarationDescriptor;
}
}
@@ -0,0 +1,11 @@
package test
import test.A
class <caret>A {
}
class B {
}
@@ -0,0 +1,5 @@
package test
class B {
}
@@ -0,0 +1,11 @@
package test
import test.A
class <caret>A {
}
class B: A {
}
@@ -0,0 +1,2 @@
Class A has 1 usage that is not safe to delete.
Of those 0 usages are in strings, comments, or non-code files.
@@ -0,0 +1,13 @@
package test
import test.A.C
class A {
class <caret>C {
}
}
class B {
}
@@ -0,0 +1,8 @@
package test
class A {
}
class B {
}
@@ -0,0 +1,13 @@
package test
import test.A.C
class A {
class <caret>C {
}
}
class B {
val x = A.C()
}
@@ -0,0 +1,2 @@
Class C has 1 usage that is not safe to delete.
Of those 0 usages are in strings, comments, or non-code files.
@@ -0,0 +1,11 @@
class C {
}
class <caret>A {
}
class B {
}
@@ -0,0 +1,7 @@
class C {
}
class B {
}
@@ -0,0 +1,10 @@
package test
import test.A
trait <caret>A {
}
class B {
}
@@ -0,0 +1,5 @@
package test
class B {
}
@@ -0,0 +1,11 @@
package test
import test.A
trait <caret>A {
}
class B: A {
}
@@ -0,0 +1,2 @@
Trait A has 1 usage that is not safe to delete.
Of those 0 usages are in strings, comments, or non-code files.
@@ -0,0 +1,13 @@
package test
import test.A.C
class <caret>A {
class C {
}
}
class B {
}
@@ -0,0 +1,2 @@
Class A has 1 usage that is not safe to delete.
Of those 0 usages are in strings, comments, or non-code files.
@@ -0,0 +1,11 @@
package test
object A {
object <caret>O {
}
}
class B {
}
@@ -0,0 +1,8 @@
package test
object A {
}
class B {
}
@@ -0,0 +1,13 @@
package test
import test.A.O
object A {
object <caret>O {
}
}
class B {
val x = A.O
}
@@ -0,0 +1,2 @@
Object O has 1 usage that is not safe to delete.
Of those 0 usages are in strings, comments, or non-code files.
@@ -0,0 +1,11 @@
class C {
}
object <caret>A {
}
class B {
}
@@ -0,0 +1,7 @@
class C {
}
class B {
}
@@ -0,0 +1,11 @@
package test
import test.A
object <caret>A {
}
class B {
}
@@ -0,0 +1,5 @@
package test
class B {
}
@@ -0,0 +1,11 @@
package test
import test.A
object <caret>A {
}
class B {
val x = A
}
@@ -0,0 +1,2 @@
Object A has 1 usage that is not safe to delete.
Of those 0 usages are in strings, comments, or non-code files.
@@ -0,0 +1,11 @@
package test
import test.A.x
object <caret>A {
val x = ""
}
class B {
}
@@ -0,0 +1,2 @@
Object A has 1 usage that is not safe to delete.
Of those 0 usages are in strings, comments, or non-code files.
@@ -0,0 +1,73 @@
/*
* 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.safeDelete;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
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.util.PsiTreeUtil;
import com.intellij.refactoring.BaseRefactoringProcessor;
import com.intellij.refactoring.safeDelete.SafeDeleteHandler;
import com.intellij.testFramework.LightCodeInsightTestCase;
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.JetObjectDeclarationName;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public abstract class AbstractJetSafeDeleteTest extends LightCodeInsightTestCase {
public void doClassTest(@NotNull String path) throws Exception {
doTest(path, JetClass.class);
}
public void doObjectTest(@NotNull String path) throws Exception {
doTest(path, JetObjectDeclarationName.class);
}
private <T extends JetElement> void doTest(@NotNull String path, @NotNull Class<T> elementClass) throws Exception {
configureByFile(path);
DataContext dataContext = getCurrentEditorDataContext();
PsiElement element = PsiTreeUtil.getParentOfType(LangDataKeys.PSI_ELEMENT.getData(dataContext), elementClass, false);
try {
new SafeDeleteHandler().invoke(getProject(), new PsiElement[] {element}, dataContext);
checkResultByFile(path + ".after");
}
catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
List<String> messages = new ArrayList<String>(e.getMessages());
Collections.sort(messages);
File messageFile = new File(path + ".messages");
String expectedMessage = FileUtil.loadFile(messageFile, CharsetToolkit.UTF8, true);
assertEquals(expectedMessage, StringUtil.join(messages, "\n"));
}
}
@NotNull
@Override
protected String getTestDataPath() {
return "";
}
}
@@ -0,0 +1,127 @@
/*
* 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.safeDelete;
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.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})
public class JetSafeDeleteTestGenerated extends AbstractJetSafeDeleteTest {
@TestMetadata("idea/testData/safeDelete/deleteClass")
public static class DeleteClass extends AbstractJetSafeDeleteTest {
public void testAllFilesPresentInDeleteClass() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/safeDelete/deleteClass"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("class1.kt")
public void testClass1() throws Exception {
doClassTest("idea/testData/safeDelete/deleteClass/class1.kt");
}
@TestMetadata("class2.kt")
public void testClass2() throws Exception {
doClassTest("idea/testData/safeDelete/deleteClass/class2.kt");
}
@TestMetadata("nestedClass1.kt")
public void testNestedClass1() throws Exception {
doClassTest("idea/testData/safeDelete/deleteClass/nestedClass1.kt");
}
@TestMetadata("nestedClass2.kt")
public void testNestedClass2() throws Exception {
doClassTest("idea/testData/safeDelete/deleteClass/nestedClass2.kt");
}
@TestMetadata("noUsages.kt")
public void testNoUsages() throws Exception {
doClassTest("idea/testData/safeDelete/deleteClass/noUsages.kt");
}
@TestMetadata("trait1.kt")
public void testTrait1() throws Exception {
doClassTest("idea/testData/safeDelete/deleteClass/trait1.kt");
}
@TestMetadata("trait2.kt")
public void testTrait2() throws Exception {
doClassTest("idea/testData/safeDelete/deleteClass/trait2.kt");
}
@TestMetadata("unsafeImport.kt")
public void testUnsafeImport() throws Exception {
doClassTest("idea/testData/safeDelete/deleteClass/unsafeImport.kt");
}
}
@TestMetadata("idea/testData/safeDelete/deleteObject")
public static class DeleteObject extends AbstractJetSafeDeleteTest {
public void testAllFilesPresentInDeleteObject() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/safeDelete/deleteObject"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("nestedObject1.kt")
public void testNestedObject1() throws Exception {
doObjectTest("idea/testData/safeDelete/deleteObject/nestedObject1.kt");
}
@TestMetadata("nestedObject2.kt")
public void testNestedObject2() throws Exception {
doObjectTest("idea/testData/safeDelete/deleteObject/nestedObject2.kt");
}
@TestMetadata("noUsages.kt")
public void testNoUsages() throws Exception {
doObjectTest("idea/testData/safeDelete/deleteObject/noUsages.kt");
}
@TestMetadata("object1.kt")
public void testObject1() throws Exception {
doObjectTest("idea/testData/safeDelete/deleteObject/object1.kt");
}
@TestMetadata("object2.kt")
public void testObject2() throws Exception {
doObjectTest("idea/testData/safeDelete/deleteObject/object2.kt");
}
@TestMetadata("unsafeImport.kt")
public void testUnsafeImport() throws Exception {
doObjectTest("idea/testData/safeDelete/deleteObject/unsafeImport.kt");
}
}
public static Test suite() {
TestSuite suite = new TestSuite("JetSafeDeleteTestGenerated");
suite.addTestSuite(DeleteClass.class);
suite.addTestSuite(DeleteObject.class);
return suite;
}
}