extHostConfiguration.ts 9.9 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
				} else {
					let clonedConfig = void 0;
					const cloneOnWriteProxy = (target: any, accessor: string): any => {
						let clonedTarget = void 0;
						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) => {
									clonedConfig = clonedConfig ? clonedConfig : deepClone(config);
									clonedTarget = clonedTarget ? clonedTarget : lookUp(clonedConfig, accessor);
									clonedTarget[property] = value;
									return true;
								}
							}) : target;
					};
					result = cloneOnWriteProxy(result, key);
125
				}
126
				return result;
127
			},
S
Sandeep Somavarapu 已提交
128
			update: (key: string, value: any, arg: ExtHostConfigurationTarget | boolean) => {
129
				key = section ? `${section}.${key}` : key;
S
Sandeep Somavarapu 已提交
130
				const target = parseConfigurationTarget(arg);
131
				if (value !== void 0) {
S
Sandeep Somavarapu 已提交
132
					return this._proxy.$updateConfigurationOption(target, key, value, resource);
133
				} else {
S
Sandeep Somavarapu 已提交
134
					return this._proxy.$removeConfigurationOption(target, key, resource);
135
				}
136
			},
S
Sandeep Somavarapu 已提交
137
			inspect: <T>(key: string): ConfigurationInspect<T> => {
138
				key = section ? `${section}.${key}` : key;
J
Johannes Rieken 已提交
139
				const config = deepClone(this._configuration.inspect<T>(key, { resource }, this._extHostWorkspace.workspace));
140
				if (config) {
S
Sandeep Somavarapu 已提交
141
					return {
142 143 144
						key,
						defaultValue: config.default,
						globalValue: config.user,
145
						workspaceValue: config.workspace,
146
						workspaceFolderValue: config.workspaceFolder
147 148
					};
				}
149
				return undefined;
E
Erich Gamma 已提交
150
			}
B
Benjamin Pasero 已提交
151
		};
152

153 154
		if (typeof config === 'object') {
			mixin(result, config, false);
155 156
		}

157
		return <vscode.WorkspaceConfiguration>Object.freeze(result);
158
	}
159

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	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 已提交
176
	private _validateConfigurationAccess(key: string, resource: URI, extensionId: string): void {
S
Sandeep Somavarapu 已提交
177
		const scope = this._configurationScopes[key];
S
Sandeep Somavarapu 已提交
178 179
		const extensionIdText = extensionId ? `[${extensionId}] ` : '';
		if (ConfigurationScope.RESOURCE === scope) {
180 181
			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 已提交
182 183 184 185 186 187 188 189 190 191 192 193
			}
			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 {
194 195 196 197 198 199 200
		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 已提交
201 202 203 204
		const event = new WorkspaceConfigurationChangeEvent(new ConfigurationChangeEvent(changedConfiguration, changedConfigurationByResource), this._extHostWorkspace.workspace);
		return Object.freeze({
			affectsConfiguration: (section: string, resource?: URI) => event.affectsConfiguration(section, resource)
		});
205
	}
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

	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();
	}
221
}