All files / src/frontend LexerSymbolsProvider.ts

0% Statements 0/42
0% Branches 0/21
0% Functions 0/4
0% Lines 0/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                                                                                                                                                                                                   
/*
 * This file is released under the MIT license.
 * Copyright (c) 2018, 2023, Mike Lischke
 *
 * See LICENSE file for more info.
 */
 
/* eslint-disable max-classes-per-file */
 
import { Vocabulary } from "antlr4ts";
import * as path from "path";
 
import { TreeItem, TreeItemCollapsibleState, Command, ProviderResult } from "vscode";
import { AntlrTreeDataProvider } from "./AntlrTreeDataProvider";
 
export class LexerSymbolItem extends TreeItem {
 
    public iconPath = {
        light: path.join(__dirname, "..", "..", "..", "misc", "token-light.svg"),
        dark: path.join(__dirname, "..", "..", "..", "misc", "token-dark.svg"),
    };
 
    public contextValue = "lexerSymbols";
 
    public constructor(
        public readonly label: string,
        public readonly collapsibleState: TreeItemCollapsibleState,
        command?: Command,
    ) {
        super(label, collapsibleState);
        this.command = command;
    }
}
 
export class LexerSymbolsProvider extends AntlrTreeDataProvider<LexerSymbolItem> {
 
    public getChildren(element?: LexerSymbolItem): ProviderResult<LexerSymbolItem[]> {
        return new Promise((resolve) => {
            if (!element) {
                let vocabulary;
                Iif (this.currentFile) {
                    vocabulary = this.backend.getLexerVocabulary(this.currentFile);
                }
 
                if (vocabulary) {
                    const items: LexerSymbolItem[] = [];
                    items.push(new LexerSymbolItem("-1: EOF", TreeItemCollapsibleState.None, {
                        title: "<unused>",
                        command: "",
                        arguments: [],
                    }));
                    for (let i = 0; i <= vocabulary.maxTokenType; ++i) {
                        items.push(this.generateTreeItem(i, vocabulary));
                    }
 
                    resolve(items);
                } else {
                    resolve(null);
                }
            } else {
                resolve(null);
            }
        });
    }
 
    private generateTreeItem(index: number, vocabulary: Vocabulary): LexerSymbolItem {
        const literal = vocabulary.getLiteralName(index);
        const symbolic = vocabulary.getSymbolicName(index);
        let caption = `${index}: `;
        if (!literal && !symbolic) {
            caption += "<unused>";
        } else {
            if (symbolic) {
                caption += symbolic;
            } else {
                caption += "<implicit token>";
            }
 
            Iif (literal) {
                caption += " (" + literal + ")";
            }
        }
 
        const alternative = literal ?? "";
        const info = this.backend.infoForSymbol(this.currentFile ?? "",
            symbolic ?? alternative.substring(1, alternative.length - 2));
 
        const parameters: Command = { title: "", command: "" };
        Iif (info && info.definition) {
            parameters.title = "";
            parameters.command = "antlr.selectGrammarRange";
            parameters.arguments = [info.definition.range];
        }
 
        return new LexerSymbolItem(caption, TreeItemCollapsibleState.None, parameters);
    }
}