diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/XMLTextDocumentService.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/XMLTextDocumentService.java index f6206f5c8b..9e7d2f5691 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/XMLTextDocumentService.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/XMLTextDocumentService.java @@ -36,6 +36,7 @@ import org.eclipse.lemminx.client.LimitFeature; import org.eclipse.lemminx.commons.ModelTextDocument; import org.eclipse.lemminx.commons.ModelTextDocuments; +import org.eclipse.lemminx.commons.ModelValidatorDelayer; import org.eclipse.lemminx.commons.TextDocument; import org.eclipse.lemminx.commons.TextDocuments; import org.eclipse.lemminx.dom.DOMDocument; @@ -114,6 +115,8 @@ public class XMLTextDocumentService implements TextDocumentService { private final XMLLanguageServer xmlLanguageServer; private final TextDocuments> documents; + private final ModelValidatorDelayer xmlValidatorDelayer; + private SharedSettings sharedSettings; private LimitExceededWarner limitExceededWarner; @@ -165,7 +168,6 @@ public void triggerValidationIfNeeded() { } } - final ScheduledExecutorService delayer = Executors.newScheduledThreadPool(2); private boolean codeActionLiteralSupport; private boolean hierarchicalDocumentSymbolSupport; private boolean definitionLinkSupport; @@ -179,6 +181,11 @@ public XMLTextDocumentService(XMLLanguageServer xmlLanguageServer) { }); this.sharedSettings = new SharedSettings(); this.limitExceededWarner = null; + + ScheduledExecutorService delayer = Executors.newScheduledThreadPool(2); + this.xmlValidatorDelayer = new ModelValidatorDelayer(delayer, (document) -> { + validate(document, Collections.emptyMap()); + }); } public void updateClientCapabilities(ClientCapabilities capabilities, @@ -568,7 +575,7 @@ private void triggerValidationFor(Collection> doc xmlLanguageServer.schedule(() -> { documents.forEach(document -> { try { - validate(document.getModel().getNow(null)); + validate(document.getModel().getNow(null), false); } catch (CancellationException e) { // Ignore the error and continue to validate other documents } @@ -587,7 +594,11 @@ private void triggerValidationFor(TextDocument document, TriggeredBy triggeredBy ((ModelTextDocument) document).getModel()// .thenAcceptAsync(xmlDocument -> { // Validate the DOM document - validate(xmlDocument); + // When validation is triggered by a didChange, we process the validation with + // delay to avoid + // reporting to many 'textDocument/publishDiagnostics' notifications on client + // side. + validate(xmlDocument, triggeredBy == TriggeredBy.didChange); // Manage didOpen, didChange document lifecycle participants switch (triggeredBy) { case didOpen: @@ -624,8 +635,12 @@ private void triggerValidationFor(TextDocument document, TriggeredBy triggeredBy * @throws CancellationException when the DOM document content changed and * diagnostics must be stopped. */ - void validate(DOMDocument xmlDocument) throws CancellationException { - validate(xmlDocument, Collections.emptyMap()); + void validate(DOMDocument xmlDocument, boolean withDelay) throws CancellationException { + if (withDelay) { + xmlValidatorDelayer.validateWithDelay(xmlDocument.getDocumentURI(), xmlDocument); + } else { + validate(xmlDocument, Collections.emptyMap()); + } } /** diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/commons/ModelValidatorDelayer.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/commons/ModelValidatorDelayer.java new file mode 100644 index 0000000000..2a0386fbaa --- /dev/null +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/commons/ModelValidatorDelayer.java @@ -0,0 +1,81 @@ +/******************************************************************************* +* Copyright (c) 2022 Red Hat Inc. and others. +* All rights reserved. This program and the accompanying materials +* 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.commons; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +/** + * Validate a given model document with delay. + * + * @author Angelo ZERR + * + * @param + */ +public class ModelValidatorDelayer { + + private static final long DEFAULT_VALIDATION_DELAY_MS = 200; + + private final ScheduledExecutorService executorService; + + private final Consumer validator; + + private final Map> pendingValidationRequests; + + private final long validationDelayMs; + + public ModelValidatorDelayer(ScheduledExecutorService executorService, Consumer validator) { + this(executorService, validator, DEFAULT_VALIDATION_DELAY_MS); + } + + public ModelValidatorDelayer(ScheduledExecutorService executorService, Consumer validator, + long validationDelayMs) { + this.executorService = executorService; + this.validator = validator; + this.pendingValidationRequests = new HashMap<>(); + this.validationDelayMs = validationDelayMs; + } + + /** + * Validate the given model document identified by the given + * uri with a delay. + * + * @param uri the document URI. + * @param document the document model to validate. + */ + public void validateWithDelay(String uri, T document) { + cleanPendingValidation(uri); + Future request = executorService.schedule(() -> { + synchronized (pendingValidationRequests) { + pendingValidationRequests.remove(uri); + } + validator.accept(document); + }, validationDelayMs, TimeUnit.MILLISECONDS); + synchronized (pendingValidationRequests) { + pendingValidationRequests.put(uri, request); + } + } + + private void cleanPendingValidation(String uri) { + synchronized (pendingValidationRequests) { + Future request = pendingValidationRequests.get(uri); + if (request != null) { + request.cancel(true); + pendingValidationRequests.remove(uri); + } + } + } +}