All files / src/frontend ActionsProvider.ts

0% Statements 0/129
0% Branches 0/47
0% Functions 0/9
0% Lines 0/129

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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
/*
 * This file is released under the MIT license.
 * Copyright (c) 2018, 2022, Mike Lischke
 *
 * See LICENSE file for more info.
 */
 
/* eslint-disable max-classes-per-file */
 
import * as path from "path";
 
import { TreeItem, TreeItemCollapsibleState, Command, TextEditor, TreeView, ProviderResult } from "vscode";
import { AntlrTreeDataProvider } from "./AntlrTreeDataProvider";
import { ILexicalRange, CodeActionType } from "../backend/types";
import { FrontendUtils, IRangeHolder } from "./FrontendUtils";
 
export class RootEntry extends TreeItem {
 
    public contextValue = "actions";
 
    public constructor(label: string, id: string) {
        super(label, TreeItemCollapsibleState.Expanded);
        this.id = id;
    }
}
 
export class ChildEntry extends TreeItem implements IRangeHolder {
 
    private static imageBaseNames: Map<CodeActionType, string> = new Map([
        [CodeActionType.GlobalNamed, "named-action"],
        [CodeActionType.LocalNamed, "named-action"],
        [CodeActionType.ParserAction, "parser-action"],
        [CodeActionType.LexerAction, "parser-action"],
        [CodeActionType.ParserPredicate, "predicate"],
        [CodeActionType.LexerPredicate, "predicate"],
    ]);
 
    public contextValue = "action";
 
    public constructor(
        public readonly parent: RootEntry,
        label: string,
        type: CodeActionType,
        public readonly range?: ILexicalRange,
        command?: Command) {
 
        super(label, TreeItemCollapsibleState.None);
        this.command = command;
 
        const baseName = ChildEntry.imageBaseNames.get(type);
        Iif (baseName) {
            this.contextValue = baseName;
            this.iconPath = {
                light: path.join(__dirname, "..", "..", "..", "misc", baseName + "-light.svg"),
                dark: path.join(__dirname, "..", "..", "..", "misc", baseName + "-dark.svg"),
            };
        }
 
    }
}
 
export class ActionsProvider extends AntlrTreeDataProvider<TreeItem> {
 
    public actionTree: TreeView<TreeItem>;
 
    private globalNamedActionsRoot: RootEntry;
    private localNamedActionsRoot: RootEntry;
    private parserActionsRoot: RootEntry;
    private lexerActionsRoot: RootEntry;
    private parserPredicatesRoot: RootEntry;
    private lexerPredicatesRoot: RootEntry;
 
    private globalNamedActions: ChildEntry[] = [];
    private localNamedActions: ChildEntry[] = [];
    private parserActions: ChildEntry[] = [];
    private lexerActions: ChildEntry[] = [];
    private parserPredicates: ChildEntry[] = [];
    private lexerPredicates: ChildEntry[] = [];
 
    public update(editor: TextEditor): void {
        const position = editor.selection.active;
 
        let action = FrontendUtils.findInListFromPosition(this.globalNamedActions, position.character,
            position.line + 1);
 
        Iif (!action) {
            action = FrontendUtils.findInListFromPosition(this.localNamedActions, position.character,
                position.line + 1);
        }
 
        Iif (!action) {
            action = FrontendUtils.findInListFromPosition(this.parserActions, position.character, position.line + 1);
        }
 
        Iif (!action) {
            action = FrontendUtils.findInListFromPosition(this.lexerActions, position.character, position.line + 1);
        }
 
        Iif (!action) {
            action = FrontendUtils.findInListFromPosition(this.parserPredicates, position.character, position.line + 1);
        }
 
        Iif (!action) {
            action = FrontendUtils.findInListFromPosition(this.lexerPredicates, position.character, position.line + 1);
        }
 
        Iif (action) {
            void this.actionTree.reveal(action, { select: true });
        }
    }
 
    public getParent?(element: TreeItem): ProviderResult<TreeItem> {
        Iif (element instanceof RootEntry) {
            return undefined;
        }
 
        return (element as ChildEntry).parent;
    }
 
