Skip to content

Commit

Permalink
Fix persisting property values directly (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-wiemer authored Nov 9, 2024
1 parent 560aade commit bf91d87
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
36 changes: 36 additions & 0 deletions client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,39 @@ export function getConfigIDE<T = unknown>(
if (rawResult === undefined) return defaultValue;
return rawResult;
}

export function updateConfig<T>(
configKey: CfgKey,
value: T,
isProperty: boolean,
configTarget: vscode.ConfigurationTarget | undefined = undefined,
outputChannel: vscode.OutputChannel | undefined = undefined,
) {
const log = outputChannel?.appendLine;
log?.(
`updateConfig(${configKey}, ${JSON.stringify(value)}, ${isProperty}, ${configTarget}`,
);
/**
* The key to the corresponding object for this config value.
* Note that we can only update objects, not individual properties.
*/
const configObjectKey = isProperty
? configKey.substring(0, configKey.lastIndexOf('.'))
: configKey;
const lastKeyPart = configKey.substring(configKey.lastIndexOf('.') + 1);
const configRoot = getConfigRoot();
log?.(`Fetching ${configPrefix}.${configObjectKey}`);
const currentObjectValue = configRoot.get(configObjectKey, {});
log?.(`currentObjectValue: ${JSON.stringify(currentObjectValue)}`);
const newObjectValue = isProperty
? {
...currentObjectValue,
[lastKeyPart]: value,
}
: {
...currentObjectValue,
...value,
};
log?.(`newObjectValue: ${JSON.stringify(newObjectValue)}`);
configRoot.update(configObjectKey, newObjectValue, configTarget);
}
6 changes: 3 additions & 3 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import {
serverResetInterpreterPath,
ahkIsRunningContext,
} from '../../util/src/env';
import { getConfigIDE, getConfigRoot } from './config';
import { getConfigIDE, getConfigRoot, updateConfig } from './config';

let client: LanguageClient, outputchannel: OutputChannel, ahkStatusBarItem: StatusBarItem;
const ahkprocesses = new Map<number, ChildProcess & { path?: string }>();
Expand Down Expand Up @@ -572,7 +572,7 @@ async function setInterpreter() {
pick.dispose();
if (sel.detail) {
ahkStatusBarItem.tooltip = interpreterPath = sel.detail;
getConfigRoot().update(CfgKey.InterpreterPath, interpreterPath, from);
updateConfig(CfgKey.InterpreterPath, interpreterPath, true, from, outputchannel);
ahkStatusBarItem.text = sel.label ||= (await getAHKVersion([interpreterPath]))[0];
if (server_is_ready)
commands.executeCommand(serverResetInterpreterPath, interpreterPath);
Expand Down Expand Up @@ -617,7 +617,7 @@ async function selectSyntaxes() {
}
if (path === undefined || v.toLowerCase() === path.toLowerCase())
return;
getConfigRoot().update(CfgKey.Syntaxes, path || undefined, f);
updateConfig(CfgKey.Syntaxes, path || undefined, false, f, outputchannel);
}

function getAHKVersion(paths: string[]): Thenable<string[]> {
Expand Down
1 change: 1 addition & 0 deletions util/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ export const getCfg = <T = string>(
* Sets the value of the key in the provided config.
* If no config provided, updates the global config.
* Does not update IDE settings.
* To update IDE settings, see /extension/config.ts
*/
export const setCfg = <T>(
key: CfgKey,
Expand Down

0 comments on commit bf91d87

Please sign in to comment.