Skip to content

Commit

Permalink
Initialize reference extension participant (see #58)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Oct 29, 2018
1 parent ccda65b commit 3d4b945
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.PublishDiagnosticsParams;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.ReferenceParams;
import org.eclipse.lsp4j.RenameParams;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.TextDocumentClientCapabilities;
Expand Down Expand Up @@ -270,6 +271,15 @@ public CompletableFuture<List<? extends Location>> definition(TextDocumentPositi
return getXMLLanguageService().findDefinition(xmlDocument, params.getPosition());
});
}

@Override
public CompletableFuture<List<? extends Location>> references(ReferenceParams params) {
return computeAsync((monitor) -> {
TextDocument document = getDocument(params.getTextDocument().getUri());
XMLDocument xmlDocument = getXMLDocument(document);
return getXMLLanguageService().findReferences(xmlDocument, params.getPosition(), params.getContext());
});
}

@Override
public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActionParams params) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.ReferenceContext;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.WorkspaceEdit;
Expand All @@ -53,6 +54,7 @@ public class XMLLanguageService extends XMLExtensionsRegistry {
private final XMLFoldings foldings;
private final XMLDocumentLink documentLink;
private XMLDefinition definition;
private XMLReference reference;
private final XMLCodeActions codeActions;

public XMLLanguageService() {
Expand All @@ -65,6 +67,7 @@ public XMLLanguageService() {
this.foldings = new XMLFoldings(this);
this.documentLink = new XMLDocumentLink(this);
this.definition = new XMLDefinition(this);
this.reference = new XMLReference(this);
this.codeActions = new XMLCodeActions(this);
}

Expand Down Expand Up @@ -113,6 +116,11 @@ public List<? extends Location> findDefinition(XMLDocument xmlDocument, Position
return definition.findDefinition(xmlDocument, position);
}

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

public List<CodeAction> doCodeActions(CodeActionContext context, Range range, XMLDocument document,
XMLFormattingOptions formattingSettings) {
return codeActions.doCodeActions(context, range, document, formattingSettings);
Expand All @@ -137,5 +145,4 @@ public String doAutoClose(XMLDocument xmlDocument, Position position) {
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2018 Angelo ZERR
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Angelo Zerr <[email protected]> - initial API and implementation
*/
package org.eclipse.lsp4xml.services;

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

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

/**
* XML reference support.
*
*/
class XMLReference {

private final XMLExtensionsRegistry extensionsRegistry;

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

public List<? extends Location> findReferences(XMLDocument document, Position position, ReferenceContext context) {
List<Location> locations = new ArrayList<>();
for (IReferenceParticipant participant : extensionsRegistry.getReferenceParticipants()) {
participant.findReference(document, position, context, locations);
}
return locations;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2018 Angelo ZERR.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Angelo Zerr <[email protected]> - initial API and implementation
*/
package org.eclipse.lsp4xml.services.extensions;

import java.util.List;

import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.ReferenceContext;
import org.eclipse.lsp4xml.dom.XMLDocument;

/**
* Reference participant API.
*
*/
public interface IReferenceParticipant {

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class XMLExtensionsRegistry {
private final List<ICodeActionParticipant> codeActionsParticipants;
private final List<IDocumentLinkParticipant> documentLinkParticipants;
private final List<IDefinitionParticipant> definitionParticipants;
private final List<IReferenceParticipant> referenceParticipants;

private IXMLDocumentProvider documentProvider;

Expand All @@ -52,6 +53,7 @@ public XMLExtensionsRegistry() {
codeActionsParticipants = new ArrayList<>();
documentLinkParticipants = new ArrayList<>();
definitionParticipants = new ArrayList<>();
referenceParticipants = new ArrayList<>();
}

public void initializeParams(InitializeParams params) {
Expand Down Expand Up @@ -105,6 +107,11 @@ public Collection<IDefinitionParticipant> getDefinitionParticipants() {
return definitionParticipants;
}

public Collection<IReferenceParticipant> getReferenceParticipants() {
initializeIfNeeded();
return referenceParticipants;
}

private void initializeIfNeeded() {
if (initialized) {
return;
Expand Down Expand Up @@ -186,6 +193,14 @@ public void unregisterDefinitionParticipant(IDefinitionParticipant definitionPar
definitionParticipants.add(definitionParticipant);
}

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

public void unregisterReferenceParticipant(IReferenceParticipant referenceParticipant) {
referenceParticipants.add(referenceParticipant);
}

/**
* Returns the XML Document provider and null otherwise.
*
Expand Down

0 comments on commit 3d4b945

Please sign in to comment.