Search in sources :

Example 11 with TextDocumentSurrogate

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

the class TruffleAdapter method getLanguageId.

String getLanguageId(URI uri) {
    TextDocumentSurrogate doc = surrogateMap.get(uri);
    if (doc != null) {
        return doc.getLanguageId();
    }
    Future<String> future = contextAwareExecutor.executeWithDefaultContext(() -> {
        try {
            return Source.findLanguage(envInternal.getTruffleFile(uri));
        } catch (IOException ex) {
            return null;
        }
    });
    try {
        return future.get();
    } catch (InterruptedException | ExecutionException e) {
        throw new RuntimeException(e);
    }
}
Also used : TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 12 with TextDocumentSurrogate

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

the class TruffleAdapter method parseWithEnteredContext.

protected CallTarget parseWithEnteredContext(final String text, final String langId, final URI uri) throws DiagnosticsNotification {
    LanguageInfo languageInfo = findLanguageInfo(langId, envInternal.getTruffleFile(uri));
    TextDocumentSurrogate surrogate = getOrCreateSurrogate(uri, text, languageInfo);
    return parseWithEnteredContext(surrogate);
}
Also used : LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate)

Example 13 with TextDocumentSurrogate

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

the class TruffleAdapter method getOrCreateSurrogate.

TextDocumentSurrogate getOrCreateSurrogate(URI uri, String text, LanguageInfo languageInfo) {
    TextDocumentSurrogate surrogate = surrogateMap.getOrCreateSurrogate(uri, languageInfo);
    surrogate.setEditorText(text);
    return surrogate;
}
Also used : TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate)

Example 14 with TextDocumentSurrogate

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

the class CompletionRequestHandler method fillCompletionsWithScopesValues.

