configurationService.ts 5.1 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { Registry } from 'vs/platform/registry/common/platform';
7
import { IConfigurationRegistry, Extensions } from 'vs/platform/configuration/common/configurationRegistry';
B
Benjamin Pasero 已提交
8
import { IDisposable, Disposable } from 'vs/base/common/lifecycle';
9
import { IConfigurationService, IConfigurationChangeEvent, IConfigurationOverrides, ConfigurationTarget, compare, isConfigurationOverrides, IConfigurationData } from 'vs/platform/configuration/common/configuration';
S
Sandeep Somavarapu 已提交
10
import { DefaultConfigurationModel, Configuration, ConfigurationChangeEvent, ConfigurationModel } from 'vs/platform/configuration/common/configurationModels';
M
Matt Bierner 已提交
11
import { Event, Emitter } from 'vs/base/common/event';
J
Johannes Rieken 已提交
12
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
13
import { IWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
S
Sandeep Somavarapu 已提交
14
import { UserConfiguration } from 'vs/platform/configuration/node/configuration';
15

16
export class ConfigurationService extends Disposable implements IConfigurationService, IDisposable {
17 18 19

	_serviceBrand: any;

20
	private _configuration: Configuration;
S
Sandeep Somavarapu 已提交
21
	private userConfiguration: UserConfiguration;
22

M
Matt Bierner 已提交
23
	private readonly _onDidChangeConfiguration: Emitter<IConfigurationChangeEvent> = this._register(new Emitter<IConfigurationChangeEvent>());
24
	readonly onDidChangeConfiguration: Event<IConfigurationChangeEvent> = this._onDidChangeConfiguration.event;
25 26 27 28

	constructor(
		@IEnvironmentService environmentService: IEnvironmentService
	) {
29 30
		super();

S
Sandeep Somavarapu 已提交
31
		this.userConfiguration = this._register(new UserConfiguration(environmentService.appSettingsPath));
32

S
Sandeep Somavarapu 已提交
33 34 35 36
		// Initialize
		const defaults = new DefaultConfigurationModel();
		const user = this.userConfiguration.initializeSync();
		this._configuration = new Configuration(defaults, user);
37

B
Benjamin Pasero 已提交
38
		// Listeners
S
Sandeep Somavarapu 已提交
39
		this._register(this.userConfiguration.onDidChangeConfiguration(userConfigurationModel => this.onDidChangeUserConfiguration(userConfigurationModel)));
S
Sandeep Somavarapu 已提交
40
		this._register(Registry.as<IConfigurationRegistry>(Extensions.Configuration).onDidUpdateConfiguration(configurationProperties => this.onDidDefaultConfigurationChange(configurationProperties)));
41 42
	}

43
	get configuration(): Configuration {
44
		return this._configuration;
45 46
	}

47 48 49 50
	getConfigurationData(): IConfigurationData {
		return this.configuration.toData();
	}

51 52 53 54 55
	getValue<T>(): T;
	getValue<T>(section: string): T;
	getValue<T>(overrides: IConfigurationOverrides): T;
	getValue<T>(section: string, overrides: IConfigurationOverrides): T;
	getValue(arg1?: any, arg2?: any): any {
R
Rob Lourens 已提交
56
		const section = typeof arg1 === 'string' ? arg1 : undefined;
57 58
		const overrides = isConfigurationOverrides(arg1) ? arg1 : isConfigurationOverrides(arg2) ? arg2 : {};
		return this.configuration.getValue(section, overrides, null);
B
Benjamin Pasero 已提交
59 60
	}

S
Sandeep Somavarapu 已提交
61 62 63 64 65 66
	updateValue(key: string, value: any): Promise<void>;
	updateValue(key: string, value: any, overrides: IConfigurationOverrides): Promise<void>;
	updateValue(key: string, value: any, target: ConfigurationTarget): Promise<void>;
	updateValue(key: string, value: any, overrides: IConfigurationOverrides, target: ConfigurationTarget): Promise<void>;
	updateValue(key: string, value: any, arg3?: any, arg4?: any): Promise<void> {
		return Promise.reject(new Error('not supported'));
67
	}
68

69
	inspect<T>(key: string): {
70 71
		default: T,
		user: T,
M
Matt Bierner 已提交
72 73
		workspace?: T,
		workspaceFolder?: T
74 75
		value: T
	} {
76
		return this.configuration.inspect<T>(key, {}, null);
77 78
	}

79
	keys(): {
80 81 82 83 84
		default: string[];
		user: string[];
		workspace: string[];
		workspaceFolder: string[];
	} {
S
Sandeep Somavarapu 已提交
85
		return this.configuration.keys(null);
B
Benjamin Pasero 已提交
86 87
	}

S
Sandeep Somavarapu 已提交
88
	reloadConfiguration(folder?: IWorkspaceFolder): Promise<void> {
R
Rob Lourens 已提交
89
		return folder ? Promise.resolve(undefined) :
S
Sandeep Somavarapu 已提交
90
			this.userConfiguration.reload().then(userConfigurationModel => this.onDidChangeUserConfiguration(userConfigurationModel));
91
	}
92

S
Sandeep Somavarapu 已提交
93 94
	private onDidChangeUserConfiguration(userConfigurationModel: ConfigurationModel): void {
		const { added, updated, removed } = compare(this._configuration.user, userConfigurationModel);
S
Sandeep Somavarapu 已提交
95
		const changedKeys = [...added, ...updated, ...removed];
96
		if (changedKeys.length) {
S
Sandeep Somavarapu 已提交
97
			this._configuration.updateUserConfiguration(userConfigurationModel);
S
Sandeep Somavarapu 已提交
98
			this.trigger(changedKeys, ConfigurationTarget.USER);
99 100 101
		}
	}

S
Sandeep Somavarapu 已提交
102
	private onDidDefaultConfigurationChange(keys: string[]): void {
S
Sandeep Somavarapu 已提交
103
		this._configuration.updateDefaultConfiguration(new DefaultConfigurationModel());
104 105 106 107
		this.trigger(keys, ConfigurationTarget.DEFAULT);
	}

	private trigger(keys: string[], source: ConfigurationTarget): void {
108
		this._onDidChangeConfiguration.fire(new ConfigurationChangeEvent().change(keys).telemetryData(source, this.getTargetConfiguration(source)));
109 110 111 112 113 114 115 116 117 118
	}

	private getTargetConfiguration(target: ConfigurationTarget): any {
		switch (target) {
			case ConfigurationTarget.DEFAULT:
				return this._configuration.defaults.contents;
			case ConfigurationTarget.USER:
				return this._configuration.user.contents;
		}
		return {};
119 120
	}
}