Search in sources :

Example 1 with DocumentHighlight

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;
}
Also used : DocumentHighlight(org.eclipse.lsp4j.DocumentHighlight) Position(org.eclipse.lsp4j.Position) Range(org.eclipse.lsp4j.Range)

Example 2 with DocumentHighlight

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();
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) DocumentHighlight(org.eclipse.lsp4j.DocumentHighlight) JavaModelException(org.eclipse.jdt.core.JavaModelException) ArrayList(java.util.ArrayList) OccurrenceLocation(org.eclipse.jdt.internal.core.manipulation.search.IOccurrencesFinder.OccurrenceLocation) IOccurrencesFinder(org.eclipse.jdt.internal.core.manipulation.search.IOccurrencesFinder) OccurrencesFinder(org.eclipse.jdt.internal.core.manipulation.search.OccurrencesFinder)

Example 3 with DocumentHighlight

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);
    }
}
Also used : DocumentHighlight(org.eclipse.lsp4j.DocumentHighlight) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) Extension(org.eclipse.xtext.xbase.lib.Extension) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) DocumentHighlightParams(org.eclipse.lsp4j.DocumentHighlightParams) List(java.util.List) CompletionList(org.eclipse.lsp4j.CompletionList)

Example 4 with DocumentHighlight

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

DocumentHighlight (org.eclipse.lsp4j.DocumentHighlight)4 Position (org.eclipse.lsp4j.Position)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 IOccurrencesFinder (org.eclipse.jdt.internal.core.manipulation.search.IOccurrencesFinder)1 OccurrenceLocation (org.eclipse.jdt.internal.core.manipulation.search.IOccurrencesFinder.OccurrenceLocation)1 OccurrencesFinder (org.eclipse.jdt.internal.core.manipulation.search.OccurrencesFinder)1 CompletionList (org.eclipse.lsp4j.CompletionList)1 DocumentHighlightParams (org.eclipse.lsp4j.DocumentHighlightParams)1 Range (org.eclipse.lsp4j.Range)1 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)1 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)1 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)1 Acceptor (org.eclipse.xtext.findReferences.IReferenceFinder.Acceptor)1 TargetURIs (org.eclipse.xtext.findReferences.TargetURIs)1