configuration.ts 2.9 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

B
Benjamin Pasero 已提交
6
import { IConfigurationService, IConfigurationValue, IConfigurationKeys } from 'vs/platform/configuration/common/configuration';
J
Johannes Rieken 已提交
7
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
8

B
Benjamin Pasero 已提交
9 10
export const CONFIG_DEFAULT_NAME = 'settings';
export const WORKSPACE_CONFIG_FOLDER_DEFAULT_NAME = '.vscode';
B
Benjamin Pasero 已提交
11
export const WORKSPACE_CONFIG_DEFAULT_PATH = `${WORKSPACE_CONFIG_FOLDER_DEFAULT_NAME}/${CONFIG_DEFAULT_NAME}.json`;
B
Benjamin Pasero 已提交
12

B
Benjamin Pasero 已提交
13
export const IWorkspaceConfigurationService = createDecorator<IWorkspaceConfigurationService>('configurationService');
14

B
Benjamin Pasero 已提交
15
export interface IWorkspaceConfigurationService extends IConfigurationService {
16 17 18 19 20 21

	/**
	 * Returns iff the workspace has configuration or not.
	 */
	hasWorkspaceConfiguration(): boolean;

B
Benjamin Pasero 已提交
22 23 24
	/**
	 * Override for the IConfigurationService#lookup() method that adds information about workspace settings.
	 */
B
Benjamin Pasero 已提交
25
	lookup<T>(key: string): IWorkspaceConfigurationValue<T>;
B
Benjamin Pasero 已提交
26 27 28 29 30

	/**
	 * Override for the IConfigurationService#keys() method that adds information about workspace settings.
	 */
	keys(): IWorkspaceConfigurationKeys;
B
Benjamin Pasero 已提交
31 32
}

B
Benjamin Pasero 已提交
33
export interface IWorkspaceConfigurationValue<T> extends IConfigurationValue<T> {
B
Benjamin Pasero 已提交
34
	workspace: T;
35 36
}

B
Benjamin Pasero 已提交
37 38 39 40
export interface IWorkspaceConfigurationKeys extends IConfigurationKeys {
	workspace: string[];
}

41
export const WORKSPACE_STANDALONE_CONFIGURATIONS = {
B
Benjamin Pasero 已提交
42 43 44
	'tasks': `${WORKSPACE_CONFIG_FOLDER_DEFAULT_NAME}/tasks.json`,
	'launch': `${WORKSPACE_CONFIG_FOLDER_DEFAULT_NAME}/launch.json`,
	'extensions': `${WORKSPACE_CONFIG_FOLDER_DEFAULT_NAME}/extensions.json`
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
};

export interface WorkspaceConfigurationNode {
	[part: string]: IWorkspaceConfigurationValue<any> | WorkspaceConfigurationNode;
}

export function getWorkspaceConfigurationTree(configurationService: IWorkspaceConfigurationService): WorkspaceConfigurationNode {
	const result: WorkspaceConfigurationNode = Object.create(null);
	const keyset = configurationService.keys();
	const keys = [...keyset.workspace, ...keyset.user, ...keyset.default].sort();
	let lastKey: string;
	for (const key of keys) {
		if (key !== lastKey) {
			lastKey = key;
			const config = configurationService.lookup(key);
			insert(result, key, config);
		}
	}
	return result;
}

function insert(root: WorkspaceConfigurationNode, key: string, value: any): void {
	const parts = key.split('.');
	let i = 0;
	while (i < parts.length - 1) {
		let child = root[parts[i]];
		if (child) {
			root = <any>child;
			i += 1;
		} else {
			break;
		}
	}
	while (i < parts.length - 1) {
		root = root[parts[i]] = Object.create(null);
		i += 1;
	}
	root[parts[parts.length - 1]] = value;
}