Search in sources :

Example 1 with EvaluationResult

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

the class SourceCodeEvaluator method tryDifferentEvalStrategies.

public EvaluationResult tryDifferentEvalStrategies(TextDocumentSurrogate surrogate, Node nearestNode) throws DiagnosticsNotification {
    logger.fine("Trying literal eval...");
    EvaluationResult literalResult = evalLiteral(nearestNode);
    if (literalResult.isEvaluationDone() && !literalResult.isError()) {
        return literalResult;
    }
    EvaluationResult coverageEvalResult = evalWithCoverageData(surrogate, nearestNode);
    if (coverageEvalResult.isEvaluationDone() && !coverageEvalResult.isError()) {
        return coverageEvalResult;
    }
    logger.fine("Trying run-to-section eval...");
    EvaluationResult runToSectionEvalResult = runToSectionAndEval(surrogate, nearestNode);
    if (runToSectionEvalResult.isEvaluationDone()) {
        return runToSectionEvalResult;
    }
    logger.fine("Trying global eval...");
    EvaluationResult globalScopeEvalResult = evalInGlobalScope(surrogate.getLanguageId(), nearestNode);
    if (globalScopeEvalResult.isError()) {
        return EvaluationResult.createEvaluationSectionNotReached();
    }
    return globalScopeEvalResult;
}
Also used : EvaluationResult(org.graalvm.tools.lsp.server.utils.EvaluationResult)

Example 2 with EvaluationResult

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

the class SignatureHelpRequestHandler method signatureHelpWithEnteredContext.

public SignatureHelp signatureHelpWithEnteredContext(URI uri, int line, int originalCharacter) throws DiagnosticsNotification {
    TextDocumentSurrogate surrogate = surrogateMap.get(uri);
    if (isSignatureHelpTriggerCharOfLanguage(surrogate, line, originalCharacter)) {
        InstrumentableNode nodeAtCaret = findNodeAtCaret(surrogate, line, originalCharacter, StandardTags.CallTag.class);
        if (nodeAtCaret != null) {
            SourceSection signatureSection = ((Node) nodeAtCaret).getSourceSection();
            SourceSectionFilter.Builder builder = SourceCodeEvaluator.createSourceSectionFilter(surrogate.getUri(), signatureSection);
            SourceSectionFilter eventFilter = builder.tagIs(StandardTags.CallTag.class).build();
            SourceSectionFilter inputFilter = SourceSectionFilter.ANY;
            EvaluationResult evalResult = sourceCodeEvaluator.runToSectionAndEval(surrogate, signatureSection, eventFilter, inputFilter);
            // TODO: Are we asking for the signature on the correct object?
            if (evalResult.isEvaluationDone() && !evalResult.isError()) {
                Object result = evalResult.getResult();
                if (INTEROP.accepts(result) && INTEROP.isExecutable(result)) {
                    try {
                        Object signature = LSP_INTEROP.getSignature(result);
                        LanguageInfo langInfo = surrogate.getLanguageInfo();
                        String label = INTEROP.asString(INTEROP.toDisplayString(env.getLanguageView(langInfo, signature)));
                        SignatureInformation info = SignatureInformation.create(label, null);
                        if (signature instanceof TruffleObject) {
                            if (INTEROP.isMemberReadable(signature, PROP_DOCUMENTATION)) {
                                Object doc = INTEROP.readMember(signature, PROP_DOCUMENTATION);
                                Object documentation = completionHandler.getDocumentation(doc, langInfo);
                                if (documentation != null) {
                                    info.setDocumentation(documentation);
                                }
                            }
                            if (INTEROP.isMemberReadable(signature, PROP_PARAMETERS)) {
                                Object paramsObject = INTEROP.readMember(signature, PROP_PARAMETERS);
                                if (paramsObject instanceof TruffleObject && INTEROP.hasArrayElements(paramsObject)) {
                                    long size = INTEROP.getArraySize(paramsObject);
                                    List<ParameterInformation> paramInfos = new ArrayList<>((int) size);
                                    for (long i = 0; i < size; i++) {
                                        if (!INTEROP.isArrayElementReadable(paramsObject, i)) {
                                            continue;
                                        }
                                        Object param = INTEROP.readArrayElement(paramsObject, i);
                                        if (param instanceof TruffleObject) {
                                            ParameterInformation paramInfo = getParameterInformation(param, label, langInfo);
                                            if (paramInfo != null) {
                                                paramInfos.add(paramInfo);
                                            }
                                        }
                                    }
                                    info.setParameters(paramInfos);
                                }
                            }
                        }
                        Object nodeObject = nodeAtCaret.getNodeObject();
                        Integer numberOfArguments = InteropUtils.getNumberOfArguments(nodeObject, logger);
                        // parameter
                        return SignatureHelp.create(Arrays.asList(info), 0, numberOfArguments != null ? numberOfArguments - 1 : 0);
                    } catch (UnsupportedMessageException e) {
                        logger.log(Level.FINEST, "GET_SIGNATURE message not supported for TruffleObject: {0}", result);
                    } catch (InteropException e) {
                        e.printStackTrace(err);
                    }
                }
            }
        }
    }
    return SignatureHelp.create(Collections.emptyList(), null, null);
}
Also used : Node(com.oracle.truffle.api.nodes.Node) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate) ArrayList(java.util.ArrayList) SourceSectionFilter(com.oracle.truffle.api.instrumentation.SourceSectionFilter) InteropException(com.oracle.truffle.api.interop.InteropException) ParameterInformation(org.graalvm.tools.lsp.server.types.ParameterInformation) StandardTags(com.oracle.truffle.api.instrumentation.StandardTags) EvaluationResult(org.graalvm.tools.lsp.server.utils.EvaluationResult) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) SignatureInformation(org.graalvm.tools.lsp.server.types.SignatureInformation) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) SourceSection(com.oracle.truffle.api.source.SourceSection)

Example 3 with EvaluationResult

use of org.graalvm.tools.lsp.server.utils.EvaluationResult 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

EvaluationResult (org.graalvm.tools.lsp.server.utils.EvaluationResult)3 InteropException (com.oracle.truffle.api.interop.InteropException)2 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)2 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)2 Node (com.oracle.truffle.api.nodes.Node)2 SourceSection (com.oracle.truffle.api.source.SourceSection)2 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)1 SourceSectionFilter (com.oracle.truffle.api.instrumentation.SourceSectionFilter)1 StandardTags (com.oracle.truffle.api.instrumentation.StandardTags)1 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)1 Source (com.oracle.truffle.api.source.Source)1 ArrayList (java.util.ArrayList)1 ParameterInformation (org.graalvm.tools.lsp.server.types.ParameterInformation)1 SignatureInformation (org.graalvm.tools.lsp.server.types.SignatureInformation)1 NearestNode (org.graalvm.tools.lsp.server.utils.NearestNode)1 SourceWrapper (org.graalvm.tools.lsp.server.utils.SourceWrapper)1 TextDocumentSurrogate (org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate)1