diff options
Diffstat (limited to 'src/util.ts')
| -rw-r--r-- | src/util.ts | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/util.ts b/src/util.ts new file mode 100644 index 0000000..f2068e7 --- /dev/null +++ b/src/util.ts @@ -0,0 +1,54 @@ +import * as env from "env-var"; +import * as vscode from "vscode"; + +/** + * Check if a specific line is inside a comment block + * @param activeEditor the active editor + * @param activeLine the line number 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 enviroment variables in the string + * @param replace string containing environment variables + * @returns new string with expanded enviroment 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; +} |