Skip to content

Commit

Permalink
Send warning notification when xml.catalogs contains invalid path
Browse files Browse the repository at this point in the history
Signed-off-by: David Kwon <dakwon@redhat.com>
  • Loading branch information
dkwon17 authored and angelozerr committed Jun 23, 2020
1 parent c9fa0cc commit 2dafd72
Show file tree
Hide file tree
Showing 17 changed files with 987 additions and 361 deletions.
Expand Up @@ -15,6 +15,7 @@

import static org.eclipse.lsp4j.jsonrpc.CompletableFutures.computeAsync;

import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand All @@ -25,6 +26,7 @@
import org.eclipse.lemminx.client.ExtendedClientCapabilities;
import org.eclipse.lemminx.commons.ModelTextDocument;
import org.eclipse.lemminx.commons.ParentProcessWatcher.ProcessLanguageServer;
import org.eclipse.lemminx.customservice.ActionableNotification;
import org.eclipse.lemminx.customservice.AutoCloseTagResponse;
import org.eclipse.lemminx.customservice.XMLLanguageClientAPI;
import org.eclipse.lemminx.customservice.XMLLanguageServerAPI;
Expand All @@ -33,6 +35,7 @@
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationSettings;
import org.eclipse.lemminx.logs.LogHelper;
import org.eclipse.lemminx.services.IXMLDocumentProvider;
import org.eclipse.lemminx.services.IXMLNotificationService;
import org.eclipse.lemminx.services.XMLLanguageService;
import org.eclipse.lemminx.settings.AllXMLSettings;
import org.eclipse.lemminx.settings.InitializationOptionsSettings;
Expand All @@ -50,9 +53,12 @@
import org.eclipse.lemminx.settings.capabilities.XMLCapabilityManager;
import org.eclipse.lemminx.utils.FilesUtils;
import org.eclipse.lemminx.utils.ServerInfo;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.InitializedParams;
import org.eclipse.lsp4j.MessageParams;
import org.eclipse.lsp4j.MessageType;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.TextDocumentPositionParams;
Expand All @@ -65,7 +71,7 @@
*
*/
public class XMLLanguageServer
implements ProcessLanguageServer, XMLLanguageServerAPI, IXMLDocumentProvider {
implements ProcessLanguageServer, XMLLanguageServerAPI, IXMLDocumentProvider, IXMLNotificationService {

private static final Logger LOGGER = Logger.getLogger(XMLLanguageServer.class.getName());

Expand All @@ -80,6 +86,7 @@ public class XMLLanguageServer
public XMLLanguageServer() {
xmlLanguageService = new XMLLanguageService();
xmlLanguageService.setDocumentProvider(this);
xmlLanguageService.setNotificationService(this);
xmlTextDocumentService = new XMLTextDocumentService(this);
xmlWorkspaceService = new XMLWorkspaceService(this);
delayer = Executors.newScheduledThreadPool(1);
Expand All @@ -97,10 +104,12 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
ExtendedClientCapabilities extendedClientCapabilities = InitializationOptionsExtendedClientCapabilities
.getExtendedClientCapabilities(params);
capabilityManager.setClientCapabilities(params.getCapabilities(), extendedClientCapabilities);
updateSettings(InitializationOptionsSettings.getSettings(params));

xmlTextDocumentService.updateClientCapabilities(capabilityManager.getClientCapabilities().capabilities,
capabilityManager.getClientCapabilities().getExtendedCapabilities());

updateSettings(InitializationOptionsSettings.getSettings(params));

ServerCapabilities nonDynamicServerCapabilities = ServerCapabilitiesInitializer.getNonDynamicServerCapabilities(
capabilityManager.getClientCapabilities(), xmlTextDocumentService.isIncrementalSupport());

Expand Down Expand Up @@ -255,4 +264,23 @@ public DOMDocument getDocument(String uri) {
ModelTextDocument<DOMDocument> document = xmlTextDocumentService.getDocument(uri);
return document != null ? document.getModel().getNow(null) : null;
}

@Override
public void sendNotification(String message, MessageType messageType, Command... commands) {
SharedSettings sharedSettings = getSharedSettings();
if (sharedSettings.isActionableNotificationSupport() && sharedSettings.isOpenSettingsCommandSupport()) {
ActionableNotification notification = new ActionableNotification().withSeverity(messageType)
.withMessage(message).withCommands(Arrays.asList(commands));
languageClient.actionableNotification(notification);
} else {
// the open settings command is not supported by the client, display a simple
// message with LSP
languageClient.showMessage(new MessageParams(messageType, message));
}
}

@Override
public SharedSettings getSharedSettings() {
return xmlTextDocumentService.getSharedSettings();
}
}
Expand Up @@ -29,7 +29,7 @@
import java.util.stream.Collectors;

import org.eclipse.lemminx.client.ExtendedClientCapabilities;
import org.eclipse.lemminx.client.LimitExceededWarnings;
import org.eclipse.lemminx.client.LimitExceededWarner;
import org.eclipse.lemminx.client.LimitFeature;
import org.eclipse.lemminx.commons.ModelTextDocument;
import org.eclipse.lemminx.commons.ModelTextDocuments;
Expand Down Expand Up @@ -100,7 +100,7 @@ public class XMLTextDocumentService implements TextDocumentService {
private final XMLLanguageServer xmlLanguageServer;
private final TextDocuments<ModelTextDocument<DOMDocument>> documents;
private SharedSettings sharedSettings;
private LimitExceededWarnings limitExceededWarnings;
private LimitExceededWarner limitExceededWarner;

/**
* Save context.
Expand Down Expand Up @@ -153,7 +153,7 @@ public XMLTextDocumentService(XMLLanguageServer xmlLanguageServer) {
return parser.parse(document, getXMLLanguageService().getResolverExtensionManager(), true, cancelChecker);
});
this.sharedSettings = new SharedSettings();
this.limitExceededWarnings = null;
this.limitExceededWarner = null;
}

public void updateClientCapabilities(ClientCapabilities capabilities,
Expand Down Expand Up @@ -254,7 +254,7 @@ public CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> docume

if (resultLimitExceeded) {
// send warning
getLimitExceededWarnings().onResultLimitExceeded(xmlDocument.getTextDocument().getUri(),
getLimitExceededWarner().onResultLimitExceeded(xmlDocument.getTextDocument().getUri(),
LimitFeature.SYMBOLS);
}
return symbols;
Expand Down Expand Up @@ -310,7 +310,7 @@ public void didClose(DidCloseTextDocumentParams params) {
String uri = document.getUri();
xmlLanguageServer.getLanguageClient()
.publishDiagnostics(new PublishDiagnosticsParams(uri, Collections.emptyList()));
getLimitExceededWarnings().evict(uri);
getLimitExceededWarner().evictValue(uri);
}

@Override
Expand Down Expand Up @@ -560,11 +560,11 @@ private static <R, M> CompletableFuture<R> computeModelAsync(CompletableFuture<M
return result;
}

public LimitExceededWarnings getLimitExceededWarnings() {
if (this.limitExceededWarnings == null) {
this.limitExceededWarnings = new LimitExceededWarnings(this.xmlLanguageServer.getLanguageClient(),
sharedSettings);
public LimitExceededWarner getLimitExceededWarner() {
if (this.limitExceededWarner == null) {
this.limitExceededWarner =
new LimitExceededWarner(this.xmlLanguageServer);
}
return this.limitExceededWarnings;
return this.limitExceededWarner;
}
}
@@ -0,0 +1,122 @@
/*******************************************************************************
* Copyright (c) 2020 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.client;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.lemminx.services.IXMLNotificationService;
import org.eclipse.lemminx.settings.SharedSettings;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.MessageType;

/** Sends a notification to the client via a jsonrpc notification
* while managing cache
*/
public abstract class AbstractXMLNotifier {

private final IXMLNotificationService notificationService;

protected final Map<IXMLSettingFeature /* feature name */, Set<String>> cache;

public AbstractXMLNotifier(IXMLNotificationService notificationService) {
this.notificationService = notificationService;
this.cache = new HashMap<IXMLSettingFeature, Set<String>>();
}

/**
* Returns true if <code>value</code> exists in the cache set
* identified by <code>key</code> and false otherwise
*
* @param key the key for the cache set
* @param value the value to check
* @return true if <code>value</code> exists in the cache set
* identified by <code>key</code> and false otherwise
*/
public boolean existsInCache(IXMLSettingFeature key, String value) {
return this.cache.get(key) != null && this.cache.get(key).contains(value);
}

/**
* Returns true if <code>values</code> is equal to the cache set
* identified by <code>key</code> and false otherwise
*
* @param key the key for the cache set
* @param values the values set to check
* @return true if <code>values</code> is equal to the cache set
* identified by <code>key</code> and false otherwise
*/
public boolean existsInCache(IXMLSettingFeature key, Set<String> values) {
return this.cache.get(key) != null && this.cache.get(key).equals(values);
}

/**
* Adds <code>value</code> to the cache set identified by <code>key</code>
*
* If such cache set does not exist, this method will initialize it with
* <code>value</code> added
*
* @param key the key for the cache set to add to
* @param value the value to add
*/
public void addToCache(IXMLSettingFeature key, String value) {
if (this.cache.get(key) == null) {
this.cache.put(key, new HashSet<String>());
}
this.cache.get(key).add(value);
}

/**
* Sets the cache set identified by <code>key</code>
*
* @param key the key for the cache set
* @param values the cache set
*/
public void setCacheValues(IXMLSettingFeature key, Set<String> values) {
evictKey(key);
this.cache.put(key, new HashSet<String>(values));
}


/**
* Evicts the set identified by the provided key, from the cache
*
* @param key
*/
public void evictKey(IXMLSettingFeature key) {
Set<String> setToEvict = this.cache.get(key);
if (setToEvict != null) {
setToEvict.clear();
}
}

/**
* Evicts the provided value from all cached sets
*
* @param value
*/
public void evictValue(String value) {
for (Set<String> set: this.cache.values()) {
set.remove(value);
}
}

protected void sendNotification(String message, MessageType messageType, Command... commands) {
notificationService.sendNotification(message, messageType, commands);
}

protected SharedSettings getSharedSettings() {
return notificationService.getSharedSettings();
}
}
Expand Up @@ -38,7 +38,7 @@ public void setCodeLens(ExtendedCodeLensCapabilities codeLens) {
* Returns true if the client supports actionable notifications and false otherwise
*
* See {@link org.eclipse.lemminx.customservice.ActionableNotification} and
* {@link org.eclipse.lemminx.customservice.XMLLanguageClientAPI}
* {@link org.eclipse.lemminx.customservice.XMLLanguageClientAPI#actionableNotification}
*
* @return true if the client supports actionable notifications and false otherwise
*/
Expand Down
@@ -0,0 +1,31 @@
/*******************************************************************************
* Copyright (c) 2020 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.client;

/**
* Features that are associated with a particuliar settingId
*/
public interface IXMLSettingFeature {
/**
* Returns a displayable name for this feature
*
* @return a displayable name for this feature
*/
public String getName();

/**
* Returns the related settingId for this feature
*
* @return the related settingId for this feature
*/
public String getSettingId();
}

0 comments on commit 2dafd72

Please sign in to comment.