-
Notifications
You must be signed in to change notification settings - Fork 28k
/
editor.ts
371 lines (308 loc) · 12.6 KB
/
editor.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { GroupIdentifier, IWorkbenchEditorConfiguration, IEditorIdentifier, IEditorCloseEvent, IEditorPartOptions, IEditorPartOptionsChangeEvent, SideBySideEditor, EditorCloseContext, IEditorPane, IEditorPartLimitConfiguration, IEditorPartDecorationsConfiguration } from 'vs/workbench/common/editor';
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
import { IEditorGroup, GroupDirection, IMergeGroupOptions, GroupsOrder, GroupsArrangement } from 'vs/workbench/services/editor/common/editorGroupsService';
import { IDisposable } from 'vs/base/common/lifecycle';
import { Dimension } from 'vs/base/browser/dom';
import { Event } from 'vs/base/common/event';
import { IConfigurationChangeEvent, IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { ISerializableView } from 'vs/base/browser/ui/grid/grid';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { OptionalBooleanKey, OptionalNumberKey, OptionalStringKey, isObject } from 'vs/base/common/types';
import { IEditorOptions } from 'vs/platform/editor/common/editor';
import { IWindowsConfiguration } from 'vs/platform/window/common/window';
import { ensureOptionalBooleanValue, ensureOptionalNumberValue, ensureOptionalStringValue } from 'vs/base/common/objects';
export interface IEditorPartCreationOptions {
readonly restorePreviousState: boolean;
}
export const DEFAULT_EDITOR_MIN_DIMENSIONS = new Dimension(220, 70);
export const DEFAULT_EDITOR_MAX_DIMENSIONS = new Dimension(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
export const DEFAULT_EDITOR_PART_OPTIONS: IEditorPartOptions = {
showTabs: 'multiple',
highlightModifiedTabs: false,
tabCloseButton: 'right',
tabSizing: 'fit',
tabSizingFixedMinWidth: 50,
tabSizingFixedMaxWidth: 160,
pinnedTabSizing: 'normal',
pinnedTabsOnSeparateRow: false,
tabHeight: 'default',
preventPinnedEditorClose: 'keyboardAndMouse',
titleScrollbarSizing: 'default',
focusRecentEditorAfterClose: true,
showIcons: true,
hasIcons: true, // 'vs-seti' is our default icon theme
enablePreview: true,
openPositioning: 'right',
openSideBySideDirection: 'right',
closeEmptyGroups: true,
labelFormat: 'default',
splitSizing: 'auto',
splitOnDragAndDrop: true,
centeredLayoutFixedWidth: false,
doubleClickTabToToggleEditorGroupSizes: true,
};
export function impactsEditorPartOptions(event: IConfigurationChangeEvent): boolean {
return event.affectsConfiguration('workbench.editor') || event.affectsConfiguration('workbench.iconTheme') || event.affectsConfiguration('window.density');
}
export function getEditorPartOptions(configurationService: IConfigurationService, themeService: IThemeService): IEditorPartOptions {
const options = {
...DEFAULT_EDITOR_PART_OPTIONS,
hasIcons: themeService.getFileIconTheme().hasFileIcons
};
const config = configurationService.getValue<IWorkbenchEditorConfiguration>();
if (config?.workbench?.editor) {
// Assign all primitive configuration over
Object.assign(options, config.workbench.editor);
// Special handle array types and convert to Set
if (isObject(config.workbench.editor.autoLockGroups)) {
options.autoLockGroups = new Set();
for (const [editorId, enablement] of Object.entries(config.workbench.editor.autoLockGroups)) {
if (enablement === true) {
options.autoLockGroups.add(editorId);
}
}
} else {
options.autoLockGroups = undefined;
}
}
const windowConfig = configurationService.getValue<IWindowsConfiguration>();
if (windowConfig?.window?.density?.editorTabHeight) {
options.tabHeight = windowConfig.window.density.editorTabHeight;
}
validateEditorPartOptions(options);
return options;
}
function validateEditorPartOptions(options: IEditorPartOptions): void {
// Migrate: Show tabs (config migration kicks in very late and can cause flicker otherwise)
if (typeof options.showTabs === 'boolean') {
options.showTabs = options.showTabs ? 'multiple' : 'single';
}
// Boolean options
const booleanOptions: Array<OptionalBooleanKey<IEditorPartOptions>> = [
'wrapTabs',
'scrollToSwitchTabs',
'highlightModifiedTabs',
'pinnedTabsOnSeparateRow',
'focusRecentEditorAfterClose',
'showIcons',
'enablePreview',
'enablePreviewFromQuickOpen',
'enablePreviewFromCodeNavigation',
'closeOnFileDelete',
'closeEmptyGroups',
'revealIfOpen',
'mouseBackForwardToNavigate',
'restoreViewState',
'splitOnDragAndDrop',
'centeredLayoutFixedWidth',
'doubleClickTabToToggleEditorGroupSizes'
];
for (const option of booleanOptions) {
if (typeof option === 'string') {
ensureOptionalBooleanValue(options, option, Boolean(DEFAULT_EDITOR_PART_OPTIONS[option]));
}
}
// Number options
const numberOptions: Array<OptionalNumberKey<IEditorPartOptions>> = [
'tabSizingFixedMinWidth',
'tabSizingFixedMaxWidth'
];
for (const option of numberOptions) {
if (typeof option === 'string') {
ensureOptionalNumberValue(options, option, Number(DEFAULT_EDITOR_PART_OPTIONS[option]));
}
}
// String options
const stringOptions: Array<[OptionalStringKey<IEditorPartOptions>, Array<string>]> = [
['showTabs', ['multiple', 'single', 'none']],
['tabCloseButton', ['left', 'right', 'off']],
['tabSizing', ['fit', 'shrink', 'fixed']],
['pinnedTabSizing', ['normal', 'compact', 'shrink']],
['tabHeight', ['default', 'compact']],
['preventPinnedEditorClose', ['keyboardAndMouse', 'keyboard', 'mouse', 'never']],
['titleScrollbarSizing', ['default', 'large']],
['openPositioning', ['left', 'right', 'first', 'last']],
['openSideBySideDirection', ['right', 'down']],
['labelFormat', ['default', 'short', 'medium', 'long']],
['splitInGroupLayout', ['vertical', 'horizontal']],
['splitSizing', ['distribute', 'split', 'auto']],
];
for (const [option, allowed] of stringOptions) {
if (typeof option === 'string') {
ensureOptionalStringValue(options, option, allowed, String(DEFAULT_EDITOR_PART_OPTIONS[option]));
}
}
// Complex options
if (options.autoLockGroups && !(options.autoLockGroups instanceof Set)) {
options.autoLockGroups = undefined;
}
if (options.limit && !isObject(options.limit)) {
options.limit = undefined;
} else if (options.limit) {
const booleanLimitOptions: Array<OptionalBooleanKey<IEditorPartLimitConfiguration>> = [
'enabled',
'excludeDirty',
'perEditorGroup'
];
for (const option of booleanLimitOptions) {
if (typeof option === 'string') {
ensureOptionalBooleanValue(options.limit, option, undefined);
}
}
ensureOptionalNumberValue(options.limit, 'value', undefined);
}
if (options.decorations && !isObject(options.decorations)) {
options.decorations = undefined;
} else if (options.decorations) {
const booleanDecorationOptions: Array<OptionalBooleanKey<IEditorPartDecorationsConfiguration>> = [
'badges',
'colors'
];
for (const option of booleanDecorationOptions) {
if (typeof option === 'string') {
ensureOptionalBooleanValue(options.decorations, option, undefined);
}
}
}
}
/**
* A helper to access editor groups across all opened editor parts.
*/
export interface IEditorPartsView {
/**
* An array of all editor groups across all editor parts.
*/
readonly groups: IEditorGroupView[];
/**
* Get the group based on an identifier across all opened
* editor parts.
*/
getGroup(identifier: GroupIdentifier): IEditorGroupView | undefined;
}
/**
* A helper to access and mutate editor groups within an editor part.
*/
export interface IEditorGroupsView {
readonly groups: IEditorGroupView[];
readonly activeGroup: IEditorGroupView;
readonly partOptions: IEditorPartOptions;
readonly onDidChangeEditorPartOptions: Event<IEditorPartOptionsChangeEvent>;
readonly onDidVisibilityChange: Event<boolean>;
getGroup(identifier: GroupIdentifier): IEditorGroupView | undefined;
getGroups(order: GroupsOrder): IEditorGroupView[];
activateGroup(identifier: IEditorGroupView | GroupIdentifier): IEditorGroupView;
restoreGroup(identifier: IEditorGroupView | GroupIdentifier): IEditorGroupView;
addGroup(location: IEditorGroupView | GroupIdentifier, direction: GroupDirection, groupToCopy?: IEditorGroupView): IEditorGroupView;
mergeGroup(group: IEditorGroupView | GroupIdentifier, target: IEditorGroupView | GroupIdentifier, options?: IMergeGroupOptions): IEditorGroupView;
moveGroup(group: IEditorGroupView | GroupIdentifier, location: IEditorGroupView | GroupIdentifier, direction: GroupDirection): IEditorGroupView;
copyGroup(group: IEditorGroupView | GroupIdentifier, location: IEditorGroupView | GroupIdentifier, direction: GroupDirection): IEditorGroupView;
removeGroup(group: IEditorGroupView | GroupIdentifier): void;
arrangeGroups(arrangement: GroupsArrangement, target?: IEditorGroupView | GroupIdentifier): void;
}
export interface IEditorGroupTitleHeight {
/**
* The overall height of the editor group title control.
*/
readonly total: number;
/**
* The height offset to e.g. use when drawing drop overlays.
* This number may be smaller than `height` if the title control
* decides to have an `offset` that is within the title control
* (e.g. when breadcrumbs are enabled).
*/
readonly offset: number;
}
/**
* A helper to access and mutate an editor group within an editor part.
*/
export interface IEditorGroupView extends IDisposable, ISerializableView, IEditorGroup {
readonly onDidFocus: Event<void>;
readonly onDidOpenEditorFail: Event<EditorInput>;
readonly onDidCloseEditor: Event<IEditorCloseEvent>;
readonly groupsView: IEditorGroupsView;
/**
* A promise that resolves when the group has been restored.
*
* For a group with active editor, the promise will resolve
* when the active editor has finished to resolve.
*/
readonly whenRestored: Promise<void>;
readonly titleHeight: IEditorGroupTitleHeight;
readonly disposed: boolean;
setActive(isActive: boolean): void;
notifyIndexChanged(newIndex: number): void;
openEditor(editor: EditorInput, options?: IEditorOptions, internalOptions?: IInternalEditorOpenOptions): Promise<IEditorPane | undefined>;
relayout(): void;
}
export function fillActiveEditorViewState(group: IEditorGroup, expectedActiveEditor?: EditorInput, presetOptions?: IEditorOptions): IEditorOptions {
if (!expectedActiveEditor || !group.activeEditor || expectedActiveEditor.matches(group.activeEditor)) {
const options: IEditorOptions = {
...presetOptions,
viewState: group.activeEditorPane?.getViewState()
};
return options;
}
return presetOptions || Object.create(null);
}
/**
* A sub-interface of IEditorService to hide some workbench-core specific
* events from clients.
*/
export interface EditorServiceImpl extends IEditorService {
/**
* Emitted when an editor failed to open.
*/
readonly onDidOpenEditorFail: Event<IEditorIdentifier>;
/**
* Emitted when the list of most recently active editors change.
*/
readonly onDidMostRecentlyActiveEditorsChange: Event<void>;
}
export interface IInternalEditorTitleControlOptions {
/**
* A hint to defer updating the title control for perf reasons.
* The caller must ensure to update the title control then.
*/
readonly skipTitleUpdate?: boolean;
}
export interface IInternalEditorOpenOptions extends IInternalEditorTitleControlOptions {
/**
* Whether to consider a side by side editor as matching
* when figuring out if the editor to open is already
* opened or not. By default, side by side editors will
* not be considered as matching, even if the editor is
* opened in one of the sides.
*/
readonly supportSideBySide?: SideBySideEditor.ANY | SideBySideEditor.BOTH;
/**
* When set to `true`, pass DOM focus into the tab control.
*/
readonly focusTabControl?: boolean;
/**
* When set to `true`, will not attempt to move the window to
* the top that the editor opens in.
*/
readonly preserveWindowOrder?: boolean;
}
export interface IInternalEditorCloseOptions extends IInternalEditorTitleControlOptions {
/**
* A hint that the editor is closed due to an error opening. This can be
* used to optimize how error toasts are appearing if any.
*/
readonly fromError?: boolean;
/**
* Additional context as to why an editor is closed.
*/
readonly context?: EditorCloseContext;
}
export interface IInternalMoveCopyOptions extends IInternalEditorOpenOptions {
/**
* Whether to close the editor at the source or keep it.
*/
readonly keepCopy?: boolean;
}