Search in sources :

Example 1 with CoverageData

use of org.graalvm.tools.lsp.server.utils.CoverageData in project graal by oracle.

the class SourceCodeEvaluator method findCoverageDataBeforeNode.

static List<CoverageData> findCoverageDataBeforeNode(TextDocumentSurrogate surrogate, Node targetNode) {
    List<CoverageData> coveragesBeforeNode = new ArrayList<>();
    targetNode.getRootNode().accept(new NodeVisitor() {

        boolean found = false;

        @Override
        public boolean visit(Node node) {
            if (found) {
                return false;
            }
            if (node.equals(targetNode)) {
                found = true;
                return false;
            }
            SourceSection sourceSection = node.getSourceSection();
            if (sourceSection != null && sourceSection.isAvailable()) {
                List<CoverageData> coverageData = surrogate.getCoverageData(sourceSection);
                if (coverageData != null) {
                    coveragesBeforeNode.addAll(coverageData);
                }
            }
            return true;
        }
    });
    return coveragesBeforeNode;
}
Also used : CoverageData(org.graalvm.tools.lsp.server.utils.CoverageData) ExecutableNode(com.oracle.truffle.api.nodes.ExecutableNode) Node(com.oracle.truffle.api.nodes.Node) ExecutionEventNode(com.oracle.truffle.api.instrumentation.ExecutionEventNode) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) CoverageEventNode(org.graalvm.tools.lsp.server.utils.CoverageEventNode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SourceSection(com.oracle.truffle.api.source.SourceSection) NodeVisitor(com.oracle.truffle.api.nodes.NodeVisitor)

Example 2 with CoverageData

use of org.graalvm.tools.lsp.server.utils.CoverageData in project graal by oracle.

the class AbstractRequestHandler method getScope.

protected static Object getScope(TextDocumentSurrogate surrogate, InstrumentableNode node) {
    List<CoverageData> coverageData = surrogate.getCoverageData(((Node) node).getSourceSection());
    MaterializedFrame frame = null;
    if (coverageData != null) {
        CoverageData data = coverageData.stream().findFirst().orElse(null);
        if (data != null) {
            frame = data.getFrame();
        }
    }
    NodeLibrary nodeLibrary = NodeLibrary.getUncached(node);
    if (nodeLibrary.hasScope(node, frame)) {
        try {
            return nodeLibrary.getScope(node, frame, true);
        } catch (UnsupportedMessageException e) {
            throw CompilerDirectives.shouldNotReachHere(e);
        }
    } else {
        return null;
    }
}
Also used : NodeLibrary(com.oracle.truffle.api.interop.NodeLibrary) CoverageData(org.graalvm.tools.lsp.server.utils.CoverageData) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) MaterializedFrame(com.oracle.truffle.api.frame.MaterializedFrame)

Example 3 with CoverageData

use of org.graalvm.tools.lsp.server.utils.CoverageData in project graal by oracle.

the class HoverRequestHandler method evalHoverInfos.

private Hover evalHoverInfos(List<CoverageData> coverages, SourceSection hoverSection, LanguageInfo langInfo) {
    String textAtHoverPosition = hoverSection.getCharacters().toString();
    for (CoverageData coverageData : coverages) {
        Hover frameSlotHover = tryFrameScope(coverageData.getFrame(), coverageData.getCoverageEventNode(), textAtHoverPosition, langInfo, hoverSection);
        if (frameSlotHover != null) {
            return frameSlotHover;
        }
        Hover coverageDataHover = tryCoverageDataEvaluation(hoverSection, langInfo, textAtHoverPosition, coverageData);
        if (coverageDataHover != null) {
            return coverageDataHover;
        }
    }
    return Hover.create(Collections.emptyList());
}
Also used : CoverageData(org.graalvm.tools.lsp.server.utils.CoverageData) Hover(org.graalvm.tools.lsp.server.types.Hover)

Example 4 with CoverageData

use of org.graalvm.tools.lsp.server.utils.CoverageData in project graal by oracle.

the class HoverRequestHandler method hoverWithEnteredContext.

public Hover hoverWithEnteredContext(URI uri, int line, int column) {
    TextDocumentSurrogate surrogate = surrogateMap.get(uri);
    InstrumentableNode nodeAtCaret = findNodeAtCaret(surrogate, line, column);
    if (nodeAtCaret != null) {
        SourceSection hoverSection = ((Node) nodeAtCaret).getSourceSection();
        logger.log(Level.FINER, "Hover: SourceSection({0})", hoverSection.getCharacters());
        if (surrogate.hasCoverageData()) {
            List<CoverageData> coverages = surrogate.getCoverageData(hoverSection);
            if (coverages != null) {
                return evalHoverInfos(coverages, hoverSection, surrogate.getLanguageInfo());
            }
        } else if (developerMode) {
            String sourceText = hoverSection.getCharacters().toString();
            MarkupContent content = MarkupContent.create(MarkupKind.PlainText, "Language: " + surrogate.getLanguageId() + ", Section: " + sourceText + "\n" + "Node class: " + nodeAtCaret.getClass().getSimpleName() + "\n" + "Tags: " + getTags(nodeAtCaret));
            return Hover.create(content).setRange(SourceUtils.sourceSectionToRange(hoverSection));
        }
    }
    return Hover.create(Collections.emptyList());
}
Also used : InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) CoverageData(org.graalvm.tools.lsp.server.utils.CoverageData) ExecutableNode(com.oracle.truffle.api.nodes.ExecutableNode) Node(com.oracle.truffle.api.nodes.Node) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) CoverageEventNode(org.graalvm.tools.lsp.server.utils.CoverageEventNode) TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate) SourceSection(com.oracle.truffle.api.source.SourceSection) MarkupContent(org.graalvm.tools.lsp.server.types.MarkupContent)

