mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Context-dependent refactoring actions in popup menu and refactor this popup, continued
GitOrigin-RevId: ce9c010f8fb8a3313d9bb59c6e1197956042b7bd
This commit is contained in:
committed by
intellij-monorepo-bot
parent
7a5d54cd82
commit
4192cfeaf0
@@ -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;
|
||||
|
||||
+28
-2
@@ -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
|
||||
|
||||
+4
-3
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user