extHostConfiguration.ts 5.6 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';
10
import * as vscode from 'vscode';
11
import { ExtHostWorkspace } from 'vs/workbench/api/node/extHostWorkspace';
12
import { ExtHostConfigurationShape, MainThreadConfigurationShape, IWorkspaceConfigurationChangeEventData } from './extHost.protocol';
S
Sandeep Somavarapu 已提交
13
import { ConfigurationTarget as ExtHostConfigurationTarget } from './extHostTypes';
14 15 16 17
import { IConfigurationData, ConfigurationTarget } from 'vs/platform/configuration/common/configuration';
import { Configuration, ConfigurationModel, ConfigurationChangeEvent } from 'vs/platform/configuration/common/configurationModels';
import { WorkspaceConfigurationChangeEvent } from 'vs/workbench/services/configuration/common/configurationModels';
import { StrictResourceMap } from 'vs/base/common/map';
18 19 20 21 22 23 24 25 26 27 28 29

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

38
export class ExtHostConfiguration implements ExtHostConfigurationShape {
E
Erich Gamma 已提交
39

S
Sandeep Somavarapu 已提交
40
	private readonly _onDidChangeConfiguration = new Emitter<vscode.ConfigurationChangeEvent>();
J
Johannes Rieken 已提交
41 42
	private readonly _proxy: MainThreadConfigurationShape;
	private readonly _extHostWorkspace: ExtHostWorkspace;
43
	private _configuration: Configuration;
E
Erich Gamma 已提交
44

45
	constructor(proxy: MainThreadConfigurationShape, extHostWorkspace: ExtHostWorkspace, data: IConfigurationData) {
J
Johannes Rieken 已提交
46
		this._proxy = proxy;
J
Johannes Rieken 已提交
47
		this._extHostWorkspace = extHostWorkspace;
S
Sandeep Somavarapu 已提交
48
		this._configuration = Configuration.parse(data);
E
Erich Gamma 已提交
49 50
	}

S
Sandeep Somavarapu 已提交
51
	get onDidChangeConfiguration(): Event<vscode.ConfigurationChangeEvent> {
E
Erich Gamma 已提交
52 53 54
		return this._onDidChangeConfiguration && this._onDidChangeConfiguration.event;
	}

55
	$acceptConfigurationChanged(data: IConfigurationData, eventData: IWorkspaceConfigurationChangeEventData) {
S
Sandeep Somavarapu 已提交
56
		this._configuration = Configuration.parse(data);
S
Sandeep Somavarapu 已提交
57
		this._onDidChangeConfiguration.fire(this.toConfigurationChangeEvent(eventData));
E
Erich Gamma 已提交
58 59
	}

60
	getConfiguration(section?: string, resource?: URI): vscode.WorkspaceConfiguration {
E
Erich Gamma 已提交
61
		const config = section
S
Sandeep Somavarapu 已提交
62 63
			? lookUp(this._configuration.getSection(null, { resource }, this._extHostWorkspace.workspace), section)
			: this._configuration.getSection(null, { resource }, this._extHostWorkspace.workspace);
E
Erich Gamma 已提交
64

S
Sandeep Somavarapu 已提交
65 66
		function parseConfigurationTarget(arg: boolean | ExtHostConfigurationTarget): ConfigurationTarget {
			if (arg === void 0 || arg === null) {
S
Sandeep Somavarapu 已提交
67
				return null;
S
Sandeep Somavarapu 已提交
68 69 70 71 72 73 74 75
			}
			if (typeof arg === 'boolean') {
				return arg ? ConfigurationTarget.USER : ConfigurationTarget.WORKSPACE;
			}

			switch (arg) {
				case ExtHostConfigurationTarget.Global: return ConfigurationTarget.USER;
				case ExtHostConfigurationTarget.Workspace: return ConfigurationTarget.WORKSPACE;
76
				case ExtHostConfigurationTarget.WorkspaceFolder: return ConfigurationTarget.WORKSPACE_FOLDER;
S
Sandeep Somavarapu 已提交
77 78 79
			}
		}

80
		const result: vscode.WorkspaceConfiguration = {
81
			has(key: string): boolean {
82
				return typeof lookUp(config, key) !== 'undefined';
83
			},
84 85
			get<T>(key: string, defaultValue?: T): T {
				let result = lookUp(config, key);
86
				if (typeof result === 'undefined') {
87
					result = defaultValue;
88
				}
89
				return result;
90
			},
S
Sandeep Somavarapu 已提交
91
			update: (key: string, value: any, arg: ExtHostConfigurationTarget | boolean) => {
92
				key = section ? `${section}.${key}` : key;
S
Sandeep Somavarapu 已提交
93
				const target = parseConfigurationTarget(arg);
94
				if (value !== void 0) {
S
Sandeep Somavarapu 已提交
95
					return this._proxy.$updateConfigurationOption(target, key, value, resource);
96
				} else {
S
Sandeep Somavarapu 已提交
97
					return this._proxy.$removeConfigurationOption(target, key, resource);
98
				}
99
			},
S
Sandeep Somavarapu 已提交
100
			inspect: <T>(key: string): ConfigurationInspect<T> => {
101
				key = section ? `${section}.${key}` : key;
S
Sandeep Somavarapu 已提交
102
				const config = this._configuration.lookup<T>(key, { resource }, this._extHostWorkspace.workspace);
103
				if (config) {
S
Sandeep Somavarapu 已提交
104
					return {
105 106 107
						key,
						defaultValue: config.default,
						globalValue: config.user,
108
						workspaceValue: config.workspace,
109
						workspaceFolderValue: config.workspaceFolder
110 111
					};
				}
112
				return undefined;
E
Erich Gamma 已提交
113
			}
B
Benjamin Pasero 已提交
114
		};
115

116 117
		if (typeof config === 'object') {
			mixin(result, config, false);
118 119
		}

120
		return <vscode.WorkspaceConfiguration>Object.freeze(result);
121
	}
122

S
Sandeep Somavarapu 已提交
123
	private toConfigurationChangeEvent(data: IWorkspaceConfigurationChangeEventData): vscode.ConfigurationChangeEvent {
124 125 126 127 128 129 130
		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 已提交
131 132 133 134
		const event = new WorkspaceConfigurationChangeEvent(new ConfigurationChangeEvent(changedConfiguration, changedConfigurationByResource), this._extHostWorkspace.workspace);
		return Object.freeze({
			affectsConfiguration: (section: string, resource?: URI) => event.affectsConfiguration(section, resource)
		});
135
	}
136
}