use of nl.ramsolutions.sw.magik.api.MagikKeyword in project magik-tools by StevenLooman.
the class CompletionProvider method provideGlobalCompletion.
/**
* Provide global completion.
* @param magikFile MagikFile.
* @param position Position in source.
* @param tokenNode Current node.
* @return Completions items.
*/
@SuppressWarnings("checkstyle:NestedIfDepth")
private List<CompletionItem> provideGlobalCompletion(final MagikTypedFile magikFile, final Position position, @Nullable final AstNode tokenNode) {
final List<CompletionItem> items = new ArrayList<>();
// Keyword entries.
Stream.of(MagikKeyword.values()).map(magikKeyword -> {
final String name = magikKeyword.toString().toLowerCase();
final CompletionItem item = new CompletionItem(name);
item.setKind(CompletionItemKind.Keyword);
item.setInsertText(magikKeyword.getValue());
return item;
}).forEach(items::add);
// Scope entries.
final AstNode topNode = magikFile.getTopNode();
AstNode scopeNode = AstQuery.nodeSurrounding(topNode, Lsp4jConversion.positionFromLsp4j(position));
if (scopeNode != null) {
if (scopeNode.getFirstChild(MagikGrammar.BODY) != null) {
scopeNode = scopeNode.getFirstChild(MagikGrammar.BODY);
}
final GlobalScope globalScope = magikFile.getGlobalScope();
final Scope scopeForNode = globalScope.getScopeForNode(scopeNode);
if (scopeForNode != null) {
scopeForNode.getSelfAndAncestorScopes().stream().flatMap(scope -> scope.getScopeEntriesInScope().stream()).filter(scopeEntry -> {
final AstNode definingNode = scopeEntry.getNode();
final Range range = new Range(definingNode);
return Lsp4jConversion.positionFromLsp4j(position).isAfterRange(range);
}).map(scopeEntry -> {
final CompletionItem item = new CompletionItem(scopeEntry.getIdentifier());
item.setInsertText(scopeEntry.getIdentifier());
item.setDetail(scopeEntry.getIdentifier());
item.setKind(CompletionItemKind.Variable);
return item;
}).forEach(items::add);
}
}
// Slots.
final ITypeKeeper typeKeeper = magikFile.getTypeKeeper();
if (scopeNode != null) {
final AstNode methodDefinitionNode = scopeNode.getFirstAncestor(MagikGrammar.METHOD_DEFINITION);
if (methodDefinitionNode != null) {
final MethodDefinitionNodeHelper helper = new MethodDefinitionNodeHelper(methodDefinitionNode);
final GlobalReference globalRef = helper.getTypeGlobalReference();
final AbstractType type = typeKeeper.getType(globalRef);
type.getSlots().stream().map(slot -> {
final String slotName = slot.getName();
final String fullSlotName = slot.getOwner().getFullName() + "." + slot.getName();
final CompletionItem item = new CompletionItem(slotName);
item.setInsertText(slotName);
item.setDetail(fullSlotName);
item.setKind(CompletionItemKind.Property);
return item;
}).forEach(items::add);
}
}
// Global types.
final String identifierPart = tokenNode != null ? tokenNode.getTokenValue() : "";
typeKeeper.getTypes().stream().filter(type -> type.getFullName().indexOf(identifierPart) != -1).map(type -> {
final CompletionItem item = new CompletionItem(type.getFullName());
// TODO: if visible from current package, don't prefix package.
item.setInsertText(type.getFullName());
item.setDetail(type.getFullName());
item.setDocumentation(type.getDoc());
item.setKind(CompletionItemKind.Class);
return item;
}).forEach(items::add);
return items;
}
Aggregations