-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathutil.ts
65 lines (58 loc) · 1.71 KB
/
util.ts
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
"use strict";
import * as vscode from "vscode";
/**
* String helpers for OPA types.
*/
export function getPackage(parsed: any): string {
return getPathString(parsed["package"].path.slice(1));
}
export function getImports(parsed: any): string[] {
if (parsed.imports !== undefined) {
return parsed.imports.map((x: any) => {
const str = getPathString(x.path.value);
if (!x.alias) {
return str;
}
return str + " as " + x.alias;
});
}
return [];
}
export function getPathString(path: any): string {
let i = -1;
return path.map((x: any) => {
i++;
if (i === 0) {
return x.value;
} else {
if (x.value.match("^[a-zA-Z_][a-zA-Z_0-9]*$")) {
return "." + x.value;
}
return "[\"" + x.value + "\"]";
}
}).join("");
}
export function getPrettyTime(ns: number): string {
const seconds = ns / 1e9;
if (seconds >= 1) {
return seconds.toString() + "s";
}
const milliseconds = ns / 1e6;
if (milliseconds >= 1) {
return milliseconds.toString() + "ms";
}
return (ns / 1e3).toString() + "µs";
}
export function replaceWorkspaceFolderPathVariable(path: string): string {
if (vscode.workspace.workspaceFolders !== undefined) {
// here, uri.fsPath is used as this returns a usable path on both Windows and Unix
const workspaceFolderPath = vscode.workspace.workspaceFolders![0].uri.fsPath;
path = path.replace("${workspaceFolder}", workspaceFolderPath);
path = path.replace("${workspacePath}", workspaceFolderPath);
} else if (path.indexOf("${workspaceFolder}") >= 0) {
vscode.window.showWarningMessage(
"${workspaceFolder} variable configured in settings, but no workspace is active",
);
}
return path;
}