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
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { EditorExtensions, EditorInputCapabilities, IEditorOpenContext, IVisibleEditorPane } from 'vs/workbench/common/editor';
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
import { Dimension, show, hide } from 'vs/base/browser/dom';
import { Registry } from 'vs/platform/registry/common/platform';
import { IEditorPaneRegistry, IEditorPaneDescriptor } from 'vs/workbench/browser/editor';
import { IWorkbenchLayoutService } from 'vs/workbench/services/layout/browser/layoutService';
import { EditorPane } from 'vs/workbench/browser/parts/editor/editorPane';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IEditorProgressService, LongRunningOperation } from 'vs/platform/progress/common/progress';
import { IEditorGroupView, DEFAULT_EDITOR_MIN_DIMENSIONS, DEFAULT_EDITOR_MAX_DIMENSIONS } from 'vs/workbench/browser/parts/editor/editor';
import { Emitter } from 'vs/base/common/event';
import { assertIsDefined } from 'vs/base/common/types';
import { IWorkspaceTrustManagementService } from 'vs/platform/workspace/common/workspaceTrust';
import { UnavailableResourceErrorEditor, UnknownErrorEditor, WorkspaceTrustRequiredEditor } from 'vs/workbench/browser/parts/editor/editorPlaceholder';
import { IEditorOptions } from 'vs/platform/editor/common/editor';
import { FileOperationError, FileOperationResult } from 'vs/platform/files/common/files';
export interface IOpenEditorResult {
/**
* The editor pane used for opening. This can be a generic
* placeholder in certain cases, e.g. when workspace trust
* is required, or an editor fails to restore.
*
* Will be `undefined` if an error occurred while trying to
* open the editor and in cases where no placeholder is being
* used.
*/
readonly pane?: EditorPane;
/**
* Whether the editor changed as a result of opening.
*/
readonly changed?: boolean;
/**
* This property is set when an editor fails to restore and
* is shown with a generic place holder. It allows callers
* to still present the error to the user in that case.
*/
readonly error?: Error;
/**
* This property indicates whether the open editor operation was
* cancelled or not. The operation may have been cancelled
* in case another editor open operation was triggered right
* after cancelling this one out.
*/
readonly cancelled?: boolean;
}
export class EditorPanes extends Disposable {
//#region Events
private readonly _onDidFocus = this._register(new Emitter<void>());
readonly onDidFocus = this._onDidFocus.event;
private _onDidChangeSizeConstraints = this._register(new Emitter<{ width: number; height: number } | undefined>());
readonly onDidChangeSizeConstraints = this._onDidChangeSizeConstraints.event;
//#endregion
get minimumWidth() { return this._activeEditorPane?.minimumWidth ?? DEFAULT_EDITOR_MIN_DIMENSIONS.width; }
get minimumHeight() { return this._activeEditorPane?.minimumHeight ?? DEFAULT_EDITOR_MIN_DIMENSIONS.height; }
get maximumWidth() { return this._activeEditorPane?.maximumWidth ?? DEFAULT_EDITOR_MAX_DIMENSIONS.width; }
get maximumHeight() { return this._activeEditorPane?.maximumHeight ?? DEFAULT_EDITOR_MAX_DIMENSIONS.height; }
private _activeEditorPane: EditorPane | null = null;
get activeEditorPane(): IVisibleEditorPane | null { return this._activeEditorPane as IVisibleEditorPane | null; }
private readonly editorPanes: EditorPane[] = [];
private readonly activeEditorPaneDisposables = this._register(new DisposableStore());
private dimension: Dimension | undefined;
private readonly editorOperation = this._register(new LongRunningOperation(this.editorProgressService));
private readonly editorPanesRegistry = Registry.as<IEditorPaneRegistry>(EditorExtensions.EditorPane);
constructor(
private parent: HTMLElement,
private groupView: IEditorGroupView,
@IWorkbenchLayoutService private readonly layoutService: IWorkbenchLayoutService,
@IInstantiationService private readonly instantiationService: IInstantiationService,
@IEditorProgressService private readonly editorProgressService: IEditorProgressService,
@IWorkspaceTrustManagementService private readonly workspaceTrustService: IWorkspaceTrustManagementService
) {
super();
this.registerListeners();
}
private registerListeners(): void {
this._register(this.workspaceTrustService.onDidChangeTrust(() => this.onDidChangeWorkspaceTrust()));
}
private onDidChangeWorkspaceTrust() {
// If the active editor pane requires workspace trust
// we need to re-open it anytime trust changes to
// account for it.
// For that we explicitly call into the group-view
// to handle errors properly.
const editor = this._activeEditorPane?.input;
const options = this._activeEditorPane?.options;
if (editor?.hasCapability(EditorInputCapabilities.RequiresTrust)) {
this.groupView.openEditor(editor, options);
}
}
async openEditor(editor: EditorInput, options: IEditorOptions | undefined, context: IEditorOpenContext = Object.create(null)): Promise<IOpenEditorResult> {
try {
return await this.doOpenEditor(this.getEditorPaneDescriptor(editor), editor, options, context);
} catch (error) {
// We failed to open an editor and thus fallback to show a generic
// editor in error state as a way for the user to be aware.
// Previously we would immediately close the editor and show a
// error notification which was easy to not see.
//
// Besides, we want to preserve the users editor UI state as much
// as possible, so closing editors is never really an option.
//
// Related issues:
// - https://github.com/microsoft/vscode/issues/110062
// - https://github.com/microsoft/vscode/issues/142875
const isUnavailableResource = (<FileOperationError>error).fileOperationResult === FileOperationResult.FILE_NOT_FOUND;
const editorPlaceholder = isUnavailableResource ? UnavailableResourceErrorEditor.DESCRIPTOR : UnknownErrorEditor.DESCRIPTOR;
return {
...(await this.doOpenEditor(editorPlaceholder, editor, options, context)),
error
};
}
}
private async doOpenEditor(descriptor: IEditorPaneDescriptor, editor: EditorInput, options: IEditorOptions | undefined, context: IEditorOpenContext = Object.create(null)): Promise<IOpenEditorResult> {
// Editor pane
const pane = this.doShowEditorPane(descriptor);
// Apply input to pane
const { changed, cancelled } = await this.doSetInput(pane, editor, options, context);
// Focus unless cancelled
if (!cancelled) {
const focus = !options || !options.preserveFocus;
if (focus) {
pane.focus();
}
}
return { pane, changed, cancelled };
}
private getEditorPaneDescriptor(editor: EditorInput): IEditorPaneDescriptor {
if (editor.hasCapability(EditorInputCapabilities.RequiresTrust) && !this.workspaceTrustService.isWorkspaceTrusted()) {
// Workspace trust: if an editor signals it needs workspace trust
// but the current workspace is untrusted, we fallback to a generic
// editor descriptor to indicate this an do NOT load the registered
// editor.
return WorkspaceTrustRequiredEditor.DESCRIPTOR;
}
return assertIsDefined(this.editorPanesRegistry.getEditorPane(editor));
}
private doShowEditorPane(descriptor: IEditorPaneDescriptor): EditorPane {
// Return early if the currently active editor pane can handle the input
if (this._activeEditorPane && descriptor.describes(this._activeEditorPane)) {
return this._activeEditorPane;
}
// Hide active one first
this.doHideActiveEditorPane();
// Create editor pane
const editorPane = this.doCreateEditorPane(descriptor);
// Set editor as active
this.doSetActiveEditorPane(editorPane);
// Show editor
const container = assertIsDefined(editorPane.getContainer());
this.parent.appendChild(container);
show(container);
// Indicate to editor that it is now visible
editorPane.setVisible(true, this.groupView);
// Layout
if (this.dimension) {
editorPane.layout(this.dimension);
}
return editorPane;
}
private doCreateEditorPane(descriptor: IEditorPaneDescriptor): EditorPane {
// Instantiate editor
const editorPane = this.doInstantiateEditorPane(descriptor);
// Create editor container as needed
if (!editorPane.getContainer()) {
const editorPaneContainer = document.createElement('div');
editorPaneContainer.classList.add('editor-instance');
editorPane.create(editorPaneContainer);
}
return editorPane;
}
private doInstantiateEditorPane(descriptor: IEditorPaneDescriptor): EditorPane {
// Return early if already instantiated
const existingEditorPane = this.editorPanes.find(editorPane => descriptor.describes(editorPane));
if (existingEditorPane) {
return existingEditorPane;
}
// Otherwise instantiate new
const editorPane = this._register(descriptor.instantiate(this.instantiationService));
this.editorPanes.push(editorPane);
return editorPane;
}
private doSetActiveEditorPane(editorPane: EditorPane | null) {
this._activeEditorPane = editorPane;
// Clear out previous active editor pane listeners
this.activeEditorPaneDisposables.clear();
// Listen to editor pane changes
if (editorPane) {
this.activeEditorPaneDisposables.add(editorPane.onDidChangeSizeConstraints(e => this._onDidChangeSizeConstraints.fire(e)));
this.activeEditorPaneDisposables.add(editorPane.onDidFocus(() => this._onDidFocus.fire()));
}
// Indicate that size constraints could have changed due to new editor
this._onDidChangeSizeConstraints.fire(undefined);
}
private async doSetInput(editorPane: EditorPane, editor: EditorInput, options: IEditorOptions | undefined, context: IEditorOpenContext): Promise<{ changed: boolean; cancelled: boolean }> {
// If the input did not change, return early and only
// apply the options unless the options instruct us to
// force open it even if it is the same
const inputMatches = editorPane.input?.matches(editor);
if (inputMatches && !options?.forceReload) {
editorPane.setOptions(options);
return { changed: false, cancelled: false };
}
// Start a new editor input operation to report progress
// and to support cancellation. Any new operation that is
// started will cancel the previous one.
const operation = this.editorOperation.start(this.layoutService.isRestored() ? 800 : 3200);
let cancelled = false;
try {
// Clear the current input before setting new input
// This ensures that a slow loading input will not
// be visible for the duration of the new input to
// load (https://github.com/microsoft/vscode/issues/34697)
editorPane.clearInput();
// Set the input to the editor pane
await editorPane.setInput(editor, options, context, operation.token);
if (!operation.isCurrent()) {
cancelled = true;
}
} finally {
operation.stop();
}
return { changed: !inputMatches, cancelled };
}
private doHideActiveEditorPane(): void {
if (!this._activeEditorPane) {
return;
}
// Stop any running operation
this.editorOperation.stop();
// Indicate to editor pane before removing the editor from
// the DOM to give a chance to persist certain state that
// might depend on still being the active DOM element.
this._activeEditorPane.clearInput();
this._activeEditorPane.setVisible(false, this.groupView);
// Remove editor pane from parent
const editorPaneContainer = this._activeEditorPane.getContainer();
if (editorPaneContainer) {
this.parent.removeChild(editorPaneContainer);
hide(editorPaneContainer);
}
// Clear active editor pane
this.doSetActiveEditorPane(null);
}
closeEditor(editor: EditorInput): void {
if (this._activeEditorPane?.input && editor.matches(this._activeEditorPane.input)) {
this.doHideActiveEditorPane();
}
}
setVisible(visible: boolean): void {
this._activeEditorPane?.setVisible(visible, this.groupView);
}
layout(dimension: Dimension): void {
this.dimension = dimension;
this._activeEditorPane?.layout(dimension);
}
}
|