extHostConfiguration.ts 10.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';

J
Johannes Rieken 已提交
7
import { mixin, deepClone } from 'vs/base/common/objects';
8
import URI from 'vs/base/common/uri';
9
import Event, { Emitter } from 'vs/base/common/event';
10
import * as vscode from 'vscode';
11
import { ExtHostWorkspace } from 'vs/workbench/api/node/extHostWorkspace';
S
Sandeep Somavarapu 已提交
12
import { ExtHostConfigurationShape, MainThreadConfigurationShape, IWorkspaceConfigurationChangeEventData, IConfigurationInitData } from './extHost.protocol';
S
Sandeep Somavarapu 已提交
13
import { ConfigurationTarget as ExtHostConfigurationTarget } from './extHostTypes';
14
import { IConfigurationData, ConfigurationTarget, IConfigurationModel } from 'vs/platform/configuration/common/configuration';
S
Sandeep Somavarapu 已提交
15
import { Configuration, ConfigurationChangeEvent, ConfigurationModel } from 'vs/platform/configuration/common/configurationModels';
16 17
import { WorkspaceConfigurationChangeEvent } from 'vs/workbench/services/configuration/common/configurationModels';
import { StrictResourceMap } from 'vs/base/common/map';
S
Sandeep Somavarapu 已提交
18
import { ConfigurationScope } from 'vs/platform/configuration/common/configurationRegistry';
S
Sandeep Somavarapu 已提交
19 20 21
import { isObject } from 'vs/base/common/types';

declare var Proxy: any; // TODO@TypeScript
22 23 24 25 26 27 28 29 30 31 32 33

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 已提交
34 35 36 37 38
type ConfigurationInspect<T> = {
	key: string;
	defaultValue?: T;
	globalValue?: T;
	workspaceValue?: T;
S
Sandeep Somavarapu 已提交
39
	workspaceFolderValue?: T;
S
Sandeep Somavarapu 已提交
40 41
};

