Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress validation kinds based on file pattern #1290

Merged
merged 1 commit into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.eclipse.lemminx.customservice.XMLLanguageServerAPI;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.extensions.contentmodel.settings.ContentModelSettings;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationSettings;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationRootSettings;
import org.eclipse.lemminx.logs.LogHelper;
import org.eclipse.lemminx.services.IXMLDocumentProvider;
import org.eclipse.lemminx.services.IXMLNotificationService;
Expand Down Expand Up @@ -222,7 +222,7 @@ private synchronized void updateSettings(Object initOptions, boolean initLogs) {
}
ContentModelSettings cmSettings = ContentModelSettings.getContentModelXMLSettings(initSettings);
if (cmSettings != null) {
XMLValidationSettings validationSettings = cmSettings.getValidation();
XMLValidationRootSettings validationSettings = cmSettings.getValidation();
xmlTextDocumentService.getValidationSettings().merge(validationSettings);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.eclipse.lemminx.commons.TextDocument;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMParser;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationSettings;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationRootSettings;
import org.eclipse.lemminx.services.DocumentSymbolsResult;
import org.eclipse.lemminx.services.SymbolInformationResult;
import org.eclipse.lemminx.services.XMLLanguageService;
Expand Down Expand Up @@ -250,7 +250,8 @@ public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completio
@Override
public CompletableFuture<CompletionItem> resolveCompletionItem(CompletionItem unresolved) {
return computeDOMAsync(unresolved.getData(), (xmlDocument, cancelChecker) -> {
return getXMLLanguageService().resolveCompletionItem(unresolved, xmlDocument, sharedSettings, cancelChecker);
return getXMLLanguageService().resolveCompletionItem(unresolved, xmlDocument, sharedSettings,
cancelChecker);
});
}

Expand Down Expand Up @@ -660,7 +661,8 @@ void validate(DOMDocument xmlDocument, Map<String, Object> validationArgs) throw
cancelChecker.checkCanceled();
getXMLLanguageService().publishDiagnostics(xmlDocument,
params -> xmlLanguageServer.getLanguageClient().publishDiagnostics(params),
(doc) -> triggerValidationFor(doc, TriggeredBy.Other), sharedSettings.getValidationSettings(),
(doc) -> triggerValidationFor(doc, TriggeredBy.Other),
sharedSettings.getValidationSettings(),
validationArgs, cancelChecker);
}

Expand Down Expand Up @@ -704,7 +706,7 @@ public XMLFormattingOptions getSharedFormattingSettings() {
return sharedSettings.getFormattingSettings();
}

