extHostConfiguration.ts 3.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 URI from 'vs/base/common/uri';
9 10
import Event, { Emitter } from 'vs/base/common/event';
import { WorkspaceConfiguration } from 'vscode';
11
import { ExtHostWorkspace } from 'vs/workbench/api/node/extHostWorkspace';
12
import { ExtHostConfigurationShape, MainThreadConfigurationShape } from './extHost.protocol';
13
import { IConfigurationData, Configuration } from 'vs/platform/configuration/common/configuration';
14
import { ConfigurationTarget } from 'vs/workbench/services/configuration/common/configurationEditing';
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;
	}
}

A
Alex Dima 已提交
27
export class ExtHostConfiguration extends ExtHostConfigurationShape {
E
Erich Gamma 已提交
28

J
Johannes Rieken 已提交
29 30 31
	private readonly _onDidChangeConfiguration = new Emitter<void>();
	private readonly _proxy: MainThreadConfigurationShape;
	private readonly _extHostWorkspace: ExtHostWorkspace;
32
	private _configuration: Configuration<any>;
E
Erich Gamma 已提交
33

J
Johannes Rieken 已提交
34
	constructor(proxy: MainThreadConfigurationShape, extHostWorkspace: ExtHostWorkspace, data: IConfigurationData<any>) {
A
Alex Dima 已提交
35
		super();
J
Johannes Rieken 已提交
36
		this._proxy = proxy;
J
Johannes Rieken 已提交
37 38
		this._extHostWorkspace = extHostWorkspace;
		this._configuration = Configuration.parse(data, extHostWorkspace.workspace);
E
Erich Gamma 已提交
39 40
	}

J
Johannes Rieken 已提交
41
	get onDidChangeConfiguration(): Event<void> {
E
Erich Gamma 已提交
42 43 44
		return this._onDidChangeConfiguration && this._onDidChangeConfiguration.event;
	}

J
Johannes Rieken 已提交
45 46
	$acceptConfigurationChanged(data: IConfigurationData<any>) {
		this._configuration = Configuration.parse(data, this._extHostWorkspace.workspace);
E
Erich Gamma 已提交
47 48 49
		this._onDidChangeConfiguration.fire(undefined);
	}

50
	getConfiguration(section?: string, resource?: URI): WorkspaceConfiguration {
E
Erich Gamma 已提交
51 52

		const config = section
53 54
			? lookUp(this._configuration.getValue(null, { resource }), section)
			: this._configuration.getValue(null, { resource });
E
Erich Gamma 已提交
55

56 57
		const result: WorkspaceConfiguration = {
			has(key: string): boolean {
58
				return typeof lookUp(config, key) !== 'undefined';
59
			},
60 61
			get<T>(key: string, defaultValue?: T): T {
				let result = lookUp(config, key);
62
				if (typeof result === 'undefined') {
63
					result = defaultValue;
64
				}
65
				return result;
66
			},
67
			update: (key: string, value: any, global: boolean = false) => {
68 69
				key = section ? `${section}.${key}` : key;
				const target = global ? ConfigurationTarget.USER : ConfigurationTarget.WORKSPACE;
70 71 72 73 74
				if (value !== void 0) {
					return this._proxy.$updateConfigurationOption(target, key, value);
				} else {
					return this._proxy.$removeConfigurationOption(target, key);
				}
75
			},
76
			inspect: <T>(key: string): { key: string; defaultValue?: T; globalValue?: T; workspaceValue?: T, folderValue?: T } => {
77
				key = section ? `${section}.${key}` : key;
J
Johannes Rieken 已提交
78
				const config = this._configuration.values()[key];
79
				if (config) {
80
					return {
81 82 83
						key,
						defaultValue: config.default,
						globalValue: config.user,
84 85
						workspaceValue: config.workspace,
						folderValue: config.folder
86 87
					};
				}
88
				return undefined;
E
Erich Gamma 已提交
89
			}
B
Benjamin Pasero 已提交
90
		};
91

92 93
		if (typeof config === 'object') {
			mixin(result, config, false);
94 95
		}

96
		return <WorkspaceConfiguration>Object.freeze(result);
97 98
	}
}