IDEA-259023 refactor this: suggest Safe Delete for declarations only

GitOrigin-RevId: ad8a16589415a16ce8b4df2c8b16e2aed1fc6430
This commit is contained in:
Alexandr Suhinin
2021-01-08 10:43:42 +00:00
committed by intellij-monorepo-bot
parent ac72177dfa
commit a6f67cae1f
8 changed files with 63 additions and 3 deletions
@@ -0,0 +1,8 @@
public class Test {
void test(){
foo<caret>();
}
void foo(){ }
}
@@ -0,0 +1,7 @@
public class Test {
void test(){
int x = 42;
System.out.println(x<caret>);
}
}
@@ -0,0 +1,6 @@
public class <caret>Test {
void test(){
System.out.println();
}
}
@@ -0,0 +1,6 @@
public class Test {
void test<caret>(){
System.out.println();
}
}
@@ -0,0 +1,6 @@
public class Test {
void test(){
int x<caret> = 42;
}
}
@@ -83,10 +83,30 @@ class RefactorThisTest: LightJavaCodeInsightTestCase() {
assertFalse(doActionExists<TurnRefsToSuperAction>())
}
fun testSafeDeleteIsFiltered() {
fun testSafeDeleteIsFilteredOnClassReference() {
assertFalse(doActionExists<SafeDeleteAction>())
}
fun testSafeDeleteIsFilteredOnMethodReference() {
assertFalse(doActionExists<SafeDeleteAction>())
}
fun testSafeDeleteIsFilteredOnVariableReference() {
assertFalse(doActionExists<SafeDeleteAction>())
}
fun testSafeDeleteOnClassDeclaration() {
assertTrue(doActionExists<SafeDeleteAction>())
}
fun testSafeDeleteOnMethodDeclaration() {
assertTrue(doActionExists<SafeDeleteAction>())
}
fun testSafeDeleteOnVariableDeclaration() {
assertTrue(doActionExists<SafeDeleteAction>())
}
fun testMoveIsFilteredOnStatement() {
assertFalse(doActionExists<MoveAction>())
}
@@ -22,6 +22,7 @@ import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.RefactoringActionHandler;
import com.intellij.refactoring.safeDelete.SafeDeleteHandler;
import com.intellij.refactoring.safeDelete.SafeDeleteProcessor;
@@ -61,8 +62,14 @@ public class SafeDeleteAction extends BaseRefactoringAction {
@NotNull PsiFile file,
@NotNull DataContext context,
@NotNull String place) {
if (place.equals(ActionPlaces.REFACTORING_QUICKLIST)) return false;
return isAvailableOnElementInEditorAndFile(element, editor, file, context);
PsiElement targetElement = element;
if (place.equals(ActionPlaces.REFACTORING_QUICKLIST)) {
PsiElement caretElement = BaseRefactoringAction.getElementAtCaret(editor, file);
if (! PsiTreeUtil.isAncestor(element, caretElement, false)) {
targetElement = caretElement;
}
}
return isAvailableOnElementInEditorAndFile(targetElement, editor, file, context);
}
@Override