42
export class ExtHostConfiguration implements ExtHostConfigurationShape {
E
Erich Gamma 已提交
43

S
Sandeep Somavarapu 已提交
44
	private readonly _onDidChangeConfiguration = new Emitter<vscode.ConfigurationChangeEvent>();
J
Johannes Rieken 已提交
45 46
	private readonly _proxy: MainThreadConfigurationShape;
	private readonly _extHostWorkspace: ExtHostWorkspace;
S
Sandeep Somavarapu 已提交
47
	private _configurationScopes: { [key: string]: ConfigurationScope };
48
	private _configuration: Configuration;
E
Erich Gamma 已提交
49

S
Sandeep Somavarapu 已提交
50
	constructor(proxy: MainThreadConfigurationShape, extHostWorkspace: ExtHostWorkspace, data: IConfigurationInitData) {
J
Johannes Rieken 已提交
51
		this._proxy = proxy;
J
Johannes Rieken 已提交
52
		this._extHostWorkspace = extHostWorkspace;
53
		this._configuration = ExtHostConfiguration.parse(data);
S
Sandeep Somavarapu 已提交
54
		this._configurationScopes = data.configurationScopes;
E
Erich Gamma 已提交
55 56
	}

S
Sandeep Somavarapu 已提交
57
	get onDidChangeConfiguration(): Event<vscode.ConfigurationChangeEvent> {
E
Erich Gamma 已提交
58 59 60
		return this._onDidChangeConfiguration && this._onDidChangeConfiguration.event;
	}

61
	$acceptConfigurationChanged(data: IConfigurationData, eventData: IWorkspaceConfigurationChangeEventData) {
62
		this._configuration = ExtHostConfiguration.parse(data);
S
Sandeep Somavarapu 已提交
63
		this._onDidChangeConfiguration.fire(this._toConfigurationChangeEvent(eventData));
E
Erich Gamma 已提交
64 65
	}

S
Sandeep Somavarapu 已提交
66
	getConfiguration(section?: string, resource?: URI, extensionId?: string): vscode.WorkspaceConfiguration {
67
		const config = this._toReadonlyValue(section
68
			? lookUp(this._configuration.getValue(null, { resource }, this._extHostWorkspace.workspace), section)
69
			: this._configuration.getValue(null, { resource }, this._extHostWorkspace.workspace));
E
Erich Gamma 已提交
70

S
Sandeep Somavarapu 已提交
71 72 73 74
		if (section) {
			this._validateConfigurationAccess(section, resource, extensionId);
		}

S
Sandeep Somavarapu 已提交
75 76
		function parseConfigurationTarget(arg: boolean | ExtHostConfigurationTarget): ConfigurationTarget {
			if (arg === void 0 || arg === null) {
S
Sandeep Somavarapu 已提交
77
				return null;
S
Sandeep Somavarapu 已提交
78 79 80 81 82 83 84 85
			}
			if (typeof arg === 'boolean') {
				return arg ? ConfigurationTarget.USER : ConfigurationTarget.WORKSPACE;
			}

			switch (arg) {
				case ExtHostConfigurationTarget.Global: return ConfigurationTarget.USER;
				case ExtHostConfigurationTarget.Workspace: return ConfigurationTarget.WORKSPACE;
86
				case ExtHostConfigurationTarget.WorkspaceFolder: return ConfigurationTarget.WORKSPACE_FOLDER;
S
Sandeep Somavarapu 已提交
87 88 89
			}
		}

90
		const result: vscode.WorkspaceConfiguration = {
91
			has(key: string): boolean {
92
				return typeof lookUp(config, key) !== 'undefined';
93
			},
S
Sandeep Somavarapu 已提交
94 95
			get: <T>(key: string, defaultValue?: T) => {
				this._validateConfigurationAccess(section ? `${section}.${key}` : key, resource, extensionId);
96
				let result = lookUp(config, key);
97
				if (typeof result === 'undefined') {
98
					result = defaultValue;
S
Sandeep Somavarapu 已提交
99 100 101 102
				} else {
					let clonedConfig = void 0;
					const cloneOnWriteProxy = (target: any, accessor: string): any => {
						let clonedTarget = void 0;
S
Sandeep Somavarapu 已提交
103 104 105 106
						const cloneTarget = () => {
							clonedConfig = clonedConfig ? clonedConfig : deepClone(config);
							clonedTarget = clonedTarget ? clonedTarget : lookUp(clonedConfig, accessor);
						};
S
Sandeep Somavarapu 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120
						return isObject(target) ?
							new Proxy(target, {
								get: (target: any, property: string) => {
									if (clonedConfig) {
										clonedTarget = clonedTarget ? clonedTarget : lookUp(clonedConfig, accessor);
										return clonedTarget[property];
									}
									const result = target[property];
									if (typeof property === 'string' && property.toLowerCase() !== 'tojson') {
										return cloneOnWriteProxy(result, `${accessor}.${property}`);
									}
									return result;
								},
								set: (target: any, property: string, value: any) => {
S
Sandeep Somavarapu 已提交
121
									cloneTarget();
S
Sandeep Somavarapu 已提交
122 123
									clonedTarget[property] = value;
									return true;
S
Sandeep Somavarapu 已提交
124 125 126 127 128 129 130 131 132 133
								},
								deleteProperty: (target: any, property: string) => {
									cloneTarget();
									delete clonedTarget[property];
									return true;
								},
								defineProperty: (target: any, property: string, descriptor: any) => {
									cloneTarget();
									Object.defineProperty(clonedTarget, property, descriptor);
									return true;
S
Sandeep Somavarapu 已提交
134 135 136 137
								}
							}) : target;
					};
					result = cloneOnWriteProxy(result, key);
138
				}
139
				return result;
140
			},
S
Sandeep Somavarapu 已提交
141
			update: (key: string, value: any, arg: ExtHostConfigurationTarget | boolean) => {
142
				key = section ? `${section}.${key}` : key;
S
Sandeep Somavarapu 已提交
143
				const target = parseConfigurationTarget(arg);
144
				if (value !== void 0) {
S
Sandeep Somavarapu 已提交
145
					return this._proxy.$updateConfigurationOption(target, key, value, resource);
146
				} else {
S
Sandeep Somavarapu 已提交
147
					return this._proxy.$removeConfigurationOption(target, key, resource);
148
				}
149
			},
S
Sandeep Somavarapu 已提交
150
			inspect: <T>(key: string): ConfigurationInspect<T> => {
151
				key = section ? `${section}.${key}` : key;
J
Johannes Rieken 已提交
152
				const config = deepClone(this._configuration.inspect<T>(key, { resource }, this._extHostWorkspace.workspace));
153
				if (config) {
S
Sandeep Somavarapu 已提交
154
					return {
155 156 157
						key,
						defaultValue: config.default,
						globalValue: config.user,
158
						workspaceValue: config.workspace,
159
						workspaceFolderValue: config.workspaceFolder
160 161
					};
				}
162
				return undefined;
E
Erich Gamma 已提交
163
			}
B
Benjamin Pasero 已提交
164
		};
165

166 167
		if (typeof config === 'object') {
			mixin(result, config, false);
168 169
		}

170
		return <vscode.WorkspaceConfiguration>Object.freeze(result);
171
	}
172

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	private _toReadonlyValue(result: any): any {
		const readonlyProxy = (target) => {
			return isObject(target) ?
				new Proxy(target, {
					get: (target: any, property: string) => readonlyProxy(target[property]),
					set: (target: any, property: string, value: any) => { throw new Error(`TypeError: Cannot assign to read only property '${property}' of object`); },
					deleteProperty: (target: any, property: string) => { throw new Error(`TypeError: Cannot delete read only property '${property}' of object`); },
					defineProperty: (target: any, property: string) => { throw new Error(`TypeError: Cannot define property '${property}' for a readonly object`); },
					setPrototypeOf: (target: any) => { throw new Error(`TypeError: Cannot set prototype for a readonly object`); },
					isExtensible: () => false,
					preventExtensions: () => true
				}) : target;
		};
		return readonlyProxy(result);
	}

S
Sandeep Somavarapu 已提交
189
	private _validateConfigurationAccess(key: string, resource: URI, extensionId: string): void {
S
Sandeep Somavarapu 已提交
190
		const scope = this._configurationScopes[key];
S
Sandeep Somavarapu 已提交
191 192
		const extensionIdText = extensionId ? `[${extensionId}] ` : '';
		if (ConfigurationScope.RESOURCE === scope) {
193 194
			if (resource === void 0) {
				console.warn(`${extensionIdText}Accessing a resource scoped configuration without providing a resource is not expected. To get the effective value for '${key}', provide the URI of a resource or 'null' for any resource.`);
S
Sandeep Somavarapu 已提交
195 196 197 198 199 200 201 202 203 204 205 206
			}
			return;
		}
		if (ConfigurationScope.WINDOW === scope) {
			if (resource) {
				console.warn(`${extensionIdText}Accessing a window scoped configuration for a resource is not expected. To associate '${key}' to a resource, define its scope to 'resource' in configuration contributions in 'package.json'.`);
			}
			return;
		}
	}

