Search in sources :

Example 1 with Acceptor

use of org.eclipse.xtext.findReferences.IReferenceFinder.Acceptor in project xtext-core by eclipse.

the class DefaultDocumentHighlightService method getDocumentHighlights.

public List<DocumentHighlight> getDocumentHighlights(final XtextResource resource, final int offset) {
    if (resource == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.warn("Resource was null.");
        }
        return emptyList();
    }
    final URI uri = resource.getURI();
    if (offset < 0) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.warn("Invalid offset argument. Offset must be a non-negative integer for resource: " + uri);
        }
        return emptyList();
    }
    final IParseResult parseResult = resource.getParseResult();
    if (parseResult == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.warn("Parse result was null for resource: " + uri);
        }
        return emptyList();
    }
    final ICompositeNode rootNode = parseResult.getRootNode();
    final String docContent = rootNode.getText();
    final int docLength = docContent.length();
    if (offset >= docLength) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.warn("Offset exceeds document lenght. Document was " + docLength + " and offset was: " + offset + " for resource: " + uri);
        }
        return emptyList();
    }
    final EObject selectedElement = offsetHelper.resolveElementAt(resource, offset);
    if (!isDocumentHighlightAvailableFor(selectedElement, resource, offset)) {
        return emptyList();
    }
    final Supplier<Document> docSupplier = Suppliers.memoize(() -> new Document(UNUSED_VERSION, docContent));
    Iterable<URI> targetURIs = getTargetURIs(selectedElement);
    if (!(targetURIs instanceof TargetURIs)) {
        final TargetURIs result = targetURIsProvider.get();
        result.addAllURIs(targetURIs);
        targetURIs = result;
    }
    final Builder<DocumentHighlight> resultBuilder = ImmutableList.<DocumentHighlight>builder();
    final Acceptor acceptor = (Acceptor2) (source, sourceURI, eReference, index, targetOrProxy, targetURI) -> {
        final ITextRegion region = locationInFileProvider.getSignificantTextRegion(source, eReference, index);
        if (!isNullOrEmpty(region)) {
            resultBuilder.add(textRegionTransformer.apply(docSupplier.get(), region, DocumentHighlightKind.Read));
        }
    };
    referenceFinder.findReferences((TargetURIs) targetURIs, resource, acceptor, new NullProgressMonitor());
    if (resource.equals(selectedElement.eResource())) {
        final ITextRegion region = locationInFileProvider.getSignificantTextRegion(selectedElement);
        if (!isNullOrEmpty(region)) {
            resultBuilder.add(textRegionTransformer.apply(docSupplier.get(), region, DocumentHighlightKind.Write));
        }
    }
    return FluentIterable.from(resultBuilder.build()).toSortedList(comparator);
}
Also used : DocumentHighlight(org.eclipse.lsp4j.DocumentHighlight) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Acceptor(org.eclipse.xtext.findReferences.IReferenceFinder.Acceptor) TargetURIs(org.eclipse.xtext.findReferences.TargetURIs) Document(org.eclipse.xtext.ide.server.Document) URI(org.eclipse.emf.common.util.URI) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) IParseResult(org.eclipse.xtext.parser.IParseResult)

Aggregations

NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 DocumentHighlight (org.eclipse.lsp4j.DocumentHighlight)1 Acceptor (org.eclipse.xtext.findReferences.IReferenceFinder.Acceptor)1 TargetURIs (org.eclipse.xtext.findReferences.TargetURIs)1 Document (org.eclipse.xtext.ide.server.Document)1 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)1 IParseResult (org.eclipse.xtext.parser.IParseResult)1 ITextRegion (org.eclipse.xtext.util.ITextRegion)1