-
Notifications
You must be signed in to change notification settings - Fork 29.8k
/
workbenchModeService.ts
235 lines (212 loc) · 8.79 KB
/
workbenchModeService.ts
1
2
3
4
5
6
7
8
9
10
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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*---------------------------------------------------------------------------------------------
* 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 mime from 'vs/base/common/mime';
import * as resources from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import { ModesRegistry } from 'vs/editor/common/modes/modesRegistry';
import { ILanguageExtensionPoint, IModeService } from 'vs/editor/common/services/modeService';
import { ModeServiceImpl } from 'vs/editor/common/services/modeServiceImpl';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { FILES_ASSOCIATIONS_CONFIG, IFilesConfiguration } from 'vs/platform/files/common/files';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
import { ExtensionMessageCollector, ExtensionsRegistry, IExtensionPoint, IExtensionPointUser } from 'vs/workbench/services/extensions/common/extensionsRegistry';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
export interface IRawLanguageExtensionPoint {
id: string;
extensions: string[];
filenames: string[];
filenamePatterns: string[];
firstLine: string;
aliases: string[];
mimetypes: string[];
configuration: string;
}
export const languagesExtPoint: IExtensionPoint<IRawLanguageExtensionPoint[]> = ExtensionsRegistry.registerExtensionPoint<IRawLanguageExtensionPoint[]>({
extensionPoint: 'languages',
jsonSchema: {
description: nls.localize('vscode.extension.contributes.languages', 'Contributes language declarations.'),
type: 'array',
items: {
type: 'object',
defaultSnippets: [{ body: { id: '${1:languageId}', aliases: ['${2:label}'], extensions: ['${3:extension}'], configuration: './language-configuration.json' } }],
properties: {
id: {
description: nls.localize('vscode.extension.contributes.languages.id', 'ID of the language.'),
type: 'string'
},
aliases: {
description: nls.localize('vscode.extension.contributes.languages.aliases', 'Name aliases for the language.'),
type: 'array',
items: {
type: 'string'
}
},
extensions: {
description: nls.localize('vscode.extension.contributes.languages.extensions', 'File extensions associated to the language.'),
default: ['.foo'],
type: 'array',
items: {
type: 'string'
}
},
filenames: {
description: nls.localize('vscode.extension.contributes.languages.filenames', 'File names associated to the language.'),
type: 'array',
items: {
type: 'string'
}
},
filenamePatterns: {
description: nls.localize('vscode.extension.contributes.languages.filenamePatterns', 'File name glob patterns associated to the language.'),
type: 'array',
items: {
type: 'string'
}
},
mimetypes: {
description: nls.localize('vscode.extension.contributes.languages.mimetypes', 'Mime types associated to the language.'),
type: 'array',
items: {
type: 'string'
}
},
firstLine: {
description: nls.localize('vscode.extension.contributes.languages.firstLine', 'A regular expression matching the first line of a file of the language.'),
type: 'string'
},
configuration: {
description: nls.localize('vscode.extension.contributes.languages.configuration', 'A relative path to a file containing configuration options for the language.'),
type: 'string',
default: './language-configuration.json'
}
}
}
}
});
export class WorkbenchModeServiceImpl extends ModeServiceImpl {
private _configurationService: IConfigurationService;
private _extensionService: IExtensionService;
private _onReadyPromise: Promise<boolean>;
constructor(
@IExtensionService extensionService: IExtensionService,
@IConfigurationService configurationService: IConfigurationService,
@IEnvironmentService environmentService: IEnvironmentService
) {
super(environmentService.verbose || environmentService.isExtensionDevelopment || !environmentService.isBuilt);
this._configurationService = configurationService;
this._extensionService = extensionService;
languagesExtPoint.setHandler((extensions: IExtensionPointUser<IRawLanguageExtensionPoint[]>[]) => {
let allValidLanguages: ILanguageExtensionPoint[] = [];
for (let i = 0, len = extensions.length; i < len; i++) {
let extension = extensions[i];
if (!Array.isArray(extension.value)) {
extension.collector.error(nls.localize('invalid', "Invalid `contributes.{0}`. Expected an array.", languagesExtPoint.name));
continue;
}
for (let j = 0, lenJ = extension.value.length; j < lenJ; j++) {
let ext = extension.value[j];
if (isValidLanguageExtensionPoint(ext, extension.collector)) {
let configuration: URI | undefined = undefined;
if (ext.configuration) {
configuration = resources.joinPath(extension.description.extensionLocation, ext.configuration);
}
allValidLanguages.push({
id: ext.id,
extensions: ext.extensions,
filenames: ext.filenames,
filenamePatterns: ext.filenamePatterns,
firstLine: ext.firstLine,
aliases: ext.aliases,
mimetypes: ext.mimetypes,
configuration: configuration
});
}
}
}
ModesRegistry.setDynamicLanguages(allValidLanguages);
});
this.updateMime();
this._configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration(FILES_ASSOCIATIONS_CONFIG)) {
this.updateMime();
}
});
this._extensionService.whenInstalledExtensionsRegistered().then(() => {
this.updateMime();
});
this.onDidCreateMode((mode) => {
this._extensionService.activateByEvent(`onLanguage:${mode.getId()}`);
});
}
protected _onReady(): Promise<boolean> {
if (!this._onReadyPromise) {
this._onReadyPromise = Promise.resolve(
this._extensionService.whenInstalledExtensionsRegistered().then(() => true)
);
}
return this._onReadyPromise;
}
private updateMime(): void {
const configuration = this._configurationService.getValue<IFilesConfiguration>();
// Clear user configured mime associations
mime.clearTextMimes(true /* user configured */);
// Register based on settings
if (configuration.files && configuration.files.associations) {
Object.keys(configuration.files.associations).forEach(pattern => {
const langId = configuration.files.associations[pattern];
const mimetype = this.getMimeForMode(langId) || `text/x-${langId}`;
mime.registerTextMime({ id: langId, mime: mimetype, filepattern: pattern, userConfigured: true });
});
}
this._onLanguagesMaybeChanged.fire();
}
}
function isUndefinedOrStringArray(value: string[]): boolean {
if (typeof value === 'undefined') {
return true;
}
if (!Array.isArray(value)) {
return false;
}
return value.every(item => typeof item === 'string');
}
function isValidLanguageExtensionPoint(value: IRawLanguageExtensionPoint, collector: ExtensionMessageCollector): boolean {
if (!value) {
collector.error(nls.localize('invalid.empty', "Empty value for `contributes.{0}`", languagesExtPoint.name));
return false;
}
if (typeof value.id !== 'string') {
collector.error(nls.localize('require.id', "property `{0}` is mandatory and must be of type `string`", 'id'));
return false;
}
if (!isUndefinedOrStringArray(value.extensions)) {
collector.error(nls.localize('opt.extensions', "property `{0}` can be omitted and must be of type `string[]`", 'extensions'));
return false;
}
if (!isUndefinedOrStringArray(value.filenames)) {
collector.error(nls.localize('opt.filenames', "property `{0}` can be omitted and must be of type `string[]`", 'filenames'));
return false;
}
if (typeof value.firstLine !== 'undefined' && typeof value.firstLine !== 'string') {
collector.error(nls.localize('opt.firstLine', "property `{0}` can be omitted and must be of type `string`", 'firstLine'));
return false;
}
if (typeof value.configuration !== 'undefined' && typeof value.configuration !== 'string') {
collector.error(nls.localize('opt.configuration', "property `{0}` can be omitted and must be of type `string`", 'configuration'));
return false;
}
if (!isUndefinedOrStringArray(value.aliases)) {
collector.error(nls.localize('opt.aliases', "property `{0}` can be omitted and must be of type `string[]`", 'aliases'));
return false;
}
if (!isUndefinedOrStringArray(value.mimetypes)) {
collector.error(nls.localize('opt.mimetypes', "property `{0}` can be omitted and must be of type `string[]`", 'mimetypes'));
return false;
}
return true;
}
registerSingleton(IModeService, WorkbenchModeServiceImpl);