extHostConfiguration.ts 2.4 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';
8
import {illegalState} from 'vs/base/common/errors';
E
Erich Gamma 已提交
9 10
import Event, {Emitter} from 'vs/base/common/event';
import {WorkspaceConfiguration} from 'vscode';
11 12
import {ExtHostConfigurationShape, MainContext, MainThreadConfigurationShape} from './extHost.protocol';
import {IThreadService} from 'vs/workbench/services/thread/common/threadService';
E
Erich Gamma 已提交
13

A
Alex Dima 已提交
14
export class ExtHostConfiguration extends ExtHostConfigurationShape {
E
Erich Gamma 已提交
15

16
	private _proxy: MainThreadConfigurationShape;
E
Erich Gamma 已提交
17
	private _hasConfig: boolean;
18
	private _config: any;
E
Erich Gamma 已提交
19 20
	private _onDidChangeConfiguration: Emitter<void>;

21
	constructor(threadService: IThreadService) {
A
Alex Dima 已提交
22
		super();
23
		this._proxy = threadService.get(MainContext.MainThreadConfiguration);
E
Erich Gamma 已提交
24 25 26
		this._onDidChangeConfiguration = new Emitter<void>();
	}

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

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

	public getConfiguration(section?: string): WorkspaceConfiguration {
		if (!this._hasConfig) {
39
			throw illegalState('missing config');
E
Erich Gamma 已提交
40 41 42
		}

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

46 47 48 49 50 51 52
		let result: any;
		if (typeof config !== 'object') {
			// this catches missing config and accessing values
			result = {};
		} else {
			result = clone(config);
		}
E
Erich Gamma 已提交
53 54

		result.has = function(key: string): boolean {
55
			return typeof ExtHostConfiguration._lookUp(key, config) !== 'undefined';
B
Benjamin Pasero 已提交
56
		};
E
Erich Gamma 已提交
57
		result.get = function <T>(key: string, defaultValue?: T): T {
58
			let result = ExtHostConfiguration._lookUp(key, config);
E
Erich Gamma 已提交
59 60 61 62
			if (typeof result === 'undefined') {
				result = defaultValue;
			}
			return result;
B
Benjamin Pasero 已提交
63
		};
E
Erich Gamma 已提交
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;
	}
}