private void fillCompletionsWithScopesValues(TextDocumentSurrogate surrogate, List<CompletionItem> completions, Object scopeOriginal, CompletionItemKind completionItemKindDefault, int displayPriority) {
    LanguageInfo langInfo = surrogate.getLanguageInfo();
    String[] existingCompletions = completions.stream().map((item) -> item.getLabel()).toArray(String[]::new);
    // Filter duplicates
    Set<String> completionKeys = new HashSet<>(Arrays.asList(existingCompletions));
    int scopeCounter = 0;
    Object scope = scopeOriginal;
    while (scope != null) {
        Object scopeParent = null;
        if (INTEROP.hasScopeParent(scope)) {
            try {
                scopeParent = INTEROP.getScopeParent(scope);
            } catch (UnsupportedMessageException e) {
                throw CompilerDirectives.shouldNotReachHere(e);
            }
        }
        ++scopeCounter;
        Object keys;
        long size;
        try {
            keys = INTEROP.getMembers(scope, false);
            size = INTEROP.getArraySize(keys);
            if (scopeParent != null) {
                size -= INTEROP.getArraySize(INTEROP.getMembers(scopeParent, false));
            }
        } catch (Exception ex) {
            logger.log(Level.INFO, ex.getLocalizedMessage(), ex);
            break;
        }
        for (long i = 0; i < size; i++) {
            String key;
            Object object;
            try {
                key = INTEROP.asString(INTEROP.readArrayElement(keys, i));
                if (completionKeys.contains(key)) {
                    // keys and only take those from the most inner scope
                    continue;
                } else {
                    completionKeys.add(key);
                }
                object = INTEROP.readMember(scope, key);
            } catch (ThreadDeath td) {
                throw td;
            } catch (Throwable t) {
                logger.log(Level.CONFIG, scope.toString(), t);
                continue;
            }
            CompletionItem completion = CompletionItem.create(key);
            // Inner scopes should be displayed first, so sort by priority and scopeCounter
            // (the innermost scope has the lowest counter)
            completion.setSortText(String.format("%s%d.%04d.%s", "+", displayPriority, scopeCounter, key));
            if (completionItemKindDefault != null) {
                completion.setKind(completionItemKindDefault);
            } else {
                completion.setKind(findCompletionItemKind(object));
            }
            completion.setDetail(createCompletionDetail(object, langInfo));
            try {
                completion.setDocumentation(createDocumentation(object, surrogate.getLanguageInfo(), "in " + INTEROP.asString(INTEROP.toDisplayString(scope))));
            } catch (UnsupportedMessageException e) {
                throw CompilerDirectives.shouldNotReachHere(e);
            }
            completions.add(completion);
        }
        scope = scopeParent;
    }
}
Also used : Arrays(java.util.Arrays) ContextAwareExecutor(org.graalvm.tools.lsp.server.ContextAwareExecutor) MarkupKind(org.graalvm.tools.lsp.server.types.MarkupKind) InteropException(com.oracle.truffle.api.interop.InteropException) SourceFix(org.graalvm.tools.lsp.server.utils.SourceUtils.SourceFix) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) MarkupContent(org.graalvm.tools.lsp.server.types.MarkupContent) CompletionList(org.graalvm.tools.lsp.server.types.CompletionList) CompilerDirectives(com.oracle.truffle.api.CompilerDirectives) CompletionItemKind(org.graalvm.tools.lsp.server.types.CompletionItemKind) DiagnosticsNotification(org.graalvm.tools.lsp.exceptions.DiagnosticsNotification) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) SourceUtils(org.graalvm.tools.lsp.server.utils.SourceUtils) Diagnostic(org.graalvm.tools.lsp.server.types.Diagnostic) SourceSection(com.oracle.truffle.api.source.SourceSection) URI(java.net.URI) NearestSectionsFinder(org.graalvm.tools.lsp.server.utils.NearestSectionsFinder) NodeLibrary(com.oracle.truffle.api.interop.NodeLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TextDocumentSurrogateMap(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogateMap) Node(com.oracle.truffle.api.nodes.Node) CompletionItem(org.graalvm.tools.lsp.server.types.CompletionItem) EvaluationResult(org.graalvm.tools.lsp.server.utils.EvaluationResult) CompletionTriggerKind(org.graalvm.tools.lsp.server.types.CompletionTriggerKind) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) DiagnosticSeverity(org.graalvm.tools.lsp.server.types.DiagnosticSeverity) Set(java.util.Set) SourceWrapper(org.graalvm.tools.lsp.server.utils.SourceWrapper) CallTarget(com.oracle.truffle.api.CallTarget) CoverageData(org.graalvm.tools.lsp.server.utils.CoverageData) TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate) LanguageTriggerCharacters(org.graalvm.tools.lsp.server.LanguageTriggerCharacters) List(java.util.List) Source(com.oracle.truffle.api.source.Source) NearestNode(org.graalvm.tools.lsp.server.utils.NearestNode) CompletionContext(org.graalvm.tools.lsp.server.types.CompletionContext) MaterializedFrame(com.oracle.truffle.api.frame.MaterializedFrame) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) Collections(java.util.Collections) LSPLibrary(org.graalvm.tools.api.lsp.LSPLibrary) InteropException(com.oracle.truffle.api.interop.InteropException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) CompletionItem(org.graalvm.tools.lsp.server.types.CompletionItem) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) HashSet(java.util.HashSet)

Example 15 with TextDocumentSurrogate

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

the class ParsingTest method checkChange.

private TextDocumentSurrogate checkChange(URI uri, Range range, String change, String editorText) throws InterruptedException, ExecutionException {
    TextDocumentContentChangeEvent event = TextDocumentContentChangeEvent.create(change).setRange(range).setRangeLength(change.length());
    Future<TextDocumentSurrogate> future = truffleAdapter.processChangesAndParse(Arrays.asList(event), uri);
    TextDocumentSurrogate surrogate = future.get();
    assertEquals(editorText, surrogate.getEditorText());
    assertSame(surrogate.getEditorText(), surrogate.getEditorText());
    return surrogate;
}
Also used : TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate) TextDocumentContentChangeEvent(org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent)

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