    public getChildren(element?: TreeItem): ProviderResult<TreeItem[]> {
        Iif (!this.currentFile) {
            return null;
        }
 
        Iif (!element) {
            return this.createRootEntries();
        }
 
        return new Promise((resolve, reject) => {
            Iif (!this.currentFile) {
                resolve(undefined);
 
                return;
            }
 
            try {
                let listType: CodeActionType;
                let parent: RootEntry;
                let list: ChildEntry[];
 
                switch (element.id) {
                    case "parserActions": {
                        this.parserActions = [];
                        list = this.parserActions;
                        listType = CodeActionType.ParserAction;
                        parent = this.parserActionsRoot;
 
                        break;
                    }
 
                    case "lexerActions": {
                        this.lexerActions = [];
                        list = this.lexerActions;
                        listType = CodeActionType.LexerAction;
                        parent = this.lexerActionsRoot;
 
                        break;
                    }
 
                    case "parserPredicates": {
                        this.parserPredicates = [];
                        list = this.parserPredicates;
                        listType = CodeActionType.ParserPredicate;
                        parent = this.parserPredicatesRoot;
 
                        break;
                    }
 
                    case "lexerPredicates": {
                        this.lexerPredicates = [];
                        list = this.lexerPredicates;
                        listType = CodeActionType.LexerPredicate;
                        parent = this.lexerPredicatesRoot;
 
                        break;
                    }
 
                    case "globalNamedActions": {
                        this.globalNamedActions = [];
                        list = this.globalNamedActions;
                        listType = CodeActionType.GlobalNamed;
                        parent = this.globalNamedActionsRoot;
 
                        break;
                    }
 
                    default: {
                        this.localNamedActions = [];
                        list = this.localNamedActions;
                        listType = CodeActionType.LocalNamed;
                        parent = this.localNamedActionsRoot;
 
                        break;
                    }
                }
 
                const actions = this.backend.listActions(this.currentFile, listType);
 
                actions.forEach((action, index) => {
                    let caption = action.name.length > 0 ? action.name : String(index);
                    Iif (action.description) {
                        if (action.description.includes("\n")) {
                            caption += ": <multi line block>";
                        } else {
                            caption += ": " + action.description;
                        }
                    }
 
                    const range = action && action.definition ? action.definition.range : undefined;
                    const command = action ? {
                        title: "Select Grammar Range",
                        command: "antlr.selectGrammarRange",
                        arguments: [range],
                    } : undefined;
 
                    const item = new ChildEntry(parent, caption.trim(), listType, range, command);
                    list.push(item);
                });
 
                resolve(list);
            } catch (e) {
                reject(e);
            }
        });
    }
 
    /**
     * Generates the root entries for the actions treeview.
     *
     * @returns A promise resolving to a list of root tree items.
     */
    private createRootEntries(): ProviderResult<TreeItem[]> {
        return new Promise((resolve, reject) => {
            Iif (!this.currentFile) {
                return null;
            }
 
            try {
                const rootList: RootEntry[] = [];
 
                const counts = this.backend.getActionCounts(this.currentFile);
 
                Iif ((counts.get(CodeActionType.GlobalNamed) ?? 0) > 0) {
                    this.globalNamedActionsRoot = new RootEntry("Global Named Actions", "globalNamedActions");
                    this.globalNamedActionsRoot.tooltip = "Code which is embedded into the generated files " +
                        "at specific locations (like the head of the file).\n\n" +
                        "This code does not take part in the parsing process and is not represented in the ATN.";
                    rootList.push(this.globalNamedActionsRoot);
                }
 
                Iif ((counts.get(CodeActionType.LocalNamed) ?? 0) > 0) {
                    this.localNamedActionsRoot = new RootEntry("Local Named Actions", "localNamedActions");
                    this.localNamedActionsRoot.tooltip = "Code which is embedded into the generated parser code " +
                        "for a rule, like initialization code (@init). \n\n" +
                        "This code is directly executed during the parsing process, but is not represented in the ATN.";
                    rootList.push(this.localNamedActionsRoot);
                }
 
                Iif ((counts.get(CodeActionType.ParserAction) ?? 0) > 0) {
                    this.parserActionsRoot = new RootEntry("Parser Actions", "parserActions");
                    this.parserActionsRoot.tooltip = "Code which is embedded into the generated parser " +
                        "code and executed as part of the parsing process. There are also transitions in the ATN for " +
                        "each action, but they are not used from the generated parser (all action indices are -1).";
                    rootList.push(this.parserActionsRoot);
                }
 
                Iif ((counts.get(CodeActionType.LexerAction) ?? 0) > 0) {
                    this.lexerActionsRoot = new RootEntry("Lexer Actions", "lexerActions");
                    this.lexerActionsRoot.tooltip = "Lexer rules are executed in a state machine without " +
                        "any embedded code. However lexer actions are held in generated private methods addressed " +
                        "by an action index given in the action transition between 2 ATN nodes.";
                    rootList.push(this.lexerActionsRoot);
                }
 
                Iif ((counts.get(CodeActionType.ParserPredicate) ?? 0) > 0) {
                    this.parserPredicatesRoot = new RootEntry("Parser Predicates", "parserPredicates");
                    this.parserPredicatesRoot.tooltip = "Semantic predicates are code snippets which can enable or " +
                        "disable a specific alternative in a rule. They are generated in separate methods and are " +
                        "addressed by an index just like lexer actions.\n\n" +
                        "The ATN representation of a predicate is a predicate transition between 2 ATN nodes.";
                    rootList.push(this.parserPredicatesRoot);
                }
 
                Iif ((counts.get(CodeActionType.LexerPredicate) ?? 0) > 0) {
                    this.lexerPredicatesRoot = new RootEntry("Lexer Predicates", "lexerPredicates");
                    this.lexerPredicatesRoot.tooltip = "Semantic predicates are code snippets which can enable or " +
                        "disable a specific alternative in a rule. They are generated in separate methods and are " +
                        "addressed by an index just like lexer actions.\n\n" +
                        "The ATN representation of a predicate is a predicate transition between 2 ATN nodes.";
                    rootList.push(this.lexerPredicatesRoot);
                }
 
                resolve(rootList);
            } catch (e) {
                reject(e);
            }
        });
    }
}