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 Aug 31, 2023
1 parent 3fbeb41 commit 9dfc27a
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
* 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 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 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
@@ -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 @@ -16,14 +16,19 @@
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;
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 org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
Expand All @@ -39,6 +44,10 @@ 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,6 +61,25 @@ 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);

InspectionToolWrapper<?, ?> toolWrapper = profile.getInspectionTool(MicroProfilePropertiesUnknownInspection.ID, project);
if (toolWrapper != null) {
MicroProfilePropertiesUnknownInspection unknownPropertiesInspection = (MicroProfilePropertiesUnknownInspection) toolWrapper.getTool();
List<String> excludeList = unknownPropertiesInspection.excludeList;
if (!excludeList.isEmpty()) {
wrapper.excludedUnknownProperties = new ArrayList<>(excludeList);
}
}

toolWrapper = profile.getInspectionTool(MicroProfilePropertiesUnassignedInspection.ID, project);
if (toolWrapper != null) {
MicroProfilePropertiesUnassignedInspection unassignedInspection = (MicroProfilePropertiesUnassignedInspection) toolWrapper.getTool();
List<String> excludeList = unassignedInspection.excludeList;
if (!excludeList.isEmpty()) {
wrapper.excludedUnassignedProperties = new ArrayList<>(excludeList);
}
}
return wrapper;
}

Expand Down Expand Up @@ -79,19 +107,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,11 +142,28 @@ 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()));

//Unknown properties
Map<String, Object> unknown = new HashMap<>();
validation.put("unknown", unknown);
unknown.put("severity", SeverityMapping.toString(inspectionsInfo.unknownSeverity()));
List<String> excludedUnknownProperties = inspectionsInfo.getExcludedUnknownProperties();
if (excludedUnknownProperties != null && !excludedUnknownProperties.isEmpty()) {
unknown.put("excluded", excludedUnknownProperties);
}

//Unassigned properties
Map<String, Object> unassigned = new HashMap<>();
validation.put("unassigned", unassigned);
unassigned.put("severity", SeverityMapping.toString(inspectionsInfo.unassignedSeverity()));
List<String> excludedUnassignedProperties = inspectionsInfo.getExcludedUnassignedProperties();
if (excludedUnassignedProperties != null && !excludedUnassignedProperties.isEmpty()) {
unassigned.put("excluded", excludedUnassignedProperties);
}
return settings;
}

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 9dfc27a

Please sign in to comment.