Skip to content

Commit

Permalink
Add support for textDocument/typeDefinition from XML to XMLSchema/DTD
Browse files Browse the repository at this point in the history
Fix #371

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Jul 1, 2019
1 parent fe2ec42 commit 3c84a1d
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public void triggerValidationIfNeeded() {
private boolean codeActionLiteralSupport;
private boolean hierarchicalDocumentSymbolSupport;
private boolean definitionLinkSupport;

private boolean typeDefinitionLinkSupport;

public XMLTextDocumentService(XMLLanguageServer xmlLanguageServer) {
this.xmlLanguageServer = xmlLanguageServer;
DOMParser parser = DOMParser.getInstance();
Expand All @@ -156,6 +157,9 @@ public void updateClientCapabilities(ClientCapabilities capabilities) {
definitionLinkSupport = textDocumentClientCapabilities.getDefinition() != null
&& textDocumentClientCapabilities.getDefinition().getLinkSupport() != null
&& textDocumentClientCapabilities.getDefinition().getLinkSupport();
typeDefinitionLinkSupport = textDocumentClientCapabilities.getTypeDefinition() != null
&& textDocumentClientCapabilities.getTypeDefinition().getLinkSupport() != null
&& textDocumentClientCapabilities.getTypeDefinition().getLinkSupport();
}
}

Expand Down Expand Up @@ -299,6 +303,23 @@ public CompletableFuture<Either<List<? extends Location>, List<? extends Locatio
return Either.forLeft(locations);
});
}

@Override
public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> typeDefinition(
TextDocumentPositionParams params) {
return computeDOMAsync(params.getTextDocument(), (cancelChecker, xmlDocument) -> {
if (typeDefinitionLinkSupport) {
return Either.forRight(
getXMLLanguageService().findTypeDefinition(xmlDocument, params.getPosition(), cancelChecker));
}
List<? extends Location> locations = getXMLLanguageService()
.findTypeDefinition(xmlDocument, params.getPosition(), cancelChecker) //
.stream() //
.map(locationLink -> XMLPositionUtility.toLocation(locationLink)) //
.collect(Collectors.toList());
return Either.forLeft(locations);
});
}

@Override
public CompletableFuture<List<? extends Location>> references(ReferenceParams params) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public void checkCanceled() {
private final XMLFoldings foldings;
private final XMLDocumentLink documentLink;
private final XMLDefinition definition;
private final XMLTypeDefinition typeDefinition;
private final XMLReference reference;
private final XMLCodeLens codelens;
private final XMLCodeActions codeActions;
Expand All @@ -88,6 +89,7 @@ public XMLLanguageService() {
this.foldings = new XMLFoldings(this);
this.documentLink = new XMLDocumentLink(this);
this.definition = new XMLDefinition(this);
this.typeDefinition = new XMLTypeDefinition(this);
this.reference = new XMLReference(this);
this.codelens = new XMLCodeLens(this);
this.codeActions = new XMLCodeActions(this);
Expand Down Expand Up @@ -196,7 +198,7 @@ public List<FoldingRange> getFoldingRanges(DOMDocument xmlDocument, FoldingRange

public List<FoldingRange> getFoldingRanges(DOMDocument xmlDocument, FoldingRangeCapabilities context,
CancelChecker cancelChecker) {
return foldings.getFoldingRanges(xmlDocument.getTextDocument(), context, cancelChecker);
return foldings.getFoldingRanges(xmlDocument, context, cancelChecker);
}

public WorkspaceEdit doRename(DOMDocument xmlDocument, Position position, String newText) {
Expand All @@ -212,6 +214,11 @@ public List<? extends LocationLink> findDefinition(DOMDocument xmlDocument, Posi
return definition.findDefinition(xmlDocument, position, cancelChecker);
}

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

public List<? extends Location> findReferences(DOMDocument xmlDocument, Position position, ReferenceContext context,
CancelChecker cancelChecker) {
return reference.findReferences(xmlDocument, position, context, cancelChecker);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*******************************************************************************
* 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;

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

import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.eclipse.lsp4xml.dom.DOMDocument;
import org.eclipse.lsp4xml.services.extensions.ITypeDefinitionParticipant;
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry;

/**
* XML type definition support.
*
*/
class XMLTypeDefinition {

private final XMLExtensionsRegistry extensionsRegistry;

public XMLTypeDefinition(XMLExtensionsRegistry extensionsRegistry) {
this.extensionsRegistry = extensionsRegistry;
}

public List<? extends LocationLink> findTypeDefinition(DOMDocument document, Position position,
CancelChecker cancelChecker) {
List<LocationLink> locations = new ArrayList<>();
for (ITypeDefinitionParticipant participant : extensionsRegistry.getTypeDefinitionParticipants()) {
participant.findTypeDefinition(document, position, locations, cancelChecker);
}
return locations;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*******************************************************************************
* 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;

import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.eclipse.lsp4xml.dom.DOMDocument;

/**
* Type Definition participant API.
*
*/
public interface ITypeDefinitionParticipant {

/**
* Find type definition.
*
* @param document
* @param position
* @param locations
* @param cancelChecker
*/
void findTypeDefinition(DOMDocument document, Position position, List<LocationLink> locations,
CancelChecker cancelChecker);

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class XMLExtensionsRegistry implements IComponentProvider {
private final List<ICodeActionParticipant> codeActionsParticipants;
private final List<IDocumentLinkParticipant> documentLinkParticipants;
private final List<IDefinitionParticipant> definitionParticipants;
private final List<ITypeDefinitionParticipant> typeDefinitionParticipants;
private final List<IReferenceParticipant> referenceParticipants;
private final List<ICodeLensParticipant> codeLensParticipants;
private final List<IHighlightingParticipant> highlightingParticipants;
Expand All @@ -63,6 +64,7 @@ public XMLExtensionsRegistry() {
codeActionsParticipants = new ArrayList<>();
documentLinkParticipants = new ArrayList<>();
definitionParticipants = new ArrayList<>();
typeDefinitionParticipants = new ArrayList<>();
referenceParticipants = new ArrayList<>();
codeLensParticipants = new ArrayList<>();
highlightingParticipants = new ArrayList<>();
Expand Down Expand Up @@ -131,6 +133,11 @@ public Collection<IDefinitionParticipant> getDefinitionParticipants() {
return definitionParticipants;
}

public Collection<ITypeDefinitionParticipant> getTypeDefinitionParticipants() {
initializeIfNeeded();
return typeDefinitionParticipants;
}

public Collection<IReferenceParticipant> getReferenceParticipants() {
initializeIfNeeded();
return referenceParticipants;
Expand Down Expand Up @@ -229,6 +236,14 @@ public void unregisterDefinitionParticipant(IDefinitionParticipant definitionPar
definitionParticipants.add(definitionParticipant);
}

public void registerTypeDefinitionParticipant(ITypeDefinitionParticipant typeDefinitionParticipant) {
typeDefinitionParticipants.add(typeDefinitionParticipant);
}

public void unregisterTypeDefinitionParticipant(ITypeDefinitionParticipant typeDefinitionParticipant) {
typeDefinitionParticipants.add(typeDefinitionParticipant);
}

public void registerReferenceParticipant(IReferenceParticipant referenceParticipant) {
referenceParticipants.add(referenceParticipant);
}
Expand Down

0 comments on commit 3c84a1d

Please sign in to comment.