@theia/editor 1.53.0-next.55 → 1.53.0-next.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +30 -30
  2. package/lib/browser/editor-manager.d.ts.map +1 -1
  3. package/lib/browser/editor-manager.js +2 -4
  4. package/lib/browser/editor-manager.js.map +1 -1
  5. package/lib/browser/navigation/navigation-location-service.js +3 -3
  6. package/package.json +4 -4
  7. package/src/browser/decorations/editor-decoration-style.ts +41 -41
  8. package/src/browser/decorations/editor-decoration.ts +127 -127
  9. package/src/browser/decorations/editor-decorator.ts +36 -36
  10. package/src/browser/decorations/index.ts +19 -19
  11. package/src/browser/diff-navigator.ts +27 -27
  12. package/src/browser/editor-command.ts +393 -393
  13. package/src/browser/editor-contribution.ts +185 -185
  14. package/src/browser/editor-frontend-module.ts +90 -90
  15. package/src/browser/editor-generated-preference-schema.ts +2956 -2956
  16. package/src/browser/editor-keybinding.ts +55 -55
  17. package/src/browser/editor-language-quick-pick-service.ts +68 -68
  18. package/src/browser/editor-linenumber-contribution.ts +88 -88
  19. package/src/browser/editor-manager.ts +460 -462
  20. package/src/browser/editor-menu.ts +224 -224
  21. package/src/browser/editor-navigation-contribution.ts +343 -343
  22. package/src/browser/editor-preferences.ts +226 -226
  23. package/src/browser/editor-variable-contribution.ts +62 -62
  24. package/src/browser/editor-widget-factory.ts +82 -82
  25. package/src/browser/editor-widget.ts +139 -139
  26. package/src/browser/editor.ts +366 -366
  27. package/src/browser/index.ts +26 -26
  28. package/src/browser/language-status/editor-language-status-service.ts +271 -271
  29. package/src/browser/language-status/editor-language-status.css +101 -101
  30. package/src/browser/navigation/navigation-location-service.spec.ts +245 -245
  31. package/src/browser/navigation/navigation-location-service.ts +284 -284
  32. package/src/browser/navigation/navigation-location-similarity.spec.ts +46 -46
  33. package/src/browser/navigation/navigation-location-similarity.ts +58 -58
  34. package/src/browser/navigation/navigation-location-updater.spec.ts +197 -197
  35. package/src/browser/navigation/navigation-location-updater.ts +220 -220
  36. package/src/browser/navigation/navigation-location.ts +418 -418
  37. package/src/browser/navigation/test/mock-navigation-location-updater.ts +41 -41
  38. package/src/browser/quick-editor-service.ts +94 -94
  39. package/src/browser/style/index.css +19 -19
  40. package/src/browser/undo-redo-service.ts +120 -120
  41. package/src/common/language-selector.ts +104 -104
  42. package/src/package.spec.ts +28 -28
