extHostConfiguration.ts 4.0 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
import Event, { Emitter } from 'vs/base/common/event';
S
Sandeep Somavarapu 已提交
10
import { WorkspaceConfiguration, WorkspaceConfiguration2 } 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;
	}
}

S
Sandeep Somavarapu 已提交
27 28 29 30 31 32 33 34
type ConfigurationInspect<T> = {
	key: string;
	defaultValue?: T;
	globalValue?: T;
	workspaceValue?: T;
	folderValue?: T;
};

A
Alex Dima 已提交
35
export class ExtHostConfiguration extends ExtHostConfigurationShape {
E
Erich Gamma 已提交
36

J
Johannes Rieken 已提交
37 38 39
	private readonly _onDidChangeConfiguration = new Emitter<void>();
	private readonly _proxy: MainThreadConfigurationShape;
	private readonly _extHostWorkspace: ExtHostWorkspace;
40
	private _configuration: Configuration<any>;
E
Erich Gamma 已提交
41

J
Johannes Rieken 已提交
42
	constructor(proxy: MainThreadConfigurationShape, extHostWorkspace: ExtHostWorkspace, data: IConfigurationData<any>) {
A
Alex Dima 已提交
43
		super();
J
Johannes Rieken 已提交
44
		this._proxy = proxy;
J
Johannes Rieken 已提交
45 46
		this._extHostWorkspace = extHostWorkspace;
		this._configuration = Configuration.parse(data, extHostWorkspace.workspace);
E
Erich Gamma 已提交
47 48
	}

J
Johannes Rieken 已提交
49
	get onDidChangeConfiguration(): Event<void> {
E
Erich Gamma 已提交
50 51 52
		return this._onDidChangeConfiguration && this._onDidChangeConfiguration.event;
	}

J
Johannes Rieken 已提交
53 54
	$acceptConfigurationChanged(data: IConfigurationData<any>) {
		this._configuration = Configuration.parse(data, this._extHostWorkspace.workspace);
E
Erich Gamma 已提交
55 56 57
		this._onDidChangeConfiguration.fire(undefined);
	}

S
Sandeep Somavarapu 已提交
58 59 60 61 62 63 64 65 66
	getConfiguration(section?: string): WorkspaceConfiguration {
		return this._getConfiguration(section, null, true);
	}

	getConfiguration2(section?: string, resource?: URI): WorkspaceConfiguration2 {
		return this._getConfiguration(section, resource, false);
	}

	private _getConfiguration(section: string, resource: URI, legacy: boolean): WorkspaceConfiguration {
E
Erich Gamma 已提交
67 68

		const config = section
69 70
			? lookUp(this._configuration.getValue(null, { resource }), section)
			: this._configuration.getValue(null, { resource });
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
			},
S
Sandeep Somavarapu 已提交
92
			inspect: <T>(key: string): ConfigurationInspect<T> => {
93
				key = section ? `${section}.${key}` : key;
S
Sandeep Somavarapu 已提交
94
				const config = legacy ? this._configuration.lookupLegacy<T>(key) : this._configuration.lookup<T>(key, { resource });
95
				if (config) {
S
Sandeep Somavarapu 已提交
96
					const inspect: ConfigurationInspect<T> = {
97 98 99
						key,
						defaultValue: config.default,
						globalValue: config.user,
100
						workspaceValue: config.workspace,
101
					};
S
Sandeep Somavarapu 已提交
102 103 104 105
					if (!legacy) {
						inspect.folderValue = config.folder;
					}
					return inspect;
106
				}
107
				return undefined;
E
Erich Gamma 已提交
108
			}
B
Benjamin Pasero 已提交
109
		};
110

111 112
		if (typeof config === 'object') {
			mixin(result, config, false);
113 114
		}

115
		return <WorkspaceConfiguration>Object.freeze(result);
116 117
	}
}