From c431475c6e8bb61e37aaa631b02dd345ca84f5fc Mon Sep 17 00:00:00 2001 From: Louis Vignier Date: Fri, 8 Dec 2023 20:38:58 +0100 Subject: [PATCH] [codeInspection.ui] Don't register custom inspection actions in ActionManager #IDEA-340285 Fixed GitOrigin-RevId: ff5089a6f5200963dbe447f58618f72753f710ec --- .../custom/RegExpProfileActionProvider.java | 18 ++++++++++++--- .../ui/CustomInspectionActions.java | 7 ++---- .../ui/InspectionProfileActionProvider.java | 12 +++++++--- .../ui/SingleInspectionProfilePanel.java | 22 +++++++++++++------ ...StructuralSearchProfileActionProvider.java | 17 +++++++++++--- 5 files changed, 55 insertions(+), 21 deletions(-) diff --git a/RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/RegExpProfileActionProvider.java b/RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/RegExpProfileActionProvider.java index 5c040cf782a8..c9bf829f4113 100644 --- a/RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/RegExpProfileActionProvider.java +++ b/RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/RegExpProfileActionProvider.java @@ -3,6 +3,7 @@ package org.intellij.lang.regexp.inspection.custom; import com.intellij.codeInspection.InspectionProfileEntry; import com.intellij.codeInspection.ex.InspectionProfileModifiableModel; +import com.intellij.openapi.actionSystem.ActionGroup; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.DefaultActionGroup; @@ -16,14 +17,25 @@ import org.intellij.lang.regexp.RegExpBundle; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.List; + public class RegExpProfileActionProvider extends InspectionProfileActionProvider { @Override - public @Nullable AddInspectionActionGroup getAddActions(@NotNull SingleInspectionProfilePanel panel) { - final var group = new DefaultActionGroup( + public @Nullable ActionGroup getAddActions(@NotNull SingleInspectionProfilePanel panel) { + return getActionGroup(panel); + } + + @Override + public List getActionsToRegister(SingleInspectionProfilePanel panel) { + return List.of(new ActionToRegister(getActionGroup(panel), "regexp.profile.action.provider.add.group")); + } + + @NotNull + private static DefaultActionGroup getActionGroup(@NotNull SingleInspectionProfilePanel panel) { + return new DefaultActionGroup( new AddCustomRegExpInspectionAction(panel, RegExpBundle.message("action.add.regexp.search.inspection.text"), false), new AddCustomRegExpInspectionAction(panel, RegExpBundle.message("action.add.regexp.replace.inspection.text"), true) ); - return new AddInspectionActionGroup(group, "regexp.profile.action.provider.add.group"); } static final class AddCustomRegExpInspectionAction extends DumbAwareAction { diff --git a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/CustomInspectionActions.java b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/CustomInspectionActions.java index ec1f9990634d..1b15f17a12cd 100644 --- a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/CustomInspectionActions.java +++ b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/CustomInspectionActions.java @@ -21,11 +21,8 @@ public final class CustomInspectionActions { public static DefaultActionGroup getAddActionGroup(SingleInspectionProfilePanel panel) { final DefaultActionGroup actionGroup = new DefaultActionGroup(); InspectionProfileActionProvider.EP_NAME.getExtensionList().forEach(provider -> { - final var groupInfo = provider.getAddActions(panel); - if (groupInfo != null) { - ActionManager.getInstance().replaceAction(groupInfo.actionId(), groupInfo.group()); - actionGroup.add(groupInfo.group()); - } + final var group = provider.getAddActions(panel); + if (group != null) actionGroup.add(group); }); if (actionGroup.getChildrenCount() == 0) return null; actionGroup.setPopup(true); diff --git a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/InspectionProfileActionProvider.java b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/InspectionProfileActionProvider.java index e9563d8f585e..cc6a9c10415d 100644 --- a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/InspectionProfileActionProvider.java +++ b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/InspectionProfileActionProvider.java @@ -30,13 +30,11 @@ public abstract class InspectionProfileActionProvider { return List.of(); } - public record AddInspectionActionGroup(@NotNull ActionGroup group, @NotNull String actionId) {} - /** * @return actions to add custom inspections in the given inspection profile panel. */ @Nullable - public AddInspectionActionGroup getAddActions(@NotNull SingleInspectionProfilePanel panel) { + public ActionGroup getAddActions(@NotNull SingleInspectionProfilePanel panel) { return null; } @@ -51,4 +49,12 @@ public abstract class InspectionProfileActionProvider { * Called when an inspection entry has been deleted. */ public void deleteInspection(InspectionProfileEntry entry, String shortName) {} + + public record ActionToRegister(@NotNull AnAction action, @NotNull String actionId) {} + + /** + * @return list of actions registered in the inspection profile configurable UI.
+ * Such actions can be called from inspection descriptions with {@code }. + */ + public List getActionsToRegister(SingleInspectionProfilePanel panel) { return List.of(); } } diff --git a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/SingleInspectionProfilePanel.java b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/SingleInspectionProfilePanel.java index 8b4b0021c11c..b86a9b19df00 100644 --- a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/SingleInspectionProfilePanel.java +++ b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/SingleInspectionProfilePanel.java @@ -1126,7 +1126,11 @@ public class SingleInspectionProfilePanel extends JPanel { myDisposable = null; } - public static HyperlinkAdapter createSettingsHyperlinkListener(Project project){ + public static HyperlinkAdapter createSettingsHyperlinkListener(Project project) { + return createSettingsHyperlinkListener(project, null); + } + + public static HyperlinkAdapter createSettingsHyperlinkListener(Project project, SingleInspectionProfilePanel panel) { return new HyperlinkAdapter() { @Override protected void hyperlinkActivated(@NotNull HyperlinkEvent e) { @@ -1149,10 +1153,14 @@ public class SingleInspectionProfilePanel extends JPanel { } } } - else if (url.getScheme().equals("action")) { - AnAction action = ActionManager.getInstance().getAction(url.getAuthority()); - if (action != null) { - if (action instanceof ActionGroup group) { + else if (url.getScheme().equals("action") && panel != null) { + final var action = InspectionProfileActionProvider.EP_NAME.getExtensionList().stream() + .flatMap(provider -> provider.getActionsToRegister(panel).stream()) + .filter(a -> a.actionId().equals(url.getAuthority())) + .findFirst(); + if (action.isPresent()) { + final AnAction urlAction = action.get().action(); + if (urlAction instanceof ActionGroup group) { final ActionPopupMenu menu = ActionManager.getInstance().createActionPopupMenu(ActionPlaces.POPUP, group); final Point point = new Point(); if (e.getInputEvent() instanceof MouseEvent mouseEvent) { @@ -1164,7 +1172,7 @@ public class SingleInspectionProfilePanel extends JPanel { final AnActionEvent event = AnActionEvent.createFromInputEvent( e.getInputEvent(), ActionPlaces.UNKNOWN, null, DataContext.EMPTY_CONTEXT ); - action.actionPerformed(event); + action.get().action().actionPerformed(event); } } } @@ -1182,7 +1190,7 @@ public class SingleInspectionProfilePanel extends JPanel { private JPanel createInspectionProfileSettingsPanel() { myDescription = new DescriptionEditorPane(); - myDescription.addHyperlinkListener(createSettingsHyperlinkListener(getProject())); + myDescription.addHyperlinkListener(createSettingsHyperlinkListener(getProject(), this)); initToolStates(); fillTreeData(myProfileFilter != null ? myProfileFilter.getFilter() : null, true); diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/inspection/StructuralSearchProfileActionProvider.java b/platform/structuralsearch/source/com/intellij/structuralsearch/inspection/StructuralSearchProfileActionProvider.java index c920031bf729..8e71f86c7f30 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/inspection/StructuralSearchProfileActionProvider.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/inspection/StructuralSearchProfileActionProvider.java @@ -7,6 +7,7 @@ import com.intellij.codeInspection.ex.InspectionProfileImpl; import com.intellij.codeInspection.ex.InspectionProfileModifiableModel; import com.intellij.codeInspection.ex.InspectionToolWrapper; import com.intellij.codeInspection.ex.ScopeToolState; +import com.intellij.openapi.actionSystem.ActionGroup; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.DefaultActionGroup; import com.intellij.openapi.application.ApplicationManager; @@ -27,6 +28,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Collections; +import java.util.List; /** * @author Bas Leijdekkers @@ -48,13 +50,22 @@ public class StructuralSearchProfileActionProvider extends InspectionProfileActi } @Override - public @Nullable AddInspectionActionGroup getAddActions(@NotNull SingleInspectionProfilePanel panel) { + public @Nullable ActionGroup getAddActions(@NotNull SingleInspectionProfilePanel panel) { enableSSIfDisabled(panel.getProfile(), panel.getProject()); - final var group = new DefaultActionGroup( + return getActionGroup(panel); + } + + @Override + public List getActionsToRegister(SingleInspectionProfilePanel panel) { + return List.of(new ActionToRegister(getActionGroup(panel), "ssr.profile.action.provider.add.group")); + } + + @NotNull + private static DefaultActionGroup getActionGroup(@NotNull SingleInspectionProfilePanel panel) { + return new DefaultActionGroup( new AddInspectionAction(panel, SSRBundle.message("SSRInspection.add.search.template.button"), false), new AddInspectionAction(panel, SSRBundle.message("SSRInspection.add.replace.template.button"), true) ); - return new AddInspectionActionGroup(group, "ssr.profile.action.provider.add.group"); } @Override