Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for user home variable in settings #1916

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as os from "node:os";
import { HatStability } from "@cursorless/common";
import { get } from "lodash";
import * as vscode from "vscode";
Expand All @@ -15,6 +16,9 @@ const translators = {
hatStability(value: string) {
return HatStability[value as keyof typeof HatStability];
},
snippetsDir: (value?: string) => {
return value != null ? evaluateStringVariables(value) : undefined;
},
},
};

Expand Down Expand Up @@ -42,3 +46,32 @@ export default class VscodeConfiguration implements Configuration {

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.snippetsDir`
* @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 Error(`Unknown vscode configuration variable '${variable}'`);
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { cloneDeep, isEqual } from "lodash";
import * as fs from "node:fs";
import * as path from "node:path";
import * as vscode from "vscode";
import { vscodeGetConfigurationString } from "../VscodeConfiguration";
import VscodeEnabledHatStyleManager, {
ExtendedHatStyleMap,
} from "../VscodeEnabledHatStyleManager";
Expand Down Expand Up @@ -40,23 +41,20 @@ const hatConfigSections = [
"cursorless.individualHatAdjustments",
];

/**
* Maintains the VSCode decoration type objects corresponding to each hat style.
* This class is responsible for the actual svgs / colors used to render the
* hats. The decision about which hat styles should be available is up to
* {@link VscodeEnabledHatStyles}
*/

const SETTING_SECTION_HAT_SHAPES_DIR = "cursorless.private";
const SETTING_NAME_HAT_SHAPES_DIR = "hatShapesDir";
const hatShapesDirSettingId = `${SETTING_SECTION_HAT_SHAPES_DIR}.${SETTING_NAME_HAT_SHAPES_DIR}`;
const hatShapesDirSettingId = "cursorless.private.hatShapesDir";

interface SvgInfo {
svg: string;
svgHeightPx: number;
svgWidthPx: number;
}

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

private async updateHatsDirWatcher() {
this.hatsDirWatcherDisposable?.dispose();

const hatsDir = vscode.workspace
.getConfiguration(SETTING_SECTION_HAT_SHAPES_DIR)
.get<string>(SETTING_NAME_HAT_SHAPES_DIR)!;
const hatsDir = vscodeGetConfigurationString(hatShapesDirSettingId);

if (hatsDir) {
await this.updateShapeOverrides(hatsDir);
Expand Down