use of org.eclipse.lsp4j.DocumentHighlight in project eclipse.jdt.ls by eclipse.
the class DocumentHighlightHandler method convertToHighlight.
private DocumentHighlight convertToHighlight(ITypeRoot unit, OccurrenceLocation occurrence) throws JavaModelException {
DocumentHighlight h = new DocumentHighlight();
if ((occurrence.getFlags() | IOccurrencesFinder.F_WRITE_OCCURRENCE) == IOccurrencesFinder.F_WRITE_OCCURRENCE) {
h.setKind(DocumentHighlightKind.Write);
} else if ((occurrence.getFlags() | IOccurrencesFinder.F_READ_OCCURRENCE) == IOccurrencesFinder.F_READ_OCCURRENCE) {
h.setKind(DocumentHighlightKind.Read);
}
int[] loc = JsonRpcHelpers.toLine(unit.getBuffer(), occurrence.getOffset());
int[] endLoc = JsonRpcHelpers.toLine(unit.getBuffer(), occurrence.getOffset() + occurrence.getLength());
h.setRange(new Range(new Position(loc[0], loc[1]), new Position(endLoc[0], endLoc[1])));
return h;
}
use of org.eclipse.lsp4j.DocumentHighlight in project eclipse.jdt.ls by eclipse.
the class DocumentHighlightHandler method computeOccurrences.
private List<DocumentHighlight> computeOccurrences(ITypeRoot unit, int line, int column, IProgressMonitor monitor) {
if (unit != null) {
try {
int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column);
OccurrencesFinder finder = new OccurrencesFinder();
CompilationUnit ast = CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, monitor);
if (ast != null) {
String error = finder.initialize(ast, offset, 0);
if (error == null) {
List<DocumentHighlight> result = new ArrayList<>();
OccurrenceLocation[] occurrences = finder.getOccurrences();
if (occurrences != null) {
for (OccurrenceLocation loc : occurrences) {
if (monitor.isCanceled()) {
return Collections.emptyList();
}
result.add(convertToHighlight(unit, loc));
}
}
return result;
}
}
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException("Problem with compute occurrences for" + unit.getElementName(), e);
}
}
return Collections.emptyList();
}
use of org.eclipse.lsp4j.DocumentHighlight in project xtext-core by eclipse.
the class AbstractLanguageServerTest method testDocumentHighlight.
protected void testDocumentHighlight(final Procedure1<? super DocumentHighlightConfiguration> configurator) {
try {
DocumentHighlightConfiguration _documentHighlightConfiguration = new DocumentHighlightConfiguration();
final Procedure1<DocumentHighlightConfiguration> _function = (DocumentHighlightConfiguration it) -> {
StringConcatenation _builder = new StringConcatenation();
_builder.append("MyModel.");
_builder.append(this.fileExtension);
it.setFilePath(_builder.toString());
};
@Extension final DocumentHighlightConfiguration configuration = ObjectExtensions.<DocumentHighlightConfiguration>operator_doubleArrow(_documentHighlightConfiguration, _function);
configurator.apply(configuration);
final String fileUri = this.initializeContext(configuration).getUri();
DocumentHighlightParams _documentHighlightParams = new DocumentHighlightParams();
final Procedure1<DocumentHighlightParams> _function_1 = (DocumentHighlightParams it) -> {
TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(fileUri);
it.setTextDocument(_textDocumentIdentifier);
int _line = configuration.getLine();
int _column = configuration.getColumn();
Position _position = new Position(_line, _column);
it.setPosition(_position);
};
DocumentHighlightParams _doubleArrow = ObjectExtensions.<DocumentHighlightParams>operator_doubleArrow(_documentHighlightParams, _function_1);
final CompletableFuture<List<? extends DocumentHighlight>> highlights = this.languageServer.documentHighlight(_doubleArrow);
final Function1<DocumentHighlight, String> _function_2 = (DocumentHighlight it) -> {
return this.toExpectation(it);
};
final String actualDocumentHighlight = IterableExtensions.join(ListExtensions.map(highlights.get(), _function_2), " | ");
this.assertEquals(configuration.getExpectedDocumentHighlight(), actualDocumentHighlight);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.lsp4j.DocumentHighlight 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