extHostConfiguration.ts 2.8 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 {illegalState} from 'vs/base/common/errors';
E
Erich Gamma 已提交
9 10
import Event, {Emitter} from 'vs/base/common/event';
import {WorkspaceConfiguration} from 'vscode';
11 12
import {ExtHostConfigurationShape, MainContext, MainThreadConfigurationShape} from './extHost.protocol';
import {IThreadService} from 'vs/workbench/services/thread/common/threadService';
13
import {ConfigurationTarget, ConfigurationEditingResult} from 'vs/workbench/services/configuration/common/configurationEditing';
E
Erich Gamma 已提交
14

A
Alex Dima 已提交
15
export class ExtHostConfiguration extends ExtHostConfigurationShape {
E
Erich Gamma 已提交
16

17
	private _proxy: MainThreadConfigurationShape;
E
Erich Gamma 已提交
18
	private _hasConfig: boolean;
19
	private _config: any;
E
Erich Gamma 已提交
20 21
	private _onDidChangeConfiguration: Emitter<void>;

22
	constructor(threadService: IThreadService) {
A
Alex Dima 已提交
23
		super();
24
		this._proxy = threadService.get(MainContext.MainThreadConfiguration);
E
Erich Gamma 已提交
25 26 27
		this._onDidChangeConfiguration = new Emitter<void>();
	}

J
Johannes Rieken 已提交
28
	get onDidChangeConfiguration(): Event<void> {
E
Erich Gamma 已提交
29 30 31
		return this._onDidChangeConfiguration && this._onDidChangeConfiguration.event;
	}

32
	public $acceptConfigurationChanged(config: any) {
E
Erich Gamma 已提交
33 34 35 36 37 38 39
		this._config = config;
		this._hasConfig = true;
		this._onDidChangeConfiguration.fire(undefined);
	}

	public getConfiguration(section?: string): WorkspaceConfiguration {
		if (!this._hasConfig) {
40
			throw illegalState('missing config');
E
Erich Gamma 已提交
41 42 43
		}

		const config = section
44
			? ExtHostConfiguration._lookUp(section, this._config)
E
Erich Gamma 已提交
45 46
			: this._config;

47 48 49 50 51 52 53 54 55 56
		const result: WorkspaceConfiguration = {
			has(key: string): boolean {
				return typeof ExtHostConfiguration._lookUp(key, config) !== 'undefined';
			},
			get<T>(key: string, defaultValue?: T): T {
				let result = ExtHostConfiguration._lookUp(key, config);
				if (typeof result === 'undefined') {
					result = defaultValue;
				}
				return result;
57 58 59 60 61 62 63 64
			},
			update: (key: string, value: any, global: boolean) => {
				const target = global ? ConfigurationTarget.USER : ConfigurationTarget.WORKSPACE;
				return this._proxy.$updateConfigurationOption(target, key, value).then(value => {
					if (value !== ConfigurationEditingResult.OK) {
						throw new Error(ConfigurationEditingResult[value]);
					}
				});
E
Erich Gamma 已提交
65
			}
B
Benjamin Pasero 已提交
66
		};
67

68 69 70 71
		if (typeof config === 'object') {
			mixin(result, config, false);
		}

72
		return Object.freeze(result);
E
Erich Gamma 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	}

	private static _lookUp(section: string, config: any) {
		if (!section) {
			return;
		}
		let parts = section.split('.');
		let node = config;
		while (node && parts.length) {
			node = node[parts.shift()];
		}

		return node;
	}
}