Search in sources :

Example 1 with TextDocumentSurrogate

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

the class HighlightRequestHandler method highlightWithEnteredContext.

public List<? extends DocumentHighlight> highlightWithEnteredContext(URI uri, int line, int character) {
    TextDocumentSurrogate surrogate = surrogateMap.get(uri);
    InstrumentableNode nodeAtCaret = findNodeAtCaret(surrogate, line, character);
    if (nodeAtCaret != null) {
        if (nodeAtCaret.hasTag(StandardTags.ReadVariableTag.class) || nodeAtCaret.hasTag(StandardTags.WriteVariableTag.class)) {
            return findOtherReadOrWrites(surrogate, nodeAtCaret, line, character);
        }
    }
    return Collections.emptyList();
}
Also used : InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate)

Example 2 with TextDocumentSurrogate

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

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

the class TruffleAdapter method processChangesAndParseWithContextEntered.

protected TextDocumentSurrogate processChangesAndParseWithContextEntered(List<? extends TextDocumentContentChangeEvent> list, URI uri) throws DiagnosticsNotification {
    TextDocumentSurrogate surrogate = surrogateMap.get(uri);
    if (surrogate == null) {
        throw new IllegalStateException("No internal mapping for uri=" + uri.toString() + " found.");
    }
    if (list.isEmpty()) {
        return surrogate;
    }
    surrogate.getChangeEventsSinceLastSuccessfulParsing().addAll(list);
    surrogate.setLastChange(list.get(list.size() - 1));
    surrogate.setEditorText(SourceUtils.applyTextDocumentChanges(list, surrogate.getSource(), surrogate, logger));
    sourceCodeEvaluator.parse(surrogate);
    return surrogate;
}
Also used : TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate)

Example 4 with TextDocumentSurrogate

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

the class CompletionRequestHandler method completionWithEnteredContext.

public CompletionList completionWithEnteredContext(final URI uri, int line, int column, CompletionContext completionContext) throws DiagnosticsNotification {
    logger.log(Level.FINER, "Start finding completions for {0}:{1}:{2}", new Object[] { uri, line, column });
    TextDocumentSurrogate surrogate = surrogateMap.get(uri);
    if (surrogate == null) {
        logger.info("Completion requested in an unknown document: " + uri);
        return emptyList;
    }
    Source source = surrogate.getSource();
    if (!SourceUtils.isLineValid(line, source) || !SourceUtils.isColumnValid(line, column, source)) {
        logger.fine("line or column is out of range, line=" + line + ", column=" + column);
        return emptyList;
    }
    List<String> completionTriggerCharacters = languageCompletionTriggerCharacters.getTriggerCharacters(surrogate.getLanguageId());
    CompletionKind completionKind = getCompletionKind(source, SourceUtils.zeroBasedLineToOneBasedLine(line, source), column, completionTriggerCharacters, completionContext);
    if (surrogate.isSourceCodeReadyForCodeCompletion()) {
        return createCompletions(surrogate, line, column, completionKind);
    } else {
        // Try fixing the source code, parse again, then create the completions
        SourceFix sourceFix = SourceUtils.removeLastTextInsertion(surrogate, column, logger);
        if (sourceFix == null) {
            logger.fine("Unable to fix unparsable source code. No completion possible.");
            return emptyList;
        }
        TextDocumentSurrogate fixedSurrogate = surrogate.copy();
        // TODO(ds) Should we reset coverage data etc? Or adjust the SourceLocations?
        fixedSurrogate.setEditorText(sourceFix.text);
        SourceWrapper sourceWrapper = fixedSurrogate.prepareParsing();
        CallTarget callTarget = null;
        try {
            callTarget = env.parse(sourceWrapper.getSource());
        } catch (Exception e) {
            err.println("Parsing a fixed source caused an exception: " + e.getClass().getSimpleName() + " > " + e.getLocalizedMessage());
            return emptyList;
        } finally {
            fixedSurrogate.notifyParsingDone(callTarget);
        }
        // We need to replace the original surrogate with the fixed one so that when a run
        // script wants to import this fixed source, it will find the fixed surrogate via the
        // custom file system callback
        surrogateMap.put(uri, fixedSurrogate);
        try {
            return createCompletions(fixedSurrogate, line, sourceFix.characterIdx, getCompletionKind(sourceFix.removedCharacters, completionTriggerCharacters));
        } finally {
            surrogateMap.put(uri, surrogate);
        }
    }
}
Also used : SourceWrapper(org.graalvm.tools.lsp.server.utils.SourceWrapper) CallTarget(com.oracle.truffle.api.CallTarget) TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate) SourceFix(org.graalvm.tools.lsp.server.utils.SourceUtils.SourceFix) Source(com.oracle.truffle.api.source.Source) InteropException(com.oracle.truffle.api.interop.InteropException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException)

Example 5 with TextDocumentSurrogate

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

the class CoverageRequestHandler method clearRelatedCoverageData.

/**
 * Clears all coverage data from previous runs, which was collected by running the runScriptUri
 * resource. This avoids clearing coverage data collected by other script runs.
 *
 * Also clears all coverage data for the runScriptUri resource itself.
 *
 * @param runScriptUri URI of the script to kick-off the coverage analysis
 */
private void clearRelatedCoverageData(final URI runScriptUri) {
    TextDocumentSurrogate surrogateOfRunScript = surrogateMap.get(runScriptUri);
    assert surrogateOfRunScript != null;
    surrogateOfRunScript.clearCoverage();
    surrogateMap.getSurrogates().stream().forEach(surrogate -> surrogate.clearCoverage(runScriptUri));
}
Also used : TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate)

Aggregations

TextDocumentSurrogate (org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate)16 SourceSection (com.oracle.truffle.api.source.SourceSection)6 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)5 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)5 URI (java.net.URI)5 CallTarget (com.oracle.truffle.api.CallTarget)4 Node (com.oracle.truffle.api.nodes.Node)4 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)3 SourceSectionFilter (com.oracle.truffle.api.instrumentation.SourceSectionFilter)3 InteropException (com.oracle.truffle.api.interop.InteropException)3 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)3 ArrayList (java.util.ArrayList)3 EventContext (com.oracle.truffle.api.instrumentation.EventContext)2 ExecutionEventNode (com.oracle.truffle.api.instrumentation.ExecutionEventNode)2 ExecutionEventNodeFactory (com.oracle.truffle.api.instrumentation.ExecutionEventNodeFactory)2 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)2 Source (com.oracle.truffle.api.source.Source)2 HashSet (java.util.HashSet)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2