Search in sources :

Example 1 with CompletionItemKind

use of org.graalvm.tools.lsp.server.types.CompletionItemKind 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)

Aggregations

CallTarget (com.oracle.truffle.api.CallTarget)1 CompilerDirectives (com.oracle.truffle.api.CompilerDirectives)1 MaterializedFrame (com.oracle.truffle.api.frame.MaterializedFrame)1 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)1 InteropException (com.oracle.truffle.api.interop.InteropException)1 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)1 NodeLibrary (com.oracle.truffle.api.interop.NodeLibrary)1 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)1 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)1 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)1 Node (com.oracle.truffle.api.nodes.Node)1 Source (com.oracle.truffle.api.source.Source)1 SourceSection (com.oracle.truffle.api.source.SourceSection)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1