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
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Emitter, Event } from 'vs/base/common/event';
import { Disposable, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import * as strings from 'vs/base/common/strings';
import { ITextModel } from 'vs/editor/common/model';
import { DEFAULT_WORD_REGEXP, ensureValidWordDefinition } from 'vs/editor/common/core/wordHelper';
import { EnterAction, FoldingRules, IAutoClosingPair, IndentationRule, LanguageConfiguration, AutoClosingPairs, CharacterPair, ExplicitLanguageConfiguration } from 'vs/editor/common/languages/languageConfiguration';
import { createScopedLineTokens, ScopedLineTokens } from 'vs/editor/common/languages/supports';
import { CharacterPairSupport } from 'vs/editor/common/languages/supports/characterPair';
import { BracketElectricCharacterSupport } from 'vs/editor/common/languages/supports/electricCharacter';
import { IndentRulesSupport } from 'vs/editor/common/languages/supports/indentRules';
import { OnEnterSupport } from 'vs/editor/common/languages/supports/onEnter';
import { RichEditBrackets } from 'vs/editor/common/languages/supports/richEditBrackets';
import { EditorAutoIndentStrategy } from 'vs/editor/common/config/editorOptions';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ILanguageService } from 'vs/editor/common/languages/language';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { PLAINTEXT_LANGUAGE_ID } from 'vs/editor/common/languages/modesRegistry';
import { LanguageBracketsConfiguration } from 'vs/editor/common/languages/supports/languageBracketsConfiguration';
/**
* Interface used to support insertion of mode specific comments.
*/
export interface ICommentsConfiguration {
lineCommentToken?: string;
blockCommentStartToken?: string;
blockCommentEndToken?: string;
}
export interface ILanguageConfigurationService {
readonly _serviceBrand: undefined;
onDidChange: Event<LanguageConfigurationServiceChangeEvent>;
/**
* @param priority Use a higher number for higher priority
*/
register(languageId: string, configuration: LanguageConfiguration, priority?: number): IDisposable;
getLanguageConfiguration(languageId: string): ResolvedLanguageConfiguration;
}
export class LanguageConfigurationServiceChangeEvent {
constructor(public readonly languageId: string | undefined) { }
public affects(languageId: string): boolean {
return !this.languageId ? true : this.languageId === languageId;
}
}
export const ILanguageConfigurationService = createDecorator<ILanguageConfigurationService>('languageConfigurationService');
export class LanguageConfigurationService extends Disposable implements ILanguageConfigurationService {
_serviceBrand: undefined;
private readonly _registry = this._register(new LanguageConfigurationRegistry());
private readonly onDidChangeEmitter = this._register(new Emitter<LanguageConfigurationServiceChangeEvent>());
public readonly onDidChange = this.onDidChangeEmitter.event;
private readonly configurations = new Map<string, ResolvedLanguageConfiguration>();
constructor(
@IConfigurationService private readonly configurationService: IConfigurationService,
@ILanguageService private readonly languageService: ILanguageService
) {
super();
const languageConfigKeys = new Set(Object.values(customizedLanguageConfigKeys));
this._register(this.configurationService.onDidChangeConfiguration((e) => {
const globalConfigChanged = e.change.keys.some((k) =>
languageConfigKeys.has(k)
);
const localConfigChanged = e.change.overrides
.filter(([overrideLangName, keys]) =>
keys.some((k) => languageConfigKeys.has(k))
)
.map(([overrideLangName]) => overrideLangName);
if (globalConfigChanged) {
this.configurations.clear();
this.onDidChangeEmitter.fire(new LanguageConfigurationServiceChangeEvent(undefined));
} else {
for (const languageId of localConfigChanged) {
if (this.languageService.isRegisteredLanguageId(languageId)) {
this.configurations.delete(languageId);
this.onDidChangeEmitter.fire(new LanguageConfigurationServiceChangeEvent(languageId));
}
}
}
}));
this._register(this._registry.onDidChange((e) => {
this.configurations.delete(e.languageId);
this.onDidChangeEmitter.fire(new LanguageConfigurationServiceChangeEvent(e.languageId));
}));
}
public register(languageId: string, configuration: LanguageConfiguration, priority?: number): IDisposable {
return this._registry.register(languageId, configuration, priority);
}
public getLanguageConfiguration(languageId: string): ResolvedLanguageConfiguration {
let result = this.configurations.get(languageId);
if (!result) {
result = computeConfig(languageId, this._registry, this.configurationService, this.languageService);
this.configurations.set(languageId, result);
}
return result;
}
}
function computeConfig(
languageId: string,
registry: LanguageConfigurationRegistry,
configurationService: IConfigurationService,
languageService: ILanguageService,
): ResolvedLanguageConfiguration {
let languageConfig = registry.getLanguageConfiguration(languageId);
if (!languageConfig) {
if (!languageService.isRegisteredLanguageId(languageId)) {
throw new Error(`Language id "${languageId}" is not configured nor known`);
}
languageConfig = new ResolvedLanguageConfiguration(languageId, {});
}
const customizedConfig = getCustomizedLanguageConfig(languageConfig.languageId, configurationService);
const data = combineLanguageConfigurations([languageConfig.underlyingConfig, customizedConfig]);
const config = new ResolvedLanguageConfiguration(languageConfig.languageId, data);
return config;
}
const customizedLanguageConfigKeys = {
brackets: 'editor.language.brackets',
colorizedBracketPairs: 'editor.language.colorizedBracketPairs'
};
function getCustomizedLanguageConfig(languageId: string, configurationService: IConfigurationService): LanguageConfiguration {
const brackets = configurationService.getValue(customizedLanguageConfigKeys.brackets, {
overrideIdentifier: languageId,
});
const colorizedBracketPairs = configurationService.getValue(customizedLanguageConfigKeys.colorizedBracketPairs, {
overrideIdentifier: languageId,
});
return {
brackets: validateBracketPairs(brackets),
colorizedBracketPairs: validateBracketPairs(colorizedBracketPairs),
};
}
function validateBracketPairs(data: unknown): CharacterPair[] | undefined {
if (!Array.isArray(data)) {
return undefined;
}
return data.map(pair => {
if (!Array.isArray(pair) || pair.length !== 2) {
return undefined;
}
return [pair[0], pair[1]] as CharacterPair;
}).filter((p): p is CharacterPair => !!p);
}
export function getIndentationAtPosition(model: ITextModel, lineNumber: number, column: number): string {
const lineText = model.getLineContent(lineNumber);
let indentation = strings.getLeadingWhitespace(lineText);
if (indentation.length > column - 1) {
indentation = indentation.substring(0, column - 1);
}
return indentation;
}
export function getScopedLineTokens(model: ITextModel, lineNumber: number, columnNumber?: number): ScopedLineTokens {
model.tokenization.forceTokenization(lineNumber);
const lineTokens = model.tokenization.getLineTokens(lineNumber);
const column = (typeof columnNumber === 'undefined' ? model.getLineMaxColumn(lineNumber) - 1 : columnNumber - 1);
return createScopedLineTokens(lineTokens, column);
}
class ComposedLanguageConfiguration {
private readonly _entries: LanguageConfigurationContribution[];
private _order: number;
private _resolved: ResolvedLanguageConfiguration | null = null;
constructor(public readonly languageId: string) {
this._entries = [];
this._order = 0;
this._resolved = null;
}
public register(
configuration: LanguageConfiguration,
priority: number
): IDisposable {
const entry = new LanguageConfigurationContribution(
configuration,
priority,
++this._order
);
this._entries.push(entry);
this._resolved = null;
return toDisposable(() => {
for (let i = 0; i < this._entries.length; i++) {
if (this._entries[i] === entry) {
this._entries.splice(i, 1);
this._resolved = null;
break;
}
}
});
}
public getResolvedConfiguration(): ResolvedLanguageConfiguration | null {
if (!this._resolved) {
const config = this._resolve();
if (config) {
this._resolved = new ResolvedLanguageConfiguration(
this.languageId,
config
);
}
}
return this._resolved;
}
private _resolve(): LanguageConfiguration | null {
if (this._entries.length === 0) {
return null;
}
this._entries.sort(LanguageConfigurationContribution.cmp);
return combineLanguageConfigurations(this._entries.map(e => e.configuration));
}
}
function combineLanguageConfigurations(configs: LanguageConfiguration[]): LanguageConfiguration {
let result: ExplicitLanguageConfiguration = {
comments: undefined,
brackets: undefined,
wordPattern: undefined,
indentationRules: undefined,
onEnterRules: undefined,
autoClosingPairs: undefined,
surroundingPairs: undefined,
autoCloseBefore: undefined,
folding: undefined,
colorizedBracketPairs: undefined,
__electricCharacterSupport: undefined,
};
for (const entry of configs) {
result = {
comments: entry.comments || result.comments,
brackets: entry.brackets || result.brackets,
wordPattern: entry.wordPattern || result.wordPattern,
indentationRules: entry.indentationRules || result.indentationRules,
onEnterRules: entry.onEnterRules || result.onEnterRules,
autoClosingPairs: entry.autoClosingPairs || result.autoClosingPairs,
surroundingPairs: entry.surroundingPairs || result.surroundingPairs,
autoCloseBefore: entry.autoCloseBefore || result.autoCloseBefore,
folding: entry.folding || result.folding,
colorizedBracketPairs: entry.colorizedBracketPairs || result.colorizedBracketPairs,
__electricCharacterSupport: entry.__electricCharacterSupport || result.__electricCharacterSupport,
};
}
return result;
}
class LanguageConfigurationContribution {
constructor(
public readonly configuration: LanguageConfiguration,
public readonly priority: number,
public readonly order: number
) { }
public static cmp(a: LanguageConfigurationContribution, b: LanguageConfigurationContribution) {
if (a.priority === b.priority) {
// higher order last
return a.order - b.order;
}
// higher priority last
return a.priority - b.priority;
}
}
export class LanguageConfigurationChangeEvent {
constructor(public readonly languageId: string) { }
}
export class LanguageConfigurationRegistry extends Disposable {
private readonly _entries = new Map<string, ComposedLanguageConfiguration>();
private readonly _onDidChange = this._register(new Emitter<LanguageConfigurationChangeEvent>());
public readonly onDidChange: Event<LanguageConfigurationChangeEvent> = this._onDidChange.event;
constructor() {
super();
this._register(this.register(PLAINTEXT_LANGUAGE_ID, {
brackets: [
['(', ')'],
['[', ']'],
['{', '}'],
],
surroundingPairs: [
{ open: '{', close: '}' },
{ open: '[', close: ']' },
{ open: '(', close: ')' },
{ open: '<', close: '>' },
{ open: '\"', close: '\"' },
{ open: '\'', close: '\'' },
{ open: '`', close: '`' },
],
colorizedBracketPairs: [],
folding: {
offSide: true
}
}, 0));
}
/**
* @param priority Use a higher number for higher priority
*/
public register(languageId: string, configuration: LanguageConfiguration, priority: number = 0): IDisposable {
let entries = this._entries.get(languageId);
if (!entries) {
entries = new ComposedLanguageConfiguration(languageId);
this._entries.set(languageId, entries);
}
const disposable = entries.register(configuration, priority);
this._onDidChange.fire(new LanguageConfigurationChangeEvent(languageId));
return toDisposable(() => {
disposable.dispose();
this._onDidChange.fire(new LanguageConfigurationChangeEvent(languageId));
});
}
public getLanguageConfiguration(languageId: string): ResolvedLanguageConfiguration | null {
const entries = this._entries.get(languageId);
return entries?.getResolvedConfiguration() || null;
}
}
/**
* Immutable.
*/
export class ResolvedLanguageConfiguration {
private _brackets: RichEditBrackets | null;
private _electricCharacter: BracketElectricCharacterSupport | null;
private readonly _onEnterSupport: OnEnterSupport | null;
public readonly comments: ICommentsConfiguration | null;
public readonly characterPair: CharacterPairSupport;
public readonly wordDefinition: RegExp;
public readonly indentRulesSupport: IndentRulesSupport | null;
public readonly indentationRules: IndentationRule | undefined;
public readonly foldingRules: FoldingRules;
public readonly bracketsNew: LanguageBracketsConfiguration;
constructor(
public readonly languageId: string,
public readonly underlyingConfig: LanguageConfiguration
) {
this._brackets = null;
this._electricCharacter = null;
this._onEnterSupport =
this.underlyingConfig.brackets ||
this.underlyingConfig.indentationRules ||
this.underlyingConfig.onEnterRules
? new OnEnterSupport(this.underlyingConfig)
: null;
this.comments = ResolvedLanguageConfiguration._handleComments(this.underlyingConfig);
this.characterPair = new CharacterPairSupport(this.underlyingConfig);
this.wordDefinition = this.underlyingConfig.wordPattern || DEFAULT_WORD_REGEXP;
this.indentationRules = this.underlyingConfig.indentationRules;
if (this.underlyingConfig.indentationRules) {
this.indentRulesSupport = new IndentRulesSupport(
this.underlyingConfig.indentationRules
);
} else {
this.indentRulesSupport = null;
}
this.foldingRules = this.underlyingConfig.folding || {};
this.bracketsNew = new LanguageBracketsConfiguration(
languageId,
this.underlyingConfig
);
}
public getWordDefinition(): RegExp {
return ensureValidWordDefinition(this.wordDefinition);
}
public get brackets(): RichEditBrackets | null {
if (!this._brackets && this.underlyingConfig.brackets) {
this._brackets = new RichEditBrackets(
this.languageId,
this.underlyingConfig.brackets
);
}
return this._brackets;
}
public get electricCharacter(): BracketElectricCharacterSupport | null {
if (!this._electricCharacter) {
this._electricCharacter = new BracketElectricCharacterSupport(
this.brackets
);
}
return this._electricCharacter;
}
public onEnter(
autoIndent: EditorAutoIndentStrategy,
previousLineText: string,
beforeEnterText: string,
afterEnterText: string
): EnterAction | null {
if (!this._onEnterSupport) {
return null;
}
return this._onEnterSupport.onEnter(
autoIndent,
previousLineText,
beforeEnterText,
afterEnterText
);
}
public getAutoClosingPairs(): AutoClosingPairs {
return new AutoClosingPairs(this.characterPair.getAutoClosingPairs());
}
public getAutoCloseBeforeSet(): string {
return this.characterPair.getAutoCloseBeforeSet();
}
public getSurroundingPairs(): IAutoClosingPair[] {
return this.characterPair.getSurroundingPairs();
}
private static _handleComments(
conf: LanguageConfiguration
): ICommentsConfiguration | null {
const commentRule = conf.comments;
if (!commentRule) {
return null;
}
// comment configuration
const comments: ICommentsConfiguration = {};
if (commentRule.lineComment) {
comments.lineCommentToken = commentRule.lineComment;
}
if (commentRule.blockComment) {
const [blockStart, blockEnd] = commentRule.blockComment;
comments.blockCommentStartToken = blockStart;
comments.blockCommentEndToken = blockEnd;
}
return comments;
}
}
registerSingleton(ILanguageConfigurationService, LanguageConfigurationService);
|