extHostConfiguration.ts 2.7 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 8 9 10 11 12
import { mixin } from 'vs/base/common/objects';
import { illegalState } from 'vs/base/common/errors';
import Event, { Emitter } from 'vs/base/common/event';
import { WorkspaceConfiguration } from 'vscode';
import { ExtHostConfigurationShape, MainThreadConfigurationShape } from './extHost.protocol';
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
			},
			update: (key: string, value: any, global: boolean) => {
				key = section ? `${section}.${key}` : key;
				const target = global ? ConfigurationTarget.USER : ConfigurationTarget.WORKSPACE;
59 60 61 62 63
				if (value !== void 0) {
					return this._proxy.$updateConfigurationOption(target, key, value);
				} else {
					return this._proxy.$removeConfigurationOption(target, key);
				}
E
Erich Gamma 已提交
64
			}
B
Benjamin Pasero 已提交
65
		};
66

67 68 69 70
		if (typeof config === 'object') {
			mixin(result, config, false);
		}

71
		return Object.freeze(result);
E
Erich Gamma 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	}

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