extHostConfiguration.ts 2.5 KB
Newer Older
E
Erich Gamma 已提交
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 {mixin} 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';
J
Johannes Rieken 已提交
11
import {ExtHostConfigurationShape, MainThreadConfigurationShape} from './extHost.protocol';
12
// import {ConfigurationTarget} from 'vs/workbench/services/configuration/common/configurationEditing';
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;
J
Johannes Rieken 已提交
19
	private _onDidChangeConfiguration = new Emitter<void>();
E
Erich Gamma 已提交
20

J
Johannes Rieken 已提交
21
	constructor(proxy: MainThreadConfigurationShape) {
A
Alex Dima 已提交
22
		super();
J
Johannes Rieken 已提交
23
		this._proxy = proxy;
E
Erich Gamma 已提交
24 25
	}

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

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

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

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

45 46 47 48 49 50 51 52 53 54
		const result: WorkspaceConfiguration = {
			has(key: string): boolean {
				return typeof ExtHostConfiguration._lookUp(key, config) !== 'undefined';
			},
			get<T>(key: string, defaultValue?: T): T {
				let result = ExtHostConfiguration._lookUp(key, config);
				if (typeof result === 'undefined') {
					result = defaultValue;
				}
				return result;
55 56 57 58 59
			// },
			// update: (key: string, value: any, global: boolean) => {
			// 	key = section ? `${section}.${key}` : key;
			// 	const target = global ? ConfigurationTarget.USER : ConfigurationTarget.WORKSPACE;
			// 	return this._proxy.$updateConfigurationOption(target, key, value);
E
Erich Gamma 已提交
60
			}
B
Benjamin Pasero 已提交
61
		};
62

63 64 65 66
		if (typeof config === 'object') {
			mixin(result, config, false);
		}

67
		return Object.freeze(result);
E
Erich Gamma 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	}

	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;
	}
}