From c05bb45aa1d942a5be1b2f4bb6df674b328625f7 Mon Sep 17 00:00:00 2001 From: Fred Bricon Date: Wed, 30 Aug 2023 19:33:25 +0200 Subject: [PATCH] feat: support for excluding properties from validation Signed-off-by: Fred Bricon --- ...egateInspectionWithExcludedProperties.java | 22 +++++++++++++++ ...ProfilePropertiesUnassignedInspection.java | 28 +++++++++++++++++++ ...croProfilePropertiesUnknownInspection.java | 20 +++++++++++++ .../settings/MicroProfileInspectionsInfo.java | 26 +++++++++++++---- .../UserDefinedMicroProfileSettings.java | 10 ++++++- .../MicroProfilePropertiesUnassigned.html | 25 +++++++++++++++++ 6 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/inspections/AbstractDelegateInspectionWithExcludedProperties.java create mode 100644 src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/inspections/MicroProfilePropertiesUnassignedInspection.java create mode 100644 src/main/resources/inspectionDescriptions/MicroProfilePropertiesUnassigned.html diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/inspections/AbstractDelegateInspectionWithExcludedProperties.java b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/inspections/AbstractDelegateInspectionWithExcludedProperties.java new file mode 100644 index 000000000..9c5f28726 --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/inspections/AbstractDelegateInspectionWithExcludedProperties.java @@ -0,0 +1,22 @@ +/******************************************************************************* + * 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.lsp4mp4ij.psi.core.inspections; + +import com.intellij.codeInspection.LocalInspectionTool; + +/** + * No-op {@link LocalInspectionTool} used as a basis for mapping inspection severities to matching LSP severities. + */ +public abstract class AbstractDelegateInspection extends LocalInspectionTool { +} diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/inspections/MicroProfilePropertiesUnassignedInspection.java b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/inspections/MicroProfilePropertiesUnassignedInspection.java new file mode 100644 index 000000000..838207e67 --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/inspections/MicroProfilePropertiesUnassignedInspection.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * 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.lsp4mp4ij.psi.core.inspections; + +import com.intellij.codeInspection.ui.ListEditForm; +import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; + +import javax.swing.*; +import java.util.ArrayList; +import java.util.List; + +/** + * Dummy inspection for unknown properties in Microprofile properties files + */ +public class MicroProfilePropertiesUnknownInspection extends AbstractDelegateInspectionWithExcludedProperties { + public static final String ID = getShortName(MicroProfilePropertiesUnknownInspection.class.getSimpleName()); +} diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/inspections/MicroProfilePropertiesUnknownInspection.java b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/inspections/MicroProfilePropertiesUnknownInspection.java index 1672ea0e8..3398708ec 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/inspections/MicroProfilePropertiesUnknownInspection.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/inspections/MicroProfilePropertiesUnknownInspection.java @@ -13,9 +13,29 @@ *******************************************************************************/ package com.redhat.devtools.intellij.lsp4mp4ij.psi.core.inspections; +import com.intellij.codeInspection.ui.ListEditForm; +import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; + +import javax.swing.*; +import java.util.ArrayList; +import java.util.List; + /** * Dummy inspection for unknown properties in Microprofile properties files */ public class MicroProfilePropertiesUnknownInspection extends AbstractDelegateInspection { public static final String ID = getShortName(MicroProfilePropertiesUnknownInspection.class.getSimpleName()); + + public final List excludeList = new ArrayList<>(); + + public JComponent createOptionsPanel() { + var panel = new MultipleCheckboxOptionsPanel(this); + + var injectionListTable = new ListEditForm("", "Exclude properties. Patterns can be used ('*' = any string, '?' = any character).", excludeList); + + panel.addGrowing(injectionListTable.getContentPanel()); + + return panel; + } + } diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/settings/MicroProfileInspectionsInfo.java b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/settings/MicroProfileInspectionsInfo.java index df6607bf8..3f8ae4116 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/settings/MicroProfileInspectionsInfo.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/settings/MicroProfileInspectionsInfo.java @@ -16,6 +16,8 @@ import com.intellij.codeHighlighting.HighlightDisplayLevel; import com.intellij.codeInsight.daemon.HighlightDisplayKey; import com.intellij.codeInspection.InspectionProfile; +import com.intellij.codeInspection.InspectionProfileEntry; +import com.intellij.codeInspection.ex.InspectionToolWrapper; import com.intellij.lang.annotation.HighlightSeverity; import com.intellij.openapi.project.Project; import com.intellij.profile.codeInspection.InspectionProfileManager; @@ -23,7 +25,10 @@ import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.inspections.*; import org.eclipse.lsp4j.DiagnosticSeverity; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; /** @@ -39,6 +44,7 @@ public class MicroProfileInspectionsInfo { private DiagnosticSeverity valueSeverity = DiagnosticSeverity.Error; private DiagnosticSeverity requiredSeverity = null; private DiagnosticSeverity expressionSeverity = DiagnosticSeverity.Error; + private List excludedUnknownProperties; private MicroProfileInspectionsInfo() { } @@ -52,6 +58,15 @@ public static MicroProfileInspectionsInfo getMicroProfileInspectionInfo(Project wrapper.valueSeverity = SeverityMapping.getSeverity(MicroProfilePropertiesValueInspection.ID, profile); wrapper.requiredSeverity = SeverityMapping.getSeverity(MicroProfilePropertiesRequiredInspection.ID, profile); wrapper.expressionSeverity = SeverityMapping.getSeverity(MicroProfilePropertiesExpressionsInspection.ID, profile); + + InspectionToolWrapper toolWrapper = profile.getInspectionTool(MicroProfilePropertiesUnknownInspection.ID, project); + if (toolWrapper != null) { + MicroProfilePropertiesUnknownInspection unknownPropertiesInspection = (MicroProfilePropertiesUnknownInspection) toolWrapper.getTool(); + List excludeList = unknownPropertiesInspection.excludeList; + if (!excludeList.isEmpty()) { + wrapper.excludedUnknownProperties = new ArrayList<>(excludeList); + } + } return wrapper; } @@ -79,19 +94,20 @@ public DiagnosticSeverity requiredSeverity() { return requiredSeverity; } + public List getExcludedUnknownProperties() { + return excludedUnknownProperties; + } + @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; MicroProfileInspectionsInfo that = (MicroProfileInspectionsInfo) o; - return syntaxSeverity == that.syntaxSeverity && unknownSeverity == that.unknownSeverity - && duplicateSeverity == that.duplicateSeverity && valueSeverity == that.valueSeverity - && requiredSeverity == that.requiredSeverity && expressionSeverity == that.expressionSeverity; + return syntaxSeverity == that.syntaxSeverity && unknownSeverity == that.unknownSeverity && duplicateSeverity == that.duplicateSeverity && valueSeverity == that.valueSeverity && requiredSeverity == that.requiredSeverity && expressionSeverity == that.expressionSeverity && Objects.equals(excludedUnknownProperties, that.excludedUnknownProperties); } @Override public int hashCode() { - return Objects.hash(syntaxSeverity, unknownSeverity, duplicateSeverity, valueSeverity, requiredSeverity, - expressionSeverity); + return Objects.hash(syntaxSeverity, unknownSeverity, duplicateSeverity, valueSeverity, requiredSeverity, expressionSeverity, excludedUnknownProperties); } } diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/settings/UserDefinedMicroProfileSettings.java b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/settings/UserDefinedMicroProfileSettings.java index 5a8c99a31..1f1b51f1f 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/settings/UserDefinedMicroProfileSettings.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/settings/UserDefinedMicroProfileSettings.java @@ -142,11 +142,19 @@ public Map toSettingsForMicroProfileLS() { tools.put("validation", validation); validation.put("enabled", isValidationEnabled()); validation.put("syntax", getSeverityNode(inspectionsInfo.syntaxSeverity())); - validation.put("unknown", getSeverityNode(inspectionsInfo.unknownSeverity())); validation.put("duplicate", getSeverityNode(inspectionsInfo.duplicateSeverity())); validation.put("value", getSeverityNode(inspectionsInfo.valueSeverity())); validation.put("required", getSeverityNode(inspectionsInfo.requiredSeverity())); validation.put("expression", getSeverityNode(inspectionsInfo.expressionSeverity())); + + //Unknown properties + Map unknown = new HashMap<>(); + validation.put("unknown", unknown); + unknown.put("severity", SeverityMapping.toString(inspectionsInfo.unknownSeverity())); + List excludedUnknownProperties = inspectionsInfo.getExcludedUnknownProperties(); + if (excludedUnknownProperties != null && !excludedUnknownProperties.isEmpty()) { + unknown.put("excluded", excludedUnknownProperties); + } return settings; } diff --git a/src/main/resources/inspectionDescriptions/MicroProfilePropertiesUnassigned.html b/src/main/resources/inspectionDescriptions/MicroProfilePropertiesUnassigned.html new file mode 100644 index 000000000..6bdc6b985 --- /dev/null +++ b/src/main/resources/inspectionDescriptions/MicroProfilePropertiesUnassigned.html @@ -0,0 +1,25 @@ + + +Write your description here. +Start the description with a verb in 3rd person singular, like reports, detects, highlights. +In the first sentence, briefly explain what exactly the inspection helps you detect. +Make sure the sentence is not very long and complicated. +

+The first sentence must be in a dedicated paragraph separated from the rest of the text. This will make the description easier to read. +Make sure the description doesn’t just repeat the inspection title. +

+

+See https://jetbrains.design/intellij/text/inspections/#descriptions for more information. +

+

+Embed code snippets: +

+

+// automatically highlighted according to inspection registration 'language' attribute
+
+ +

Text after this comment will only be shown in the settings of the inspection.

+ +

To open related settings directly from the description, add a link with `settings://$` optionally followed by `?$` to pre-select a UI element.

+ + \ No newline at end of file