summaryrefslogtreecommitdiffstats
path: root/src/util.ts
blob: ac7ad4cb75ce742407841b7119a23dd01291c48c (plain)
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
import * as env from "env-var";
import * as vscode from "vscode";

/**
 * Check if a specific line will be inside a comment block if comment block is inserted,
 * that is a line before the active line
 * @param activeEditor the active editor
 * @param activeLine the !previous! line to be checked
 */
export function inComment(activeEditor: vscode.TextEditor, activeLine: number): boolean {
    if (activeLine === 0) {
        return false;
    }

    const txt: string = activeEditor.document.lineAt(activeLine - 1).text.trim();
    if (!txt.startsWith("///") && !txt.startsWith("*") &&
        !txt.startsWith("/**") && !txt.startsWith("/*!")) {
        return false;
    } else {
        return true;
    }
}

/**
 * Get the indentation string for the current line (line at the current cursor position)
 */
export function getIndentation(editor: vscode.TextEditor = vscode.window.activeTextEditor): string {
    return editor.document.lineAt(editor.selection.start.line).text.match("^\\s*")[0];
}

/**
 * Expand environment variables in the string
 * @param replace string containing environment variables
 * @returns new string with expanded environment variables
 */
export function getEnvVars(replace: string): string {
    let replacement = replace;
    const regex = /\$\{env\:([\w|\d|_]+)\}/m;
    let match: RegExpExecArray;

    // tslint:disable-next-line:no-conditional-assignment
    while ((match = regex.exec(replacement)) !== null) {
        if (match.index === regex.lastIndex) {
            regex.lastIndex++;
        }

        const m = match[1];

        const envVar: string = env.get(m, m).asString();

        replacement = replacement.replace("${env:" + m + "}", envVar);
    }

    return replacement;
}