extHostConfiguration.ts 3.2 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import {clone} from 'vs/base/common/objects';
import {IDisposable, disposeAll} from 'vs/base/common/lifecycle';
import {IThreadService, Remotable} from 'vs/platform/thread/common/thread';
import {IConfigurationService, ConfigurationServiceEventTypes, IConfigurationServiceEvent} from 'vs/platform/configuration/common/configuration';
import Event, {Emitter} from 'vs/base/common/event';
import {INullService} from 'vs/platform/instantiation/common/instantiation';
import {WorkspaceConfiguration} from 'vscode';

15 16
@Remotable.PluginHostContext('ExtHostConfiguration')
export class ExtHostConfiguration {
E
Erich Gamma 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

	private _config: any;
	private _hasConfig: boolean;
	private _onDidChangeConfiguration: Emitter<void>;

	constructor(@INullService ns) {
		this._onDidChangeConfiguration = new Emitter<void>();
	}

	get onDidChangeConfiguration() {
		return this._onDidChangeConfiguration && this._onDidChangeConfiguration.event;
	}

	public _acceptConfigurationChanged(config:any) {
		this._config = config;
		this._hasConfig = true;
		this._onDidChangeConfiguration.fire(undefined);
	}

	public getConfiguration(section?: string): WorkspaceConfiguration {
		if (!this._hasConfig) {
			return;
		}

		const config = section
42
			? ExtHostConfiguration._lookUp(section, this._config)
E
Erich Gamma 已提交
43 44 45 46 47 48
			: this._config;


		let result = config ? clone(config) : {};
		// result = Object.freeze(result);
		result.has = function(key: string): boolean {
49
			return typeof ExtHostConfiguration._lookUp(key, config) !== 'undefined';
B
Benjamin Pasero 已提交
50
		};
E
Erich Gamma 已提交
51
		result.get = function <T>(key: string, defaultValue?: T): T {
52
			let result = ExtHostConfiguration._lookUp(key, config);
E
Erich Gamma 已提交
53 54 55 56
			if (typeof result === 'undefined') {
				result = defaultValue;
			}
			return result;
B
Benjamin Pasero 已提交
57
		};
E
Erich Gamma 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
		return result;
	}

	private static _lookUp(section: string, config: any) {
		if (!section) {
			return;
		}
		let parts = section.split('.');
		let node = config;
		while (node && parts.length) {
			node = node[parts.shift()];
		}

		return node;
	}
}

@Remotable.MainContext('MainProcessConfigurationServiceHelper')
export class MainThreadConfiguration {

	private _configurationService: IConfigurationService;
	private _toDispose: IDisposable[];
80
	private _proxy: ExtHostConfiguration;
E
Erich Gamma 已提交
81 82 83 84 85

	constructor(@IConfigurationService configurationService: IConfigurationService,
		@IThreadService threadService: IThreadService) {

		this._configurationService = configurationService;
86
		this._proxy = threadService.getRemotable(ExtHostConfiguration);
E
Erich Gamma 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100

		this._toDispose = [];
		this._toDispose.push(this._configurationService.addListener2(ConfigurationServiceEventTypes.UPDATED, (e:IConfigurationServiceEvent) => {
			this._proxy._acceptConfigurationChanged(e.config);
		}));
		this._configurationService.loadConfiguration().then((config) => {
			this._proxy._acceptConfigurationChanged(config);
		});
	}

	public dispose(): void {
		this._toDispose = disposeAll(this._toDispose);
	}
}