public XMLValidationSettings getValidationSettings() {
public XMLValidationRootSettings getValidationSettings() {
return sharedSettings.getValidationSettings();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ private static void warnNoGrammar(DOMDocument document, List<Diagnostic> diagnos
range = new Range(new Position(0, 0), new Position(0, 0));
}
diagnostics.add(new Diagnostic(range, "No grammar constraints (DTD or XML Schema).", severity,
document.getDocumentURI(), XMLSyntaxErrorCode.NoGrammarConstraints.name()));
"xml", XMLSyntaxErrorCode.NoGrammarConstraints.name()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ContentModelSettings {

private XMLFileAssociation[] fileAssociations;

private XMLValidationSettings validation;
private XMLValidationRootSettings validation;

private XMLSymbolsSettings symbols;

Expand Down Expand Up @@ -90,11 +90,11 @@ public static ContentModelSettings getContentModelXMLSettings(Object initializat
return JSONUtility.toModel(initializationOptionsSettings, ContentModelSettings.class);
}

public void setValidation(XMLValidationSettings validation) {
public void setValidation(XMLValidationRootSettings validation) {
this.validation = validation;
}

public XMLValidationSettings getValidation() {
public XMLValidationRootSettings getValidation() {
return validation;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (c) 2022 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*/
package org.eclipse.lemminx.extensions.contentmodel.settings;

import org.eclipse.lemminx.settings.PathPatternMatcher;

/**
* XML validation settings used for a given pattern file.
*
* <code>
* {
"pattern": "**{.project,.classpath,plugin.xml,feature.xml,category.xml,.target,.product}",
"noGrammar": "ignore"
}
* </code>
*
* @author Angelo ZERR
*
*/
public class XMLValidationFilter extends XMLValidationSettings {

private String pattern;

private transient PathPatternMatcher matcher;

public boolean matches(String uri) {
if (matcher == null) {
matcher = new PathPatternMatcher();
matcher.setPattern(pattern);
}
return matcher.matches(uri);
}

public String getPattern() {
return pattern;
}

public void setPattern(String pattern) {
this.pattern = pattern;
angelozerr marked this conversation as resolved.
Show resolved Hide resolved
this.matcher = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/**
* Copyright (c) 2022 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*/
package org.eclipse.lemminx.extensions.contentmodel.settings;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* XML validation settings root which defines validation rules for all files.
*
* The {@link XMLValidationRootSettings}{@link #filters} can be used to define
* custom validation rule for a given file pattern:
*
* <code>
[
{
"pattern": "**.exsd",
"enabled": false
},
{
"pattern": "**{.project,.classpath,plugin.xml,feature.xml,category.xml,.target,.product}",
"noGrammar": "ignore"
}
]
* </code>
*
*/
public class XMLValidationRootSettings extends XMLValidationSettings {

private static final XMLValidationFilter[] DEFAULT_FILTERS;

private XMLValidationFilter[] filters;

static {
DEFAULT_FILTERS = createDefaultFilters();
}

public XMLValidationRootSettings() {
super();
setFilters(DEFAULT_FILTERS);
}

/**
* Returns validation filters to define custom validation rule for a given file
* pattern and null otherwise.
*
* @return validation filters to define custom validation rule for a given file
* pattern and null otherwise.
*/
public XMLValidationFilter[] getFilters() {
angelozerr marked this conversation as resolved.
Show resolved Hide resolved
return filters;
}

public void setFilters(XMLValidationFilter[] filters) {
this.filters = filters;
}

/**
* Returns the validation settings for the given uri and the global validation
* settings otherwise.
*
* @param uri the XML document to validate.
*
* @returnthe validation settings for the given uri and the global validation
* settings otherwise.
*/
public XMLValidationSettings getValidationSettings(String uri) {
if (filters != null) {
for (XMLValidationFilter filter : filters) {
if (filter.matches(uri)) {
return filter;
}
}
}
return this;
}

public XMLValidationRootSettings merge(XMLValidationRootSettings settings) {
if (settings != null) {
this.filters = settings.getFilters();
}
super.merge(settings);
return this;
}

@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + Arrays.hashCode(filters);
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
XMLValidationRootSettings other = (XMLValidationRootSettings) obj;
return Arrays.equals(filters, other.filters);
}

private static XMLValidationFilter[] createDefaultFilters() {
List<XMLValidationFilter> filters = new ArrayList<>();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@merks here the default validation filter that LemMinX will use by default. Do you see another filter which should be done for other files?

// Ignore validation for Eclipse '*.exsd' files
XMLValidationFilter filter = new XMLValidationFilter();
filter.setEnabled(false);
filter.setPattern("**.exsd");
filters.add(filter);
// Don't warn that XML file have no grammar for Eclipse '.project',
// '.classpath',
// 'plugin.xml', 'feature.xml' files
filter = new XMLValidationFilter();
filter.setNoGrammar("ignore");
filter.setPattern("**{.project,.classpath,plugin.xml,feature.xml,category.xml,.target,.product}");
filters.add(filter);
return filters.toArray(new XMLValidationFilter[filters.size()]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import org.eclipse.lsp4j.PublishDiagnosticsCapabilities;

/**
* XMLValidationSettings
* XML validation settings for a given file.
datho7561 marked this conversation as resolved.
Show resolved Hide resolved
*
*/
public class XMLValidationSettings {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.lemminx.customservice.AutoCloseTagResponse;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMParser;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationRootSettings;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationSettings;
import org.eclipse.lemminx.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lemminx.services.extensions.diagnostics.DiagnosticsResult;
Expand Down Expand Up @@ -174,12 +175,16 @@ public List<Diagnostic> doDiagnostics(DOMDocument xmlDocument, XMLValidationSett

public CompletableFuture<Path> publishDiagnostics(DOMDocument xmlDocument,
Consumer<PublishDiagnosticsParams> publishDiagnostics, Consumer<TextDocument> triggerValidation,
XMLValidationSettings validationSettings, Map<String, Object> validationArgs, CancelChecker cancelChecker) {
XMLValidationRootSettings validationSettings, Map<String, Object> validationArgs,
CancelChecker cancelChecker) {
String uri = xmlDocument.getDocumentURI();
TextDocument document = xmlDocument.getTextDocument();
XMLValidationSettings validationSettingsForUri = validationSettings != null
? validationSettings.getValidationSettings(xmlDocument.getDocumentURI())
: null;

// Process validation
DiagnosticsResult diagnostics = (DiagnosticsResult) this.doDiagnostics(xmlDocument, validationSettings,
DiagnosticsResult diagnostics = (DiagnosticsResult) this.doDiagnostics(xmlDocument, validationSettingsForUri,
validationArgs, cancelChecker);
cancelChecker.checkCanceled();
publishDiagnostics.accept(new PublishDiagnosticsParams(uri, diagnostics));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*******************************************************************************/
package org.eclipse.lemminx.settings;

import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationRootSettings;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationSettings;

/**
Expand All @@ -21,7 +22,7 @@ public class SharedSettings {
private final XMLCompletionSettings completionSettings;
private final XMLFoldingSettings foldingSettings;
private final XMLFormattingOptions formattingSettings;
private final XMLValidationSettings validationSettings;
private final XMLValidationRootSettings validationSettings;
private final XMLSymbolSettings symbolSettings;

private final XMLCodeActionSettings codeActionSettings;
Expand All @@ -37,7 +38,7 @@ public SharedSettings() {
this.completionSettings = new XMLCompletionSettings();
this.foldingSettings = new XMLFoldingSettings();
this.formattingSettings = new XMLFormattingOptions(true);
this.validationSettings = new XMLValidationSettings();
this.validationSettings = new XMLValidationRootSettings();
this.symbolSettings = new XMLSymbolSettings();
this.codeActionSettings = new XMLCodeActionSettings();
this.codeLensSettings = new XMLCodeLensSettings();
Expand Down Expand Up @@ -75,7 +76,7 @@ public XMLFormattingOptions getFormattingSettings() {
return formattingSettings;
}

public XMLValidationSettings getValidationSettings() {
public XMLValidationRootSettings getValidationSettings() {
return validationSettings;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@
"parameterTypes": []
}]
},
{
"name": "org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationRootSettings",
"allDeclaredFields": true,
"methods": [{
"name": "<init>",
"parameterTypes": []
}]
},
{
"name": "org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationSettings",
"allDeclaredFields": true,
Expand All @@ -221,6 +229,14 @@
"parameterTypes": []
}]
},
{
"name": "org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationFilter",
"allDeclaredFields": true,
"methods": [{
"name": "<init>",
"parameterTypes": []
}]
},
{
"name": "org.eclipse.lemminx.utils.platform.Memory",
"allDeclaredFields": true,
Expand Down
Loading