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

ai.vespa.schemals.lsp.yqlplus.semantictokens.YQLPlusSemanticTokens Maven / Gradle / Ivy

There is a newer version: 8.441.21
Show newest version
package ai.vespa.schemals.lsp.yqlplus.semantictokens;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.lsp4j.SemanticTokenTypes;
import org.eclipse.lsp4j.SemanticTokens;

import ai.vespa.schemals.common.ClientLogger;
import ai.vespa.schemals.context.EventDocumentContext;
import ai.vespa.schemals.lsp.common.semantictokens.CommonSemanticTokens;
import ai.vespa.schemals.lsp.common.semantictokens.SemanticTokenMarker;
import ai.vespa.schemals.parser.yqlplus.ast.pipeline_step;
import ai.vespa.schemals.tree.Node;
import ai.vespa.schemals.tree.YQLNode;
import ai.vespa.schemals.tree.Node.LanguageType;

public class YQLPlusSemanticTokens {

    public static void init() {
        CommonSemanticTokens.addTokenType(SemanticTokenTypes.Keyword);
        CommonSemanticTokens.addTokenTypes(new ArrayList(YQLPlusSemanticTokenConfig.tokensMap.values()));
        CommonSemanticTokens.addTokenTypes(new ArrayList(VespaGroupingSemanticToken.tokensMap.values()));
    }

    private static List parseYQLLanguage(YQLNode node, ClientLogger logger) {
        List ret = new ArrayList<>();

        Class nodeClass = node.getASTClass();
        String tokenString = YQLPlusSemanticTokenConfig.tokensMap.get(nodeClass);
        if (tokenString != null) {
            int tokenType = CommonSemanticTokens.getType(tokenString);
            ret.add(new SemanticTokenMarker(tokenType, node));
        } else if (YQLPlusSemanticTokenConfig.keywordTokens.contains(nodeClass)) {
            int keywordType = CommonSemanticTokens.getType(SemanticTokenTypes.Keyword);
            ret.add(new SemanticTokenMarker(keywordType, node));
        } else if (node.getASTClass() != pipeline_step.class) {
            for (Node child : node) {
                ret.addAll(traverseCST(child.getYQLNode(), logger));
            }
        }

        return ret;
    }

    private static List traverseGroupingLanguage(YQLNode node, ClientLogger logger) {
        List ret = new ArrayList<>();

        Class nodeClass = node.getASTClass();
        String tokenString = VespaGroupingSemanticToken.tokensMap.get(nodeClass);
        if (tokenString != null) {
            int tokenType = CommonSemanticTokens.getType(tokenString);
            ret.add(new SemanticTokenMarker(tokenType, node));
        } else {
            for (Node child : node) {
                ret.addAll(traverseCST(child.getYQLNode(), logger));
            }
        }

        return ret;
    }

    private static List traverseCST(YQLNode node, ClientLogger logger) {

        if (node.getIsDirty()) return new ArrayList<>();

        if (node.getLanguageType() == LanguageType.YQLPlus) return parseYQLLanguage(node, logger);

        if (node.getLanguageType() == LanguageType.GROUPING) return traverseGroupingLanguage(node, logger);

        List ret = new ArrayList<>();

        for (Node child : node) {
            ret.addAll(traverseCST(child.getYQLNode(), logger));
        }

        return ret;
    }

    public static SemanticTokens getSemanticTokens(EventDocumentContext context) {

        if (context.document == null) {
            return new SemanticTokens(new ArrayList<>());
        }

        YQLNode node = context.document.getRootYQLNode();
        if (node == null) {
            return new SemanticTokens(new ArrayList<>());
        }

        List markers = traverseCST(node, context.logger);
        List compactMarkers = SemanticTokenMarker.concatCompactForm(markers);

        return new SemanticTokens(compactMarkers);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy