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);
}
Aggregations