-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
editorConfiguration.ts
181 lines (157 loc) · 7.84 KB
/
editorConfiguration.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from 'vs/nls';
import { Registry } from 'vs/platform/registry/common/platform';
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { Disposable } from 'vs/base/common/lifecycle';
import { IConfigurationRegistry, Extensions as ConfigurationExtensions, IConfigurationNode, ConfigurationScope } from 'vs/platform/configuration/common/configurationRegistry';
import { workbenchConfigurationNodeBase } from 'vs/workbench/common/configuration';
import { IEditorResolverService, RegisteredEditorInfo, RegisteredEditorPriority } from 'vs/workbench/services/editor/common/editorResolverService';
import { IJSONSchemaMap } from 'vs/base/common/jsonSchema';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
import { coalesce } from 'vs/base/common/arrays';
import { Event } from 'vs/base/common/event';
import { isWeb } from 'vs/base/common/platform';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
export class DynamicEditorConfigurations extends Disposable implements IWorkbenchContribution {
private static readonly AUTO_LOCK_DEFAULT_ENABLED = new Set<string>(['terminalEditor']);
private static readonly AUTO_LOCK_EXTRA_EDITORS: RegisteredEditorInfo[] = [
// List some editor input identifiers that are not
// registered yet via the editor resolver infrastructure
{
id: 'workbench.input.interactive',
label: localize('interactiveWindow', 'Interactive Window'),
priority: RegisteredEditorPriority.builtin
},
{
id: 'mainThreadWebview-markdown.preview',
label: localize('markdownPreview', "Markdown Preview"),
priority: RegisteredEditorPriority.builtin
}
];
private static readonly AUTO_LOCK_REMOVE_EDITORS = new Set<string>([
// List some editor types that the above `AUTO_LOCK_EXTRA_EDITORS`
// already covers to avoid duplicates.
'vscode-interactive-input',
'interactive',
'vscode.markdown.preview.editor'
]);
private readonly configurationRegistry = Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration);
private autoLockConfigurationNode: IConfigurationNode | undefined;
private defaultBinaryEditorConfigurationNode: IConfigurationNode | undefined;
private editorAssociationsConfigurationNode: IConfigurationNode | undefined;
private editorLargeFileConfirmationConfigurationNode: IConfigurationNode | undefined;
constructor(
@IEditorResolverService private readonly editorResolverService: IEditorResolverService,
@IExtensionService extensionService: IExtensionService,
@IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService
) {
super();
// Editor configurations are getting updated very aggressively
// (atleast 20 times) while the extensions are getting registered.
// As such push out the dynamic configuration until after extensions
// are registered.
(async () => {
await extensionService.whenInstalledExtensionsRegistered();
this.updateDynamicEditorConfigurations();
this.registerListeners();
})();
}
private registerListeners(): void {
// Registered editors (debounced to reduce perf overhead)
Event.debounce(this.editorResolverService.onDidChangeEditorRegistrations, (_, e) => e)(() => this.updateDynamicEditorConfigurations());
}
private updateDynamicEditorConfigurations(): void {
const lockableEditors = [...this.editorResolverService.getEditors(), ...DynamicEditorConfigurations.AUTO_LOCK_EXTRA_EDITORS].filter(e => !DynamicEditorConfigurations.AUTO_LOCK_REMOVE_EDITORS.has(e.id));
const binaryEditorCandidates = this.editorResolverService.getEditors().filter(e => e.priority !== RegisteredEditorPriority.exclusive).map(e => e.id);
// Build config from registered editors
const autoLockGroupConfiguration: IJSONSchemaMap = Object.create(null);
for (const editor of lockableEditors) {
autoLockGroupConfiguration[editor.id] = {
type: 'boolean',
default: DynamicEditorConfigurations.AUTO_LOCK_DEFAULT_ENABLED.has(editor.id),
description: editor.label
};
}
// Build default config too
const defaultAutoLockGroupConfiguration = Object.create(null);
for (const editor of lockableEditors) {
defaultAutoLockGroupConfiguration[editor.id] = DynamicEditorConfigurations.AUTO_LOCK_DEFAULT_ENABLED.has(editor.id);
}
// Register setting for auto locking groups
const oldAutoLockConfigurationNode = this.autoLockConfigurationNode;
this.autoLockConfigurationNode = {
...workbenchConfigurationNodeBase,
properties: {
'workbench.editor.autoLockGroups': {
type: 'object',
description: localize('workbench.editor.autoLockGroups', "If an editor matching one of the listed types is opened as the first in an editor group and more than one group is open, the group is automatically locked. Locked groups will only be used for opening editors when explicitly chosen by a user gesture (for example drag and drop), but not by default. Consequently, the active editor in a locked group is less likely to be replaced accidentally with a different editor."),
properties: autoLockGroupConfiguration,
default: defaultAutoLockGroupConfiguration,
additionalProperties: false
}
}
};
// Registers setting for default binary editors
const oldDefaultBinaryEditorConfigurationNode = this.defaultBinaryEditorConfigurationNode;
this.defaultBinaryEditorConfigurationNode = {
...workbenchConfigurationNodeBase,
properties: {
'workbench.editor.defaultBinaryEditor': {
type: 'string',
default: '',
// This allows for intellisense autocompletion
enum: [...binaryEditorCandidates, ''],
description: localize('workbench.editor.defaultBinaryEditor', "The default editor for files detected as binary. If undefined, the user will be presented with a picker."),
}
}
};
// Registers setting for editorAssociations
const oldEditorAssociationsConfigurationNode = this.editorAssociationsConfigurationNode;
this.editorAssociationsConfigurationNode = {
...workbenchConfigurationNodeBase,
properties: {
'workbench.editorAssociations': {
type: 'object',
markdownDescription: localize('editor.editorAssociations', "Configure glob patterns to editors (for example `\"*.hex\": \"hexEditor.hexEdit\"`). These have precedence over the default behavior."),
patternProperties: {
'.*': {
type: 'string',
enum: binaryEditorCandidates,
}
}
}
}
};
// Registers setting for large file confirmation based on environment
const oldEditorLargeFileConfirmationConfigurationNode = this.editorLargeFileConfirmationConfigurationNode;
this.editorLargeFileConfirmationConfigurationNode = {
...workbenchConfigurationNodeBase,
properties: {
'workbench.editorLargeFileConfirmation': {
type: 'number',
default: isWeb ? 10 : this.environmentService.remoteAuthority ? 50 : 1024,
minimum: 1,
scope: ConfigurationScope.RESOURCE,
markdownDescription: localize('editorLargeFileSizeConfirmation', "Controls the minimum size of a file in MB before asking for confirmation when opening in the editor."),
}
}
};
this.configurationRegistry.updateConfigurations({
add: [
this.autoLockConfigurationNode,
this.defaultBinaryEditorConfigurationNode,
this.editorAssociationsConfigurationNode,
this.editorLargeFileConfirmationConfigurationNode
],
remove: coalesce([
oldAutoLockConfigurationNode,
oldDefaultBinaryEditorConfigurationNode,
oldEditorAssociationsConfigurationNode,
oldEditorLargeFileConfirmationConfigurationNode
])
});
}
}