Search in sources :

Example 1 with MagikKeyword

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;
}
Also used : AstNode(com.sonar.sslr.api.AstNode) Arrays(java.util.Arrays) MethodDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper) MagikPunctuator(nl.ramsolutions.sw.magik.api.MagikPunctuator) MagikOperator(nl.ramsolutions.sw.magik.api.MagikOperator) LoggerFactory(org.slf4j.LoggerFactory) SelfType(nl.ramsolutions.sw.magik.analysis.typing.types.SelfType) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LocalTypeReasoner(nl.ramsolutions.sw.magik.analysis.typing.LocalTypeReasoner) UndefinedType(nl.ramsolutions.sw.magik.analysis.typing.types.UndefinedType) MagikKeyword(nl.ramsolutions.sw.magik.api.MagikKeyword) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) CompletionItemKind(org.eclipse.lsp4j.CompletionItemKind) Lsp4jConversion(nl.ramsolutions.sw.magik.languageserver.Lsp4jConversion) Map(java.util.Map) Position(org.eclipse.lsp4j.Position) AstQuery(nl.ramsolutions.sw.magik.analysis.AstQuery) MagikGrammar(nl.ramsolutions.sw.magik.api.MagikGrammar) Nullable(javax.annotation.Nullable) GlobalScope(nl.ramsolutions.sw.magik.analysis.scope.GlobalScope) MagikCommentExtractor(nl.ramsolutions.sw.magik.parser.MagikCommentExtractor) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) Logger(org.slf4j.Logger) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) Set(java.util.Set) Collectors(java.util.stream.Collectors) ServerCapabilities(org.eclipse.lsp4j.ServerCapabilities) CompletionItem(org.eclipse.lsp4j.CompletionItem) List(java.util.List) Stream(java.util.stream.Stream) CompletionOptions(org.eclipse.lsp4j.CompletionOptions) Optional(java.util.Optional) Range(nl.ramsolutions.sw.magik.analysis.Range) Scope(nl.ramsolutions.sw.magik.analysis.scope.Scope) Collections(java.util.Collections) CheckForNull(javax.annotation.CheckForNull) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) GlobalScope(nl.ramsolutions.sw.magik.analysis.scope.GlobalScope) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) ArrayList(java.util.ArrayList) MethodDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper) Range(nl.ramsolutions.sw.magik.analysis.Range) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) GlobalScope(nl.ramsolutions.sw.magik.analysis.scope.GlobalScope) Scope(nl.ramsolutions.sw.magik.analysis.scope.Scope) CompletionItem(org.eclipse.lsp4j.CompletionItem) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) AstNode(com.sonar.sslr.api.AstNode)

Aggregations

AstNode (com.sonar.sslr.api.AstNode)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 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 CheckForNull (javax.annotation.CheckForNull)1 Nullable (javax.annotation.Nullable)1 MagikTypedFile (nl.ramsolutions.sw.magik.MagikTypedFile)1 AstQuery (nl.ramsolutions.sw.magik.analysis.AstQuery)1 Range (nl.ramsolutions.sw.magik.analysis.Range)1 MethodDefinitionNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper)1 GlobalScope (nl.ramsolutions.sw.magik.analysis.scope.GlobalScope)1 Scope (nl.ramsolutions.sw.magik.analysis.scope.Scope)1 ITypeKeeper (nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper)1