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

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

36
export class ExtHostConfiguration implements ExtHostConfigurationShape {
E
Erich Gamma 已提交
37

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

43
	constructor(proxy: MainThreadConfigurationShape, extHostWorkspace: ExtHostWorkspace, data: IConfigurationData) {
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;
	}

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

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

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

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

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

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

118
		return <WorkspaceConfiguration>Object.freeze(result);
119 120
	}
}