Skip to content

Commit fee6d41

Browse files
AndreasArvidssonpokeypre-commit-ci-lite[bot]
authored
Added support for user home variable in settings (#1916)
eg `"cursorless.private.hatShapesDir": "C:\\Users\\Andreas\\repositories\\pokey-dotfiles\\cursorless-hat-shapes",` ## Checklist - [/] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [/] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [/] I have not broken the cheatsheet --------- Co-authored-by: Pokey Rule <755842+pokey@users.noreply.github.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 054b193 commit fee6d41

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

packages/cursorless-vscode/src/ide/vscode/VscodeConfiguration.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as os from "node:os";
12
import { HatStability } from "@cursorless/common";
23
import { get } from "lodash";
34
import * as vscode from "vscode";
@@ -15,6 +16,9 @@ const translators = {
1516
hatStability(value: string) {
1617
return HatStability[value as keyof typeof HatStability];
1718
},
19+
snippetsDir: (value?: string) => {
20+
return value != null ? evaluateStringVariables(value) : undefined;
21+
},
1822
},
1923
};
2024

@@ -42,3 +46,32 @@ export default class VscodeConfiguration implements Configuration {
4246

4347
onDidChangeConfiguration = this.notifier.registerListener;
4448
}
49+
50+
/**
51+
* Gets a configuration value from vscode, with supported variables expanded.
52+
* For example, `${userHome}` will be expanded to the user's home directory.
53+
*
54+
* We currently only support `${userHome}`.
55+
*
56+
* @param path The path to the configuration value, eg `cursorless.snippetsDir`
57+
* @returns The configuration value, with variables expanded, or undefined if
58+
* the value is not set
59+
*/
60+
export function vscodeGetConfigurationString(path: string): string | undefined {
61+
const index = path.lastIndexOf(".");
62+
const section = path.substring(0, index);
63+
const field = path.substring(index + 1);
64+
const value = vscode.workspace.getConfiguration(section).get<string>(field);
65+
return value != null ? evaluateStringVariables(value) : undefined;
66+
}
67+
68+
function evaluateStringVariables(value: string): string {
69+
return value.replace(/\${(\w+)}/g, (match, variable) => {
70+
switch (variable) {
71+
case "userHome":
72+
return os.homedir();
73+
default:
74+
throw Error(`Unknown vscode configuration variable '${variable}'`);
75+
}
76+
});
77+
}

packages/cursorless-vscode/src/ide/vscode/hats/VscodeHatRenderer.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { cloneDeep, isEqual } from "lodash";
88
import * as fs from "node:fs";
99
import * as path from "node:path";
1010
import * as vscode from "vscode";
11+
import { vscodeGetConfigurationString } from "../VscodeConfiguration";
1112
import VscodeEnabledHatStyleManager, {
1213
ExtendedHatStyleMap,
1314
} from "../VscodeEnabledHatStyleManager";
@@ -40,23 +41,20 @@ const hatConfigSections = [
4041
"cursorless.individualHatAdjustments",
4142
];
4243

43-
/**
44-
* Maintains the VSCode decoration type objects corresponding to each hat style.
45-
* This class is responsible for the actual svgs / colors used to render the
46-
* hats. The decision about which hat styles should be available is up to
47-
* {@link VscodeEnabledHatStyles}
48-
*/
49-
50-
const SETTING_SECTION_HAT_SHAPES_DIR = "cursorless.private";
51-
const SETTING_NAME_HAT_SHAPES_DIR = "hatShapesDir";
52-
const hatShapesDirSettingId = `${SETTING_SECTION_HAT_SHAPES_DIR}.${SETTING_NAME_HAT_SHAPES_DIR}`;
44+
const hatShapesDirSettingId = "cursorless.private.hatShapesDir";
5345

5446
interface SvgInfo {
5547
svg: string;
5648
svgHeightPx: number;
5749
svgWidthPx: number;
5850
}
5951

52+
/**
53+
* Maintains the VSCode decoration type objects corresponding to each hat style.
54+
* This class is responsible for the actual svgs / colors used to render the
55+
* hats. The decision about which hat styles should be available is up to
56+
* {@link VscodeEnabledHatStyles}
57+
*/
6058
export default class VscodeHatRenderer {
6159
private decorationMap!: HatDecorationMap;
6260
private disposables: vscode.Disposable[] = [];
@@ -124,10 +122,7 @@ export default class VscodeHatRenderer {
124122

125123
private async updateHatsDirWatcher() {
126124
this.hatsDirWatcherDisposable?.dispose();
127-
128-
const hatsDir = vscode.workspace
129-
.getConfiguration(SETTING_SECTION_HAT_SHAPES_DIR)
130-
.get<string>(SETTING_NAME_HAT_SHAPES_DIR)!;
125+
const hatsDir = vscodeGetConfigurationString(hatShapesDirSettingId);
131126

132127
if (hatsDir) {
133128
await this.updateShapeOverrides(hatsDir);

0 commit comments

Comments
 (0)