extHostConfiguration.ts 3.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
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';
11
import { IConfigurationValues } from 'vs/platform/configuration/common/configuration';
12
import { ConfigurationTarget } from 'vs/workbench/services/configuration/common/configurationEditing';
13
import { toValuesTree } from 'vs/platform/configuration/common/model';
14 15 16 17 18 19 20 21 22 23 24 25 26

function lookUp(tree: any, key: string) {
	if (key) {
		const parts = key.split('.');
		let node = tree;
		for (let i = 0; node && i < parts.length; i++) {
			node = node[parts[i]];
		}
		return node;
	}
}

interface UsefulConfiguration {
27
	data: IConfigurationValues;
28 29 30
	valueTree: any;
}

31
function createUsefulConfiguration(data: IConfigurationValues): { data: IConfigurationValues, valueTree: any } {
32
	const valueMap: { [key: string]: any } = Object.create(null);
33 34
	for (let key in data) {
		if (Object.prototype.hasOwnProperty.call(data, key)) {
35
			valueMap[key] = data[key].value;
36 37
		}
	}
38
	const valueTree = toValuesTree(valueMap, message => console.error(`Conflict in configuration settings: ${message}`));
39 40 41 42 43
	return {
		data,
		valueTree
	};
}
E
Erich Gamma 已提交
44

A
Alex Dima 已提交
45
export class ExtHostConfiguration extends ExtHostConfigurationShape {
E
Erich Gamma 已提交
46

J
Johannes Rieken 已提交
47
	private _onDidChangeConfiguration = new Emitter<void>();
48 49
	private _proxy: MainThreadConfigurationShape;
	private _configuration: UsefulConfiguration;
E
Erich Gamma 已提交
50

51
	constructor(proxy: MainThreadConfigurationShape, data: IConfigurationValues) {
A
Alex Dima 已提交
52
		super();
J
Johannes Rieken 已提交
53
		this._proxy = proxy;
54
		this._configuration = createUsefulConfiguration(data);
E
Erich Gamma 已提交
55 56
	}

J
Johannes Rieken 已提交
57
	get onDidChangeConfiguration(): Event<void> {
E
Erich Gamma 已提交
58 59 60
		return this._onDidChangeConfiguration && this._onDidChangeConfiguration.event;
	}

61
	public $acceptConfigurationChanged(data: IConfigurationValues) {
62
		this._configuration = createUsefulConfiguration(data);
E
Erich Gamma 已提交
63 64 65 66 67 68
		this._onDidChangeConfiguration.fire(undefined);
	}

	public getConfiguration(section?: string): WorkspaceConfiguration {

		const config = section
69 70
			? lookUp(this._configuration.valueTree, section)
			: this._configuration.valueTree;
E
Erich Gamma 已提交
71

72 73
		const result: WorkspaceConfiguration = {
			has(key: string): boolean {
74
				return typeof lookUp(config, key) !== 'undefined';
75
			},
76 77
			get<T>(key: string, defaultValue?: T): T {
				let result = lookUp(config, key);
78
				if (typeof result === 'undefined') {
79
					result = defaultValue;
80
				}
81
				return result;
82
			},
83
			update: (key: string, value: any, global: boolean = false) => {
84 85
				key = section ? `${section}.${key}` : key;
				const target = global ? ConfigurationTarget.USER : ConfigurationTarget.WORKSPACE;
86 87 88 89 90
				if (value !== void 0) {
					return this._proxy.$updateConfigurationOption(target, key, value);
				} else {
					return this._proxy.$removeConfigurationOption(target, key);
				}
91
			},
92 93 94 95
			inspect: <T>(key: string): { key: string; defaultValue?: T; globalValue?: T; workspaceValue?: T } => {
				key = section ? `${section}.${key}` : key;
				const config = this._configuration.data[key];
				if (config) {
96
					return {
97 98 99 100
						key,
						defaultValue: config.default,
						globalValue: config.user,
						workspaceValue: config.workspace
101 102
					};
				}
103
				return undefined;
E
Erich Gamma 已提交
104
			}
B
Benjamin Pasero 已提交
105
		};
106

107 108
		if (typeof config === 'object') {
			mixin(result, config, false);
109 110
		}

111
		return <WorkspaceConfiguration>Object.freeze(result);
112 113
	}
}