-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathVscodeConfiguration.ts
More file actions
77 lines (67 loc) · 2.39 KB
/
VscodeConfiguration.ts
File metadata and controls
77 lines (67 loc) · 2.39 KB
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
import * as os from "node:os";
import * as vscode from "vscode";
import type {
Configuration,
ConfigurationScope,
CursorlessConfiguration,
GetFieldType,
Paths,
} from "@cursorless/lib-common";
import { HatStability, Notifier } from "@cursorless/lib-common";
import type { VscodeIDE } from "./VscodeIDE";
type TranslatorMap = {
[K in Paths<CursorlessConfiguration>]?: (
arg: any,
) => GetFieldType<CursorlessConfiguration, K>;
};
const translators: TranslatorMap = {
["experimental.hatStability"]: (value: string) => {
return HatStability[value as keyof typeof HatStability];
},
};
export default class VscodeConfiguration implements Configuration {
private notifier = new Notifier();
constructor(ide: VscodeIDE) {
this.onDidChangeConfiguration = this.onDidChangeConfiguration.bind(this);
ide.disposeOnExit(
vscode.workspace.onDidChangeConfiguration(this.notifier.notifyListeners),
);
}
getOwnConfiguration<Path extends Paths<CursorlessConfiguration>>(
path: Path,
scope?: ConfigurationScope,
): GetFieldType<CursorlessConfiguration, Path> {
const rawValue = vscode.workspace
.getConfiguration("cursorless", scope)
.get<GetFieldType<CursorlessConfiguration, Path>>(path)!;
return translators[path]?.(rawValue) ?? rawValue;
}
onDidChangeConfiguration = this.notifier.registerListener;
}
/**
* Gets a configuration value from vscode, with supported variables expanded.
* For example, `${userHome}` will be expanded to the user's home directory.
*
* We currently only support `${userHome}`.
*
* @param path The path to the configuration value, eg `cursorless.experimental.hatStability`
* @returns The configuration value, with variables expanded, or undefined if
* the value is not set
*/
export function vscodeGetConfigurationString(path: string): string | undefined {
const index = path.lastIndexOf(".");
const section = path.substring(0, index);
const field = path.substring(index + 1);
const value = vscode.workspace.getConfiguration(section).get<string>(field);
return value != null ? evaluateStringVariables(value) : undefined;
}
function evaluateStringVariables(value: string): string {
return value.replace(/\${(\w+)}/g, (match, variable) => {
switch (variable) {
case "userHome":
return os.homedir();
default:
throw new Error(`Unknown vscode configuration variable '${variable}'`);
}
});
}