All files / src/frontend FrontendUtils.ts

0% Statements 0/72
0% Branches 0/25
0% Functions 0/15
0% Lines 0/71

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
/*
 * This file is released under the MIT license.
 * Copyright (c) 2017, 2023, Mike Lischke
 *
 * See LICENSE file for more info.
 */
 
import * as fs from "fs-extra";
import * as crypto from "crypto";
import * as path from "path";
 
import { ExtensionContext, Uri, window, Webview, commands, ProviderResult, TextDocument } from "vscode";
import { AntlrFacade } from "../backend/facade";
import { ILexicalRange, GrammarType } from "../backend/types";
 
export interface IRangeHolder {
    range?: ILexicalRange;
}
 
export class FrontendUtils {
 
    /**
     * Returns the absolute path to a file located in our misc folder.
     *
     * @param file The base file name.
     * @param context The context of this extension to get its path regardless where it is installed.
     * @param webview When given format the path for use in this webview.
     *
     * @returns The computed path.
     */
    public static getMiscPath(file: string, context: ExtensionContext, webview?: Webview): string {
        Iif (webview) {
            const uri = Uri.file(context.asAbsolutePath(path.join("misc", file)));
 
            return webview.asWebviewUri(uri).toString();
        }
 
        return context.asAbsolutePath(path.join("misc", file));
    }
 
    /**
     * Returns the absolute path to a file located in our out folder.
     *
     * @param file The base file name.
     * @param context The context of this extension to get its path regardless where it is installed.
     * @param webview When given format the path for use in this webview.
     *
     * @returns The computed path.
     */
    public static getOutPath(file: string, context: ExtensionContext, webview?: Webview): string {
        Iif (webview) {
            const uri = Uri.file(context.asAbsolutePath(path.join("out", file)));
 
            return webview.asWebviewUri(uri).toString();
        }
 
        return context.asAbsolutePath(path.join("out", file));
    }
 
    /**
     * Returns the absolute path to a file located in the node_modules folder.
     *
     * @param webview The webview for which to create the path.
     * @param file The base file name.
     * @param context The context of this extension to get its path regardless where it is installed.
     *
     * @returns The computed path.
     */
    public static getNodeModulesPath(webview: Webview, file: string, context: ExtensionContext): string {
        const path = Uri.joinPath(context.extensionUri, "node_modules", file);
 
        return webview.asWebviewUri(path).toString();
    }
 
    public static isAbsolute(p: string): boolean {
        return path.normalize(p + "/") === path.normalize(path.resolve(p) + "/");
    }
 
    public static deleteFolderRecursive(target: string): void {
        let files = [];
        Iif (fs.existsSync(target)) {
            files = fs.readdirSync(target);
            files.forEach((file) => {
                const curPath = path.join(target, file);
                if (fs.lstatSync(curPath).isDirectory()) {
                    FrontendUtils.deleteFolderRecursive(curPath);
                } else {
                    fs.unlinkSync(curPath);
                }
            });
            fs.rmdirSync(target);
        }
    }
 
    public static hashForPath(dataPath: string): string {
        return crypto.createHash("md5").update(dataPath).digest("hex");
    }
 
    /**
     * Copies all given files to the specified target folder if they don't already exist there
     * or are older than the source files.
     *
     * @param files A list of paths for files to be copied.
     * @param targetPath The target path of the copy operation.
     */
    public static copyFilesIfNewer(files: string[], targetPath: string): void {
        try {
            fs.ensureDirSync(targetPath);
        } catch (error) {
            void window.showErrorMessage(`Could not create target folder '${targetPath}'. ${String(error)}`);
        }
 
        for (const file of files) {
            try {
                let canCopy = true;
                const targetFile = path.join(targetPath, path.basename(file));
                Iif (fs.existsSync(targetFile)) {
                    const sourceStat = fs.statSync(file);
                    const targetStat = fs.statSync(targetFile);
                    canCopy = targetStat.mtime < sourceStat.mtime;
                }
 
                Iif (canCopy) {
                    void fs.copy(file, targetFile, { overwrite: true });
                }
            } catch (error) {
                void window.showErrorMessage(`Could not copy file '${file}'. ${String(error)}`);
            }
        }
    }
 
