Skip to content

Commit

Permalink
feat: Use LSP command action
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Dec 11, 2023
1 parent 18567c0 commit 55c6bed
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

import com.google.gson.JsonPrimitive;
import com.intellij.ide.BrowserUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.redhat.devtools.lsp4ij.commands.CommandExecutor;
import com.redhat.devtools.lsp4ij.commands.LSPCommandAction;
import org.eclipse.lsp4j.Command;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class MicroprofileOpenURIAction extends AnAction {
public class MicroprofileOpenURIAction extends LSPCommandAction {

@Override
public void actionPerformed(AnActionEvent e) {
String url = getURL(e);
protected void commandPerformed(@NotNull Command command, @NotNull AnActionEvent anActionEvent) {
String url = getURL(command.getArguments());
if (url != null) {
BrowserUtil.browse(url);
}
}

private String getURL(AnActionEvent e) {
private String getURL(List<Object> arguments) {
String url = null;
List<Object> arguments = e.getData(CommandExecutor.LSP_COMMAND).getArguments();
if (!arguments.isEmpty()) {
Object arg = arguments.get(0);
if (arg instanceof JsonPrimitive) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@
import com.google.gson.JsonObject;
import com.intellij.codeInspection.InspectionProfile;
import com.intellij.codeInspection.ex.InspectionToolWrapper;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.profile.codeInspection.InspectionProfileManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.impl.FakePsiElement;
import com.redhat.devtools.lsp4ij.commands.CommandExecutor;
import com.redhat.devtools.lsp4ij.inspections.AbstractDelegateInspectionWithExclusions;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.inspections.MicroProfilePropertiesUnassignedInspection;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.inspections.MicroProfilePropertiesUnknownInspection;
import com.redhat.devtools.lsp4ij.commands.LSPCommandAction;
import com.redhat.devtools.lsp4ij.inspections.AbstractDelegateInspectionWithExclusions;
import org.eclipse.lsp4j.Command;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -39,7 +38,7 @@
/**
* Action for updating the Microprofile configuration, typically requested by LSP4MP.
*/
public class MicroprofileUpdateConfigurationAction extends AnAction {
public class MicroprofileUpdateConfigurationAction extends LSPCommandAction {
private final Map<String, ConfigurationUpdater> updaters = new HashMap<>();

public MicroprofileUpdateConfigurationAction() {
Expand All @@ -48,24 +47,20 @@ public MicroprofileUpdateConfigurationAction() {
}

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
JsonObject configUpdate = getConfigUpdate(e);
protected void commandPerformed(@NotNull Command command, @NotNull AnActionEvent e) {
JsonObject configUpdate = getConfigUpdate(command);
if (configUpdate != null && e.getProject() != null) {
String section = configUpdate.get("section").getAsString();
ConfigurationUpdater updater = updaters.get(section);
if (updater == null) {
throw new UnsupportedOperationException("Updating "+section+" is not supported yet!");
throw new UnsupportedOperationException("Updating " + section + " is not supported yet!");
}
JsonElement value = configUpdate.get("value");
updater.updateConfiguration(e.getProject(), value);
}
}

private @Nullable JsonObject getConfigUpdate(@NotNull AnActionEvent e) {
@Nullable Command command = e.getData(CommandExecutor.LSP_COMMAND);
if (command == null) {
return null;
}
private @Nullable JsonObject getConfigUpdate(@NotNull Command command) {
List<Object> arguments = command.getArguments();
if (arguments != null && !arguments.isEmpty()) {
Object arg = arguments.get(0);
Expand All @@ -77,7 +72,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
}

interface ConfigurationUpdater {
void updateConfiguration(Project project, JsonElement value);
void updateConfiguration(Project project, JsonElement value);
}

private static class InspectionConfigurationUpdater implements ConfigurationUpdater {
Expand All @@ -95,7 +90,7 @@ public void updateConfiguration(Project project, JsonElement value) {
}
}

private void updateConfiguration(Project project, @NotNull String value) {
private void updateConfiguration(Project project, @NotNull String value) {
InspectionProfile profile = InspectionProfileManager.getInstance(project).getCurrentProfile();
InspectionToolWrapper<?, ?> toolWrapper = profile.getInspectionTool(inspectionId, project);
if (toolWrapper != null && toolWrapper.getTool() instanceof AbstractDelegateInspectionWithExclusions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.Comparator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import com.redhat.devtools.lsp4ij.internal.StringUtils;
import org.eclipse.lsp4j.CodeAction;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.redhat.devtools.lsp4ij.commands.CommandExecutor;
import com.redhat.devtools.lsp4ij.commands.LSPCommandAction;

import java.util.List;

public abstract class QuteAction extends AnAction {
protected String getURL(AnActionEvent e) {
public abstract class QuteAction extends LSPCommandAction {

protected String getURL(List<Object> arguments) {
String url = null;
List<Object> arguments = e.getData(CommandExecutor.LSP_COMMAND).getArguments();
if (!arguments.isEmpty()) {
Object arg = arguments.get(0);
if (arg instanceof JsonPrimitive) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.lsp4j.ExecuteCommandOptions;
import org.eclipse.lsp4j.ExecuteCommandParams;
import org.eclipse.lsp4j.services.LanguageServer;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.net.URI;
Expand All @@ -43,9 +44,9 @@ private LanguageServer getFirstServer(AnActionEvent e) {
return servers.isEmpty()?null:servers.get(0);
}


@Override
public void actionPerformed(AnActionEvent e) {
Command command = e.getData(CommandExecutor.LSP_COMMAND);
protected void commandPerformed(@NotNull Command command, @NotNull AnActionEvent e) {
LanguageServer server = getFirstServer(e);
try {
if (server != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.openapi.project.Project;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.devtools.lsp4ij.commands.CommandExecutor;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.IPsiUtils;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.internal.core.ls.PsiUtilsLSImpl;
import com.redhat.devtools.intellij.qute.psi.QuteSupportForTemplate;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.qute.commons.QuteJavaDefinitionParams;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.Location;
import org.jetbrains.annotations.NotNull;

import java.util.List;

Expand All @@ -35,8 +35,7 @@ public class QuteJavaDefinitionAction extends QuteAction {
private static System.Logger LOGGER = System.getLogger(QuteJavaDefinitionAction.class.getName());

@Override
public void actionPerformed(AnActionEvent e) {
Command command = e.getData(CommandExecutor.LSP_COMMAND);
protected void commandPerformed(@NotNull Command command, @NotNull AnActionEvent e) {
QuteJavaDefinitionParams params = getQuteJavaDefinitionParams(command.getArguments());
if (params != null) {
Project project = e.getProject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import org.eclipse.lsp4j.Command;
import org.jetbrains.annotations.NotNull;

public class QuteOpenURIAction extends QuteAction {

@Override
public void actionPerformed(AnActionEvent e) {
String url = getURL(e);
protected void commandPerformed(@NotNull Command command, @NotNull AnActionEvent e) {
String url = getURL(command.getArguments());
Project project = e.getProject();
if (url != null && project != null) {
LSPIJUtils.openInEditor(url, null, project);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.intellij.codeInspection.InspectionProfile;
import com.intellij.codeInspection.ex.InspectionProfileImpl;
import com.intellij.codeInspection.ex.InspectionToolWrapper;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
Expand All @@ -29,12 +28,12 @@
import com.intellij.profile.codeInspection.InspectionProfileManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.impl.FakePsiElement;
import com.redhat.devtools.intellij.qute.psi.core.inspections.QuteGlobalInspection;
import com.redhat.devtools.intellij.qute.psi.core.inspections.QuteUndefinedObjectInspection;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.devtools.lsp4ij.commands.CommandExecutor;
import com.redhat.devtools.lsp4ij.commands.LSPCommandAction;
import com.redhat.devtools.lsp4ij.inspections.AbstractDelegateInspectionWithExclusions;
import com.redhat.devtools.lsp4ij.operations.diagnostics.SeverityMapping;
import com.redhat.devtools.intellij.qute.psi.core.inspections.QuteGlobalInspection;
import com.redhat.devtools.intellij.qute.psi.core.inspections.QuteUndefinedObjectInspection;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.jetbrains.annotations.NotNull;
Expand All @@ -46,18 +45,18 @@
import java.util.List;
import java.util.Map;

public class QuteUpdateConfigurationAction extends AnAction {
public class QuteUpdateConfigurationAction extends LSPCommandAction {
private final Map<String, ConfigurationUpdater> updaters = new HashMap<>();

public QuteUpdateConfigurationAction() {
updaters.put("qute.validation.enabled", new InspectionEnabler(QuteGlobalInspection.ID));
updaters.put("qute.validation.excluded", new InspectionExclusionsUpdater(QuteGlobalInspection.ID));
updaters.put("qute.validation.undefinedObject.severity", new InspectionSeverityUpdater(QuteUndefinedObjectInspection.ID));
updaters.put("qute.validation.undefinedObject.severity", new InspectionSeverityUpdater(QuteUndefinedObjectInspection.ID));
}

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
JsonObject configUpdate = getConfigUpdate(e);
protected void commandPerformed(@NotNull Command command, @NotNull AnActionEvent e) {
JsonObject configUpdate = getConfigUpdate(command);
if (configUpdate != null && e.getProject() != null) {
String section = configUpdate.get("section").getAsString();
ConfigurationUpdater updater = updaters.get(section);
Expand All @@ -68,11 +67,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
}
}

private @Nullable JsonObject getConfigUpdate(@NotNull AnActionEvent e) {
@Nullable Command command = e.getData(CommandExecutor.LSP_COMMAND);
if (command == null) {
return null;
}
private @Nullable JsonObject getConfigUpdate(@NotNull Command command) {
List<Object> arguments = command.getArguments();
if (arguments != null && !arguments.isEmpty()) {
Object arg = arguments.get(0);
Expand All @@ -84,7 +79,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
}

interface ConfigurationUpdater {
void updateConfiguration(Project project, JsonObject value);
void updateConfiguration(Project project, JsonObject value);
}

private static class InspectionSeverityUpdater implements ConfigurationUpdater {
Expand Down Expand Up @@ -123,7 +118,7 @@ private void setSeverity(Project project, DiagnosticSeverity severity) {
private static class InspectionExclusionsUpdater extends InspectionSeverityUpdater {

InspectionExclusionsUpdater(String inspectionId) {
super(inspectionId) ;
super(inspectionId);
}

@Override
Expand All @@ -147,8 +142,9 @@ protected void addToExclusions(Project project, @NotNull String value) {

/**
* returns file value path relative to the given project
*
* @param project the reference project
* @param value the file path URI to get a relative path from
* @param value the file path URI to get a relative path from
* @return file value path relative to the given project
*/
private String sanitize(@NotNull Project project, @NotNull String value) {
Expand Down

0 comments on commit 55c6bed

Please sign in to comment.