extHostConfiguration.ts 2.9 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  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';
J
Joao Moreno 已提交
8
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
E
Erich Gamma 已提交
9
import {IThreadService, Remotable} from 'vs/platform/thread/common/thread';
10
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
E
Erich Gamma 已提交
11 12 13
import Event, {Emitter} from 'vs/base/common/event';
import {WorkspaceConfiguration} from 'vscode';

A
Alex Dima 已提交
14
@Remotable.ExtHostContext('ExtHostConfiguration')
15
export class ExtHostConfiguration {
E
Erich Gamma 已提交
16 17 18 19 20

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

21
	constructor() {
E
Erich Gamma 已提交
22 23 24
		this._onDidChangeConfiguration = new Emitter<void>();
	}

J
Johannes Rieken 已提交
25
	get onDidChangeConfiguration(): Event<void> {
E
Erich Gamma 已提交
26 27 28
		return this._onDidChangeConfiguration && this._onDidChangeConfiguration.event;
	}

29
	public $acceptConfigurationChanged(config: any) {
E
Erich Gamma 已提交
30 31 32 33 34 35 36 37 38 39 40
		this._config = config;
		this._hasConfig = true;
		this._onDidChangeConfiguration.fire(undefined);
	}

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

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


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

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

		this._configurationService = configurationService;
85
		this._proxy = threadService.getRemotable(ExtHostConfiguration);
E
Erich Gamma 已提交
86

87 88
		this._toDispose = this._configurationService.onDidUpdateConfiguration(event => this._proxy.$acceptConfigurationChanged(event.config));
		this._proxy.$acceptConfigurationChanged(this._configurationService.getConfiguration());
E
Erich Gamma 已提交
89 90 91
	}

	public dispose(): void {
J
Joao Moreno 已提交
92
		this._toDispose = dispose(this._toDispose);
E
Erich Gamma 已提交
93 94
	}
}