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 ec00c62
Show file tree
Hide file tree
Showing 23 changed files with 774 additions and 147 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,103 @@
/*******************************************************************************
* Copyright (c) 2023 Red Hat Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
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 @@ -26,9 +26,11 @@
*/
public class MicroProfileConfigurable extends NamedConfigurable<UserDefinedMicroProfileSettings> {

private final Project project;
private MicroProfileView myView;

public MicroProfileConfigurable(Project ignoredProject) {
public MicroProfileConfigurable(Project project) {
this.project = project;
}

@Override
Expand Down Expand Up @@ -58,13 +60,26 @@ public void setDisplayName(String name) {
return MicroProfileBundle.message("microprofile");
}


@Override
public void reset() {
if (myView == null) return;
UserDefinedMicroProfileSettings settings = UserDefinedMicroProfileSettings.getInstance(project);
myView.setValidationEnabled(settings.isValidationEnabled());
}

@Override
public boolean isModified() {
return false;
if (myView == null) return false;
UserDefinedMicroProfileSettings settings = UserDefinedMicroProfileSettings.getInstance(project);
return !(myView.isValidationEnabled()== settings.isValidationEnabled());
}

@Override
public void apply() throws ConfigurationException {
// Do nothing
if (myView == null) return;
UserDefinedMicroProfileSettings settings = UserDefinedMicroProfileSettings.getInstance(project);
settings.setValidationEnabled(myView.isValidationEnabled());
settings.fireStateChanged();
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
package com.redhat.devtools.intellij.lsp4mp4ij.settings;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.ui.IdeBorderFactory;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.util.ui.FormBuilder;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UI;
Expand All @@ -30,39 +32,22 @@ public class MicroProfileView implements Disposable {

private final JPanel myMainPanel;

private JBCheckBox validationEnabledCheckBox = new JBCheckBox(MicroProfileBundle.message("microprofile.validation.enabled"));

public MicroProfileView() {
JPanel settingsPanel = createSettings();
TitledBorder title = IdeBorderFactory.createTitledBorder(MicroProfileBundle.message("microprofile.title"));
settingsPanel.setBorder(title);
this.myMainPanel = JBUI.Panels.simplePanel(10,10)
.addToLeft(JBUI.Panels.simplePanel())
.addToCenter(settingsPanel);
}

private JPanel createSettings() {
return FormBuilder.createFormBuilder()
.addComponent(validationEnabledCheckBox)
.addComponentFillVertically(new JPanel(), 0)
.getPanel();
}

private JComponent createDescription(String description) {
/**
* Normally comments are below the controls.
* Here we want the comments to precede the controls, we therefore create an empty, 0-sized panel.
*/
JPanel titledComponent = UI.PanelFactory.grid().createPanel();
titledComponent.setMinimumSize(JBUI.emptySize());
titledComponent.setPreferredSize(JBUI.emptySize());
if (description != null && !description.isBlank()) {
titledComponent = UI.PanelFactory.panel(titledComponent)
.withComment(description)
.resizeX(true)
.resizeY(true)
.createPanel();
}
return titledComponent;
}

public JComponent getComponent() {
return myMainPanel;
}
Expand All @@ -71,4 +56,13 @@ public JComponent getComponent() {
public void dispose() {

}

public boolean isValidationEnabled() {
return validationEnabledCheckBox.isSelected();
}

public void setValidationEnabled(boolean enable) {
validationEnabledCheckBox.setSelected(enable);
}

}
Loading

0 comments on commit ec00c62

Please sign in to comment.