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

import * as nls from 'vs/nls';
import * as objects from 'vs/base/common/objects';
import { Registry } from 'vs/platform/registry/common/platform';
import { IJSONSchema } from 'vs/base/common/jsonSchema';
S
Sandeep Somavarapu 已提交
10
import { ExtensionsRegistry, IExtensionPointUser } from 'vs/platform/extensions/common/extensionsRegistry';
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
import { IConfigurationNode, IConfigurationRegistry, Extensions, editorConfigurationSchemaId, IDefaultConfigurationExtension, validateProperty, ConfigurationScope } from 'vs/platform/configuration/common/configurationRegistry';
import { IJSONContributionRegistry, Extensions as JSONExtensions } from 'vs/platform/jsonschemas/common/jsonContributionRegistry';
import { workspaceSettingsSchemaId } from 'vs/workbench/services/configuration/common/configuration';

const configurationRegistry = Registry.as<IConfigurationRegistry>(Extensions.Configuration);

const configurationEntrySchema: IJSONSchema = {
	type: 'object',
	defaultSnippets: [{ body: { title: '', properties: {} } }],
	properties: {
		title: {
			description: nls.localize('vscode.extension.contributes.configuration.title', 'A summary of the settings. This label will be used in the settings file as separating comment.'),
			type: 'string'
		},
		properties: {
			description: nls.localize('vscode.extension.contributes.configuration.properties', 'Description of the configuration properties.'),
			type: 'object',
			additionalProperties: {
				anyOf: [
					{ $ref: 'http://json-schema.org/draft-04/schema#' },
					{
						type: 'object',
						properties: {
							isExecutable: {
								type: 'boolean'
							},
							scope: {
								type: 'string',
								enum: ['window', 'resource'],
								default: 'window',
								enumDescriptions: [
									nls.localize('scope.window.description', "Window specific configuration, which can be configured in the User or Workspace settings."),
									nls.localize('scope.resource.description', "Resource specific configuration, which can be configured in the User, Workspace or Folder settings.")
								],
								description: nls.localize('scope.description', "Scope in which the configuration is applicable. Available scopes are `window` and `resource`.")
							}
						}
					}
				]
			}
		}
	}
};

S
Sandeep Somavarapu 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
let registeredDefaultConfigurations: IDefaultConfigurationExtension[] = [];

// BEGIN VSCode extension point `configurationDefaults`
const defaultConfigurationExtPoint = ExtensionsRegistry.registerExtensionPoint<IConfigurationNode>('configurationDefaults', [], {
	description: nls.localize('vscode.extension.contributes.defaultConfiguration', 'Contributes default editor configuration settings by language.'),
	type: 'object',
	defaultSnippets: [{ body: {} }],
	patternProperties: {
		'\\[.*\\]$': {
			type: 'object',
			default: {},
			$ref: editorConfigurationSchemaId,
		}
	}
});
defaultConfigurationExtPoint.setHandler(extensions => {
	registeredDefaultConfigurations = extensions.map(extension => {
		const id = extension.description.id;
		const name = extension.description.name;
J
Johannes Rieken 已提交
74
		const defaults = objects.deepClone(extension.value);
S
Sandeep Somavarapu 已提交
75 76 77 78 79 80 81
		return <IDefaultConfigurationExtension>{
			id, name, defaults
		};
	});
});
// END VSCode extension point `configurationDefaults`

82 83

// BEGIN VSCode extension point `configuration`
S
Sandeep Somavarapu 已提交
84
const configurationExtPoint = ExtensionsRegistry.registerExtensionPoint<IConfigurationNode>('configuration', [defaultConfigurationExtPoint], {
85 86 87 88 89 90 91 92 93 94 95 96
	description: nls.localize('vscode.extension.contributes.configuration', 'Contributes configuration settings.'),
	oneOf: [
		configurationEntrySchema,
		{
			type: 'array',
			items: configurationEntrySchema
		}
	]
});
configurationExtPoint.setHandler(extensions => {
	const configurations: IConfigurationNode[] = [];

S
Sandeep Somavarapu 已提交
97
	function handleConfiguration(node: IConfigurationNode, id: string, extension: IExtensionPointUser<any>) {
J
Johannes Rieken 已提交
98
		let configuration = objects.deepClone(node);
99 100

		if (configuration.title && (typeof configuration.title !== 'string')) {
S
Sandeep Somavarapu 已提交
101
			extension.collector.error(nls.localize('invalid.title', "'configuration.title' must be a string"));
102 103
		}

S
Sandeep Somavarapu 已提交
104
		validateProperties(configuration, extension);
105 106 107

		configuration.id = id;
		configurations.push(configuration);
108
	}
109 110 111 112 113

	for (let extension of extensions) {
		const value = <IConfigurationNode | IConfigurationNode[]>extension.value;
		const id = extension.description.id;
		if (!Array.isArray(value)) {
S
Sandeep Somavarapu 已提交
114
			handleConfiguration(value, id, extension);
115
		} else {
S
Sandeep Somavarapu 已提交
116
			value.forEach(v => handleConfiguration(v, id, extension));
117 118
		}
	}
S
Sandeep Somavarapu 已提交
119
	configurationRegistry.registerConfigurations(configurations, registeredDefaultConfigurations, false);
120 121 122
});
// END VSCode extension point `configuration`

