Search in sources :

Example 1 with DocumentHighlight

use of org.graalvm.tools.lsp.server.types.DocumentHighlight in project graal by oracle.

the class HighlightRequestHandler method findOtherReadOrWrites.

List<? extends DocumentHighlight> findOtherReadOrWrites(TextDocumentSurrogate surrogate, InstrumentableNode nodeAtCaret, int line, int character) {
    InteropUtils.VariableInfo[] caretVariables = InteropUtils.getNodeObjectVariables(nodeAtCaret);
    if (caretVariables.length > 0) {
        Set<String> variableNames = new HashSet<>();
        for (InteropUtils.VariableInfo varInfo : caretVariables) {
            if (contains(varInfo.getSourceSection(), line, character)) {
                variableNames.add(varInfo.getName());
            }
        }
        Object scope = getScope(surrogate, nodeAtCaret);
        List<DocumentHighlight> highlights = new ArrayList<>();
        while (scope != null) {
            InteropLibrary interop = InteropLibrary.getUncached(scope);
            if (interop.hasSourceLocation(scope)) {
                try {
                    SourceSection sourceLocation = interop.getSourceLocation(scope);
                    Node[] nodeLocation = new Node[] { null };
                    EventBinding<LoadSourceSectionListener> binding = env.getInstrumenter().attachLoadSourceSectionListener(SourceSectionFilter.newBuilder().sourceSectionEquals(sourceLocation).build(), e -> {
                        Node node = e.getNode();
                        if (nodeLocation[0] == null || isParent(node, nodeLocation[0])) {
                            nodeLocation[0] = node;
                        }
                    }, true);
                    binding.dispose();
                    addHighlights(nodeLocation[0], variableNames, highlights);
                } catch (UnsupportedMessageException e) {
                    throw CompilerDirectives.shouldNotReachHere(e);
                }
            }
            if (interop.hasScopeParent(scope)) {
                try {
                    scope = interop.getScopeParent(scope);
                } catch (UnsupportedMessageException e) {
                    throw CompilerDirectives.shouldNotReachHere(e);
                }
            } else {
                scope = null;
            }
        }
        return highlights;
    }
    return Collections.emptyList();
}
Also used : DocumentHighlight(org.graalvm.tools.lsp.server.types.DocumentHighlight) InteropUtils(org.graalvm.tools.lsp.server.utils.InteropUtils) Node(com.oracle.truffle.api.nodes.Node) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) ArrayList(java.util.ArrayList) LoadSourceSectionListener(com.oracle.truffle.api.instrumentation.LoadSourceSectionListener) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) SourceSection(com.oracle.truffle.api.source.SourceSection) HashSet(java.util.HashSet)

Example 2 with DocumentHighlight

use of org.graalvm.tools.lsp.server.types.DocumentHighlight in project graal by oracle.

the class DocumentHighlightTest method checkHighlight.

private void checkHighlight(URI uri, int line, int column, DocumentHighlight... verifiedHighlights) throws InterruptedException, ExecutionException {
    Future<List<? extends DocumentHighlight>> future = truffleAdapter.documentHighlight(uri, line, column);
    List<? extends DocumentHighlight> highlights = future.get();
    assertEquals(verifiedHighlights.length, highlights.size());
    for (int i = 0; i < verifiedHighlights.length; i++) {
        DocumentHighlight vh = verifiedHighlights[i];
        DocumentHighlight h = highlights.get(i);
        assertEquals(Integer.toString(i), vh.getKind(), h.getKind());
        assertTrue(Integer.toString(i), rangeCheck(vh.getRange(), h.getRange()));
    }
}
Also used : DocumentHighlight(org.graalvm.tools.lsp.server.types.DocumentHighlight) List(java.util.List)

Aggregations

DocumentHighlight (org.graalvm.tools.lsp.server.types.DocumentHighlight)2 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)1 LoadSourceSectionListener (com.oracle.truffle.api.instrumentation.LoadSourceSectionListener)1 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)1 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)1 Node (com.oracle.truffle.api.nodes.Node)1 SourceSection (com.oracle.truffle.api.source.SourceSection)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 InteropUtils (org.graalvm.tools.lsp.server.utils.InteropUtils)1