	private _toConfigurationChangeEvent(data: IWorkspaceConfigurationChangeEventData): vscode.ConfigurationChangeEvent {
207 208 209 210 211 212 213
		const changedConfiguration = new ConfigurationModel(data.changedConfiguration.contents, data.changedConfiguration.keys, data.changedConfiguration.overrides);
		const changedConfigurationByResource: StrictResourceMap<ConfigurationModel> = new StrictResourceMap<ConfigurationModel>();
		for (const key of Object.keys(data.changedConfigurationByResource)) {
			const resource = URI.parse(key);
			const model = data.changedConfigurationByResource[key];
			changedConfigurationByResource.set(resource, new ConfigurationModel(model.contents, model.keys, model.overrides));
		}
S
Sandeep Somavarapu 已提交
214 215 216 217
		const event = new WorkspaceConfigurationChangeEvent(new ConfigurationChangeEvent(changedConfiguration, changedConfigurationByResource), this._extHostWorkspace.workspace);
		return Object.freeze({
			affectsConfiguration: (section: string, resource?: URI) => event.affectsConfiguration(section, resource)
		});
218
	}
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

	private static parse(data: IConfigurationData): Configuration {
		const defaultConfiguration = ExtHostConfiguration.parseConfigurationModel(data.defaults);
		const userConfiguration = ExtHostConfiguration.parseConfigurationModel(data.user);
		const workspaceConfiguration = ExtHostConfiguration.parseConfigurationModel(data.workspace);
		const folders: StrictResourceMap<ConfigurationModel> = Object.keys(data.folders).reduce((result, key) => {
			result.set(URI.parse(key), ExtHostConfiguration.parseConfigurationModel(data.folders[key]));
			return result;
		}, new StrictResourceMap<ConfigurationModel>());
		return new Configuration(defaultConfiguration, userConfiguration, workspaceConfiguration, folders, new ConfigurationModel(), new StrictResourceMap<ConfigurationModel>(), false);
	}

	private static parseConfigurationModel(model: IConfigurationModel): ConfigurationModel {
		return new ConfigurationModel(model.contents, model.keys, model.overrides).freeze();
	}
234
}