Search in sources :

Example 1 with NearestNode

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

the class CompletionRequestHandler method fillCompletionsWithGlobalsAndLocals.

private void fillCompletionsWithGlobalsAndLocals(int line, TextDocumentSurrogate surrogate, int column, List<CompletionItem> completions) {
    Node nearestNode = findNearestNode(surrogate.getSourceWrapper(), line, column);
    if (nearestNode == null) {
        // Cannot locate a valid node near the caret position, therefore provide globals only
        fillCompletionsWithGlobals(surrogate, completions);
        return;
    }
    if (!surrogate.hasCoverageData()) {
        // No coverage data, so we simply display locals without specific frame information and
        // globals
        fillCompletionsWithLocals(surrogate, nearestNode, completions, null);
        fillCompletionsWithGlobals(surrogate, completions);
        return;
    }
    // We have coverage data, so we try to derive locals from coverage data
    List<CoverageData> coverages = surrogate.getCoverageData(nearestNode.getSourceSection());
    if (coverages == null || coverages.isEmpty()) {
        coverages = SourceCodeEvaluator.findCoverageDataBeforeNode(surrogate, nearestNode);
    }
    if (coverages != null && !coverages.isEmpty()) {
        CoverageData coverageData = coverages.get(coverages.size() - 1);
        MaterializedFrame frame = coverageData.getFrame();
        fillCompletionsWithLocals(surrogate, nearestNode, completions, frame);
        // Call again, regardless if it was called with a frame argument before,
        // because duplicates will be filter, but it will add missing local
        // variables which were dropped (because of null values) in the call above
        fillCompletionsWithLocals(surrogate, nearestNode, completions, null);
        fillCompletionsWithGlobals(surrogate, completions);
    } else {
        // No coverage data found for the designated source section, so use the default look-up
        // as fallback
        fillCompletionsWithGlobals(surrogate, completions);
        fillCompletionsWithLocals(surrogate, nearestNode, completions, null);
    }
}
Also used : CoverageData(org.graalvm.tools.lsp.server.utils.CoverageData) Node(com.oracle.truffle.api.nodes.Node) NearestNode(org.graalvm.tools.lsp.server.utils.NearestNode) MaterializedFrame(com.oracle.truffle.api.frame.MaterializedFrame)

Example 2 with NearestNode

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

the class CompletionRequestHandler method fillCompletionsWithObjectProperties.

private void fillCompletionsWithObjectProperties(TextDocumentSurrogate surrogate, int line, int column, List<CompletionItem> completions) throws DiagnosticsNotification {
    SourceWrapper sourceWrapper = surrogate.getSourceWrapper();
    Source source = sourceWrapper.getSource();
    NearestNode nearestNodeHolder = NearestSectionsFinder.findExprNodeBeforePos(source, line, column, env);
    Node nearestNode = nearestNodeHolder.getNode();
    if (nearestNode != null) {
        Future<EvaluationResult> future = contextAwareExecutor.executeWithNestedContext(() -> sourceCodeEvaluator.tryDifferentEvalStrategies(surrogate, nearestNode), true);
        EvaluationResult evalResult = getFutureResultOrHandleExceptions(future);
        if (evalResult != null && evalResult.isEvaluationDone()) {
            if (!evalResult.isError()) {
                fillCompletionsFromTruffleObject(completions, surrogate.getLanguageInfo(), evalResult.getResult());
            } else {
                Object result = evalResult.getResult();
                if (result != null && INTEROP.isException(result)) {
                    SourceSection sourceLocation;
                    String exceptionMessage;
                    try {
                        sourceLocation = INTEROP.hasSourceLocation(result) ? INTEROP.getSourceLocation(result) : null;
                        exceptionMessage = INTEROP.hasExceptionMessage(result) ? INTEROP.asString(INTEROP.getExceptionMessage(result)) : null;
                    } catch (UnsupportedMessageException um) {
                        throw CompilerDirectives.shouldNotReachHere(um);
                    }
                    throw DiagnosticsNotification.create(surrogate.getUri(), Diagnostic.create(SourceUtils.sourceSectionToRange(sourceLocation), "An error occurred during execution: " + exceptionMessage, DiagnosticSeverity.Warning, null, "Graal", null));
                } else {
                    ((Exception) evalResult.getResult()).printStackTrace(err);
                }
            }
        } else {
            throw DiagnosticsNotification.create(surrogate.getUri(), Diagnostic.create(SourceUtils.sourceSectionToRange(nearestNode.getSourceSection()), "No type information available for this source section.", DiagnosticSeverity.Information, null, "Graal", null));
        }
    } else {
        logger.fine("No object property completion possible. Caret is not directly at the end of a source section. Line: " + line + ", column: " + column);
    }
}
Also used : SourceWrapper(org.graalvm.tools.lsp.server.utils.SourceWrapper) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) NearestNode(org.graalvm.tools.lsp.server.utils.NearestNode) Node(com.oracle.truffle.api.nodes.Node) NearestNode(org.graalvm.tools.lsp.server.utils.NearestNode) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) SourceSection(com.oracle.truffle.api.source.SourceSection) Source(com.oracle.truffle.api.source.Source) EvaluationResult(org.graalvm.tools.lsp.server.utils.EvaluationResult) InteropException(com.oracle.truffle.api.interop.InteropException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException)

Aggregations

Node (com.oracle.truffle.api.nodes.Node)2 NearestNode (org.graalvm.tools.lsp.server.utils.NearestNode)2 MaterializedFrame (com.oracle.truffle.api.frame.MaterializedFrame)1 InteropException (com.oracle.truffle.api.interop.InteropException)1 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)1 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)1 Source (com.oracle.truffle.api.source.Source)1 SourceSection (com.oracle.truffle.api.source.SourceSection)1 CoverageData (org.graalvm.tools.lsp.server.utils.CoverageData)1 EvaluationResult (org.graalvm.tools.lsp.server.utils.EvaluationResult)1 SourceWrapper (org.graalvm.tools.lsp.server.utils.SourceWrapper)1