@theia/editor 1.45.1 → 1.46.0-next.72

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