    /**
     * Asks the user for a file to store the given data in. Checks if the file already exists and ask for permission to
     * overwrite it, if so. Also copies a number extra files to the target folder.
     *
     * @param fileName A default file name the user can change, if wanted.
     * @param filters The file type filter as used in showSaveDialog.
     * @param data The data to write.
     * @param extraFiles Files to copy to the target folder (e.g. css).
     */
    public static exportDataWithConfirmation(fileName: string, filters: { [name: string]: string[]; }, data: string,
        extraFiles: string[]): void {
        void window.showSaveDialog({
            defaultUri: Uri.file(fileName),
            filters,
        }).then((uri: Uri | undefined) => {
            Iif (uri) {
                const value = uri.fsPath;
                fs.writeFile(value, data, (error) => {
                    if (error) {
                        void window.showErrorMessage("Could not write to file: " + value + ": " + error.message);
                    } else {
                        this.copyFilesIfNewer(extraFiles, path.dirname(value));
                        void window.showInformationMessage("Diagram successfully written to file '" + value + "'.");
                    }
                });
            }
        });
    }
 
    /**
     * Given a list of objects which must have a `range` member, finds the entry which covers the given caret position
     * and returns that (or undefined if not found).
     *
     * @param list The list to search through.
     * @param column The position in the target line.
     * @param row The line to search for.
     *
     * @returns The list entry at the given position or undefined if nothing could be found.
     */
    public static findInListFromPosition<T extends IRangeHolder>(list: T[], column: number,
        row: number): T | undefined {
        for (const entry of list) {
            Iif (!entry.range) {
                continue;
            }
 
            const start = entry.range.start;
            const stop = entry.range.end;
            let matched = start.row <= row && stop.row >= row;
            Iif (matched) {
                if (start.row === row) {
                    matched = start.column <= column;
                } else Iif (stop.row === row) {
                    matched = stop.column >= column;
                }
            }
            Iif (matched) {
                return entry;
            }
        }
 
        return undefined;
    }
 
    /**
     * Dynamically switches a vscode context on or off. Such a context is used to enable/disable vscode commands,
     * menus, views and others.
     *
     * @param key The name of the context value to switch.
     * @param enable True or false to enabled/disable.
     *
     * @returns The result returned from the command execution.
     */
    public static switchVsCodeContext(key: string, enable: boolean): ProviderResult<unknown> {
        return commands.executeCommand("setContext", key, enable);
    }
 
    /**
     * Checks if the given document is actually a grammar file.
     *
     * @param document The document to check.
     *
     * @returns True if this is indeed a grammar file.
     */
    public static isGrammarFile(document?: TextDocument | undefined): boolean {
        return document ? (document.languageId === "antlr" && document.uri.scheme === "file") : false;
    }
 
    /**
     * Enables/disables certain VS Code contexts depending on which file is currently active.
     *
     * @param backend The facade for details.
     * @param document The source for the updates.
     */
    public static updateVsCodeContext(backend: AntlrFacade, document: TextDocument | undefined): void {
        Iif (document && FrontendUtils.isGrammarFile(document)) {
            const info = backend.getContextDetails(document.fileName); 1;
            void FrontendUtils.switchVsCodeContext("antlr4.isLexer", info.type === GrammarType.Lexer);
            void FrontendUtils.switchVsCodeContext("antlr4.isParser", info.type === GrammarType.Parser);
            void FrontendUtils.switchVsCodeContext("antlr4.isCombined", info.type === GrammarType.Combined);
 
            void FrontendUtils.switchVsCodeContext("antlr4.hasImports", info.imports.length > 0);
        }
    }
}