@@ -1,462 +1,460 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2017 TypeFox and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- import { injectable, postConstruct, inject } from '@theia/core/shared/inversify';
18
- import URI from '@theia/core/lib/common/uri';
19
- import { RecursivePartial, Emitter, Event, MaybePromise, CommandService, nls } from '@theia/core/lib/common';
20
- import {
21
- WidgetOpenerOptions, NavigatableWidgetOpenHandler, NavigatableWidgetOptions, Widget, PreferenceService, CommonCommands, OpenWithService, getDefaultHandler,
22
- defaultHandlerPriority
23
- } from '@theia/core/lib/browser';
24
- import { EditorWidget } from './editor-widget';
25
- import { Range, Position, Location, TextEditor } from './editor';
26
- import { EditorWidgetFactory } from './editor-widget-factory';
27
-
28
- export interface WidgetId {
29
- id: number;
30
- uri: string;
31
- }
32
-
33
- export interface EditorOpenerOptions extends WidgetOpenerOptions {
34
- selection?: RecursivePartial<Range>;
35
- preview?: boolean;
36
- counter?: number
37
- }
38
-
39
- @injectable()
40
- export class EditorManager extends NavigatableWidgetOpenHandler<EditorWidget> {
41
-
42
- readonly id = EditorWidgetFactory.ID;
43
-
44
- readonly label = nls.localizeByDefault('Text Editor');
45
-
46
- protected readonly editorCounters = new Map<string, number>();
47
-
48
- protected readonly onActiveEditorChangedEmitter = new Emitter<EditorWidget | undefined>();
49
- /**
50
- * Emit when the active editor is changed.
51
- */
52
- readonly onActiveEditorChanged: Event<EditorWidget | undefined> = this.onActiveEditorChangedEmitter.event;
53
-
54
- protected readonly onCurrentEditorChangedEmitter = new Emitter<EditorWidget | undefined>();
55
- /**
56
- * Emit when the current editor is changed.
57
- */
58
- readonly onCurrentEditorChanged: Event<EditorWidget | undefined> = this.onCurrentEditorChangedEmitter.event;
59
-
60
- @inject(CommandService) protected readonly commands: CommandService;
61
- @inject(PreferenceService) protected readonly preferenceService: PreferenceService;
62
- @inject(OpenWithService) protected readonly openWithService: OpenWithService;
63
-
64
- @postConstruct()
65
- protected override init(): void {
66
- super.init();
67
- this.shell.onDidChangeActiveWidget(() => this.updateActiveEditor());
68
- this.shell.onDidChangeCurrentWidget(() => this.updateCurrentEditor());
69
- this.shell.onDidDoubleClickMainArea(() =>
70
- this.commands.executeCommand(CommonCommands.NEW_UNTITLED_TEXT_FILE.id)
71
- );
72
- this.onCreated(widget => {
73
- widget.onDidChangeVisibility(() => {
74
- if (widget.isVisible) {
75
- this.addRecentlyVisible(widget);
76
- }
77
- this.updateCurrentEditor();
78
- });
79
- this.checkCounterForWidget(widget);
80
- widget.disposed.connect(() => {
81
- this.removeFromCounter(widget);
82
- this.removeRecentlyVisible(widget);
83
- this.updateCurrentEditor();
84
- });
85
- });
86
- for (const widget of this.all) {
87
- if (widget.isVisible) {
88
- this.addRecentlyVisible(widget);
89
- }
90
- }
91
- this.openWithService.registerHandler({
92
- id: 'default',
93
- label: this.label,
94
- providerName: nls.localizeByDefault('Built-in'),
95
- canHandle: () => 100,
96
- // Higher priority than any other handler
97
- // so that the text editor always appears first in the quick pick
98
- getOrder: () => 10000,
99
- open: uri => this.open(uri)
100
- });
101
- this.updateCurrentEditor();
102
- }
103
-
104
- override getByUri(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget | undefined> {
105
- return this.getWidget(uri, options);
106
- }
107
-
108
- override getOrCreateByUri(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {
109
- return this.getOrCreateWidget(uri, options);
110
- }
111
-
112
- protected override tryGetPendingWidget(uri: URI, options?: EditorOpenerOptions): MaybePromise<EditorWidget> | undefined {
113
- const editorPromise = super.tryGetPendingWidget(uri, options);
114
- if (editorPromise) {
115
- // Reveal selection before attachment to manage nav stack. (https://github.com/eclipse-theia/theia/issues/8955)
116
- if (!(editorPromise instanceof Widget)) {
117
- editorPromise.then(editor => this.revealSelection(editor, options, uri));
118
- } else {
119
- this.revealSelection(editorPromise, options, uri);
120
- }
121
- }
122
- return editorPromise;
123
- }
124
-
125
- protected override async getWidget(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget | undefined> {
126
- const editor = await super.getWidget(uri, options);
127
- if (editor) {
128
- // Reveal selection before attachment to manage nav stack. (https://github.com/eclipse-theia/theia/issues/8955)
129
- this.revealSelection(editor, options, uri);
130
- }
131
- return editor;
132
- }
133
-
134
- protected override async getOrCreateWidget(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {
135
- const editor = await super.getOrCreateWidget(uri, options);
136
- // Reveal selection before attachment to manage nav stack. (https://github.com/eclipse-theia/theia/issues/8955)
137
- this.revealSelection(editor, options, uri);
138
- return editor;
139
- }
140
-
141
- protected readonly recentlyVisibleIds: string[] = [];
142
- protected get recentlyVisible(): EditorWidget | undefined {
143
- const id = this.recentlyVisibleIds[0];
144
- return id && this.all.find(w => w.id === id) || undefined;
145
- }
146
- protected addRecentlyVisible(widget: EditorWidget): void {
147
- this.removeRecentlyVisible(widget);
148
- this.recentlyVisibleIds.unshift(widget.id);
149
- }
150
- protected removeRecentlyVisible(widget: EditorWidget): void {
151
- const index = this.recentlyVisibleIds.indexOf(widget.id);
152
- if (index !== -1) {
153
- this.recentlyVisibleIds.splice(index, 1);
154
- }
155
- }
156
-
157
- protected _activeEditor: EditorWidget | undefined;
158
- /**
159
- * The active editor.
160
- * If there is an active editor (one that has focus), active and current are the same.
161
- */
162
- get activeEditor(): EditorWidget | undefined {
163
- return this._activeEditor;
164
- }
165
- protected setActiveEditor(active: EditorWidget | undefined): void {
166
- if (this._activeEditor !== active) {
167
- this._activeEditor = active;
168
- this.onActiveEditorChangedEmitter.fire(this._activeEditor);
169
- }
170
- }
171
- protected updateActiveEditor(): void {
172
- const widget = this.shell.activeWidget;
173
- if (widget instanceof EditorWidget) {
174
- this.addRecentlyVisible(widget);
175
- this.setActiveEditor(widget);
176
- } else {
177
- this.setActiveEditor(undefined);
178
- }
179
- }
180
-
181
- protected _currentEditor: EditorWidget | undefined;
182
- /**
183
- * The most recently activated editor (which might not have the focus anymore, hence it is not active).
184
- * 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.
185
- */
186
- get currentEditor(): EditorWidget | undefined {
187
- return this._currentEditor;
188
- }
189
- protected setCurrentEditor(current: EditorWidget | undefined): void {
190
- if (this._currentEditor !== current) {
191
- this._currentEditor = current;
192
- this.onCurrentEditorChangedEmitter.fire(this._currentEditor);
193
- }
194
- }
195
- protected updateCurrentEditor(): void {
196
- const widget = this.shell.currentWidget;
197
- if (widget instanceof EditorWidget) {
198
- this.setCurrentEditor(widget);
199
- } else if (!this._currentEditor || !this._currentEditor.isVisible || this.currentEditor !== this.recentlyVisible) {
200
- this.setCurrentEditor(this.recentlyVisible);
201
- }
202
- }
203
-
204
- canHandle(uri: URI, options?: WidgetOpenerOptions): number {
205
- if (getDefaultHandler(uri, this.preferenceService) === 'default') {
206
- return defaultHandlerPriority;
207
- }
208
- return 100;
209
- }
210
-
211
- override open(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {
212
- if (options?.counter === undefined) {
213
- const insertionOptions = this.shell.getInsertionOptions(options?.widgetOptions);
214
- // Definitely creating a new tabbar - no widget can match.
215
- if (insertionOptions.addOptions.mode?.startsWith('split')) {
216
- return super.open(uri, { counter: this.createCounterForUri(uri), ...options });
217
- }
218
- // Check the target tabbar for an existing widget.
219
- const tabbar = insertionOptions.addOptions.ref && this.shell.getTabBarFor(insertionOptions.addOptions.ref);
220
- if (tabbar) {
221
- const currentUri = uri.toString();
222
- for (const title of tabbar.titles) {
223
- if (title.owner instanceof EditorWidget) {
224
- const { uri: otherWidgetUri, id } = this.extractIdFromWidget(title.owner);
225
- if (otherWidgetUri === currentUri) {
226
- return super.open(uri, { counter: id, ...options });
227
- }
228
- }
229
- }
230
- }
231
- // If the user has opted to prefer to open an existing editor even if it's on a different tab, check if we have anything about the URI.
232
- if (this.preferenceService.get('workbench.editor.revealIfOpen', false)) {
233
- const counter = this.getCounterForUri(uri);
234
- if (counter !== undefined) {
235
- return super.open(uri, { counter, ...options });
236
- }
237
- }
238
- // Open a new widget.
239
- return super.open(uri, { counter: this.createCounterForUri(uri), ...options });
240
- }
241
-
242
- return super.open(uri, options);
243
- }
244
-
245
- /**
246
- * Opens an editor to the side of the current editor. Defaults to opening to the right.
247
- * To modify direction, pass options with `{widgetOptions: {mode: ...}}`
248
- */
249
- openToSide(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {
250
- const counter = this.createCounterForUri(uri);
251
- const splitOptions: EditorOpenerOptions = { widgetOptions: { mode: 'split-right' }, ...options, counter };
252
- return this.open(uri, splitOptions);
253
- }
254
-
255
- protected revealSelection(widget: EditorWidget, input?: EditorOpenerOptions, uri?: URI): void {
256
- let inputSelection = input?.selection;
257
- if (!inputSelection && uri) {
258
- const match = /^L?(\d+)(?:,(\d+))?/.exec(uri.fragment);
259
- if (match) {
260
- // support file:///some/file.js#73,84
261
- // support file:///some/file.js#L73
262
- inputSelection = {
263
- start: {
264
- line: parseInt(match[1]) - 1,
265
- character: match[2] ? parseInt(match[2]) - 1 : 0
266
- }
267
- };
268
- }
269
- }
270
- if (inputSelection) {
271
- const editor = widget.editor;
272
- const selection = this.getSelection(widget, inputSelection);
273
- if (Position.is(selection)) {
274
- editor.cursor = selection;
275
- editor.revealPosition(selection);
276
- } else if (Range.is(selection)) {
277
- editor.cursor = selection.end;
278
- editor.selection = {
279
- ...selection,
280
- direction: 'ltr'
281
- };
282
- editor.revealRange(selection);
283
- }
284
- }
285
- }
286
-
287
- protected getSelection(widget: EditorWidget, selection: RecursivePartial<Range>): Range | Position | undefined {
288
- const { start, end } = selection;
289
- if (Position.is(start)) {
290
- if (Position.is(end)) {
291
- return widget.editor.document.toValidRange({ start, end });
292
- }
293
- return widget.editor.document.toValidPosition(start);
294
- }
295
- const line = start && start.line !== undefined && start.line >= 0 ? start.line : undefined;
296
- if (line === undefined) {
297
- return undefined;
298
- }
299
- const character = start && start.character !== undefined && start.character >= 0 ? start.character : widget.editor.document.getLineMaxColumn(line);
300
- const endLine = end && end.line !== undefined && end.line >= 0 ? end.line : undefined;
301
- if (endLine === undefined) {
302
- return { line, character };
303
- }
304
- const endCharacter = end && end.character !== undefined && end.character >= 0 ? end.character : widget.editor.document.getLineMaxColumn(endLine);
305
- return {
306
- start: { line, character },
307
- end: { line: endLine, character: endCharacter }
308
- };
309
- }
310
-
311
- protected removeFromCounter(widget: EditorWidget): void {
312
- const { id, uri } = this.extractIdFromWidget(widget);
313
- if (uri && !Number.isNaN(id)) {
314
- let max = -Infinity;
315
- this.all.forEach(editor => {
316
- const candidateID = this.extractIdFromWidget(editor);
317
- if ((candidateID.uri === uri) && (candidateID.id > max)) {
318
- max = candidateID.id!;
319
- }
320
- });
321
-
322
- if (max > -Infinity) {
323
- this.editorCounters.set(uri, max);
324
- } else {
325
- this.editorCounters.delete(uri);
326
- }
327
- }
328
- }
329
-
330
- protected extractIdFromWidget(widget: EditorWidget): WidgetId {
331
- const uri = widget.editor.uri.toString();
332
- const id = Number(widget.id.slice(widget.id.lastIndexOf(':') + 1));
333
- return { id, uri };
334
- }
335
-
336
- protected checkCounterForWidget(widget: EditorWidget): void {
337
- const { id, uri } = this.extractIdFromWidget(widget);
338
- const numericalId = Number(id);
339
- if (uri && !Number.isNaN(numericalId)) {
340
- const highestKnownId = this.editorCounters.get(uri) ?? -Infinity;
341
- if (numericalId > highestKnownId) {
342
- this.editorCounters.set(uri, numericalId);
343
- }
344
- }
345
- }
346
-
347
- protected createCounterForUri(uri: URI): number {
348
- const identifier = uri.toString();
349
- const next = (this.editorCounters.get(identifier) ?? 0) + 1;
350
- return next;
351
- }
352
-
353
- protected getCounterForUri(uri: URI): number | undefined {
354
- const idWithoutCounter = EditorWidgetFactory.createID(uri);
355
- const counterOfMostRecentlyVisibleEditor = this.recentlyVisibleIds.find(id => id.startsWith(idWithoutCounter))?.slice(idWithoutCounter.length + 1);
356
- return counterOfMostRecentlyVisibleEditor === undefined ? undefined : parseInt(counterOfMostRecentlyVisibleEditor);
357
- }
358
-
359
- protected getOrCreateCounterForUri(uri: URI): number {
360
- return this.getCounterForUri(uri) ?? this.createCounterForUri(uri);
361
- }
362
-
363
- protected override createWidgetOptions(uri: URI, options?: EditorOpenerOptions): NavigatableWidgetOptions {
364
- const navigatableOptions = super.createWidgetOptions(uri, options);
365
- navigatableOptions.counter = options?.counter ?? this.getOrCreateCounterForUri(uri);
366
- return navigatableOptions;
367
- }
368
- }
369
-
370
- /**
371
- * Provides direct access to the underlying text editor.
372
- */
373
- @injectable()
374
- export abstract class EditorAccess {
375
-
376
- @inject(EditorManager)
377
- protected readonly editorManager: EditorManager;
378
-
379
- /**
380
- * The URI of the underlying document from the editor.
381
- */
382
- get uri(): string | undefined {
383
- const editor = this.editor;
384
- if (editor) {
385
- return editor.uri.toString();
386
- }
387
- return undefined;
388
- }
389
-
390
- /**
391
- * The selection location from the text editor.
392
- */
393
- get selection(): Location | undefined {
394
- const editor = this.editor;
395
- if (editor) {
396
- const uri = editor.uri.toString();
397
- const range = editor.selection;
398
- return {
399
- range,
400
- uri
401
- };
402
- }
403
- return undefined;
404
- }
405
-
406
- /**
407
- * The unique identifier of the language the current editor belongs to.
408
- */
409
- get languageId(): string | undefined {
410
- const editor = this.editor;
411
- if (editor) {
412
- return editor.document.languageId;
413
- }
414
- return undefined;
415
- }
416
-
417
- /**
418
- * The text editor.
419
- */
420
- get editor(): TextEditor | undefined {
421
- const editorWidget = this.editorWidget();
422
- if (editorWidget) {
423
- return editorWidget.editor;
424
- }
425
- return undefined;
426
- }
427
-
428
- /**
429
- * The editor widget, or `undefined` if not applicable.
430
- */
431
- protected abstract editorWidget(): EditorWidget | undefined;
432
-
433
- }
434
-
435
- /**
436
- * Provides direct access to the currently active text editor.
437
- */
438
- @injectable()
439
- export class CurrentEditorAccess extends EditorAccess {
440
-
441
- protected editorWidget(): EditorWidget | undefined {
442
- return this.editorManager.currentEditor;
443
- }
444
-
445
- }
446
-
447
- /**
448
- * Provides access to the active text editor.
449
- */
450
- @injectable()
451
- export class ActiveEditorAccess extends EditorAccess {
452
-
453
- protected editorWidget(): EditorWidget | undefined {
454
- return this.editorManager.activeEditor;
455
- }
456
-
457
- }
458
-
459
- export namespace EditorAccess {
460
- export const CURRENT = 'current-editor-access';
461
- export const ACTIVE = 'active-editor-access';
462
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2017 TypeFox and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import { injectable, postConstruct, inject } from '@theia/core/shared/inversify';
18
+ import URI from '@theia/core/lib/common/uri';
19
+ import { RecursivePartial, Emitter, Event, MaybePromise, CommandService, nls } from '@theia/core/lib/common';
20
+ import {
21
+ WidgetOpenerOptions, NavigatableWidgetOpenHandler, NavigatableWidgetOptions, Widget, PreferenceService, CommonCommands, OpenWithService, getDefaultHandler,
22
+ defaultHandlerPriority
23
+ } from '@theia/core/lib/browser';
24
+ import { EditorWidget } from './editor-widget';
25
+ import { Range, Position, Location, TextEditor } from './editor';
26
+ import { EditorWidgetFactory } from './editor-widget-factory';
27
+
28
+ export interface WidgetId {
29
+ id: number;
30
+ uri: string;
31
+ }
32
+
33
+ export interface EditorOpenerOptions extends WidgetOpenerOptions {
34
+ selection?: RecursivePartial<Range>;
35
+ preview?: boolean;
36
+ counter?: number
37
+ }
38
+
39
+ @injectable()
40
+ export class EditorManager extends NavigatableWidgetOpenHandler<EditorWidget> {
41
+
42
+ readonly id = EditorWidgetFactory.ID;
43
+
44
+ readonly label = nls.localizeByDefault('Text Editor');
45
+
46
+ protected readonly editorCounters = new Map<string, number>();
47
+
48
+ protected readonly onActiveEditorChangedEmitter = new Emitter<EditorWidget | undefined>();
49
+ /**
50
+ * Emit when the active editor is changed.
51
+ */
52
+ readonly onActiveEditorChanged: Event<EditorWidget | undefined> = this.onActiveEditorChangedEmitter.event;
53
+
54
+ protected readonly onCurrentEditorChangedEmitter = new Emitter<EditorWidget | undefined>();
55
+ /**
56
+ * Emit when the current editor is changed.
57
+ */
58
+ readonly onCurrentEditorChanged: Event<EditorWidget | undefined> = this.onCurrentEditorChangedEmitter.event;
59
+
60
+ @inject(CommandService) protected readonly commands: CommandService;
61
+ @inject(PreferenceService) protected readonly preferenceService: PreferenceService;
62
+ @inject(OpenWithService) protected readonly openWithService: OpenWithService;
63
+
64
+ @postConstruct()
65
+ protected override init(): void {
66
+ super.init();
67
+ this.shell.onDidChangeActiveWidget(() => this.updateActiveEditor());
68
+ this.shell.onDidChangeCurrentWidget(() => this.updateCurrentEditor());
69
+ this.shell.onDidDoubleClickMainArea(() =>
70
+ this.commands.executeCommand(CommonCommands.NEW_UNTITLED_TEXT_FILE.id)
71
+ );
72
+ this.onCreated(widget => {
73
+ widget.onDidChangeVisibility(() => {
74
+ if (widget.isVisible) {
75
+ this.addRecentlyVisible(widget);
76
+ }
77
+ this.updateCurrentEditor();
78
+ });
79
+ this.checkCounterForWidget(widget);
80
+ widget.disposed.connect(() => {
81
+ this.removeFromCounter(widget);
82
+ this.removeRecentlyVisible(widget);
83
+ this.updateCurrentEditor();
84
+ });
85
+ });
86
+ for (const widget of this.all) {
87
+ if (widget.isVisible) {
88
+ this.addRecentlyVisible(widget);
89
+ }
90
+ }
91
+ this.openWithService.registerHandler({
92
+ id: 'default',
93
+ label: this.label,
94
+ providerName: nls.localizeByDefault('Built-in'),
95
+ canHandle: () => 100,
96
+ // Higher priority than any other handler
97
+ // so that the text editor always appears first in the quick pick
98
+ getOrder: () => 10000,
99
+ open: uri => this.open(uri)
100
+ });
101
+ this.updateCurrentEditor();
102
+ }
103
+
104
+ override getByUri(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget | undefined> {
105
+ return this.getWidget(uri, options);
106
+ }
107
+
108
+ override getOrCreateByUri(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {
109
+ return this.getOrCreateWidget(uri, options);
110
+ }
111
+
112
+ protected override tryGetPendingWidget(uri: URI, options?: EditorOpenerOptions): MaybePromise<EditorWidget> | undefined {
113
+ const editorPromise = super.tryGetPendingWidget(uri, options);
114
+ if (editorPromise) {
115
+ // Reveal selection before attachment to manage nav stack. (https://github.com/eclipse-theia/theia/issues/8955)
116
+ if (!(editorPromise instanceof Widget)) {
117
+ editorPromise.then(editor => this.revealSelection(editor, options, uri));
118
+ } else {
119
+ this.revealSelection(editorPromise, options, uri);
120
+ }
121
+ }
122
+ return editorPromise;
123
+ }
124
+
125
+ protected override async getWidget(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget | undefined> {
126
+ const editor = await super.getWidget(uri, options);
127
+ if (editor) {
128
+ // Reveal selection before attachment to manage nav stack. (https://github.com/eclipse-theia/theia/issues/8955)
129
+ this.revealSelection(editor, options, uri);
130
+ }
131
+ return editor;
132
+ }
133
+
134
+ protected override async getOrCreateWidget(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {
135
+ const editor = await super.getOrCreateWidget(uri, options);
136
+ // Reveal selection before attachment to manage nav stack. (https://github.com/eclipse-theia/theia/issues/8955)
137
+ this.revealSelection(editor, options, uri);
138
+ return editor;
139
+ }
140
+
141
+ protected readonly recentlyVisibleIds: string[] = [];
142
+ protected get recentlyVisible(): EditorWidget | undefined {
143
+ const id = this.recentlyVisibleIds[0];
144
+ return id && this.all.find(w => w.id === id) || undefined;
145
+ }
146
+ protected addRecentlyVisible(widget: EditorWidget): void {
147
+ this.removeRecentlyVisible(widget);
148
+ this.recentlyVisibleIds.unshift(widget.id);
149
+ }
150
+ protected removeRecentlyVisible(widget: EditorWidget): void {
151
+ const index = this.recentlyVisibleIds.indexOf(widget.id);
152
+ if (index !== -1) {
153
+ this.recentlyVisibleIds.splice(index, 1);
154
+ }
155
+ }
156
+
157
+ protected _activeEditor: EditorWidget | undefined;
158
+ /**
159
+ * The active editor.
160
+ * If there is an active editor (one that has focus), active and current are the same.
161
+ */
162
+ get activeEditor(): EditorWidget | undefined {
163
+ return this._activeEditor;
164
+ }
165
+ protected setActiveEditor(active: EditorWidget | undefined): void {
166
+ if (this._activeEditor !== active) {
167
+ this._activeEditor = active;
168
+ this.onActiveEditorChangedEmitter.fire(this._activeEditor);
169
+ }
170
+ }
171
+ protected updateActiveEditor(): void {
172
+ const widget = this.shell.activeWidget;
173
+ if (widget instanceof EditorWidget) {
174
+ this.addRecentlyVisible(widget);
175
+ this.setActiveEditor(widget);
176
+ } else {
177
+ this.setActiveEditor(undefined);
178
+ }
179
+ }
180
+
181
+ protected _currentEditor: EditorWidget | undefined;
182
+ /**
183
+ * The most recently activated editor (which might not have the focus anymore, hence it is not active).
184
+ * 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.
185
+ */
186
+ get currentEditor(): EditorWidget | undefined {
187
+ return this._currentEditor;
188
+ }
189
+ protected setCurrentEditor(current: EditorWidget | undefined): void {
190
+ this._currentEditor = current;
191
+ this.onCurrentEditorChangedEmitter.fire(this._currentEditor);
192
+ }
193
+ protected updateCurrentEditor(): void {
194
+ const widget = this.shell.currentWidget;
195
+ if (widget instanceof EditorWidget) {
196
+ this.setCurrentEditor(widget);
197
+ } else if (!this._currentEditor || !this._currentEditor.isVisible || this.currentEditor !== this.recentlyVisible) {
198
+ this.setCurrentEditor(this.recentlyVisible);
199
+ }
200
+ }
201
+
202
+ canHandle(uri: URI, options?: WidgetOpenerOptions): number {
203
+ if (getDefaultHandler(uri, this.preferenceService) === 'default') {
204
+ return defaultHandlerPriority;
205
+ }
206
+ return 100;
207
+ }
208
+
209
+ override open(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {
210
+ if (options?.counter === undefined) {
211
+ const insertionOptions = this.shell.getInsertionOptions(options?.widgetOptions);
212
+ // Definitely creating a new tabbar - no widget can match.
213
+ if (insertionOptions.addOptions.mode?.startsWith('split')) {
214
+ return super.open(uri, { counter: this.createCounterForUri(uri), ...options });
215
+ }
216
+ // Check the target tabbar for an existing widget.
217
+ const tabbar = insertionOptions.addOptions.ref && this.shell.getTabBarFor(insertionOptions.addOptions.ref);
218
+ if (tabbar) {
219
+ const currentUri = uri.toString();
220
+ for (const title of tabbar.titles) {
221
+ if (title.owner instanceof EditorWidget) {
222
+ const { uri: otherWidgetUri, id } = this.extractIdFromWidget(title.owner);
223
+ if (otherWidgetUri === currentUri) {
224
+ return super.open(uri, { counter: id, ...options });
225
+ }
226
+ }
227
+ }
228
+ }
229
+ // If the user has opted to prefer to open an existing editor even if it's on a different tab, check if we have anything about the URI.
230
+ if (this.preferenceService.get('workbench.editor.revealIfOpen', false)) {
231
+ const counter = this.getCounterForUri(uri);
232
+ if (counter !== undefined) {
233
+ return super.open(uri, { counter, ...options });
234
+ }
235
+ }
236
+ // Open a new widget.
237
+ return super.open(uri, { counter: this.createCounterForUri(uri), ...options });
238
+ }
239
+
240
+ return super.open(uri, options);
241
+ }
242
+
243
+ /**
244
+ * Opens an editor to the side of the current editor. Defaults to opening to the right.
245
+ * To modify direction, pass options with `{widgetOptions: {mode: ...}}`
246
+ */
247
+ openToSide(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {
248
+ const counter = this.createCounterForUri(uri);
249
+ const splitOptions: EditorOpenerOptions = { widgetOptions: { mode: 'split-right' }, ...options, counter };
250
+ return this.open(uri, splitOptions);
251
+ }
252
+
253
+ protected revealSelection(widget: EditorWidget, input?: EditorOpenerOptions, uri?: URI): void {
254
+ let inputSelection = input?.selection;
255
+ if (!inputSelection && uri) {
256
+ const match = /^L?(\d+)(?:,(\d+))?/.exec(uri.fragment);
257
+ if (match) {
258
+ // support file:///some/file.js#73,84
259
+ // support file:///some/file.js#L73
260
+ inputSelection = {
261
+ start: {
262
+ line: parseInt(match[1]) - 1,
263
+ character: match[2] ? parseInt(match[2]) - 1 : 0
264
+ }
265
+ };
266
+ }
267
+ }
268
+ if (inputSelection) {
269
+ const editor = widget.editor;
270
+ const selection = this.getSelection(widget, inputSelection);
271
+ if (Position.is(selection)) {
272
+ editor.cursor = selection;
273
+ editor.revealPosition(selection);
274
+ } else if (Range.is(selection)) {
275
+ editor.cursor = selection.end;
276
+ editor.selection = {
277
+ ...selection,
278
+ direction: 'ltr'
279
+ };
280
+ editor.revealRange(selection);
281
+ }
282
+ }
283
+ }
284
+
285
+ protected getSelection(widget: EditorWidget, selection: RecursivePartial<Range>): Range | Position | undefined {
286
+ const { start, end } = selection;
287
+ if (Position.is(start)) {
288
+ if (Position.is(end)) {
289
+ return widget.editor.document.toValidRange({ start, end });
290
+ }
291
+ return widget.editor.document.toValidPosition(start);
292
+ }
293
+ const line = start && start.line !== undefined && start.line >= 0 ? start.line : undefined;
294
+ if (line === undefined) {
295
+ return undefined;
296
+ }
297
+ const character = start && start.character !== undefined && start.character >= 0 ? start.character : widget.editor.document.getLineMaxColumn(line);
298
+ const endLine = end && end.line !== undefined && end.line >= 0 ? end.line : undefined;
299
+ if (endLine === undefined) {
300
+ return { line, character };
301
+ }
302
+ const endCharacter = end && end.character !== undefined && end.character >= 0 ? end.character : widget.editor.document.getLineMaxColumn(endLine);
303
+ return {
304
+ start: { line, character },
305
+ end: { line: endLine, character: endCharacter }
306
+ };
307
+ }
308
+
309
+ protected removeFromCounter(widget: EditorWidget): void {
310
+ const { id, uri } = this.extractIdFromWidget(widget);
311
+ if (uri && !Number.isNaN(id)) {
312
+ let max = -Infinity;
313
+ this.all.forEach(editor => {
314
+ const candidateID = this.extractIdFromWidget(editor);
315
+ if ((candidateID.uri === uri) && (candidateID.id > max)) {
316
+ max = candidateID.id!;
317
+ }
318
+ });
319
+
320
+ if (max > -Infinity) {
321
+ this.editorCounters.set(uri, max);
322
+ } else {
323
+ this.editorCounters.delete(uri);
324
+ }
325
+ }
326
+ }
327
+
328
+ protected extractIdFromWidget(widget: EditorWidget): WidgetId {
329
+ const uri = widget.editor.uri.toString();
330
+ const id = Number(widget.id.slice(widget.id.lastIndexOf(':') + 1));
331
+ return { id, uri };
332
+ }
333
+
334
+ protected checkCounterForWidget(widget: EditorWidget): void {
335
+ const { id, uri } = this.extractIdFromWidget(widget);
336
+ const numericalId = Number(id);
337
+ if (uri && !Number.isNaN(numericalId)) {
338
+ const highestKnownId = this.editorCounters.get(uri) ?? -Infinity;
339
+ if (numericalId > highestKnownId) {
340
+ this.editorCounters.set(uri, numericalId);
341
+ }
342
+ }
343
+ }
344
+
345
+ protected createCounterForUri(uri: URI): number {
346
+ const identifier = uri.toString();
347
+ const next = (this.editorCounters.get(identifier) ?? 0) + 1;
348
+ return next;
349
+ }
350
+
351
+ protected getCounterForUri(uri: URI): number | undefined {
352
+ const idWithoutCounter = EditorWidgetFactory.createID(uri);
353
+ const counterOfMostRecentlyVisibleEditor = this.recentlyVisibleIds.find(id => id.startsWith(idWithoutCounter))?.slice(idWithoutCounter.length + 1);
354
+ return counterOfMostRecentlyVisibleEditor === undefined ? undefined : parseInt(counterOfMostRecentlyVisibleEditor);
355
+ }
356
+
357
+ protected getOrCreateCounterForUri(uri: URI): number {
358
+ return this.getCounterForUri(uri) ?? this.createCounterForUri(uri);
359
+ }
360
+
361
+ protected override createWidgetOptions(uri: URI, options?: EditorOpenerOptions): NavigatableWidgetOptions {
362
+ const navigatableOptions = super.createWidgetOptions(uri, options);
363
+ navigatableOptions.counter = options?.counter ?? this.getOrCreateCounterForUri(uri);
364
+ return navigatableOptions;
365
+ }
366
+ }
367
+
368
+ /**
369
+ * Provides direct access to the underlying text editor.
370
+ */
371
+ @injectable()
372
+ export abstract class EditorAccess {
373
+
374
+ @inject(EditorManager)
375
+ protected readonly editorManager: EditorManager;
376
+
377
+ /**
378
+ * The URI of the underlying document from the editor.
379
+ */
380
+ get uri(): string | undefined {
381
+ const editor = this.editor;
382
+ if (editor) {
383
+ return editor.uri.toString();
384
+ }
385
+ return undefined;
386
+ }
387
+
388
+ /**
389
+ * The selection location from the text editor.
390
+ */
391
+ get selection(): Location | undefined {
392
+ const editor = this.editor;
393
+ if (editor) {
394
+ const uri = editor.uri.toString();
395
+ const range = editor.selection;
396
+ return {
397
+ range,
398
+ uri
399
+ };
400
+ }
401
+ return undefined;
402
+ }
403
+
404
+ /**
405
+ * The unique identifier of the language the current editor belongs to.
406
+ */
407
+ get languageId(): string | undefined {
408
+ const editor = this.editor;
409
+ if (editor) {
410
+ return editor.document.languageId;
411
+ }
412
+ return undefined;
413
+ }
414
+
415
+ /**
416
+ * The text editor.
417
+ */
418
+ get editor(): TextEditor | undefined {
419
+ const editorWidget = this.editorWidget();
420
+ if (editorWidget) {
421
+ return editorWidget.editor;
422
+ }
423
+ return undefined;
424
+ }
425
+
426
+ /**
427
+ * The editor widget, or `undefined` if not applicable.
428
+ */
429
+ protected abstract editorWidget(): EditorWidget | undefined;
430
+
431
+ }
432
+
433
+ /**
434
+ * Provides direct access to the currently active text editor.
435
+ */
436
+ @injectable()
437
+ export class CurrentEditorAccess extends EditorAccess {
438
+
439
+ protected editorWidget(): EditorWidget | undefined {
440
+ return this.editorManager.currentEditor;
441
+ }
442
+
443
+ }
444
+
445
+ /**
446
+ * Provides access to the active text editor.
447
+ */
448
+ @injectable()
449
+ export class ActiveEditorAccess extends EditorAccess {
450
+
451
+ protected editorWidget(): EditorWidget | undefined {
452
+ return this.editorManager.activeEditor;
453
+ }
454
+
455
+ }
456
+
457
+ export namespace EditorAccess {
458
+ export const CURRENT = 'current-editor-access';
459
+ export const ACTIVE = 'active-editor-access';
460
+ }