extHostConfiguration.ts 4.3 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
import { mixin } from 'vs/base/common/objects';
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';
12
import { WorkspaceConfigurationNode, IWorkspaceConfigurationValue } from 'vs/workbench/services/configuration/common/configuration';
E
Erich Gamma 已提交
13

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

16
	private _proxy: MainThreadConfigurationShape;
17
	private _config: WorkspaceConfigurationNode;
J
Johannes Rieken 已提交
18
	private _onDidChangeConfiguration = new Emitter<void>();
E
Erich Gamma 已提交
19

20
	constructor(proxy: MainThreadConfigurationShape, config: WorkspaceConfigurationNode) {
A
Alex Dima 已提交
21
		super();
J
Johannes Rieken 已提交
22
		this._proxy = proxy;
23
		this._config = config;
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: WorkspaceConfigurationNode) {
E
Erich Gamma 已提交
31 32 33 34 35 36 37
		this._config = config;
		this._onDidChangeConfiguration.fire(undefined);
	}

	public getConfiguration(section?: string): WorkspaceConfiguration {

		const config = section
38
			? ExtHostConfiguration._lookUp(section, this._config)
E
Erich Gamma 已提交
39 40
			: this._config;

41 42
		const result: WorkspaceConfiguration = {
			has(key: string): boolean {
43
				return typeof ExtHostConfiguration._lookUp(key, <WorkspaceConfigurationNode>config) !== 'undefined';
44
			},
45 46
			get<T>(key: string, defaultValue?: T): any {
				let result = ExtHostConfiguration._lookUp(key, <WorkspaceConfigurationNode>config);
47
				if (typeof result === 'undefined') {
48 49 50 51 52
					return defaultValue;
				} else if (isConfigurationValue(result)) {
					return result.value;
				} else {
					return ExtHostConfiguration._values(result);
53
				}
54
			},
55
			update: (key: string, value: any, global: boolean = false) => {
56 57
				key = section ? `${section}.${key}` : key;
				const target = global ? ConfigurationTarget.USER : ConfigurationTarget.WORKSPACE;
58 59 60 61 62
				if (value !== void 0) {
					return this._proxy.$updateConfigurationOption(target, key, value);
				} else {
					return this._proxy.$removeConfigurationOption(target, key);
				}
63 64 65 66 67 68 69 70 71 72 73
			},
			inspect(key: string) {
				let result = ExtHostConfiguration._lookUp(key, <WorkspaceConfigurationNode>config);
				if (isConfigurationValue(result)) {
					return {
						key: section ? `${section}.${key}` : key,
						defaultValue: result.default,
						globalValue: result.user,
						workspaceValue: result.workspace
					};
				}
E
Erich Gamma 已提交
74
			}
B
Benjamin Pasero 已提交
75
		};
76

77 78
		if (!isConfigurationValue(config)) {
			mixin(result, ExtHostConfiguration._values(config), false);
79 80
		}

81
		return Object.freeze(result);
82

E
Erich Gamma 已提交
83 84
	}

85
	private static _lookUp(section: string, config: WorkspaceConfigurationNode): WorkspaceConfigurationNode | IWorkspaceConfigurationValue<any> {
E
Erich Gamma 已提交
86 87 88 89 90 91
		if (!section) {
			return;
		}
		let parts = section.split('.');
		let node = config;
		while (node && parts.length) {
92 93 94 95 96 97
			let child = node[parts.shift()];
			if (isConfigurationValue(child)) {
				return child;
			} else {
				node = child;
			}
E
Erich Gamma 已提交
98 99 100 101
		}

		return node;
	}
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

	private static _values(node: WorkspaceConfigurationNode): any {
		let target = Object.create(null);
		for (let key in node) {
			let child = node[key];
			if (isConfigurationValue(child)) {
				target[key] = child.value;
			} else {
				target[key] = ExtHostConfiguration._values(child);
			}
		}
		return target;
	}
}

function isConfigurationValue(thing: any): thing is IWorkspaceConfigurationValue<any> {
	return typeof thing === 'object'
		// must have 'value'
		&& typeof (<IWorkspaceConfigurationValue<any>>thing).value !== 'undefined'
		// and at least one source 'default', 'user', or 'workspace'
		&& (typeof (<IWorkspaceConfigurationValue<any>>thing).default !== 'undefined'
			|| typeof (<IWorkspaceConfigurationValue<any>>thing).user !== 'undefined'
			|| typeof (<IWorkspaceConfigurationValue<any>>thing).workspace !== 'undefined');
E
Erich Gamma 已提交
125
}