use of org.graalvm.tools.lsp.server.types.CompletionItem 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;
}
}
Aggregations