Skip to content

Commit

Permalink
feat: expose Qute server settings
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon committed Aug 29, 2023
1 parent d379c70 commit b7e4cd5
Show file tree
Hide file tree
Showing 20 changed files with 744 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.util.Collection;
import java.util.List;

import static com.redhat.devtools.intellij.lsp4ij.operations.diagnostics.SeverityMapping.toHighlightSeverity;

/**
* Intellij {@link ExternalAnnotator} implementation which get the current LSP diagnostics for a given file and translate
* them into Intellij {@link com.intellij.lang.annotation.Annotation}.
Expand Down Expand Up @@ -95,16 +97,4 @@ private static void createAnnotation(Diagnostic diagnostic, Document document, L
builder.create();
}

private static HighlightSeverity toHighlightSeverity(DiagnosticSeverity severity) {
switch (severity) {
case Warning:
return HighlightSeverity.WEAK_WARNING;
case Hint:
case Information:
return HighlightSeverity.INFORMATION;
default:
return HighlightSeverity.ERROR;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.redhat.devtools.intellij.lsp4ij.operations.diagnostics;

import com.intellij.codeHighlighting.HighlightDisplayLevel;
import com.intellij.codeInsight.daemon.HighlightDisplayKey;
import com.intellij.codeInspection.InspectionProfile;
import com.intellij.lang.annotation.HighlightSeverity;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Utility class to map language servers' {@link DiagnosticSeverity} to Intellij's {@link HighlightSeverity}, and vice-versa.
*/
public class SeverityMapping {

public static String NONE_SEVERITY = "none";

private SeverityMapping() {
}

/**
* Maps language server's {@link DiagnosticSeverity} to Intellij's {@link HighlightSeverity}
* @param severity the {@link DiagnosticSeverity} to map
* @return the matching {@link HighlightSeverity}
*/
public static @NotNull HighlightSeverity toHighlightSeverity(@Nullable DiagnosticSeverity severity) {
if (severity == null) {
return HighlightSeverity.INFORMATION;
}
switch (severity) {
case Warning:
return HighlightSeverity.WEAK_WARNING;
case Hint:
case Information:
return HighlightSeverity.INFORMATION;
default:
return HighlightSeverity.ERROR;
}
}

/**
* Maps {@link HighlightSeverity} to {@link DiagnosticSeverity} levels used by language servers.
* <ul>
* <li>Any severity below <code>HighlightSeverity.INFORMATION</code> is mapped to <code>null</code></li>
* <li>Any severity below <code>HighlightSeverity.WEAK_WARNING</code> is mapped to <code>DiagnosticSeverity.Information</code></li>
* <li>Any severity below <code>HighlightSeverity.ERROR</code> is mapped to <code>DiagnosticSeverity.Warning</code></li>
* <li>Any other severity is mapped to <code>DiagnosticSeverity.Error</code></li>
* </ul>
*
* @param severity the severity to map to a {@link DiagnosticSeverity}
* @return the matching {@link DiagnosticSeverity}
*/
public static @Nullable DiagnosticSeverity getSeverity(@NotNull HighlightSeverity severity) {
if (HighlightSeverity.INFORMATION.compareTo(severity) > 0) {
return null;
}
if (HighlightSeverity.WEAK_WARNING.compareTo(severity) > 0) {
return DiagnosticSeverity.Information;
}
if (HighlightSeverity.ERROR.compareTo(severity) > 0) {
return DiagnosticSeverity.Warning;
}
return DiagnosticSeverity.Error;
}

/**
* Returns {@link DiagnosticSeverity} as lower case, or <code>none</code> if severity is <code>null</code>.
* @param severity the {@link DiagnosticSeverity} to transform as {@link String}
* @return {@link DiagnosticSeverity} as lower case, or <code>none</code> if severity is <code>null</code>.
*/
public static @NotNull String toString(@Nullable DiagnosticSeverity severity) {
return (severity == null)? NONE_SEVERITY : severity.name().toLowerCase();
}

public static DiagnosticSeverity getSeverity(String inspectionId, InspectionProfile profile) {
if (!isInspectionEnabled(inspectionId, profile)) {
return null;
}
return getSeverity(getErrorLevel(inspectionId, profile));
}

private static @NotNull HighlightSeverity getErrorLevel(String inspectionId, InspectionProfile profile) {
HighlightDisplayLevel level = profile.getErrorLevel(HighlightDisplayKey.find(inspectionId), null);
return level.getSeverity();
}

private static boolean isInspectionEnabled(@NotNull String inspectionId, @NotNull InspectionProfile profile) {
return profile.isToolEnabled(HighlightDisplayKey.find(inspectionId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.openapi.project.Project;
import com.intellij.profile.codeInspection.InspectionProfileManager;
import com.redhat.devtools.intellij.lsp4ij.operations.diagnostics.SeverityMapping;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.inspections.*;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
Expand All @@ -31,84 +33,49 @@
public class MicroProfileInspectionsInfo {

//See https://github.com/eclipse/lsp4mp/blob/6b483c4d292bfebabd13311d6217291da2d5d169/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/java/org/eclipse/lsp4mp/settings/MicroProfileValidationSettings.java#L36
private boolean enabled = true;
private ProblemSeverity syntaxSeverity = ProblemSeverity.error;
private ProblemSeverity unknownSeverity = ProblemSeverity.warning;
private ProblemSeverity duplicateSeverity = ProblemSeverity.warning;
private ProblemSeverity valueSeverity = ProblemSeverity.error;
private ProblemSeverity requiredSeverity = ProblemSeverity.none;
private ProblemSeverity expressionSeverity = ProblemSeverity.error;
private DiagnosticSeverity syntaxSeverity = DiagnosticSeverity.Error;
private DiagnosticSeverity unknownSeverity = DiagnosticSeverity.Warning;
private DiagnosticSeverity duplicateSeverity = DiagnosticSeverity.Warning;
private DiagnosticSeverity valueSeverity = DiagnosticSeverity.Error;
private DiagnosticSeverity requiredSeverity = null;
private DiagnosticSeverity expressionSeverity = DiagnosticSeverity.Error;

private MicroProfileInspectionsInfo() {
}

public static MicroProfileInspectionsInfo getMicroProfileInspectionInfo(Project project) {
MicroProfileInspectionsInfo wrapper = new MicroProfileInspectionsInfo();
InspectionProfile profile = InspectionProfileManager.getInstance(project).getCurrentProfile();
boolean syntaxEnabled = isInspectionEnabled(MicroProfilePropertiesSyntaxInspection.ID, profile);
boolean unknownEnabled = isInspectionEnabled(MicroProfilePropertiesUnknownInspection.ID, profile);
boolean duplicatedEnabled = isInspectionEnabled(MicroProfilePropertiesDuplicatesInspection.ID, profile);
boolean valueEnabled = isInspectionEnabled(MicroProfilePropertiesValueInspection.ID, profile);
boolean requiredEnabled = isInspectionEnabled(MicroProfilePropertiesRequiredInspection.ID, profile);
boolean expressionsEnabled = isInspectionEnabled(MicroProfilePropertiesExpressionsInspection.ID, profile);
wrapper.enabled = syntaxEnabled
|| unknownEnabled
|| duplicatedEnabled
|| valueEnabled
|| requiredEnabled
|| expressionsEnabled;

wrapper.syntaxSeverity = getSeverity(syntaxEnabled, MicroProfilePropertiesSyntaxInspection.ID, profile);
wrapper.unknownSeverity = getSeverity(unknownEnabled, MicroProfilePropertiesUnknownInspection.ID, profile);
wrapper.duplicateSeverity = getSeverity(duplicatedEnabled, MicroProfilePropertiesDuplicatesInspection.ID, profile);
wrapper.valueSeverity = getSeverity(valueEnabled, MicroProfilePropertiesValueInspection.ID, profile);
wrapper.requiredSeverity = getSeverity(requiredEnabled, MicroProfilePropertiesRequiredInspection.ID, profile);
wrapper.expressionSeverity = getSeverity(expressionsEnabled, MicroProfilePropertiesExpressionsInspection.ID, profile);
wrapper.syntaxSeverity = SeverityMapping.getSeverity(MicroProfilePropertiesSyntaxInspection.ID, profile);
wrapper.unknownSeverity = SeverityMapping.getSeverity(MicroProfilePropertiesUnknownInspection.ID, profile);
wrapper.duplicateSeverity = SeverityMapping.getSeverity(MicroProfilePropertiesDuplicatesInspection.ID, profile);
wrapper.valueSeverity = SeverityMapping.getSeverity(MicroProfilePropertiesValueInspection.ID, profile);
wrapper.requiredSeverity = SeverityMapping.getSeverity(MicroProfilePropertiesRequiredInspection.ID, profile);
wrapper.expressionSeverity = SeverityMapping.getSeverity(MicroProfilePropertiesExpressionsInspection.ID, profile);
return wrapper;
}

private static ProblemSeverity getSeverity(boolean enabled, String inspectionId, InspectionProfile profile) {
if (!enabled) {
return ProblemSeverity.none;
}
return ProblemSeverity.getSeverity(getErrorLevel(inspectionId, profile));

}

private static @NotNull HighlightSeverity getErrorLevel(String inspectionId, InspectionProfile profile) {
HighlightDisplayLevel level = profile.getErrorLevel(HighlightDisplayKey.find(inspectionId), null);
return level.getSeverity();
}

private static boolean isInspectionEnabled(@NotNull String inspectionId, @NotNull InspectionProfile profile) {
return profile.isToolEnabled(HighlightDisplayKey.find(inspectionId));
}

public boolean enabled() {
return enabled;
}

public ProblemSeverity unknownSeverity() {
public DiagnosticSeverity unknownSeverity() {
return unknownSeverity;
}

public ProblemSeverity valueSeverity() {
public DiagnosticSeverity valueSeverity() {
return valueSeverity;
}

public ProblemSeverity expressionSeverity() {
public DiagnosticSeverity expressionSeverity() {
return expressionSeverity;
}

public ProblemSeverity duplicateSeverity() {
public DiagnosticSeverity duplicateSeverity() {
return duplicateSeverity;
}

public ProblemSeverity syntaxSeverity() {
public DiagnosticSeverity syntaxSeverity() {
return syntaxSeverity;
}

public ProblemSeverity requiredSeverity() {
public DiagnosticSeverity requiredSeverity() {
return requiredSeverity;
}

Expand All @@ -117,15 +84,14 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MicroProfileInspectionsInfo that = (MicroProfileInspectionsInfo) o;
return enabled == that.enabled
&& syntaxSeverity == that.syntaxSeverity && unknownSeverity == that.unknownSeverity
return syntaxSeverity == that.syntaxSeverity && unknownSeverity == that.unknownSeverity
&& duplicateSeverity == that.duplicateSeverity && valueSeverity == that.valueSeverity
&& requiredSeverity == that.requiredSeverity && expressionSeverity == that.expressionSeverity;
}

@Override
public int hashCode() {
return Objects.hash(enabled, syntaxSeverity, unknownSeverity, duplicateSeverity, valueSeverity, requiredSeverity,
return Objects.hash(syntaxSeverity, unknownSeverity, duplicateSeverity, valueSeverity, requiredSeverity,
expressionSeverity);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.intellij.openapi.project.Project;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.xmlb.annotations.Tag;
import com.redhat.devtools.intellij.lsp4ij.operations.diagnostics.SeverityMapping;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -44,6 +46,12 @@ public class UserDefinedMicroProfileSettings implements PersistentStateComponent

private volatile MyState myState = new MyState();

private final Project project;

public UserDefinedMicroProfileSettings(Project project) {
this.project = project;
}

private final List<Runnable> myChangeHandlers = ContainerUtil.createConcurrentList();

public static @NotNull UserDefinedMicroProfileSettings getInstance(@NotNull Project project) {
Expand All @@ -63,6 +71,13 @@ public void fireStateChanged() {
handler.run();
}
}
public boolean isValidationEnabled() {
return myState.myValidationEnabled;
}

public void setValidationEnabled(boolean validationEnabled) {
myState.myValidationEnabled = validationEnabled;
}

// ---------- Properties

Expand Down Expand Up @@ -103,8 +118,8 @@ public void loadState(@NotNull MyState state) {
*
* @return the proper settings expected by the MicroProfile language server.
*/
public Map<String, Object> toSettingsForMicroProfileLS(MicroProfileInspectionsInfo inspectionsInfo) {

public Map<String, Object> toSettingsForMicroProfileLS() {
MicroProfileInspectionsInfo inspectionsInfo = MicroProfileInspectionsInfo.getMicroProfileInspectionInfo(project);
Map<String, Object> settings = new HashMap<>();
Map<String, Object> microprofile = new HashMap<>();
settings.put("microprofile", microprofile);
Expand All @@ -125,7 +140,7 @@ public Map<String, Object> toSettingsForMicroProfileLS(MicroProfileInspectionsIn

Map<String, Object> validation = new HashMap<>();
tools.put("validation", validation);
validation.put("enabled", inspectionsInfo.enabled());
validation.put("enabled", isValidationEnabled());
validation.put("syntax", getSeverityNode(inspectionsInfo.syntaxSeverity()));
validation.put("unknown", getSeverityNode(inspectionsInfo.unknownSeverity()));
validation.put("duplicate", getSeverityNode(inspectionsInfo.duplicateSeverity()));
Expand All @@ -135,12 +150,16 @@ public Map<String, Object> toSettingsForMicroProfileLS(MicroProfileInspectionsIn
return settings;
}

private Map<String, String> getSeverityNode(ProblemSeverity severity) {
return Collections.singletonMap("severity", severity.name());
private Map<String, String> getSeverityNode(DiagnosticSeverity severity) {
return Collections.singletonMap("severity", SeverityMapping.toString(severity));
}


public static class MyState {

@Tag("validationEnabled")
public boolean myValidationEnabled = true;

@Tag("inlayHintEnabled")
public boolean myInlayHintEnabled = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void dispose() {

@Override
protected Object createSettings() {
return UserDefinedMicroProfileSettings.getInstance(getProject()).toSettingsForMicroProfileLS(inspectionsInfo);
return UserDefinedMicroProfileSettings.getInstance(getProject()).toSettingsForMicroProfileLS();
}

private void sendPropertiesChangeEvent(List<MicroProfilePropertiesScope> scope, Set<String> uris) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public QuarkusServer(Project project) {
@Override
public Object getInitializationOptions(URI rootUri) {
Map<String, Object> root = new HashMap<>();
MicroProfileInspectionsInfo inspectionsInfo = MicroProfileInspectionsInfo.getMicroProfileInspectionInfo(project);
Map<String, Object> settings = UserDefinedMicroProfileSettings.getInstance(project).toSettingsForMicroProfileLS(inspectionsInfo);
Map<String, Object> settings = UserDefinedMicroProfileSettings.getInstance(project).toSettingsForMicroProfileLS();
root.put("settings", settings);

Map<String, Object> extendedClientCapabilities = new HashMap<>();
Expand Down
Loading

0 comments on commit b7e4cd5

Please sign in to comment.