diff options
| author | HO-COOH <42881734+HO-COOH@users.noreply.github.com> | 2021-05-15 00:14:36 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-14 18:14:36 +0200 |
| commit | af70cf95d9d010f27da1c1c8987c807ee3255a10 (patch) | |
| tree | 9d6de559ea072c684f690d32816f7fe3ae1a88d3 /src/util.ts | |
| parent | be07386c572fe8258a3b4c101b85dd900867bb13 (diff) | |
| download | doxdocgen-af70cf95d9d010f27da1c1c8987c807ee3255a10.tar.gz | |
Refactor, add {file} template (#221)
* Refactor, add {file} template
* Add test for {file} expansion
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; +} |