Browse Source

Adds types for UpdateConfigurationCommand

main
Eric Amodio 1 year ago
parent
commit
0b09424966
3 changed files with 43 additions and 12 deletions
  1. +4
    -4
      src/configuration.ts
  2. +3
    -1
      src/webviews/protocol.ts
  3. +36
    -7
      src/webviews/webviewWithConfigBase.ts

+ 4
- 4
src/configuration.ts View File

@ -328,9 +328,9 @@ type SubPath = Key extends string
: never
: never;
type Path<T> = SubPath<T, keyof T> | keyof T;
export type Path<T> = SubPath<T, keyof T> | keyof T;
type PathValue<T, P extends Path<T>> = P extends `${infer Key}.${infer Rest}`
export type PathValue<T, P extends Path<T>> = P extends `${infer Key}.${infer Rest}`
? Key extends keyof T
? Rest extends Path<T[Key]>
? PathValue<T[Key], Rest>
@ -340,5 +340,5 @@ type PathValue> = P extends `${infer Key}.${infer Rest}`
? T[P]
: never;
type ConfigPath = Path<Config>;
type ConfigPathValue<P extends ConfigPath> = PathValue<Config, P>;
export type ConfigPath = Path<Config>;
export type ConfigPathValue<P extends ConfigPath> = PathValue<Config, P>;

+ 3
- 1
src/webviews/protocol.ts View File

@ -1,4 +1,6 @@
import type { Config } from '../config';
import type { ConfigPath, ConfigPathValue } from '../configuration';
import type { CustomConfigPath, CustomConfigPathValue } from './webviewWithConfigBase';
export interface IpcMessage {
id: string;
@ -61,7 +63,7 @@ export const GenerateConfigurationPreviewCommandType = new IpcCommandType
export interface UpdateConfigurationParams {
changes: {
[key: string]: any;
[key in ConfigPath | CustomConfigPath]?: ConfigPathValue<ConfigPath> | CustomConfigPathValue<CustomConfigPath>;
};
removes: string[];
scope?: 'user' | 'workspace';

+ 36
- 7
src/webviews/webviewWithConfigBase.ts View File

@ -1,5 +1,6 @@
import type { ConfigurationChangeEvent, WebviewPanelOnDidChangeViewStateEvent } from 'vscode';
import { ConfigurationTarget } from 'vscode';
import type { Path, PathValue } from '../configuration';
import { configuration } from '../configuration';
import type { Commands, ContextKeys } from '../constants';
import type { Container } from '../container';
@ -76,17 +77,24 @@ export abstract class WebviewWithConfigBase extends WebviewBase {
const target =
params.scope === 'workspace' ? ConfigurationTarget.Workspace : ConfigurationTarget.Global;
for (const key in params.changes) {
let key: keyof typeof params.changes;
for (key in params.changes) {
let value = params.changes[key];
const customSetting = this.customSettings.get(key);
if (customSetting != null) {
await customSetting.update(value);
if (isCustomConfigKey(key)) {
const customSetting = this.customSettings.get(key);
if (customSetting != null) {
if (typeof value === 'boolean') {
await customSetting.update(value);
} else {
debugger;
}
}
continue;
}
const inspect = configuration.inspect(key as any)!;
const inspect = configuration.inspect(key)!;
if (value != null) {
if (params.scope === 'workspace') {
@ -189,10 +197,10 @@ export abstract class WebviewWithConfigBase extends WebviewBase {
}
}
private _customSettings: Map<string, CustomSetting> | undefined;
private _customSettings: Map<CustomConfigPath, CustomSetting> | undefined;
private get customSettings() {
if (this._customSettings == null) {
this._customSettings = new Map<string, CustomSetting>([
this._customSettings = new Map<CustomConfigPath, CustomSetting>([
[
'rebaseEditor.enabled',
{
@ -241,3 +249,24 @@ interface CustomSetting {
enabled: () => boolean;
update: (enabled: boolean) => Promise<void>;
}
interface CustomConfig {
rebaseEditor: {
enabled: boolean;
};
currentLine: {
useUncommittedChangesFormat: boolean;
};
}
export type CustomConfigPath = Path<CustomConfig>;
export type CustomConfigPathValue<P extends CustomConfigPath> = PathValue<CustomConfig, P>;
const customConfigKeys: readonly CustomConfigPath[] = [
'rebaseEditor.enabled',
'currentLine.useUncommittedChangesFormat',
];
export function isCustomConfigKey(key: string): key is CustomConfigPath {
return customConfigKeys.includes(key as CustomConfigPath);
}

Loading…
Cancel
Save