Skip to content

Commit

Permalink
Add support for textDocument/references for XML Schema types
Browse files Browse the repository at this point in the history
Fix #58

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Jun 24, 2019
1 parent 9d8e118 commit b59bd5b
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ public CompletableFuture<Either<List<? extends Location>, List<? extends Locatio
TextDocumentPositionParams params) {
return computeDOMAsync(params.getTextDocument(), (cancelChecker, xmlDocument) -> {
if (definitionLinkSupport) {
return Either.forRight(getXMLLanguageService().findDefinition(xmlDocument, params.getPosition(), cancelChecker));
return Either.forRight(
getXMLLanguageService().findDefinition(xmlDocument, params.getPosition(), cancelChecker));
}
List<? extends Location> locations = getXMLLanguageService()
.findDefinition(xmlDocument, params.getPosition(), cancelChecker) //
Expand All @@ -299,7 +300,8 @@ public CompletableFuture<Either<List<? extends Location>, List<? extends Locatio
@Override
public CompletableFuture<List<? extends Location>> references(ReferenceParams params) {
return computeDOMAsync(params.getTextDocument(), (cancelChecker, xmlDocument) -> {
return getXMLLanguageService().findReferences(xmlDocument, params.getPosition(), params.getContext());
return getXMLLanguageService().findReferences(xmlDocument, params.getPosition(), params.getContext(),
cancelChecker);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import org.eclipse.lsp4xml.extensions.xsd.contentmodel.CMXSDContentModelProvider;
import org.eclipse.lsp4xml.extensions.xsd.participants.XSDCompletionParticipant;
import org.eclipse.lsp4xml.extensions.xsd.participants.XSDDefinitionParticipant;
import org.eclipse.lsp4xml.extensions.xsd.participants.XSDReferenceParticipant;
import org.eclipse.lsp4xml.extensions.xsd.participants.diagnostics.XSDDiagnosticsParticipant;
import org.eclipse.lsp4xml.services.extensions.ICompletionParticipant;
import org.eclipse.lsp4xml.services.extensions.IDefinitionParticipant;
import org.eclipse.lsp4xml.services.extensions.IReferenceParticipant;
import org.eclipse.lsp4xml.services.extensions.IXMLExtension;
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lsp4xml.services.extensions.diagnostics.IDiagnosticsParticipant;
Expand All @@ -37,12 +39,15 @@ public class XSDPlugin implements IXMLExtension {

private final IDiagnosticsParticipant diagnosticsParticipant;

private final IReferenceParticipant referenceParticipant;

private XSDURIResolverExtension uiResolver;

public XSDPlugin() {
completionParticipant = new XSDCompletionParticipant();
definitionParticipant = new XSDDefinitionParticipant();
diagnosticsParticipant = new XSDDiagnosticsParticipant();
referenceParticipant = new XSDReferenceParticipant();
}

@Override
Expand Down Expand Up @@ -70,6 +75,7 @@ public void start(InitializeParams params, XMLExtensionsRegistry registry) {
registry.registerCompletionParticipant(completionParticipant);
registry.registerDefinitionParticipant(definitionParticipant);
registry.registerDiagnosticsParticipant(diagnosticsParticipant);
registry.registerReferenceParticipant(referenceParticipant);
}

@Override
Expand All @@ -78,5 +84,6 @@ public void stop(XMLExtensionsRegistry registry) {
registry.unregisterCompletionParticipant(completionParticipant);
registry.unregisterDefinitionParticipant(definitionParticipant);
registry.unregisterDiagnosticsParticipant(diagnosticsParticipant);
registry.unregisterReferenceParticipant(referenceParticipant);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*******************************************************************************
* Copyright (c) 2019 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
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lsp4xml.extensions.xsd.participants;

import java.util.List;

import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.ReferenceContext;
import org.eclipse.lsp4xml.dom.DOMDocument;
import org.eclipse.lsp4xml.services.extensions.IReferenceParticipant;

/**
* XSD reference
*
* @author Angelo ZERR
*
*/
public class XSDReferenceParticipant implements IReferenceParticipant {

@Override
public void findReference(DOMDocument document, Position position, ReferenceContext context,
List<Location> locations) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public void checkCanceled() {
private final XMLDiagnostics diagnostics;
private final XMLFoldings foldings;
private final XMLDocumentLink documentLink;
private XMLDefinition definition;
private XMLReference reference;
private final XMLDefinition definition;
private final XMLReference reference;
private final XMLCodeActions codeActions;
private final XMLRename rename;

Expand Down Expand Up @@ -204,13 +204,14 @@ public List<DocumentLink> findDocumentLinks(DOMDocument document) {
return documentLink.findDocumentLinks(document);
}

public List<? extends LocationLink> findDefinition(DOMDocument xmlDocument, Position position, CancelChecker cancelChecker) {
public List<? extends LocationLink> findDefinition(DOMDocument xmlDocument, Position position,
CancelChecker cancelChecker) {
return definition.findDefinition(xmlDocument, position, cancelChecker);
}

public List<? extends Location> findReferences(DOMDocument xmlDocument, Position position,
ReferenceContext context) {
return reference.findReferences(xmlDocument, position, context);
public List<? extends Location> findReferences(DOMDocument xmlDocument, Position position, ReferenceContext context,
CancelChecker cancelChecker) {
return reference.findReferences(xmlDocument, position, context, cancelChecker);
}

public List<CodeAction> doCodeActions(CodeActionContext context, Range range, DOMDocument document,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.ReferenceContext;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.eclipse.lsp4xml.dom.DOMDocument;
import org.eclipse.lsp4xml.services.extensions.IReferenceParticipant;
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry;
Expand All @@ -32,10 +33,11 @@ public XMLReference(XMLExtensionsRegistry extensionsRegistry) {
this.extensionsRegistry = extensionsRegistry;
}

public List<? extends Location> findReferences(DOMDocument document, Position position, ReferenceContext context) {
public List<? extends Location> findReferences(DOMDocument document, Position position, ReferenceContext context,
CancelChecker cancelChecker) {
List<Location> locations = new ArrayList<>();
for (IReferenceParticipant participant : extensionsRegistry.getReferenceParticipants()) {
participant.findReference(document, position, context, locations);
participant.findReference(document, position, context, locations, cancelChecker);
}
return locations;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*******************************************************************************
* Copyright (c) 2019 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
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lsp4xml.services.extensions;

import java.util.List;
Expand All @@ -9,6 +18,12 @@
import org.eclipse.lsp4xml.dom.DOMDocument;
import org.eclipse.lsp4xml.dom.DOMNode;

/**
* Abstract class for definition.
*
* @author Angelo ZERR
*
*/
public abstract class AbstractDefinitionParticipant implements IDefinitionParticipant {

@Override
Expand All @@ -28,8 +43,25 @@ public void findDefinition(DOMDocument document, Position position, List<Locatio
}
}

/**
* Returns true if the definition support is applicable for the given document
* and false otherwise.
*
* @param document
* @return true if the definition support is applicable for the given document
* and false otherwise.
*/
protected abstract boolean match(DOMDocument document);

/**
* Find the definition
*
* @param node
* @param position
* @param offset
* @param locations
* @param cancelChecker
*/
protected abstract void findDefinition(DOMNode node, Position position, int offset, List<LocationLink> locations,
CancelChecker cancelChecker);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.ReferenceContext;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.eclipse.lsp4xml.dom.DOMDocument;

/**
Expand All @@ -23,6 +24,6 @@
*/
public interface IReferenceParticipant {

void findReference(DOMDocument document, Position position, ReferenceContext context, List<Location> locations);
void findReference(DOMDocument document, Position position, ReferenceContext context, List<Location> locations, CancelChecker cancelChecker);

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public boolean isDefinitionDynamicRegistered() {
return v3Supported && isDynamicRegistrationSupported(getTextDocument().getDefinition());
}

public boolean isReferencesDynamicRegistrationSupported() {
return v3Supported && isDynamicRegistrationSupported(getTextDocument().getReferences());
}

public boolean isCodeActionDynamicRegistered() {
return v3Supported && isDynamicRegistrationSupported(getTextDocument().getCodeAction());
}
Expand All @@ -87,7 +91,7 @@ public boolean isHoverDynamicRegistered() {
public boolean isDocumentHighlightDynamicRegistered() {
return v3Supported && isDynamicRegistrationSupported(getTextDocument().getDocumentHighlight());
}

private boolean isDynamicRegistrationSupported(DynamicRegistrationCapabilities capability) {
return capability != null && capability.getDynamicRegistration() != null
&& capability.getDynamicRegistration().booleanValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static ServerCapabilities getNonDynamicServerCapabilities(ClientCapabilit
serverCapabilities.setRenameProvider(!clientCapabilities.isRenameDynamicRegistrationSupported());
serverCapabilities.setFoldingRangeProvider(!clientCapabilities.isRangeFoldingDynamicRegistrationSupported());
serverCapabilities.setDefinitionProvider(!clientCapabilities.isDefinitionDynamicRegistered());
serverCapabilities.setReferencesProvider(!clientCapabilities.isReferencesDynamicRegistrationSupported());

if (!clientCapabilities.isLinkDynamicRegistrationSupported()) {
serverCapabilities.setDocumentLinkProvider(DEFAULT_LINK_OPTIONS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import static org.eclipse.lsp4xml.settings.capabilities.ServerCapabilitiesConstants.TEXT_DOCUMENT_HOVER;
import static org.eclipse.lsp4xml.settings.capabilities.ServerCapabilitiesConstants.TEXT_DOCUMENT_LINK;
import static org.eclipse.lsp4xml.settings.capabilities.ServerCapabilitiesConstants.TEXT_DOCUMENT_RENAME;
import static org.eclipse.lsp4xml.settings.capabilities.ServerCapabilitiesConstants.REFERENCES_ID;
import static org.eclipse.lsp4xml.settings.capabilities.ServerCapabilitiesConstants.TEXT_DOCUMENT_REFERENCES;

import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -141,14 +143,19 @@ public void initializeCapabilities() {
if (this.getClientCapabilities().isDefinitionDynamicRegistered()) {
registerCapability(DEFINITION_ID, TEXT_DOCUMENT_DEFINITION);
}
if (this.getClientCapabilities().isReferencesDynamicRegistrationSupported()) {
registerCapability(REFERENCES_ID, TEXT_DOCUMENT_REFERENCES);
}
syncDynamicCapabilitiesWithPreferences();
}

/**
* Registers(indicates the servers ability to support the service) all capabilities that have the ability to be turned
* on/off on the client side through preferences.
* Registers(indicates the servers ability to support the service) all
* capabilities that have the ability to be turned on/off on the client side
* through preferences.
*
* In the case the preference is set to off/false this server will tell the cliet it does not support this capability.
* In the case the preference is set to off/false this server will tell the
* cliet it does not support this capability.
*
* If a capability is not dynamic, it's handled by
* {@link ServerCapabilitiesInitializer}
Expand All @@ -169,8 +176,7 @@ public void syncDynamicCapabilitiesWithPreferences() {
XMLSymbolSettings symbolSettings = this.textDocumentService.getSharedSymbolSettings();

if (this.getClientCapabilities().isDocumentSymbolDynamicRegistrationSupported()) {
toggleCapability(symbolSettings.isEnabled(), DOCUMENT_SYMBOL_ID,
TEXT_DOCUMENT_DOCUMENT_SYMBOL, null);
toggleCapability(symbolSettings.isEnabled(), DOCUMENT_SYMBOL_ID, TEXT_DOCUMENT_DOCUMENT_SYMBOL, null);
}
}

Expand Down

0 comments on commit b59bd5b

Please sign in to comment.