-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy patheditor-manager.ts
423 lines (376 loc) · 15.8 KB
/
editor-manager.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
// *****************************************************************************
// Copyright (C) 2017 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************
import { injectable, postConstruct, inject } from '@theia/core/shared/inversify';
import URI from '@theia/core/lib/common/uri';
import { RecursivePartial, Emitter, Event, MaybePromise } from '@theia/core/lib/common';
import { WidgetOpenerOptions, NavigatableWidgetOpenHandler, NavigatableWidgetOptions, Widget } from '@theia/core/lib/browser';
import { EditorWidget } from './editor-widget';
import { Range, Position, Location, TextEditor } from './editor';
import { EditorWidgetFactory } from './editor-widget-factory';
export interface WidgetId {
id: number;
uri: string;
}
export interface EditorOpenerOptions extends WidgetOpenerOptions {
selection?: RecursivePartial<Range>;
preview?: boolean;
counter?: number
}
@injectable()
export class EditorManager extends NavigatableWidgetOpenHandler<EditorWidget> {
readonly id = EditorWidgetFactory.ID;
readonly label = 'Code Editor';
protected readonly editorCounters = new Map<string, number>();
protected readonly onActiveEditorChangedEmitter = new Emitter<EditorWidget | undefined>();
/**
* Emit when the active editor is changed.
*/
readonly onActiveEditorChanged: Event<EditorWidget | undefined> = this.onActiveEditorChangedEmitter.event;
protected readonly onCurrentEditorChangedEmitter = new Emitter<EditorWidget | undefined>();
/**
* Emit when the current editor is changed.
*/
readonly onCurrentEditorChanged: Event<EditorWidget | undefined> = this.onCurrentEditorChangedEmitter.event;
@postConstruct()
protected override init(): void {
super.init();
this.shell.onDidChangeActiveWidget(() => this.updateActiveEditor());
this.shell.onDidChangeCurrentWidget(() => this.updateCurrentEditor());
this.onCreated(widget => {
widget.onDidChangeVisibility(() => {
if (widget.isVisible) {
this.addRecentlyVisible(widget);
}
this.updateCurrentEditor();
});
this.checkCounterForWidget(widget);
widget.disposed.connect(() => {
this.removeFromCounter(widget);
this.removeRecentlyVisible(widget);
this.updateCurrentEditor();
});
});
for (const widget of this.all) {
if (widget.isVisible) {
this.addRecentlyVisible(widget);
}
}
this.updateCurrentEditor();
}
override getByUri(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget | undefined> {
return this.getWidget(uri, options);
}
override getOrCreateByUri(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {
return this.getOrCreateWidget(uri, options);
}
protected override tryGetPendingWidget(uri: URI, options?: EditorOpenerOptions): MaybePromise<EditorWidget> | undefined {
const editorPromise = super.tryGetPendingWidget(uri, options);
if (editorPromise) {
// Reveal selection before attachment to manage nav stack. (https://github.com/eclipse-theia/theia/issues/8955)
if (!(editorPromise instanceof Widget)) {
editorPromise.then(editor => this.revealSelection(editor, options, uri));
} else {
this.revealSelection(editorPromise, options);
}
}
return editorPromise;
}
protected override async getWidget(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget | undefined> {
const editor = await super.getWidget(uri, options);
if (editor) {
// Reveal selection before attachment to manage nav stack. (https://github.com/eclipse-theia/theia/issues/8955)
this.revealSelection(editor, options, uri);
}
return editor;
}
protected override async getOrCreateWidget(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {
const editor = await super.getOrCreateWidget(uri, options);
// Reveal selection before attachment to manage nav stack. (https://github.com/eclipse-theia/theia/issues/8955)
this.revealSelection(editor, options, uri);
return editor;
}
protected readonly recentlyVisibleIds: string[] = [];
protected get recentlyVisible(): EditorWidget | undefined {
const id = this.recentlyVisibleIds[0];
return id && this.all.find(w => w.id === id) || undefined;
}
protected addRecentlyVisible(widget: EditorWidget): void {
this.removeRecentlyVisible(widget);
this.recentlyVisibleIds.unshift(widget.id);
}
protected removeRecentlyVisible(widget: EditorWidget): void {
const index = this.recentlyVisibleIds.indexOf(widget.id);
if (index !== -1) {
this.recentlyVisibleIds.splice(index, 1);
}
}
protected _activeEditor: EditorWidget | undefined;
/**
* The active editor.
* If there is an active editor (one that has focus), active and current are the same.
*/
get activeEditor(): EditorWidget | undefined {
return this._activeEditor;
}
protected setActiveEditor(active: EditorWidget | undefined): void {
if (this._activeEditor !== active) {
this._activeEditor = active;
this.onActiveEditorChangedEmitter.fire(this._activeEditor);
}
}
protected updateActiveEditor(): void {
const widget = this.shell.activeWidget;
if (widget instanceof EditorWidget) {
this.addRecentlyVisible(widget);
this.setActiveEditor(widget);
} else {
this.setActiveEditor(undefined);
}
}
protected _currentEditor: EditorWidget | undefined;
/**
* The most recently activated editor (which might not have the focus anymore, hence it is not active).
* If no editor has focus, e.g. when a context menu is shown, the active editor is `undefined`, but current might be the editor that was active before the menu popped up.
*/
get currentEditor(): EditorWidget | undefined {
return this._currentEditor;
}
protected setCurrentEditor(current: EditorWidget | undefined): void {
if (this._currentEditor !== current) {
this._currentEditor = current;
this.onCurrentEditorChangedEmitter.fire(this._currentEditor);
}
}
protected updateCurrentEditor(): void {
const widget = this.shell.currentWidget;
if (widget instanceof EditorWidget) {
this.setCurrentEditor(widget);
} else if (!this._currentEditor || !this._currentEditor.isVisible || this.currentEditor !== this.recentlyVisible) {
this.setCurrentEditor(this.recentlyVisible);
}
}
canHandle(uri: URI, options?: WidgetOpenerOptions): number {
return 100;
}
override open(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {
if (options?.counter === undefined) {
const insertionOptions = this.shell.getInsertionOptions(options?.widgetOptions);
// Definitely creating a new tabbar - no widget can match.
if (insertionOptions.addOptions.mode?.startsWith('split')) {
return super.open(uri, { counter: this.createCounterForUri(uri), ...options });
}
// Check the target tabbar for an existing widget.
const tabbar = insertionOptions.addOptions.ref && this.shell.getTabBarFor(insertionOptions.addOptions.ref);
if (tabbar) {
const currentUri = uri.toString();
for (const title of tabbar.titles) {
if (title.owner instanceof EditorWidget) {
const { uri: otherWidgetUri, id } = this.extractIdFromWidget(title.owner);
if (otherWidgetUri === currentUri) {
return super.open(uri, { counter: id, ...options });
}
}
}
}
// Open a new widget.
return super.open(uri, { counter: this.createCounterForUri(uri), ...options });
}
return super.open(uri, options);
}
/**
* Opens an editor to the side of the current editor. Defaults to opening to the right.
* To modify direction, pass options with `{widgetOptions: {mode: ...}}`
*/
openToSide(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {
const counter = this.createCounterForUri(uri);
const splitOptions: EditorOpenerOptions = { widgetOptions: { mode: 'split-right' }, ...options, counter };
return this.open(uri, splitOptions);
}
protected revealSelection(widget: EditorWidget, input?: EditorOpenerOptions, uri?: URI): void {
let inputSelection = input?.selection;
if (!inputSelection && uri) {
const match = /^L?(\d+)(?:,(\d+))?/.exec(uri.fragment);
if (match) {
// support file:///some/file.js#73,84
// support file:///some/file.js#L73
inputSelection = {
start: {
line: parseInt(match[1]) - 1,
character: match[2] ? parseInt(match[2]) - 1 : 0
}
};
}
}
if (inputSelection) {
const editor = widget.editor;
const selection = this.getSelection(widget, inputSelection);
if (Position.is(selection)) {
editor.cursor = selection;
editor.revealPosition(selection);
} else if (Range.is(selection)) {
editor.cursor = selection.end;
editor.selection = selection;
editor.revealRange(selection);
}
}
}
protected getSelection(widget: EditorWidget, selection: RecursivePartial<Range>): Range | Position | undefined {
const { start, end } = selection;
const line = start && start.line !== undefined && start.line >= 0 ? start.line : undefined;
if (line === undefined) {
return undefined;
}
const character = start && start.character !== undefined && start.character >= 0 ? start.character : widget.editor.document.getLineMaxColumn(line);
const endLine = end && end.line !== undefined && end.line >= 0 ? end.line : undefined;
if (endLine === undefined) {
return { line, character };
}
const endCharacter = end && end.character !== undefined && end.character >= 0 ? end.character : widget.editor.document.getLineMaxColumn(endLine);
return {
start: { line, character },
end: { line: endLine, character: endCharacter }
};
}
protected removeFromCounter(widget: EditorWidget): void {
const { id, uri } = this.extractIdFromWidget(widget);
if (uri && !Number.isNaN(id)) {
let max = -Infinity;
this.all.forEach(editor => {
const candidateID = this.extractIdFromWidget(editor);
if ((candidateID.uri === uri) && (candidateID.id > max)) {
max = candidateID.id!;
}
});
if (max > -Infinity) {
this.editorCounters.set(uri, max);
} else {
this.editorCounters.delete(uri);
}
}
}
protected extractIdFromWidget(widget: EditorWidget): WidgetId {
const uri = widget.editor.uri.toString();
const id = Number(widget.id.slice(widget.id.lastIndexOf(':') + 1));
return { id, uri };
}
protected checkCounterForWidget(widget: EditorWidget): void {
const { id, uri } = this.extractIdFromWidget(widget);
const numericalId = Number(id);
if (uri && !Number.isNaN(numericalId)) {
const highestKnownId = this.editorCounters.get(uri) ?? -Infinity;
if (numericalId > highestKnownId) {
this.editorCounters.set(uri, numericalId);
}
}
}
protected createCounterForUri(uri: URI): number {
const identifier = uri.toString();
const next = (this.editorCounters.get(identifier) ?? 0) + 1;
return next;
}
protected getCounterForUri(uri: URI): number | undefined {
const idWithoutCounter = EditorWidgetFactory.createID(uri);
const counterOfMostRecentlyVisibleEditor = this.recentlyVisibleIds.find(id => id.startsWith(idWithoutCounter))?.slice(idWithoutCounter.length + 1);
return counterOfMostRecentlyVisibleEditor === undefined ? undefined : parseInt(counterOfMostRecentlyVisibleEditor);
}
protected getOrCreateCounterForUri(uri: URI): number {
return this.getCounterForUri(uri) ?? this.createCounterForUri(uri);
}
protected override createWidgetOptions(uri: URI, options?: EditorOpenerOptions): NavigatableWidgetOptions {
const navigatableOptions = super.createWidgetOptions(uri, options);
navigatableOptions.counter = options?.counter ?? this.getOrCreateCounterForUri(uri);
return navigatableOptions;
}
}
/**
* Provides direct access to the underlying text editor.
*/
@injectable()
export abstract class EditorAccess {
@inject(EditorManager)
protected readonly editorManager: EditorManager;
/**
* The URI of the underlying document from the editor.
*/
get uri(): string | undefined {
const editor = this.editor;
if (editor) {
return editor.uri.toString();
}
return undefined;
}
/**
* The selection location from the text editor.
*/
get selection(): Location | undefined {
const editor = this.editor;
if (editor) {
const uri = editor.uri.toString();
const range = editor.selection;
return {
range,
uri
};
}
return undefined;
}
/**
* The unique identifier of the language the current editor belongs to.
*/
get languageId(): string | undefined {
const editor = this.editor;
if (editor) {
return editor.document.languageId;
}
return undefined;
}
/**
* The text editor.
*/
get editor(): TextEditor | undefined {
const editorWidget = this.editorWidget();
if (editorWidget) {
return editorWidget.editor;
}
return undefined;
}
/**
* The editor widget, or `undefined` if not applicable.
*/
protected abstract editorWidget(): EditorWidget | undefined;
}
/**
* Provides direct access to the currently active text editor.
*/
@injectable()
export class CurrentEditorAccess extends EditorAccess {
protected editorWidget(): EditorWidget | undefined {
return this.editorManager.currentEditor;
}
}
/**
* Provides access to the active text editor.
*/
@injectable()
export class ActiveEditorAccess extends EditorAccess {
protected editorWidget(): EditorWidget | undefined {
return this.editorManager.activeEditor;
}
}
export namespace EditorAccess {
export const CURRENT = 'current-editor-access';
export const ACTIVE = 'active-editor-access';
}