Example 5 with CoverageData

use of org.graalvm.tools.lsp.server.utils.CoverageData in project graal by oracle.

the class SourceCodeEvaluator method evalWithCoverageData.

private EvaluationResult evalWithCoverageData(TextDocumentSurrogate textDocumentSurrogate, Node nearestNode) {
    if (!textDocumentSurrogate.hasCoverageData()) {
        return EvaluationResult.createEvaluationSectionNotReached();
    }
    List<CoverageData> dataBeforeNode = findCoverageDataBeforeNode(textDocumentSurrogate, nearestNode);
    if (dataBeforeNode == null || dataBeforeNode.isEmpty()) {
        return EvaluationResult.createEvaluationSectionNotReached();
    }
    CoverageData coverageData = dataBeforeNode.get(dataBeforeNode.size() - 1);
    if (((InstrumentableNode) nearestNode).hasTag(StandardTags.ReadVariableTag.class)) {
        // Shortcut for variables
        InteropUtils.VariableInfo[] variables = InteropUtils.getNodeObjectVariables((InstrumentableNode) nearestNode);
        if (variables.length == 1) {
            InteropUtils.VariableInfo var = variables[0];
            NodeLibrary nodeLibrary = NodeLibrary.getUncached(nearestNode);
            if (nodeLibrary.hasScope(nearestNode, coverageData.getFrame())) {
                try {
                    Object scope = nodeLibrary.getScope(nearestNode, coverageData.getFrame(), true);
                    if (INTEROP.isMemberReadable(scope, var.getName())) {
                        logger.fine("Coverage-based variable look-up");
                        Object value = INTEROP.readMember(scope, var.getName());
                        return EvaluationResult.createResult(value);
                    }
                } catch (UnknownIdentifierException | UnsupportedMessageException ex) {
                    throw CompilerDirectives.shouldNotReachHere(ex);
                }
            }
        }
    }
    LanguageInfo info = nearestNode.getRootNode().getLanguageInfo();
    String code = nearestNode.getSourceSection().getCharacters().toString();
    Source inlineEvalSource = Source.newBuilder(info.getId(), code, "in-line eval (hover request)").cached(false).build();
    ExecutableNode executableNode = env.parseInline(inlineEvalSource, nearestNode, coverageData.getFrame());
    CoverageEventNode coverageEventNode = coverageData.getCoverageEventNode();
    coverageEventNode.insertOrReplaceChild(executableNode);
    try {
        logger.fine("Trying coverage-based eval...");
        Object result = executableNode.execute(coverageData.getFrame());
        return EvaluationResult.createResult(result);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        coverageEventNode.clearChild();
    }
    return EvaluationResult.createEvaluationSectionNotReached();
}
Also used : NodeLibrary(com.oracle.truffle.api.interop.NodeLibrary) CoverageData(org.graalvm.tools.lsp.server.utils.CoverageData) InteropUtils(org.graalvm.tools.lsp.server.utils.InteropUtils) ExecutableNode(com.oracle.truffle.api.nodes.ExecutableNode) StandardTags(com.oracle.truffle.api.instrumentation.StandardTags) Source(com.oracle.truffle.api.source.Source) UnknownLanguageException(org.graalvm.tools.lsp.exceptions.UnknownLanguageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) IOException(java.io.IOException) EvaluationResultException(org.graalvm.tools.lsp.exceptions.EvaluationResultException) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) CoverageEventNode(org.graalvm.tools.lsp.server.utils.CoverageEventNode) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Aggregations

CoverageData (org.graalvm.tools.lsp.server.utils.CoverageData)6 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)3 ExecutableNode (com.oracle.truffle.api.nodes.ExecutableNode)3 Node (com.oracle.truffle.api.nodes.Node)3 CoverageEventNode (org.graalvm.tools.lsp.server.utils.CoverageEventNode)3 MaterializedFrame (com.oracle.truffle.api.frame.MaterializedFrame)2 NodeLibrary (com.oracle.truffle.api.interop.NodeLibrary)2 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)2 SourceSection (com.oracle.truffle.api.source.SourceSection)2 ExecutionEventNode (com.oracle.truffle.api.instrumentation.ExecutionEventNode)1 StandardTags (com.oracle.truffle.api.instrumentation.StandardTags)1 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)1 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)1 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)1 NodeVisitor (com.oracle.truffle.api.nodes.NodeVisitor)1 Source (com.oracle.truffle.api.source.Source)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 EvaluationResultException (org.graalvm.tools.lsp.exceptions.EvaluationResultException)1