-
Notifications
You must be signed in to change notification settings - Fork 30.4k
/
Copy pathcommonEditorConfig.ts
561 lines (497 loc) · 18.8 KB
/
commonEditorConfig.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*---------------------------------------------------------------------------------------------
* 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 { Emitter, Event } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
import * as objects from 'vs/base/common/objects';
import * as arrays from 'vs/base/common/arrays';
import { IEditorOptions, editorOptionsRegistry, ValidatedEditorOptions, IEnvironmentalOptions, IComputedEditorOptions, ConfigurationChangedEvent, EDITOR_MODEL_DEFAULTS, EditorOption, FindComputedEditorOptionValueById } from 'vs/editor/common/config/editorOptions';
import { EditorZoom } from 'vs/editor/common/config/editorZoom';
import { BareFontInfo, FontInfo } from 'vs/editor/common/config/fontInfo';
import * as editorCommon from 'vs/editor/common/editorCommon';
import { ConfigurationScope, Extensions, IConfigurationNode, IConfigurationRegistry, IConfigurationPropertySchema } from 'vs/platform/configuration/common/configurationRegistry';
import { Registry } from 'vs/platform/registry/common/platform';
import { AccessibilitySupport } from 'vs/platform/accessibility/common/accessibility';
import { forEach } from 'vs/base/common/collections';
/**
* Control what pressing Tab does.
* If it is false, pressing Tab or Shift-Tab will be handled by the editor.
* If it is true, pressing Tab or Shift-Tab will move the browser focus.
* Defaults to false.
*/
export interface ITabFocus {
onDidChangeTabFocus: Event<boolean>;
getTabFocusMode(): boolean;
setTabFocusMode(tabFocusMode: boolean): void;
}
export const TabFocus: ITabFocus = new class implements ITabFocus {
private _tabFocus: boolean = false;
private readonly _onDidChangeTabFocus = new Emitter<boolean>();
public readonly onDidChangeTabFocus: Event<boolean> = this._onDidChangeTabFocus.event;
public getTabFocusMode(): boolean {
return this._tabFocus;
}
public setTabFocusMode(tabFocusMode: boolean): void {
if (this._tabFocus === tabFocusMode) {
return;
}
this._tabFocus = tabFocusMode;
this._onDidChangeTabFocus.fire(this._tabFocus);
}
};
export interface IEnvConfiguration {
extraEditorClassName: string;
outerWidth: number;
outerHeight: number;
emptySelectionClipboard: boolean;
pixelRatio: number;
zoomLevel: number;
accessibilitySupport: AccessibilitySupport;
}
const hasOwnProperty = Object.hasOwnProperty;
export class ComputedEditorOptions implements IComputedEditorOptions {
private readonly _values: any[] = [];
public _read<T>(id: EditorOption): T {
return this._values[id];
}
public get<T extends EditorOption>(id: T): FindComputedEditorOptionValueById<T> {
return this._values[id];
}
public _write<T>(id: EditorOption, value: T): void {
this._values[id] = value;
}
}
class RawEditorOptions {
private readonly _values: any[] = [];
public _read<T>(id: EditorOption): T | undefined {
return this._values[id];
}
public _write<T>(id: EditorOption, value: T | undefined): void {
this._values[id] = value;
}
}
class EditorConfiguration2 {
public static readOptions(_options: IEditorOptions): RawEditorOptions {
const options: { [key: string]: any; } = _options;
const result = new RawEditorOptions();
for (const editorOption of editorOptionsRegistry) {
const value = (editorOption.name === '_never_' ? undefined : options[editorOption.name]);
result._write(editorOption.id, value);
}
return result;
}
public static validateOptions(options: RawEditorOptions): ValidatedEditorOptions {
const result = new ValidatedEditorOptions();
for (const editorOption of editorOptionsRegistry) {
result._write(editorOption.id, editorOption.validate(options._read(editorOption.id)));
}
return result;
}
public static computeOptions(options: ValidatedEditorOptions, env: IEnvironmentalOptions): ComputedEditorOptions {
const result = new ComputedEditorOptions();
for (const editorOption of editorOptionsRegistry) {
result._write(editorOption.id, editorOption.compute(env, result, options._read(editorOption.id)));
}
return result;
}
private static _deepEquals<T>(a: T, b: T): boolean {
if (typeof a !== 'object' || typeof b !== 'object') {
return (a === b);
}
if (Array.isArray(a) || Array.isArray(b)) {
return (Array.isArray(a) && Array.isArray(b) ? arrays.equals(a, b) : false);
}
for (let key in a) {
if (!EditorConfiguration2._deepEquals(a[key], b[key])) {
return false;
}
}
return true;
}
public static checkEquals(a: ComputedEditorOptions, b: ComputedEditorOptions): ConfigurationChangedEvent | null {
const result: boolean[] = [];
let somethingChanged = false;
for (const editorOption of editorOptionsRegistry) {
const changed = !EditorConfiguration2._deepEquals(a._read(editorOption.id), b._read(editorOption.id));
result[editorOption.id] = changed;
if (changed) {
somethingChanged = true;
}
}
return (somethingChanged ? new ConfigurationChangedEvent(result) : null);
}
}
/**
* Compatibility with old options
*/
function migrateOptions(options: IEditorOptions): void {
const wordWrap = options.wordWrap;
if (<any>wordWrap === true) {
options.wordWrap = 'on';
} else if (<any>wordWrap === false) {
options.wordWrap = 'off';
}
const lineNumbers = options.lineNumbers;
if (<any>lineNumbers === true) {
options.lineNumbers = 'on';
} else if (<any>lineNumbers === false) {
options.lineNumbers = 'off';
}
const autoClosingBrackets = options.autoClosingBrackets;
if (<any>autoClosingBrackets === false) {
options.autoClosingBrackets = 'never';
options.autoClosingQuotes = 'never';
options.autoSurround = 'never';
}
const cursorBlinking = options.cursorBlinking;
if (<any>cursorBlinking === 'visible') {
options.cursorBlinking = 'solid';
}
const renderWhitespace = options.renderWhitespace;
if (<any>renderWhitespace === true) {
options.renderWhitespace = 'boundary';
} else if (<any>renderWhitespace === false) {
options.renderWhitespace = 'none';
}
const renderLineHighlight = options.renderLineHighlight;
if (<any>renderLineHighlight === true) {
options.renderLineHighlight = 'line';
} else if (<any>renderLineHighlight === false) {
options.renderLineHighlight = 'none';
}
const acceptSuggestionOnEnter = options.acceptSuggestionOnEnter;
if (<any>acceptSuggestionOnEnter === true) {
options.acceptSuggestionOnEnter = 'on';
} else if (<any>acceptSuggestionOnEnter === false) {
options.acceptSuggestionOnEnter = 'off';
}
const tabCompletion = options.tabCompletion;
if (<any>tabCompletion === false) {
options.tabCompletion = 'off';
} else if (<any>tabCompletion === true) {
options.tabCompletion = 'onlySnippets';
}
const suggest = options.suggest;
if (suggest && typeof (<any>suggest).filteredTypes === 'object' && (<any>suggest).filteredTypes) {
const mapping: Record<string, string> = {};
mapping['method'] = 'showMethods';
mapping['function'] = 'showFunctions';
mapping['constructor'] = 'showConstructors';
mapping['field'] = 'showFields';
mapping['variable'] = 'showVariables';
mapping['class'] = 'showClasses';
mapping['struct'] = 'showStructs';
mapping['interface'] = 'showInterfaces';
mapping['module'] = 'showModules';
mapping['property'] = 'showProperties';
mapping['event'] = 'showEvents';
mapping['operator'] = 'showOperators';
mapping['unit'] = 'showUnits';
mapping['value'] = 'showValues';
mapping['constant'] = 'showConstants';
mapping['enum'] = 'showEnums';
mapping['enumMember'] = 'showEnumMembers';
mapping['keyword'] = 'showKeywords';
mapping['text'] = 'showWords';
mapping['color'] = 'showColors';
mapping['file'] = 'showFiles';
mapping['reference'] = 'showReferences';
mapping['folder'] = 'showFolders';
mapping['typeParameter'] = 'showTypeParameters';
mapping['snippet'] = 'showSnippets';
forEach(mapping, entry => {
const value = (<any>suggest).filteredTypes[entry.key];
if (value === false) {
(<any>suggest)[entry.value] = value;
}
});
// delete (<any>suggest).filteredTypes;
}
const hover = options.hover;
if (<any>hover === true) {
options.hover = {
enabled: true
};
} else if (<any>hover === false) {
options.hover = {
enabled: false
};
}
const parameterHints = options.parameterHints;
if (<any>parameterHints === true) {
options.parameterHints = {
enabled: true
};
} else if (<any>parameterHints === false) {
options.parameterHints = {
enabled: false
};
}
const autoIndent = options.autoIndent;
if (<any>autoIndent === true) {
options.autoIndent = 'full';
} else if (<any>autoIndent === false) {
options.autoIndent = 'advanced';
}
const matchBrackets = options.matchBrackets;
if (<any>matchBrackets === true) {
options.matchBrackets = 'always';
} else if (<any>matchBrackets === false) {
options.matchBrackets = 'never';
}
}
function deepCloneAndMigrateOptions(_options: IEditorOptions): IEditorOptions {
const options = objects.deepClone(_options);
migrateOptions(options);
return options;
}
export abstract class CommonEditorConfiguration extends Disposable implements editorCommon.IConfiguration {
private _onDidChange = this._register(new Emitter<ConfigurationChangedEvent>());
public readonly onDidChange: Event<ConfigurationChangedEvent> = this._onDidChange.event;
public readonly isSimpleWidget: boolean;
public options!: ComputedEditorOptions;
private _isDominatedByLongLines: boolean;
private _lineNumbersDigitCount: number;
private _rawOptions: IEditorOptions;
private _readOptions: RawEditorOptions;
protected _validatedOptions: ValidatedEditorOptions;
constructor(isSimpleWidget: boolean, _options: IEditorOptions) {
super();
this.isSimpleWidget = isSimpleWidget;
this._isDominatedByLongLines = false;
this._lineNumbersDigitCount = 1;
this._rawOptions = deepCloneAndMigrateOptions(_options);
this._readOptions = EditorConfiguration2.readOptions(this._rawOptions);
this._validatedOptions = EditorConfiguration2.validateOptions(this._readOptions);
this._register(EditorZoom.onDidChangeZoomLevel(_ => this._recomputeOptions()));
this._register(TabFocus.onDidChangeTabFocus(_ => this._recomputeOptions()));
}
public observeReferenceElement(dimension?: editorCommon.IDimension): void {
}
public dispose(): void {
super.dispose();
}
protected _recomputeOptions(): void {
const oldOptions = this.options;
const newOptions = this._computeInternalOptions();
if (!oldOptions) {
this.options = newOptions;
} else {
const changeEvent = EditorConfiguration2.checkEquals(oldOptions, newOptions);
if (changeEvent === null) {
// nothing changed!
return;
}
this.options = newOptions;
this._onDidChange.fire(changeEvent);
}
}
public getRawOptions(): IEditorOptions {
return this._rawOptions;
}
private _computeInternalOptions(): ComputedEditorOptions {
const partialEnv = this._getEnvConfiguration();
const bareFontInfo = BareFontInfo.createFromValidatedSettings(this._validatedOptions, partialEnv.zoomLevel, this.isSimpleWidget);
const env: IEnvironmentalOptions = {
outerWidth: partialEnv.outerWidth,
outerHeight: partialEnv.outerHeight,
fontInfo: this.readConfiguration(bareFontInfo),
extraEditorClassName: partialEnv.extraEditorClassName,
isDominatedByLongLines: this._isDominatedByLongLines,
lineNumbersDigitCount: this._lineNumbersDigitCount,
emptySelectionClipboard: partialEnv.emptySelectionClipboard,
pixelRatio: partialEnv.pixelRatio,
tabFocusMode: TabFocus.getTabFocusMode(),
accessibilitySupport: partialEnv.accessibilitySupport
};
return EditorConfiguration2.computeOptions(this._validatedOptions, env);
}
private static _subsetEquals(base: { [key: string]: any }, subset: { [key: string]: any }): boolean {
for (const key in subset) {
if (hasOwnProperty.call(subset, key)) {
const subsetValue = subset[key];
const baseValue = base[key];
if (baseValue === subsetValue) {
continue;
}
if (Array.isArray(baseValue) && Array.isArray(subsetValue)) {
if (!arrays.equals(baseValue, subsetValue)) {
return false;
}
continue;
}
if (typeof baseValue === 'object' && typeof subsetValue === 'object') {
if (!this._subsetEquals(baseValue, subsetValue)) {
return false;
}
continue;
}
return false;
}
}
return true;
}
public updateOptions(_newOptions: IEditorOptions): void {
if (typeof _newOptions === 'undefined') {
return;
}
const newOptions = deepCloneAndMigrateOptions(_newOptions);
if (CommonEditorConfiguration._subsetEquals(this._rawOptions, newOptions)) {
return;
}
this._rawOptions = objects.mixin(this._rawOptions, newOptions || {});
this._readOptions = EditorConfiguration2.readOptions(this._rawOptions);
this._validatedOptions = EditorConfiguration2.validateOptions(this._readOptions);
this._recomputeOptions();
}
public setIsDominatedByLongLines(isDominatedByLongLines: boolean): void {
this._isDominatedByLongLines = isDominatedByLongLines;
this._recomputeOptions();
}
public setMaxLineNumber(maxLineNumber: number): void {
let digitCount = CommonEditorConfiguration._digitCount(maxLineNumber);
if (this._lineNumbersDigitCount === digitCount) {
return;
}
this._lineNumbersDigitCount = digitCount;
this._recomputeOptions();
}
private static _digitCount(n: number): number {
let r = 0;
while (n) {
n = Math.floor(n / 10);
r++;
}
return r ? r : 1;
}
protected abstract _getEnvConfiguration(): IEnvConfiguration;
protected abstract readConfiguration(styling: BareFontInfo): FontInfo;
}
export const editorConfigurationBaseNode = Object.freeze<IConfigurationNode>({
id: 'editor',
order: 5,
type: 'object',
title: nls.localize('editorConfigurationTitle', "Editor"),
scope: ConfigurationScope.RESOURCE_LANGUAGE,
});
const configurationRegistry = Registry.as<IConfigurationRegistry>(Extensions.Configuration);
const editorConfiguration: IConfigurationNode = {
...editorConfigurationBaseNode,
properties: {
'editor.tabSize': {
type: 'number',
default: EDITOR_MODEL_DEFAULTS.tabSize,
minimum: 1,
markdownDescription: nls.localize('tabSize', "The number of spaces a tab is equal to. This setting is overridden based on the file contents when `#editor.detectIndentation#` is on.")
},
// 'editor.indentSize': {
// 'anyOf': [
// {
// type: 'string',
// enum: ['tabSize']
// },
// {
// type: 'number',
// minimum: 1
// }
// ],
// default: 'tabSize',
// markdownDescription: nls.localize('indentSize', "The number of spaces used for indentation or 'tabSize' to use the value from `#editor.tabSize#`. This setting is overridden based on the file contents when `#editor.detectIndentation#` is on.")
// },
'editor.insertSpaces': {
type: 'boolean',
default: EDITOR_MODEL_DEFAULTS.insertSpaces,
markdownDescription: nls.localize('insertSpaces', "Insert spaces when pressing `Tab`. This setting is overridden based on the file contents when `#editor.detectIndentation#` is on.")
},
'editor.detectIndentation': {
type: 'boolean',
default: EDITOR_MODEL_DEFAULTS.detectIndentation,
markdownDescription: nls.localize('detectIndentation', "Controls whether `#editor.tabSize#` and `#editor.insertSpaces#` will be automatically detected when a file is opened based on the file contents.")
},
'editor.trimAutoWhitespace': {
type: 'boolean',
default: EDITOR_MODEL_DEFAULTS.trimAutoWhitespace,
description: nls.localize('trimAutoWhitespace', "Remove trailing auto inserted whitespace.")
},
'editor.largeFileOptimizations': {
type: 'boolean',
default: EDITOR_MODEL_DEFAULTS.largeFileOptimizations,
description: nls.localize('largeFileOptimizations', "Special handling for large files to disable certain memory intensive features.")
},
'editor.wordBasedSuggestions': {
type: 'boolean',
default: true,
description: nls.localize('wordBasedSuggestions', "Controls whether completions should be computed based on words in the document.")
},
'editor.stablePeek': {
type: 'boolean',
default: false,
markdownDescription: nls.localize('stablePeek', "Keep peek editors open even when double clicking their content or when hitting `Escape`.")
},
'editor.maxTokenizationLineLength': {
type: 'integer',
default: 20_000,
description: nls.localize('maxTokenizationLineLength', "Lines above this length will not be tokenized for performance reasons")
},
'diffEditor.maxComputationTime': {
type: 'number',
default: 5000,
description: nls.localize('maxComputationTime', "Timeout in milliseconds after which diff computation is cancelled. Use 0 for no timeout.")
},
'diffEditor.renderSideBySide': {
type: 'boolean',
default: true,
description: nls.localize('sideBySide', "Controls whether the diff editor shows the diff side by side or inline.")
},
'diffEditor.ignoreTrimWhitespace': {
type: 'boolean',
default: true,
description: nls.localize('ignoreTrimWhitespace', "Controls whether the diff editor shows changes in leading or trailing whitespace as diffs.")
},
'diffEditor.renderIndicators': {
type: 'boolean',
default: true,
description: nls.localize('renderIndicators', "Controls whether the diff editor shows +/- indicators for added/removed changes.")
}
}
};
function isConfigurationPropertySchema(x: IConfigurationPropertySchema | { [path: string]: IConfigurationPropertySchema; }): x is IConfigurationPropertySchema {
return (typeof x.type !== 'undefined' || typeof x.anyOf !== 'undefined');
}
// Add properties from the Editor Option Registry
for (const editorOption of editorOptionsRegistry) {
const schema = editorOption.schema;
if (typeof schema !== 'undefined') {
if (isConfigurationPropertySchema(schema)) {
// This is a single schema contribution
editorConfiguration.properties![`editor.${editorOption.name}`] = schema;
} else {
for (let key in schema) {
if (hasOwnProperty.call(schema, key)) {
editorConfiguration.properties![key] = schema[key];
}
}
}
}
}
let cachedEditorConfigurationKeys: { [key: string]: boolean; } | null = null;
function getEditorConfigurationKeys(): { [key: string]: boolean; } {
if (cachedEditorConfigurationKeys === null) {
cachedEditorConfigurationKeys = <{ [key: string]: boolean; }>Object.create(null);
Object.keys(editorConfiguration.properties!).forEach((prop) => {
cachedEditorConfigurationKeys![prop] = true;
});
}
return cachedEditorConfigurationKeys;
}
export function isEditorConfigurationKey(key: string): boolean {
const editorConfigurationKeys = getEditorConfigurationKeys();
return (editorConfigurationKeys[`editor.${key}`] || false);
}
export function isDiffEditorConfigurationKey(key: string): boolean {
const editorConfigurationKeys = getEditorConfigurationKeys();
return (editorConfigurationKeys[`diffEditor.${key}`] || false);
}
configurationRegistry.registerConfiguration(editorConfiguration);