|
| 1 | +import { ConfigurationTarget, workspace } from 'vscode'; |
| 2 | + |
| 3 | +export class Configuration<T> { |
| 4 | + private _defaultValues: T = {} as T; |
| 5 | + private _values: T = {} as T; |
| 6 | + private identifier: string; |
| 7 | + constructor(identifier: string, defaultValues?: T) { |
| 8 | + this.identifier = identifier; |
| 9 | + this._defaultValues = Object.assign({}, defaultValues); |
| 10 | + } |
| 11 | + |
| 12 | + configuration() { |
| 13 | + return workspace.getConfiguration(this.identifier); |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Return a value from this configuration. |
| 18 | + * @param section — Configuration name, supports dotted names. |
| 19 | + * @param defaultValue — A value should be returned when no value could be found, is undefined. |
| 20 | + * @returns — The value section denotes or the default. |
| 21 | + */ |
| 22 | + get<T>(section: string): T | undefined; |
| 23 | + get<T>(section: string, defaultValue: T): T | undefined; |
| 24 | + get<T>(section: string, defaultValue?: T): T | undefined { |
| 25 | + return this.configuration().get(section, defaultValue ?? this._defaultValues[section]); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Get all Configuration values. |
| 30 | + */ |
| 31 | + values(): T { |
| 32 | + const values = Object.assign({}); |
| 33 | + const cfg = this.configuration(); |
| 34 | + Object.keys(cfg) |
| 35 | + .filter(key => typeof cfg[key] !== 'function') |
| 36 | + .forEach(key => { |
| 37 | + values[key] = cfg.get(key, this._defaultValues[key]); |
| 38 | + }); |
| 39 | + return values; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Update a configuration value. The updated configuration values are persisted. |
| 44 | + * @param section Configuration name, supports dotted names. |
| 45 | + * @param value The new value. |
| 46 | + * @param target The {@link ConfigurationTarget configuration target} or a boolean value. Defaults to `true` |
| 47 | + * - If `true` updates {@link ConfigurationTarget.Global Global settings}. |
| 48 | + * - If `false` updates {@link ConfigurationTarget.Workspace Workspace settings}. |
| 49 | + * - If `undefined` or `null` updates to {@link ConfigurationTarget.WorkspaceFolder Workspace folder settings} if configuration is resource specific, |
| 50 | + * otherwise to {@link ConfigurationTarget.Workspace Workspace settings}. |
| 51 | + */ |
| 52 | + async update( |
| 53 | + section: string, |
| 54 | + value: any, |
| 55 | + target?: ConfigurationTarget | boolean | null, |
| 56 | + ): Promise<void>; |
| 57 | + |
| 58 | + /** |
| 59 | + * Update configuration values. The updated configuration values are persisted. |
| 60 | + * @param values Configuration names and values, supports dotted names. |
| 61 | + * @param target The {@link ConfigurationTarget configuration target} or a boolean value. Defaults to `true` |
| 62 | + * - If `true` updates {@link ConfigurationTarget.Global Global settings}. |
| 63 | + * - If `false` updates {@link ConfigurationTarget.Workspace Workspace settings}. |
| 64 | + * - If `undefined` or `null` updates to {@link ConfigurationTarget.WorkspaceFolder Workspace folder settings} if configuration is resource specific, |
| 65 | + * otherwise to {@link ConfigurationTarget.Workspace Workspace settings}. |
| 66 | + */ |
| 67 | + async update(values: T, target?: ConfigurationTarget | boolean | null): Promise<void>; |
| 68 | + async update(section: string | T, value: any, target?: ConfigurationTarget | boolean | null) { |
| 69 | + const values: any = {}; |
| 70 | + let _target: ConfigurationTarget | boolean | undefined | null; |
| 71 | + if (typeof section === 'string') { |
| 72 | + values[section] = value; |
| 73 | + _target = target; |
| 74 | + } else if (typeof section === 'object') { |
| 75 | + Object.assign(values, section); |
| 76 | + _target = value; |
| 77 | + } else { |
| 78 | + throw new Error(''); |
| 79 | + } |
| 80 | + |
| 81 | + const cfg = this.configuration(); |
| 82 | + await Promise.all( |
| 83 | + Object.keys(values).map(key => |
| 84 | + cfg.update(key, values[key], _target ?? ConfigurationTarget.Global).then(() => { |
| 85 | + this._values[key] = values[key]; |
| 86 | + }), |
| 87 | + ), |
| 88 | + ); |
| 89 | + } |
| 90 | +} |
0 commit comments