Skip to content

Commit

Permalink
feat: support for excluding properties from validation
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon committed Sep 1, 2023
1 parent 3fbeb41 commit d5d2ccc
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*******************************************************************************
* 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;
import com.intellij.codeInspection.ui.InspectionOptionsPanel;
import com.intellij.codeInspection.ui.ListEditForm;
import com.redhat.devtools.intellij.lsp4mp4ij.MicroProfileBundle;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

/**
* No-op {@link LocalInspectionTool} used as a basis for mapping properties inspection severities to matching LSP severities.
* Adds the possibility to define excluded properties.
*/
public abstract class AbstractDelegateInspectionWithExcludedProperties extends LocalInspectionTool {

public final @NotNull List<String> excludeList = new ArrayList<>();

public JComponent createOptionsPanel() {
InspectionOptionsPanel panel = new InspectionOptionsPanel();

var injectionListTable = new ListEditForm("", MicroProfileBundle.message("microprofile.properties.validation.excluded.properties"), excludeList);

panel.addGrowing(injectionListTable.getContentPanel());

return panel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
/**
* Dummy inspection for expression values in Microprofile properties files
*/
public class MicroProfilePropertiesExpressionsInspection extends AbstractDelegateInspection {
public class MicroProfilePropertiesExpressionsInspection extends AbstractDelegateInspectionWithExcludedProperties {
public static final String ID = getShortName(MicroProfilePropertiesExpressionsInspection.class.getSimpleName());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************************
* 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;

/**
* Dummy inspection for unknown properties in Microprofile properties files
*/
public class MicroProfilePropertiesUnassignedInspection extends AbstractDelegateInspectionWithExcludedProperties {
public static final String ID = getShortName(MicroProfilePropertiesUnassignedInspection.class.getSimpleName());
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@
*******************************************************************************/
package com.redhat.devtools.intellij.lsp4mp4ij.psi.core.inspections;

import java.util.Arrays;

/**
* Dummy inspection for unknown properties in Microprofile properties files
*/
public class MicroProfilePropertiesUnknownInspection extends AbstractDelegateInspection {
public class MicroProfilePropertiesUnknownInspection extends AbstractDelegateInspectionWithExcludedProperties {
public static final String ID = getShortName(MicroProfilePropertiesUnknownInspection.class.getSimpleName());

public MicroProfilePropertiesUnknownInspection() {
super();
excludeList.addAll(Arrays.asList("*/mp-rest/providers/*/priority", "mp.openapi.schema.*", "kafka-streams.*", "camel.*"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@
*******************************************************************************/
package com.redhat.devtools.intellij.lsp4mp4ij.settings;

import com.intellij.codeHighlighting.HighlightDisplayLevel;
import com.intellij.codeInsight.daemon.HighlightDisplayKey;
import com.intellij.codeInspection.InspectionProfile;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.codeInspection.ex.InspectionToolWrapper;
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.ArrayList;
import java.util.List;
import java.util.Objects;

/**
Expand All @@ -39,6 +38,9 @@ public class MicroProfileInspectionsInfo {
private DiagnosticSeverity valueSeverity = DiagnosticSeverity.Error;
private DiagnosticSeverity requiredSeverity = null;
private DiagnosticSeverity expressionSeverity = DiagnosticSeverity.Error;
private DiagnosticSeverity unassignedSeverity = DiagnosticSeverity.Warning;
private List<String> excludedUnknownProperties;
private List<String> excludedUnassignedProperties;

private MicroProfileInspectionsInfo() {
}
Expand All @@ -52,9 +54,24 @@ 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);
wrapper.unassignedSeverity = SeverityMapping.getSeverity(MicroProfilePropertiesUnassignedInspection.ID, profile);

wrapper.excludedUnknownProperties = getExclusions(profile, MicroProfilePropertiesUnknownInspection.ID, project);
wrapper.excludedUnassignedProperties = getExclusions(profile, MicroProfilePropertiesUnassignedInspection.ID, project);

return wrapper;
}

private static List<String> getExclusions(InspectionProfile profile, String inspectionId, Project project) {
List<String> exclusions = new ArrayList<>();
InspectionToolWrapper<?, ?> toolWrapper = profile.getInspectionTool(inspectionId, project);
if (toolWrapper != null && toolWrapper.getTool() instanceof AbstractDelegateInspectionWithExcludedProperties) {
AbstractDelegateInspectionWithExcludedProperties inspection = (AbstractDelegateInspectionWithExcludedProperties) toolWrapper.getTool();
exclusions.addAll(inspection.excludeList);
}
return exclusions;
}

public DiagnosticSeverity unknownSeverity() {
return unknownSeverity;
}
Expand All @@ -79,19 +96,28 @@ public DiagnosticSeverity requiredSeverity() {
return requiredSeverity;
}

public DiagnosticSeverity unassignedSeverity() {
return unassignedSeverity;
}

public List<String> getExcludedUnknownProperties() {
return excludedUnknownProperties;
}

public List<String> getExcludedUnassignedProperties() {
return excludedUnassignedProperties;
}

@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 && unassignedSeverity == that.unassignedSeverity && Objects.equals(excludedUnknownProperties, that.excludedUnknownProperties) && Objects.equals(excludedUnassignedProperties, that.excludedUnassignedProperties);
}

@Override
public int hashCode() {
return Objects.hash(syntaxSeverity, unknownSeverity, duplicateSeverity, valueSeverity, requiredSeverity,
expressionSeverity);
return Objects.hash(syntaxSeverity, unknownSeverity, duplicateSeverity, valueSeverity, requiredSeverity, expressionSeverity, unassignedSeverity, excludedUnknownProperties, excludedUnassignedProperties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,30 @@ public Map<String, Object> 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()));

validation.put("unknown", getSeverityAndExclusions(inspectionsInfo.unknownSeverity(), inspectionsInfo.getExcludedUnknownProperties()));
validation.put("unassigned", getSeverityAndExclusions(inspectionsInfo.unassignedSeverity(), inspectionsInfo.getExcludedUnassignedProperties()));

return settings;
}

private Map<String, Object> getSeverityAndExclusions(DiagnosticSeverity severity, List<String> exclusions) {
Map<String, Object> inspection = new HashMap<>();
inspection.put("severity", SeverityMapping.toString(severity));
if (exclusions != null && !exclusions.isEmpty()) {
inspection.put("excluded", exclusions);
}
return inspection;
}

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


public static class MyState {

@Tag("validationEnabled")
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/META-INF/lsp4ij-quarkus.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@
enabledByDefault="true"
level="ERROR"
implementationClass="com.redhat.devtools.intellij.lsp4mp4ij.psi.core.inspections.MicroProfilePropertiesExpressionsInspection"/>
<localInspection
language="JAVA"
bundle="messages.MicroProfileBundle"
key="microprofile.properties.validation.unassigned"
groupPathKey="microprofile.inspection.group.name"
groupKey="microprofile.java.inspection.group.name"
enabledByDefault="true"
level="WARNING"
implementationClass="com.redhat.devtools.intellij.lsp4mp4ij.psi.core.inspections.MicroProfilePropertiesUnassignedInspection"/>
</extensions>

</idea-plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<html>
<body>
Reports properties in <code>.java</code> files unassigned from properties file.
<p><b>Example:</b></p>
<pre><code>
@ConfigProperty(name="missing.from.properties")
private String myConfig;
</code></pre>
<div style="border-left: 6px solid #e7af18; display: block; padding:5px">
<p style="margin:5px">
&#9888; This inspection is used to configure the <b>Tools for MicroProfile</b> server in <a href="settings://LanguageServers"> Settings | Languages &amp; Frameworks | Language Servers</a>.<br/><br/>
Consequently, some limitations apply:
</p>
<ul>
<li><i>Scope, Severity and Highlighting</i> are ignored</li>
<li><i>Excluded properties</i> are respected</li>
</ul>
</div>
</body>
</html>
1 change: 1 addition & 0 deletions src/main/resources/messages/MicroProfileBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ microprofile.properties.validation.unassigned=Unassigned properties
microprofile.java=Java
microprofile.java.title=MicroProfile configuration in Java files
microprofile.java.codeLens.url.enabled=Show Rest endpoint URL as inlay hint?
microprofile.properties.validation.excluded.properties=Excluded properties. Patterns can be used ('*' = any string, '?' = any character).

0 comments on commit d5d2ccc

Please sign in to comment.