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

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

19
export class ConfigurationService extends Disposable implements IConfigurationService, IDisposable {
20 21 22

	_serviceBrand: any;

23
	private _configuration: Configuration;
S
Sandeep Somavarapu 已提交
24
	private userConfiguration: UserConfiguration;
25

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

	constructor(
		@IEnvironmentService environmentService: IEnvironmentService
	) {
32 33
		super();

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

36 37
		this.reset();

B
Benjamin Pasero 已提交
38
		// Listeners
S
Sandeep Somavarapu 已提交
39
		this._register(this.userConfiguration.onDidChangeConfiguration(() => this.onDidChangeUserConfiguration()));
40
		this._register(Registry.as<IConfigurationRegistry>(Extensions.Configuration).onDidRegisterConfiguration(configurationProperties => this.onDidRegisterConfiguration(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 56 57 58
	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 {
		const section = typeof arg1 === 'string' ? arg1 : void 0;
		const overrides = isConfigurationOverrides(arg1) ? arg1 : isConfigurationOverrides(arg2) ? arg2 : {};
		return this.configuration.getValue(section, overrides, null);
B
Benjamin Pasero 已提交
59 60
	}

61 62 63 64
	updateValue(key: string, value: any): TPromise<void>;
	updateValue(key: string, value: any, overrides: IConfigurationOverrides): TPromise<void>;
	updateValue(key: string, value: any, target: ConfigurationTarget): TPromise<void>;
	updateValue(key: string, value: any, overrides: IConfigurationOverrides, target: ConfigurationTarget): TPromise<void>;
65 66
	updateValue(key: string, value: any, arg3?: any, arg4?: any): TPromise<void> {
		return TPromise.wrapError(new Error('not supported'));
67
	}
68

69
	inspect<T>(key: string): {
70 71 72 73 74 75
		default: T,
		user: T,
		workspace: T,
		workspaceFolder: T
		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
	}

88 89
	reloadConfiguration(folder?: IWorkspaceFolder): TPromise<void> {
		return folder ? TPromise.as(null) :
S
Sandeep Somavarapu 已提交
90
			this.userConfiguration.reload().then(() => this.onDidChangeUserConfiguration());
91
	}
92

S
Sandeep Somavarapu 已提交
93
	private onDidChangeUserConfiguration(): void {
94
		let changedKeys = [];
S
Sandeep Somavarapu 已提交
95
		const { added, updated, removed } = compare(this._configuration.user, this.userConfiguration.configurationModel);
96 97 98 99
		changedKeys = [...added, ...updated, ...removed];
		if (changedKeys.length) {
			const oldConfiguartion = this._configuration;
			this.reset();
S
Sandeep Somavarapu 已提交
100
			changedKeys = changedKeys.filter(key => !equals(oldConfiguartion.getValue(key, {}, null), this._configuration.getValue(key, {}, null)));
101 102 103 104 105 106 107 108 109 110 111 112
			if (changedKeys.length) {
				this.trigger(changedKeys, ConfigurationTarget.USER);
			}
		}
	}

	private onDidRegisterConfiguration(keys: string[]): void {
		this.reset(); // reset our caches
		this.trigger(keys, ConfigurationTarget.DEFAULT);
	}

	private reset(): void {
113
		const defaults = new DefaultConfigurationModel();
S
Sandeep Somavarapu 已提交
114
		const user = this.userConfiguration.configurationModel;
115 116 117 118
		this._configuration = new Configuration(defaults, user);
	}

	private trigger(keys: string[], source: ConfigurationTarget): void {
119
		this._onDidChangeConfiguration.fire(new ConfigurationChangeEvent().change(keys).telemetryData(source, this.getTargetConfiguration(source)));
120 121 122 123 124 125 126 127 128 129
	}

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