S
Sandeep Somavarapu 已提交
123
function validateProperties(configuration: IConfigurationNode, extension: IExtensionPointUser<any>): void {
124 125 126
	let properties = configuration.properties;
	if (properties) {
		if (typeof properties !== 'object') {
S
Sandeep Somavarapu 已提交
127
			extension.collector.error(nls.localize('invalid.properties', "'configuration.properties' must be an object"));
128 129 130 131 132 133
			configuration.properties = {};
		}
		for (let key in properties) {
			const message = validateProperty(key);
			const propertyConfiguration = configuration.properties[key];
			propertyConfiguration.scope = propertyConfiguration.scope && propertyConfiguration.scope.toString() === 'resource' ? ConfigurationScope.RESOURCE : ConfigurationScope.WINDOW;
134
			propertyConfiguration.notMultiRootAdopted = !(extension.description.isBuiltin || (Array.isArray(extension.description.keywords) && extension.description.keywords.indexOf('multi-root ready') !== -1));
135
			if (message) {
S
Sandeep Somavarapu 已提交
136
				extension.collector.warn(message);
137 138 139 140 141 142
				delete properties[key];
			}
		}
	}
	let subNodes = configuration.allOf;
	if (subNodes) {
S
Sandeep Somavarapu 已提交
143
		extension.collector.error(nls.localize('invalid.allOf', "'configuration.allOf' is deprecated and should no longer be used. Instead, pass multiple configuration sections as an array to the 'configuration' contribution point."));
144
		for (let node of subNodes) {
S
Sandeep Somavarapu 已提交
145
			validateProperties(node, extension);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
		}
	}
}

const jsonRegistry = Registry.as<IJSONContributionRegistry>(JSONExtensions.JSONContribution);
jsonRegistry.registerSchema('vscode://schemas/workspaceConfig', {
	default: {
		folders: [
			{
				path: ''
			}
		],
		settings: {
		}
	},
	required: ['folders'],
	properties: {
		'folders': {
			minItems: 0,
			uniqueItems: true,
			description: nls.localize('workspaceConfig.folders.description', "List of folders to be loaded in the workspace."),
			items: {
				type: 'object',
				default: { path: '' },
				oneOf: [{
					properties: {
						path: {
							type: 'string',
							description: nls.localize('workspaceConfig.path.description', "A file path. e.g. `/root/folderA` or `./folderA` for a relative path that will be resolved against the location of the workspace file.")
						},
						name: {
							type: 'string',
							description: nls.localize('workspaceConfig.name.description', "An optional name for the folder. ")
						}
					},
					required: ['path']
				}, {
					properties: {
						uri: {
							type: 'string',
							description: nls.localize('workspaceConfig.uri.description', "URI of the folder")
						},
						name: {
							type: 'string',
							description: nls.localize('workspaceConfig.name.description', "An optional name for the folder. ")
						}
					},
					required: ['uri']
				}]
			}
		},
		'settings': {
			type: 'object',
			default: {},
			description: nls.localize('workspaceConfig.settings.description', "Workspace settings"),
			$ref: workspaceSettingsSchemaId
		},
		'extensions': {
			type: 'object',
			default: {},
			description: nls.localize('workspaceConfig.extensions.description', "Workspace extensions"),
			$ref: 'vscode://schemas/extensions'
		}
	},
	additionalProperties: false,
	errorMessage: nls.localize('unknownWorkspaceProperty', "Unknown workspace configuration property")
J
Johannes Rieken 已提交
212
});