[codeInspection.ui] Don't register custom inspection actions in ActionManager

#IDEA-340285 Fixed

GitOrigin-RevId: ff5089a6f5200963dbe447f58618f72753f710ec
This commit is contained in:
Louis Vignier
2023-12-08 20:38:58 +01:00
committed by intellij-monorepo-bot
parent d8c0a84a53
commit c431475c6e
5 changed files with 55 additions and 21 deletions

View File

@@ -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<ActionToRegister> 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 {

View File

@@ -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);

View File

@@ -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.<br>
* Such actions can be called from inspection descriptions with {@code <a href="action:id"></a>}.
*/
public List<ActionToRegister> getActionsToRegister(SingleInspectionProfilePanel panel) { return List.of(); }
}

View File

@@ -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);

View File

@@ -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<ActionToRegister> 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