All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ai.vespa.schemals.lsp.schema.completion.provider.SimpleColonCompletion Maven / Gradle / Ivy

There is a newer version: 8.441.21
Show newest version
package ai.vespa.schemals.lsp.schema.completion.provider;

import java.util.List;

import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionItemKind;

import ai.vespa.schemals.context.EventCompletionContext;
import ai.vespa.schemals.lsp.schema.completion.provider.FixedKeywordBodies.FixedKeywordBody;
import ai.vespa.schemals.parser.Token.TokenType;
import ai.vespa.schemals.schemadocument.SchemaDocument;

/**
 * For matching KEYWORD_TOKEN IDENTIFIER? COLON *fixed set of keywords go here*
 */
public class SimpleColonCompletion implements CompletionProvider {

    private final static List colonSupporters = List.of(
        FixedKeywordBodies.ATTRIBUTE,
        FixedKeywordBodies.DISTANCE_METRIC,
        FixedKeywordBodies.INDEX,
        FixedKeywordBodies.MATCH,
        FixedKeywordBodies.RANK,
        FixedKeywordBodies.RANK_TYPE,
        FixedKeywordBodies.SORT_LOCALE,
        FixedKeywordBodies.SORT_FUNCTION,
        FixedKeywordBodies.SORT_STRENGTH,
        FixedKeywordBodies.SORTING,
        FixedKeywordBodies.STEMMING,
        FixedKeywordBodies.STRICT,
        FixedKeywordBodies.SUMMARY,
        FixedKeywordBodies.WEIGHTEDSET
    );

	private boolean match(EventCompletionContext context, TokenType keywordToken) {
        return context.document.lexer().matchBackwards(context.position, 1, true, keywordToken, TokenType.COLON) != null
            || context.document.lexer().matchBackwards(context.position, 1, true, keywordToken, TokenType.IDENTIFIER, TokenType.COLON) != null
            || context.document.lexer().matchBackwards(context.position, 1, true, keywordToken, TokenType.IDENTIFIER_WITH_DASH, TokenType.COLON) != null;
	}

	@Override
	public List getCompletionItems(EventCompletionContext context) {
        if (!(context.document instanceof SchemaDocument)) return List.of();

        for (FixedKeywordBody fixedKeywordBody : colonSupporters) {
            if (match(context, fixedKeywordBody.tokenType())) {
                // Snippets usually not wanted in colon constructs
                return fixedKeywordBody.completionItems().stream()
                                                         .filter(item -> item.getKind() != CompletionItemKind.Snippet)
                                                         .toList();
            }
        }

        return List.of();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy