@theia/editor 1.34.2 → 1.34.3

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