mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
Resource variable deletion logic moved to resource list (fix for safe delete and inline)
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.psi.impl.source.tree.java;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.PsiImplUtil;
|
||||
import com.intellij.psi.impl.source.tree.CompositePsiElement;
|
||||
@@ -63,6 +64,15 @@ public class PsiResourceListImpl extends CompositePsiElement implements PsiResou
|
||||
return PsiImplUtil.processDeclarationsInResourceList(this, processor, state, lastParent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteChildInternal(@NotNull final ASTNode child) {
|
||||
if (child.getPsi() instanceof PsiResourceVariable && getResourceVariablesCount() == 1) {
|
||||
getTreeParent().deleteChildInternal(this);
|
||||
}
|
||||
|
||||
super.deleteChildInternal(child);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PsiResourceList:" + getText();
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
class ARM {
|
||||
void f() {
|
||||
try (AutoCloseable <caret>r = null) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class ARM {
|
||||
void f() {
|
||||
try {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,10 @@ package com.intellij.refactoring;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.codeInsight.TargetElementUtilBase;
|
||||
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
|
||||
import com.intellij.openapi.roots.ProjectRootManager;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
@@ -21,6 +23,11 @@ public class SafeDeleteTest extends MultiFileTestCase {
|
||||
return JavaTestUtil.getJavaTestDataPath();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTestRoot() {
|
||||
return "/refactoring/safeDelete/";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean clearModelBeforeConfiguring() {
|
||||
return true;
|
||||
@@ -36,6 +43,7 @@ public class SafeDeleteTest extends MultiFileTestCase {
|
||||
assertTrue(message, message.startsWith("constructor <b><code>Super.Super()</code></b> has 1 usage that is not safe to delete"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testImplicitCtrCall2() throws Exception {
|
||||
try {
|
||||
doTest("Super");
|
||||
@@ -121,6 +129,11 @@ public class SafeDeleteTest extends MultiFileTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testLastResourceVariable() throws Exception {
|
||||
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_7);
|
||||
doSingleFileTest();
|
||||
}
|
||||
|
||||
private void doTest(@NonNls final String qClassName) throws Exception {
|
||||
doTest(new PerformAction() {
|
||||
@Override
|
||||
@@ -131,20 +144,26 @@ public class SafeDeleteTest extends MultiFileTestCase {
|
||||
});
|
||||
}
|
||||
|
||||
private void performAction(String qClassName) throws Exception {
|
||||
PsiClass aClass = myJavaFacade.findClass(qClassName, GlobalSearchScope.allScope(getProject()));
|
||||
private void doSingleFileTest() throws Exception {
|
||||
configureByFile(getTestRoot() + getTestName(false) + ".java");
|
||||
performAction();
|
||||
checkResultByFile(getTestRoot() + getTestName(false) + "_after.java");
|
||||
}
|
||||
|
||||
private void performAction(final String qClassName) throws Exception {
|
||||
final PsiClass aClass = myJavaFacade.findClass(qClassName, GlobalSearchScope.allScope(getProject()));
|
||||
assertNotNull("Class " + qClassName + " not found", aClass);
|
||||
|
||||
String root = ProjectRootManager.getInstance(getProject()).getContentRoots()[0].getPath();
|
||||
final String root = ProjectRootManager.getInstance(getProject()).getContentRoots()[0].getPath();
|
||||
myRootBefore = configureByFiles(new File(root), aClass.getContainingFile().getVirtualFile());
|
||||
|
||||
performAction();
|
||||
}
|
||||
|
||||
private void performAction() {
|
||||
final PsiElement psiElement = TargetElementUtilBase
|
||||
.findTargetElement(myEditor, TargetElementUtilBase.ELEMENT_NAME_ACCEPTED | TargetElementUtilBase.REFERENCED_ELEMENT_ACCEPTED);
|
||||
|
||||
assertNotNull("No element found in text:\n" + getFile().getText(), psiElement);
|
||||
SafeDeleteHandler.invoke(getProject(), new PsiElement[]{psiElement}, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTestRoot() {
|
||||
return "/refactoring/safeDelete/";
|
||||
}
|
||||
}
|
||||
@@ -71,13 +71,6 @@ public class InlineVariableFix extends InspectionGadgetsFix {
|
||||
replacedElements.add(replacedElement);
|
||||
}
|
||||
HighlightUtils.highlightElements(replacedElements);
|
||||
PsiElement elementToRemove = variable;
|
||||
if (variable instanceof PsiResourceVariable) {
|
||||
final PsiElement parent = variable.getParent();
|
||||
if (parent instanceof PsiResourceList && ((PsiResourceList)parent).getResourceVariablesCount() == 1) {
|
||||
elementToRemove = parent;
|
||||
}
|
||||
}
|
||||
elementToRemove.delete();
|
||||
variable.delete();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user