-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
plugin-contribution-handler.ts
533 lines (471 loc) · 23.3 KB
/
plugin-contribution-handler.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// *****************************************************************************
// Copyright (C) 2018 Red Hat, Inc. and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************
import { injectable, inject, named } from '@theia/core/shared/inversify';
import { ITokenTypeMap, IEmbeddedLanguagesMap, StandardTokenType } from 'vscode-textmate';
import { TextmateRegistry, getEncodedLanguageId, MonacoTextmateService, GrammarDefinition } from '@theia/monaco/lib/browser/textmate';
import { MenusContributionPointHandler } from './menus/menus-contribution-handler';
import { PluginViewRegistry } from './view/plugin-view-registry';
import { PluginCustomEditorRegistry } from './custom-editors/plugin-custom-editor-registry';
import { PluginContribution, IndentationRules, FoldingRules, ScopeMap, DeployedPlugin, GrammarsContribution } from '../../common';
import {
DefaultUriLabelProviderContribution,
LabelProviderContribution,
PreferenceSchemaProvider
} from '@theia/core/lib/browser';
import { PreferenceLanguageOverrideService, PreferenceSchema, PreferenceSchemaProperties } from '@theia/core/lib/browser/preferences';
import { KeybindingsContributionPointHandler } from './keybindings/keybindings-contribution-handler';
import { MonacoSnippetSuggestProvider } from '@theia/monaco/lib/browser/monaco-snippet-suggest-provider';
import { PluginSharedStyle } from './plugin-shared-style';
import { CommandRegistry, Command, CommandHandler } from '@theia/core/lib/common/command';
import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable';
import { Emitter } from '@theia/core/lib/common/event';
import { TaskDefinitionRegistry, ProblemMatcherRegistry, ProblemPatternRegistry } from '@theia/task/lib/browser';
import { PluginDebugService } from './debug/plugin-debug-service';
import { DebugSchemaUpdater } from '@theia/debug/lib/browser/debug-schema-updater';
import { MonacoThemingService } from '@theia/monaco/lib/browser/monaco-theming-service';
import { ColorRegistry } from '@theia/core/lib/browser/color-registry';
import { PluginIconThemeService } from './plugin-icon-theme-service';
import { ContributionProvider } from '@theia/core/lib/common';
import * as monaco from '@theia/monaco-editor-core';
import { ThemeIcon } from '@theia/monaco-editor-core/esm/vs/platform/theme/common/themeService';
@injectable()
export class PluginContributionHandler {
private injections = new Map<string, string[]>();
@inject(TextmateRegistry)
private readonly grammarsRegistry: TextmateRegistry;
@inject(PluginViewRegistry)
private readonly viewRegistry: PluginViewRegistry;
@inject(PluginCustomEditorRegistry)
private readonly customEditorRegistry: PluginCustomEditorRegistry;
@inject(MenusContributionPointHandler)
private readonly menusContributionHandler: MenusContributionPointHandler;
@inject(PreferenceSchemaProvider)
private readonly preferenceSchemaProvider: PreferenceSchemaProvider;
@inject(PreferenceLanguageOverrideService)
private readonly preferenceOverrideService: PreferenceLanguageOverrideService;
@inject(MonacoTextmateService)
private readonly monacoTextmateService: MonacoTextmateService;
@inject(KeybindingsContributionPointHandler)
private readonly keybindingsContributionHandler: KeybindingsContributionPointHandler;
@inject(MonacoSnippetSuggestProvider)
protected readonly snippetSuggestProvider: MonacoSnippetSuggestProvider;
@inject(CommandRegistry)
protected readonly commands: CommandRegistry;
@inject(PluginSharedStyle)
protected readonly style: PluginSharedStyle;
@inject(TaskDefinitionRegistry)
protected readonly taskDefinitionRegistry: TaskDefinitionRegistry;
@inject(ProblemMatcherRegistry)
protected readonly problemMatcherRegistry: ProblemMatcherRegistry;
@inject(ProblemPatternRegistry)
protected readonly problemPatternRegistry: ProblemPatternRegistry;
@inject(PluginDebugService)
protected readonly debugService: PluginDebugService;
@inject(DebugSchemaUpdater)
protected readonly debugSchema: DebugSchemaUpdater;
@inject(MonacoThemingService)
protected readonly monacoThemingService: MonacoThemingService;
@inject(ColorRegistry)
protected readonly colors: ColorRegistry;
@inject(PluginIconThemeService)
protected readonly iconThemeService: PluginIconThemeService;
@inject(ContributionProvider) @named(LabelProviderContribution)
protected readonly contributionProvider: ContributionProvider<LabelProviderContribution>;
protected readonly commandHandlers = new Map<string, CommandHandler['execute'] | undefined>();
protected readonly onDidRegisterCommandHandlerEmitter = new Emitter<string>();
readonly onDidRegisterCommandHandler = this.onDidRegisterCommandHandlerEmitter.event;
/**
* Always synchronous in order to simplify handling disconnections.
* @throws never, loading of each contribution should handle errors
* in order to avoid preventing loading of other contributions or extensions
*/
handleContributions(clientId: string, plugin: DeployedPlugin): Disposable {
const contributions = plugin.contributes;
if (!contributions) {
return Disposable.NULL;
}
const toDispose = new DisposableCollection(Disposable.create(() => { /* mark as not disposed */ }));
/* eslint-disable @typescript-eslint/no-explicit-any */
const logError = (message: string, ...args: any[]) => console.error(`[${clientId}][${plugin.metadata.model.id}]: ${message}`, ...args);
const logWarning = (message: string, ...args: any[]) => console.warn(`[${clientId}][${plugin.metadata.model.id}]: ${message}`, ...args);
const pushContribution = (id: string, contribute: () => Disposable) => {
if (toDispose.disposed) {
return;
}
try {
toDispose.push(contribute());
} catch (e) {
logError(`Failed to load '${id}' contribution.`, e);
}
};
const configuration = contributions.configuration;
if (configuration) {
for (const config of configuration) {
pushContribution('configuration', () => this.preferenceSchemaProvider.setSchema(config));
}
}
const configurationDefaults = contributions.configurationDefaults;
if (configurationDefaults) {
pushContribution('configurationDefaults', () => this.updateDefaultOverridesSchema(configurationDefaults));
}
const languages = contributions.languages;
if (languages && languages.length) {
for (const lang of languages) {
// it is not possible to unregister a language
monaco.languages.register({
id: lang.id,
aliases: lang.aliases,
extensions: lang.extensions,
filenamePatterns: lang.filenamePatterns,
filenames: lang.filenames,
firstLine: lang.firstLine,
mimetypes: lang.mimetypes
});
const langConfiguration = lang.configuration;
if (langConfiguration) {
pushContribution(`language.${lang.id}.configuration`, () => monaco.languages.setLanguageConfiguration(lang.id, {
wordPattern: this.createRegex(langConfiguration.wordPattern),
autoClosingPairs: langConfiguration.autoClosingPairs,
brackets: langConfiguration.brackets,
comments: langConfiguration.comments,
folding: this.convertFolding(langConfiguration.folding),
surroundingPairs: langConfiguration.surroundingPairs,
indentationRules: this.convertIndentationRules(langConfiguration.indentationRules)
}));
}
}
}
const grammars = contributions.grammars;
if (grammars && grammars.length) {
const grammarsWithLanguage: GrammarsContribution[] = [];
for (const grammar of grammars) {
if (grammar.injectTo) {
for (const injectScope of grammar.injectTo) {
pushContribution(`grammar.injectTo.${injectScope}`, () => {
const injections = this.injections.get(injectScope) || [];
injections.push(grammar.scope);
this.injections.set(injectScope, injections);
return Disposable.create(() => {
const index = injections.indexOf(grammar.scope);
if (index !== -1) {
injections.splice(index, 1);
}
});
});
}
}
if (grammar.language) {
// processing is deferred.
grammarsWithLanguage.push(grammar);
}
pushContribution(`grammar.textmate.scope.${grammar.scope}`, () => this.grammarsRegistry.registerTextmateGrammarScope(grammar.scope, {
async getGrammarDefinition(): Promise<GrammarDefinition> {
return {
format: grammar.format,
content: grammar.grammar || '',
location: grammar.grammarLocation
};
},
getInjections: (scopeName: string) =>
this.injections.get(scopeName)!
}));
}
// load grammars on next tick to await registration of languages from all plugins in current tick
// see https://github.com/eclipse-theia/theia/issues/6907#issuecomment-578600243
setTimeout(() => {
for (const grammar of grammarsWithLanguage) {
const language = grammar.language!;
pushContribution(`grammar.language.${language}.scope`, () => this.grammarsRegistry.mapLanguageIdToTextmateGrammar(language, grammar.scope));
pushContribution(`grammar.language.${language}.configuration`, () => this.grammarsRegistry.registerGrammarConfiguration(language, {
embeddedLanguages: this.convertEmbeddedLanguages(grammar.embeddedLanguages, logWarning),
tokenTypes: this.convertTokenTypes(grammar.tokenTypes)
}));
}
// activate grammars only once everything else is loaded.
// see https://github.com/eclipse-theia/theia-cpp-extensions/issues/100#issuecomment-610643866
setTimeout(() => {
for (const grammar of grammarsWithLanguage) {
const language = grammar.language!;
pushContribution(`grammar.language.${language}.activation`,
() => this.monacoTextmateService.activateLanguage(language)
);
}
});
});
}
pushContribution('commands', () => this.registerCommands(contributions));
pushContribution('menus', () => this.menusContributionHandler.handle(plugin));
pushContribution('keybindings', () => this.keybindingsContributionHandler.handle(contributions));
if (contributions.customEditors) {
for (const customEditor of contributions.customEditors) {
pushContribution(`customEditors.${customEditor.viewType}`,
() => this.customEditorRegistry.registerCustomEditor(customEditor)
);
}
}
if (contributions.viewsContainers) {
for (const location in contributions.viewsContainers) {
if (contributions.viewsContainers!.hasOwnProperty(location)) {
for (const viewContainer of contributions.viewsContainers[location]) {
pushContribution(`viewContainers.${viewContainer.id}`,
() => this.viewRegistry.registerViewContainer(location, viewContainer)
);
}
}
}
}
if (contributions.views) {
// eslint-disable-next-line guard-for-in
for (const location in contributions.views) {
for (const view of contributions.views[location]) {
pushContribution(`views.${view.id}`,
() => this.viewRegistry.registerView(location, view)
);
}
}
}
if (contributions.viewsWelcome) {
for (const [index, viewWelcome] of contributions.viewsWelcome.entries()) {
pushContribution(`viewsWelcome.${viewWelcome.view}.${index}`,
() => this.viewRegistry.registerViewWelcome(viewWelcome)
);
}
}
if (contributions.snippets) {
for (const snippet of contributions.snippets) {
pushContribution(`snippets.${snippet.uri}`, () => this.snippetSuggestProvider.fromURI(snippet.uri, {
language: snippet.language,
source: snippet.source
}));
}
}
if (contributions.themes && contributions.themes.length) {
const pending = {};
for (const theme of contributions.themes) {
pushContribution(`themes.${theme.uri}`, () => this.monacoThemingService.register(theme, pending));
}
}
if (contributions.iconThemes && contributions.iconThemes.length) {
for (const iconTheme of contributions.iconThemes) {
pushContribution(`iconThemes.${iconTheme.uri}`, () => this.iconThemeService.register(iconTheme, plugin));
}
}
const colors = contributions.colors;
if (colors) {
pushContribution('colors', () => this.colors.register(...colors));
}
if (contributions.taskDefinitions) {
for (const taskDefinition of contributions.taskDefinitions) {
pushContribution(`taskDefinitions.${taskDefinition.taskType}`,
() => this.taskDefinitionRegistry.register(taskDefinition)
);
}
}
if (contributions.problemPatterns) {
for (const problemPattern of contributions.problemPatterns) {
pushContribution(`problemPatterns.${problemPattern.name || problemPattern.regexp}`,
() => this.problemPatternRegistry.register(problemPattern)
);
}
}
if (contributions.problemMatchers) {
for (const problemMatcher of contributions.problemMatchers) {
pushContribution(`problemMatchers.${problemMatcher.label}`,
() => this.problemMatcherRegistry.register(problemMatcher)
);
}
}
if (contributions.debuggers && contributions.debuggers.length) {
toDispose.push(Disposable.create(() => this.debugSchema.update()));
for (const contribution of contributions.debuggers) {
pushContribution(`debuggers.${contribution.type}`,
() => this.debugService.registerDebugger(contribution)
);
}
this.debugSchema.update();
}
if (contributions.resourceLabelFormatters) {
for (const formatter of contributions.resourceLabelFormatters) {
for (const contribution of this.contributionProvider.getContributions()) {
if (contribution instanceof DefaultUriLabelProviderContribution) {
pushContribution(`resourceLabelFormatters.${formatter.scheme}`,
() => contribution.registerFormatter(formatter)
);
}
}
}
}
return toDispose;
}
protected registerCommands(contribution: PluginContribution): Disposable {
if (!contribution.commands) {
return Disposable.NULL;
}
const toDispose = new DisposableCollection();
for (const { iconUrl, themeIcon, command, category, title, originalTitle } of contribution.commands) {
const reference = iconUrl && this.style.toIconClass(iconUrl);
const icon = themeIcon && ThemeIcon.fromString(themeIcon);
let iconClass;
if (reference) {
toDispose.push(reference);
iconClass = reference.object.iconClass;
} else if (icon) {
iconClass = ThemeIcon.asClassName(icon);
}
toDispose.push(this.registerCommand({ id: command, category, label: title, originalLabel: originalTitle, iconClass }));
}
return toDispose;
}
registerCommand(command: Command): Disposable {
if (this.hasCommand(command.id)) {
console.warn(`command '${command.id}' already registered`);
return Disposable.NULL;
}
const commandHandler: CommandHandler = {
execute: async (...args) => {
const handler = this.commandHandlers.get(command.id);
if (!handler) {
throw new Error(`command '${command.id}' not found`);
}
return handler(...args);
},
// Always enabled - a command can be executed programmatically or via the commands palette.
isEnabled(): boolean { return true; },
// Visibility rules are defined via the `menus` contribution point.
isVisible(): boolean { return true; }
};
const toDispose = new DisposableCollection();
if (this.commands.getCommand(command.id)) {
// overriding built-in command, i.e. `type` by the VSCodeVim extension
toDispose.push(this.commands.registerHandler(command.id, commandHandler));
} else {
toDispose.push(this.commands.registerCommand(command, commandHandler));
}
this.commandHandlers.set(command.id, undefined);
toDispose.push(Disposable.create(() => this.commandHandlers.delete(command.id)));
return toDispose;
}
registerCommandHandler(id: string, execute: CommandHandler['execute']): Disposable {
if (this.hasCommandHandler(id)) {
console.warn(`command handler '${id}' already registered`);
return Disposable.NULL;
}
this.commandHandlers.set(id, execute);
this.onDidRegisterCommandHandlerEmitter.fire(id);
return Disposable.create(() => this.commandHandlers.set(id, undefined));
}
hasCommand(id: string): boolean {
return this.commandHandlers.has(id);
}
hasCommandHandler(id: string): boolean {
return !!this.commandHandlers.get(id);
}
protected updateDefaultOverridesSchema(configurationDefaults: PreferenceSchemaProperties): Disposable {
const defaultOverrides: PreferenceSchema = {
id: 'defaultOverrides',
title: 'Default Configuration Overrides',
properties: {}
};
// eslint-disable-next-line guard-for-in
for (const key in configurationDefaults) {
const defaultValue = configurationDefaults[key];
if (this.preferenceOverrideService.testOverrideValue(key, defaultValue)) {
defaultOverrides.properties[key] = {
type: 'object',
default: defaultValue,
description: `Configure editor settings to be overridden for ${key} language.`
};
}
}
if (Object.keys(defaultOverrides.properties).length) {
return this.preferenceSchemaProvider.setSchema(defaultOverrides);
}
return Disposable.NULL;
}
private createRegex(value: string | undefined): RegExp | undefined {
if (typeof value === 'string') {
return new RegExp(value, '');
}
return undefined;
}
private convertIndentationRules(rules?: IndentationRules): monaco.languages.IndentationRule | undefined {
if (!rules) {
return undefined;
}
return {
decreaseIndentPattern: this.createRegex(rules.decreaseIndentPattern)!,
increaseIndentPattern: this.createRegex(rules.increaseIndentPattern)!,
indentNextLinePattern: this.createRegex(rules.indentNextLinePattern),
unIndentedLinePattern: this.createRegex(rules.unIndentedLinePattern)
};
}
private convertFolding(folding?: FoldingRules): monaco.languages.FoldingRules | undefined {
if (!folding) {
return undefined;
}
const result: monaco.languages.FoldingRules = {
offSide: folding.offSide
};
if (folding.markers) {
result.markers = {
end: this.createRegex(folding.markers.end)!,
start: this.createRegex(folding.markers.start)!
};
}
return result;
}
private convertTokenTypes(tokenTypes?: ScopeMap): ITokenTypeMap | undefined {
if (typeof tokenTypes === 'undefined' || tokenTypes === null) {
return undefined;
}
const result = Object.create(null);
const scopes = Object.keys(tokenTypes);
const len = scopes.length;
for (let i = 0; i < len; i++) {
const scope = scopes[i];
const tokenType = tokenTypes[scope];
switch (tokenType) {
case 'string':
result[scope] = StandardTokenType.String;
break;
case 'other':
result[scope] = StandardTokenType.Other;
break;
case 'comment':
result[scope] = StandardTokenType.Comment;
break;
}
}
return result;
}
private convertEmbeddedLanguages(languages: ScopeMap | undefined, logWarning: (warning: string) => void): IEmbeddedLanguagesMap | undefined {
if (typeof languages === 'undefined' || languages === null) {
return undefined;
}
const result = Object.create(null);
const scopes = Object.keys(languages);
const len = scopes.length;
for (let i = 0; i < len; i++) {
const scope = scopes[i];
const langId = languages[scope];
result[scope] = getEncodedLanguageId(langId);
if (!result[scope]) {
logWarning(`Language for '${scope}' not found.`);
}
}
return result;
}
}