From 4192cfeaf0a1634e5a16e67008eaf1cf70016d00 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Fri, 26 Apr 2019 13:16:32 +0200 Subject: [PATCH] Context-dependent refactoring actions in popup menu and refactor this popup, continued GitOrigin-RevId: ce9c010f8fb8a3313d9bb59c6e1197956042b7bd --- .../actions/EncapsulateFieldsAction.java | 7 ++++- .../ReplaceConstructorWithBuilderAction.java | 30 +++++++++++++++++-- .../ReplaceConstructorWithFactoryAction.java | 7 +++-- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/java/java-impl/src/com/intellij/refactoring/actions/EncapsulateFieldsAction.java b/java/java-impl/src/com/intellij/refactoring/actions/EncapsulateFieldsAction.java index 474a10f0fff8..b50fdc04dd3f 100644 --- a/java/java-impl/src/com/intellij/refactoring/actions/EncapsulateFieldsAction.java +++ b/java/java-impl/src/com/intellij/refactoring/actions/EncapsulateFieldsAction.java @@ -16,6 +16,7 @@ package com.intellij.refactoring.actions; import com.intellij.lang.java.JavaLanguage; +import com.intellij.openapi.actionSystem.ActionPlaces; import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.editor.Editor; import com.intellij.psi.PsiClass; @@ -34,10 +35,14 @@ public class EncapsulateFieldsAction extends BaseJavaRefactoringAction { } @Override - protected boolean isAvailableOnElementInEditorAndFile(@NotNull PsiElement element, @NotNull Editor editor, @NotNull PsiFile file, @NotNull DataContext context) { + protected boolean isAvailableOnElementInEditorAndFile(@NotNull PsiElement element, @NotNull Editor editor, @NotNull PsiFile file, + @NotNull DataContext context, @NotNull String place) { final PsiElement psiElement = file.findElementAt(editor.getCaretModel().getOffset()); final PsiClass containingClass = PsiTreeUtil.getParentOfType(psiElement, PsiClass.class, false); if (containingClass != null) { + if (ActionPlaces.isPopupPlace(place) || place.equals(ActionPlaces.REFACTORING_QUICKLIST)) { + if (PsiTreeUtil.getParentOfType(psiElement, PsiField.class, false) == null) return false; + } final PsiField[] fields = containingClass.getFields(); for (PsiField field : fields) { if (isAcceptedField(field)) return true; diff --git a/java/java-impl/src/com/intellij/refactoring/actions/ReplaceConstructorWithBuilderAction.java b/java/java-impl/src/com/intellij/refactoring/actions/ReplaceConstructorWithBuilderAction.java index 048c46b06394..3f09ff09d7bd 100644 --- a/java/java-impl/src/com/intellij/refactoring/actions/ReplaceConstructorWithBuilderAction.java +++ b/java/java-impl/src/com/intellij/refactoring/actions/ReplaceConstructorWithBuilderAction.java @@ -16,14 +16,19 @@ package com.intellij.refactoring.actions; +import com.intellij.lang.java.JavaLanguage; +import com.intellij.openapi.actionSystem.ActionPlaces; import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.editor.Editor; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.util.PsiTreeUtil; import com.intellij.refactoring.RefactoringActionHandler; import com.intellij.refactoring.replaceConstructorWithBuilder.ReplaceConstructorWithBuilderHandler; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public class ReplaceConstructorWithBuilderAction extends BaseJavaRefactoringAction{ @Override @@ -32,11 +37,32 @@ public class ReplaceConstructorWithBuilderAction extends BaseJavaRefactoringActi } @Override - protected boolean isAvailableOnElementInEditorAndFile(@NotNull PsiElement element, @NotNull Editor editor, @NotNull PsiFile file, @NotNull DataContext context) { + protected boolean isAvailableOnElementInEditorAndFile(@NotNull PsiElement element, @NotNull Editor editor, @NotNull PsiFile file, + @NotNull DataContext context, @NotNull String place) { final int offset = editor.getCaretModel().getOffset(); final PsiElement elementAt = file.findElementAt(offset); final PsiClass psiClass = ReplaceConstructorWithBuilderHandler.getParentNamedClass(elementAt); - return psiClass != null && psiClass.getConstructors().length > 0 && !psiClass.isEnum(); + if (psiClass == null || psiClass.getConstructors().length == 0 || psiClass.isEnum()) { + return false; + } + if (ActionPlaces.isPopupPlace(place) || place.equals(ActionPlaces.REFACTORING_QUICKLIST)) { + PsiMethod method = getJavaMethodHeader(elementAt); + return method != null && method.isConstructor(); + } + return true; + } + + @Nullable + public static PsiMethod getJavaMethodHeader(@Nullable PsiElement element) { + if (element == null) return null; + if (element.getLanguage() != JavaLanguage.INSTANCE) return null; + PsiMethod psiMethod = PsiTreeUtil.getParentOfType(element, PsiMethod.class, false); + if (psiMethod != null && (element == psiMethod || element == psiMethod.getNameIdentifier() || + PsiTreeUtil.isAncestor(psiMethod.getModifierList(), element, false) || + PsiTreeUtil.isAncestor(psiMethod.getParameterList(), element, false))) { + return psiMethod; + } + return null; } @Override diff --git a/java/java-impl/src/com/intellij/refactoring/actions/ReplaceConstructorWithFactoryAction.java b/java/java-impl/src/com/intellij/refactoring/actions/ReplaceConstructorWithFactoryAction.java index 0b0d2028b4de..3cd07ba9ca8d 100644 --- a/java/java-impl/src/com/intellij/refactoring/actions/ReplaceConstructorWithFactoryAction.java +++ b/java/java-impl/src/com/intellij/refactoring/actions/ReplaceConstructorWithFactoryAction.java @@ -45,9 +45,10 @@ public class ReplaceConstructorWithFactoryAction extends BaseJavaRefactoringActi @NotNull Editor editor, @NotNull PsiFile file, @NotNull DataContext context) { - return (element instanceof PsiMethod && - ((PsiMethod)element).isConstructor() && - acceptClass(((PsiMethod)element).getContainingClass()) || + PsiMethod method = ReplaceConstructorWithBuilderAction.getJavaMethodHeader(element); + return (method != null && + method.isConstructor() && + acceptClass(method.getContainingClass()) || acceptClass(element)) && element.getLanguage().isKindOf(JavaLanguage.INSTANCE); }