@theia/editor 1.45.0 → 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,343 +1,343 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2018 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 { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
18
- import { ILogger } from '@theia/core/lib/common/logger';
19
- import { StorageService } from '@theia/core/lib/browser/storage-service';
20
- import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable';
21
- import { FrontendApplicationContribution } from '@theia/core/lib/browser/frontend-application-contribution';
22
- import { CommandRegistry } from '@theia/core/lib/common/command';
23
- import { EditorCommands } from './editor-command';
24
- import { EditorWidget } from './editor-widget';
25
- import { EditorManager } from './editor-manager';
26
- import { TextEditor, Position, Range, TextDocumentChangeEvent } from './editor';
27
- import { NavigationLocation, RecentlyClosedEditor } from './navigation/navigation-location';
28
- import { NavigationLocationService } from './navigation/navigation-location-service';
29
- import { PreferenceService, PreferenceScope, addEventListener } from '@theia/core/lib/browser';
30
- import { ConfirmDialog, Dialog } from '@theia/core/lib/browser/dialogs';
31
- import { nls } from '@theia/core';
32
-
33
- @injectable()
34
- export class EditorNavigationContribution implements Disposable, FrontendApplicationContribution {
35
-
36
- private static ID = 'editor-navigation-contribution';
37
- private static CLOSED_EDITORS_KEY = 'recently-closed-editors';
38
- private static MOUSE_NAVIGATION_PREFERENCE = 'workbench.editor.mouseBackForwardToNavigate';
39
-
40
- protected readonly toDispose = new DisposableCollection();
41
- protected readonly toDisposePerCurrentEditor = new DisposableCollection();
42
-
43
- @inject(ILogger)
44
- protected readonly logger: ILogger;
45
-
46
- @inject(EditorManager)
47
- protected readonly editorManager: EditorManager;
48
-
49
- @inject(NavigationLocationService)
50
- protected readonly locationStack: NavigationLocationService;
51
-
52
- @inject(StorageService)
53
- protected readonly storageService: StorageService;
54
-
55
- @inject(PreferenceService)
56
- protected readonly preferenceService: PreferenceService;
57
-
58
- @inject(CommandRegistry)
59
- protected readonly commandRegistry: CommandRegistry;
60
-
61
- @postConstruct()
62
- protected init(): void {
63
- this.toDispose.pushAll([
64
- // TODO listen on file resource changes, if a file gets deleted, remove the corresponding navigation locations (if any).
65
- // This would require introducing the FS dependency in the editor extension.
66
- this.editorManager.onCurrentEditorChanged(this.onCurrentEditorChanged.bind(this)),
67
- this.editorManager.onCreated(widget => {
68
- this.locationStack.removeClosedEditor(widget.editor.uri);
69
- widget.disposed.connect(() => this.locationStack.addClosedEditor({
70
- uri: widget.editor.uri,
71
- viewState: widget.editor.storeViewState()
72
- }));
73
- })
74
- ]);
75
- this.commandRegistry.registerHandler(EditorCommands.GO_BACK.id, {
76
- execute: () => this.locationStack.back(),
77
- isEnabled: () => this.locationStack.canGoBack()
78
- });
79
- this.commandRegistry.registerHandler(EditorCommands.GO_FORWARD.id, {
80
- execute: () => this.locationStack.forward(),
81
- isEnabled: () => this.locationStack.canGoForward()
82
- });
83
- this.commandRegistry.registerHandler(EditorCommands.GO_LAST_EDIT.id, {
84
- execute: () => this.locationStack.reveal(this.locationStack.lastEditLocation()),
85
- isEnabled: () => !!this.locationStack.lastEditLocation()
86
- });
87
- this.commandRegistry.registerHandler(EditorCommands.CLEAR_EDITOR_HISTORY.id, {
88
- execute: async () => {
89
- const shouldClear = await new ConfirmDialog({
90
- title: nls.localizeByDefault('Clear Editor History'),
91
- msg: nls.localizeByDefault('Do you want to clear the history of recently opened editors?'),
92
- ok: Dialog.YES,
93
- cancel: Dialog.NO
94
- }).open();
95
- if (shouldClear) {
96
- this.locationStack.clearHistory();
97
- }
98
- },
99
- isEnabled: () => this.locationStack.locations().length > 0
100
- });
101
- this.commandRegistry.registerHandler(EditorCommands.TOGGLE_MINIMAP.id, {
102
- execute: () => this.toggleMinimap(),
103
- isEnabled: () => true,
104
- isToggled: () => this.isMinimapEnabled()
105
- });
106
- this.commandRegistry.registerHandler(EditorCommands.TOGGLE_RENDER_WHITESPACE.id, {
107
- execute: () => this.toggleRenderWhitespace(),
108
- isEnabled: () => true,
109
- isToggled: () => this.isRenderWhitespaceEnabled()
110
- });
111
- this.commandRegistry.registerHandler(EditorCommands.TOGGLE_WORD_WRAP.id, {
112
- execute: () => this.toggleWordWrap(),
113
- isEnabled: () => true,
114
- });
115
- this.commandRegistry.registerHandler(EditorCommands.TOGGLE_STICKY_SCROLL.id, {
116
- execute: () => this.toggleStickyScroll(),
117
- isEnabled: () => true,
118
- isToggled: () => this.isStickyScrollEnabled()
119
- });
120
- this.commandRegistry.registerHandler(EditorCommands.REOPEN_CLOSED_EDITOR.id, {
121
- execute: () => this.reopenLastClosedEditor()
122
- });
123
-
124
- this.installMouseNavigationSupport();
125
- }
126
-
127
- protected async installMouseNavigationSupport(): Promise<void> {
128
- const mouseNavigationSupport = new DisposableCollection();
129
- const updateMouseNavigationListener = () => {
130
- mouseNavigationSupport.dispose();
131
- if (this.shouldNavigateWithMouse()) {
132
- mouseNavigationSupport.push(addEventListener(document.body, 'mousedown', event => this.onMouseDown(event), true));
133
- }
134
- };
135
- this.toDispose.push(this.preferenceService.onPreferenceChanged(change => {
136
- if (change.preferenceName === EditorNavigationContribution.MOUSE_NAVIGATION_PREFERENCE) {
137
- updateMouseNavigationListener();
138
- }
139
- }));
140
- updateMouseNavigationListener();
141
- this.toDispose.push(mouseNavigationSupport);
142
- }
143
-
144
- protected async onMouseDown(event: MouseEvent): Promise<void> {
145
- // Support navigation in history when mouse buttons 4/5 are pressed
146
- switch (event.button) {
147
- case 3:
148
- event.preventDefault();
149
- this.locationStack.back();
150
- break;
151
- case 4:
152
- event.preventDefault();
153
- this.locationStack.forward();
154
- break;
155
- }
156
- }
157
-
158
- /**
159
- * Reopens the last closed editor with its stored view state if possible from history.
160
- * If the editor cannot be restored, continue to the next editor in history.
161
- */
162
- protected async reopenLastClosedEditor(): Promise<void> {
163
- const lastClosedEditor = this.locationStack.getLastClosedEditor();
164
- if (lastClosedEditor === undefined) {
165
- return;
166
- }
167
-
168
- try {
169
- const widget = await this.editorManager.open(lastClosedEditor.uri);
170
- widget.editor.restoreViewState(lastClosedEditor.viewState);
171
- } catch {
172
- this.locationStack.removeClosedEditor(lastClosedEditor.uri);
173
- this.reopenLastClosedEditor();
174
- }
175
- }
176
-
177
- async onStart(): Promise<void> {
178
- this.restoreState();
179
- }
180
-
181
- onStop(): void {
182
- this.storeState();
183
- this.dispose();
184
- }
185
-
186
- dispose(): void {
187
- this.toDispose.dispose();
188
- }
189
-
190
- /**
191
- * Toggle the editor word wrap behavior.
192
- */
193
- protected async toggleWordWrap(): Promise<void> {
194
- // Get the current word wrap.
195
- const wordWrap: string | undefined = this.preferenceService.get('editor.wordWrap');
196
- if (wordWrap === undefined) {
197
- return;
198
- }
199
- // The list of allowed word wrap values.
200
- const values: string[] = ['off', 'on', 'wordWrapColumn', 'bounded'];
201
- // Get the index of the current value, and toggle to the next available value.
202
- const index = values.indexOf(wordWrap) + 1;
203
- if (index > -1) {
204
- this.preferenceService.set('editor.wordWrap', values[index % values.length], PreferenceScope.User);
205
- }
206
- }
207
-
208
- /**
209
- * Toggle the display of sticky scroll in the editor.
210
- */
211
- protected async toggleStickyScroll(): Promise<void> {
212
- const value: boolean | undefined = this.preferenceService.get('editor.stickyScroll.enabled');
213
- this.preferenceService.set('editor.stickyScroll.enabled', !value, PreferenceScope.User);
214
- }
215
-
216
- /**
217
- * Toggle the display of minimap in the editor.
218
- */
219
- protected async toggleMinimap(): Promise<void> {
220
- const value: boolean | undefined = this.preferenceService.get('editor.minimap.enabled');
221
- this.preferenceService.set('editor.minimap.enabled', !value, PreferenceScope.User);
222
- }
223
-
224
- /**
225
- * Toggle the rendering of whitespace in the editor.
226
- */
227
- protected async toggleRenderWhitespace(): Promise<void> {
228
- const renderWhitespace: string | undefined = this.preferenceService.get('editor.renderWhitespace');
229
- let updatedRenderWhitespace: string;
230
- if (renderWhitespace === 'none') {
231
- updatedRenderWhitespace = 'all';
232
- } else {
233
- updatedRenderWhitespace = 'none';
234
- }
235
- this.preferenceService.set('editor.renderWhitespace', updatedRenderWhitespace, PreferenceScope.User);
236
- }
237
-
238
- protected onCurrentEditorChanged(editorWidget: EditorWidget | undefined): void {
239
- this.toDisposePerCurrentEditor.dispose();
240
- if (editorWidget) {
241
- const { editor } = editorWidget;
242
- this.toDisposePerCurrentEditor.pushAll([
243
- // Instead of registering an `onCursorPositionChanged` listener, we treat the zero length selection as a cursor position change.
244
- // Otherwise we would have two events for a single cursor change interaction.
245
- editor.onSelectionChanged(selection => this.onSelectionChanged(editor, selection)),
246
- editor.onDocumentContentChanged(event => this.onDocumentContentChanged(editor, event))
247
- ]);
248
- this.locationStack.register(NavigationLocation.create(editor, editor.selection));
249
- }
250
- }
251
-
252
- protected onCursorPositionChanged(editor: TextEditor, position: Position): void {
253
- this.locationStack.register(NavigationLocation.create(editor, position));
254
- }
255
-
256
- protected onSelectionChanged(editor: TextEditor, selection: Range): void {
257
- if (this.isZeroLengthRange(selection)) {
258
- this.onCursorPositionChanged(editor, selection.start);
259
- } else {
260
- this.locationStack.register(NavigationLocation.create(editor, selection));
261
- }
262
- }
263
-
264
- protected onDocumentContentChanged(editor: TextEditor, event: TextDocumentChangeEvent): void {
265
- if (event.contentChanges.length > 0) {
266
- this.locationStack.register(NavigationLocation.create(editor, event.contentChanges[0]));
267
- }
268
- }
269
-
270
- /**
271
- * `true` if the `range` argument has zero length. In other words, the `start` and the `end` positions are the same. Otherwise, `false`.
272
- */
273
- protected isZeroLengthRange(range: Range): boolean {
274
- const { start, end } = range;
275
- return start.line === end.line && start.character === end.character;
276
- }
277
-
278
- protected async storeState(): Promise<void> {
279
- this.storageService.setData(EditorNavigationContribution.ID, {
280
- locations: this.locationStack.locations().map(NavigationLocation.toObject)
281
- });
282
- this.storageService.setData(EditorNavigationContribution.CLOSED_EDITORS_KEY, {
283
- closedEditors: this.shouldStoreClosedEditors() ? this.locationStack.closedEditorsStack.map(RecentlyClosedEditor.toObject) : []
284
- });
285
- }
286
-
287
- protected async restoreState(): Promise<void> {
288
- await this.restoreNavigationLocations();
289
- await this.restoreClosedEditors();
290
- }
291
-
292
- protected async restoreNavigationLocations(): Promise<void> {
293
- const raw: { locations?: ArrayLike<object> } | undefined = await this.storageService.getData(EditorNavigationContribution.ID);
294
- if (raw && raw.locations) {
295
- const locations: NavigationLocation[] = [];
296
- for (let i = 0; i < raw.locations.length; i++) {
297
- const location = NavigationLocation.fromObject(raw.locations[i]);
298
- if (location) {
299
- locations.push(location);
300
- } else {
301
- this.logger.warn('Could not restore the state of the editor navigation history.');
302
- return;
303
- }
304
- }
305
- this.locationStack.register(...locations);
306
- }
307
- }
308
-
309
- protected async restoreClosedEditors(): Promise<void> {
310
- const raw: { closedEditors?: ArrayLike<object> } | undefined = await this.storageService.getData(EditorNavigationContribution.CLOSED_EDITORS_KEY);
311
- if (raw && raw.closedEditors) {
312
- for (let i = 0; i < raw.closedEditors.length; i++) {
313
- const editor = RecentlyClosedEditor.fromObject(raw.closedEditors[i]);
314
- if (editor) {
315
- this.locationStack.addClosedEditor(editor);
316
- } else {
317
- this.logger.warn('Could not restore the state of the closed editors stack.');
318
- }
319
- }
320
- }
321
- }
322
-
323
- private isMinimapEnabled(): boolean {
324
- return !!this.preferenceService.get('editor.minimap.enabled');
325
- }
326
-
327
- private isRenderWhitespaceEnabled(): boolean {
328
- const renderWhitespace = this.preferenceService.get('editor.renderWhitespace');
329
- return renderWhitespace === 'none' ? false : true;
330
- }
331
-
332
- private shouldStoreClosedEditors(): boolean {
333
- return !!this.preferenceService.get('editor.history.persistClosedEditors');
334
- }
335
-
336
- private shouldNavigateWithMouse(): boolean {
337
- return !!this.preferenceService.get(EditorNavigationContribution.MOUSE_NAVIGATION_PREFERENCE);
338
- }
339
-
340
- private isStickyScrollEnabled(): boolean {
341
- return !!this.preferenceService.get('editor.stickyScroll.enabled');
342
- }
343
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2018 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 { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
18
+ import { ILogger } from '@theia/core/lib/common/logger';
19
+ import { StorageService } from '@theia/core/lib/browser/storage-service';
20
+ import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable';
21
+ import { FrontendApplicationContribution } from '@theia/core/lib/browser/frontend-application-contribution';
22
+ import { CommandRegistry } from '@theia/core/lib/common/command';
23
+ import { EditorCommands } from './editor-command';
24
+ import { EditorWidget } from './editor-widget';
25
+ import { EditorManager } from './editor-manager';
26
+ import { TextEditor, Position, Range, TextDocumentChangeEvent } from './editor';
27
+ import { NavigationLocation, RecentlyClosedEditor } from './navigation/navigation-location';
28
+ import { NavigationLocationService } from './navigation/navigation-location-service';
29
+ import { PreferenceService, PreferenceScope, addEventListener } from '@theia/core/lib/browser';
30
+ import { ConfirmDialog, Dialog } from '@theia/core/lib/browser/dialogs';
31
+ import { nls } from '@theia/core';
32
+
33
+ @injectable()
34
+ export class EditorNavigationContribution implements Disposable, FrontendApplicationContribution {
35
+
36
+ private static ID = 'editor-navigation-contribution';
37
+ private static CLOSED_EDITORS_KEY = 'recently-closed-editors';
38
+ private static MOUSE_NAVIGATION_PREFERENCE = 'workbench.editor.mouseBackForwardToNavigate';
39
+
40
+ protected readonly toDispose = new DisposableCollection();
41
+ protected readonly toDisposePerCurrentEditor = new DisposableCollection();
42
+
43
+ @inject(ILogger)
44
+ protected readonly logger: ILogger;
45
+
46
+ @inject(EditorManager)
47
+ protected readonly editorManager: EditorManager;
48
+
49
+ @inject(NavigationLocationService)
50
+ protected readonly locationStack: NavigationLocationService;
51
+
52
+ @inject(StorageService)
53
+ protected readonly storageService: StorageService;
54
+
55
+ @inject(PreferenceService)
56
+ protected readonly preferenceService: PreferenceService;
57
+
58
+ @inject(CommandRegistry)
59
+ protected readonly commandRegistry: CommandRegistry;
60
+
61
+ @postConstruct()
62
+ protected init(): void {
63
+ this.toDispose.pushAll([
64
+ // TODO listen on file resource changes, if a file gets deleted, remove the corresponding navigation locations (if any).
65
+ // This would require introducing the FS dependency in the editor extension.
66
+ this.editorManager.onCurrentEditorChanged(this.onCurrentEditorChanged.bind(this)),
67
+ this.editorManager.onCreated(widget => {
68
+ this.locationStack.removeClosedEditor(widget.editor.uri);
69
+ widget.disposed.connect(() => this.locationStack.addClosedEditor({
70
+ uri: widget.editor.uri,
71
+ viewState: widget.editor.storeViewState()
72
+ }));
73
+ })
74
+ ]);
75
+ this.commandRegistry.registerHandler(EditorCommands.GO_BACK.id, {
76
+ execute: () => this.locationStack.back(),
77
+ isEnabled: () => this.locationStack.canGoBack()
78
+ });
79
+ this.commandRegistry.registerHandler(EditorCommands.GO_FORWARD.id, {
80
+ execute: () => this.locationStack.forward(),
81
+ isEnabled: () => this.locationStack.canGoForward()
82
+ });
83
+ this.commandRegistry.registerHandler(EditorCommands.GO_LAST_EDIT.id, {
84
+ execute: () => this.locationStack.reveal(this.locationStack.lastEditLocation()),
85
+ isEnabled: () => !!this.locationStack.lastEditLocation()
86
+ });
87
+ this.commandRegistry.registerHandler(EditorCommands.CLEAR_EDITOR_HISTORY.id, {
88
+ execute: async () => {
89
+ const shouldClear = await new ConfirmDialog({
90
+ title: nls.localizeByDefault('Clear Editor History'),
91
+ msg: nls.localizeByDefault('Do you want to clear the history of recently opened editors?'),
92
+ ok: Dialog.YES,
93
+ cancel: Dialog.NO
94
+ }).open();
95
+ if (shouldClear) {
96
+ this.locationStack.clearHistory();
97
+ }
98
+ },
99
+ isEnabled: () => this.locationStack.locations().length > 0
100
+ });
101
+ this.commandRegistry.registerHandler(EditorCommands.TOGGLE_MINIMAP.id, {
102
+ execute: () => this.toggleMinimap(),
103
+ isEnabled: () => true,
104
+ isToggled: () => this.isMinimapEnabled()
105
+ });
106
+ this.commandRegistry.registerHandler(EditorCommands.TOGGLE_RENDER_WHITESPACE.id, {
107
+ execute: () => this.toggleRenderWhitespace(),
108
+ isEnabled: () => true,
109
+ isToggled: () => this.isRenderWhitespaceEnabled()
110
+ });
111
+ this.commandRegistry.registerHandler(EditorCommands.TOGGLE_WORD_WRAP.id, {
112
+ execute: () => this.toggleWordWrap(),
113
+ isEnabled: () => true,
114
+ });
115
+ this.commandRegistry.registerHandler(EditorCommands.TOGGLE_STICKY_SCROLL.id, {
116
+ execute: () => this.toggleStickyScroll(),
117
+ isEnabled: () => true,
118
+ isToggled: () => this.isStickyScrollEnabled()
119
+ });
120
+ this.commandRegistry.registerHandler(EditorCommands.REOPEN_CLOSED_EDITOR.id, {
121
+ execute: () => this.reopenLastClosedEditor()
122
+ });
123
+
124
+ this.installMouseNavigationSupport();
125
+ }
126
+
127
+ protected async installMouseNavigationSupport(): Promise<void> {
128
+ const mouseNavigationSupport = new DisposableCollection();
129
+ const updateMouseNavigationListener = () => {
130
+ mouseNavigationSupport.dispose();
131
+ if (this.shouldNavigateWithMouse()) {
132
+ mouseNavigationSupport.push(addEventListener(document.body, 'mousedown', event => this.onMouseDown(event), true));
133
+ }
134
+ };
135
+ this.toDispose.push(this.preferenceService.onPreferenceChanged(change => {
136
+ if (change.preferenceName === EditorNavigationContribution.MOUSE_NAVIGATION_PREFERENCE) {
137
+ updateMouseNavigationListener();
138
+ }
139
+ }));
140
+ updateMouseNavigationListener();
141
+ this.toDispose.push(mouseNavigationSupport);
142
+ }
143
+
144
+ protected async onMouseDown(event: MouseEvent): Promise<void> {
145
+ // Support navigation in history when mouse buttons 4/5 are pressed
146
+ switch (event.button) {
147
+ case 3:
148
+ event.preventDefault();
149
+ this.locationStack.back();
150
+ break;
151
+ case 4:
152
+ event.preventDefault();
153
+ this.locationStack.forward();
154
+ break;
155
+ }
156
+ }
157
+
158
+ /**
159
+ * Reopens the last closed editor with its stored view state if possible from history.
160
+ * If the editor cannot be restored, continue to the next editor in history.
161
+ */
162
+ protected async reopenLastClosedEditor(): Promise<void> {
163
+ const lastClosedEditor = this.locationStack.getLastClosedEditor();
164
+ if (lastClosedEditor === undefined) {
165
+ return;
166
+ }
167
+
168
+ try {
169
+ const widget = await this.editorManager.open(lastClosedEditor.uri);
170
+ widget.editor.restoreViewState(lastClosedEditor.viewState);
171
+ } catch {
172
+ this.locationStack.removeClosedEditor(lastClosedEditor.uri);
173
+ this.reopenLastClosedEditor();
174
+ }
175
+ }
176
+
177
+ async onStart(): Promise<void> {
178
+ this.restoreState();
179
+ }
180
+
181
+ onStop(): void {
182
+ this.storeState();
183
+ this.dispose();
184
+ }
185
+
186
+ dispose(): void {
187
+ this.toDispose.dispose();
188
+ }
189
+
190
+ /**
191
+ * Toggle the editor word wrap behavior.
192
+ */
193
+ protected async toggleWordWrap(): Promise<void> {
194
+ // Get the current word wrap.
195
+ const wordWrap: string | undefined = this.preferenceService.get('editor.wordWrap');
196
+ if (wordWrap === undefined) {
197
+ return;
198
+ }
199
+ // The list of allowed word wrap values.
200
+ const values: string[] = ['off', 'on', 'wordWrapColumn', 'bounded'];
201
+ // Get the index of the current value, and toggle to the next available value.
202
+ const index = values.indexOf(wordWrap) + 1;
203
+ if (index > -1) {
204
+ this.preferenceService.set('editor.wordWrap', values[index % values.length], PreferenceScope.User);
205
+ }
206
+ }
207
+
208
+ /**
209
+ * Toggle the display of sticky scroll in the editor.
210
+ */
211
+ protected async toggleStickyScroll(): Promise<void> {
212
+ const value: boolean | undefined = this.preferenceService.get('editor.stickyScroll.enabled');
213
+ this.preferenceService.set('editor.stickyScroll.enabled', !value, PreferenceScope.User);
214
+ }
215
+
216
+ /**
217
+ * Toggle the display of minimap in the editor.
218
+ */
219
+ protected async toggleMinimap(): Promise<void> {
220
+ const value: boolean | undefined = this.preferenceService.get('editor.minimap.enabled');
221
+ this.preferenceService.set('editor.minimap.enabled', !value, PreferenceScope.User);
222
+ }
223
+
224
+ /**
225
+ * Toggle the rendering of whitespace in the editor.
226
+ */
227
+ protected async toggleRenderWhitespace(): Promise<void> {
228
+ const renderWhitespace: string | undefined = this.preferenceService.get('editor.renderWhitespace');
229
+ let updatedRenderWhitespace: string;
230
+ if (renderWhitespace === 'none') {
231
+ updatedRenderWhitespace = 'all';
232
+ } else {
233
+ updatedRenderWhitespace = 'none';
234
+ }
235
+ this.preferenceService.set('editor.renderWhitespace', updatedRenderWhitespace, PreferenceScope.User);
236
+ }
237
+
238
+ protected onCurrentEditorChanged(editorWidget: EditorWidget | undefined): void {
239
+ this.toDisposePerCurrentEditor.dispose();
240
+ if (editorWidget) {
241
+ const { editor } = editorWidget;
242
+ this.toDisposePerCurrentEditor.pushAll([
243
+ // Instead of registering an `onCursorPositionChanged` listener, we treat the zero length selection as a cursor position change.
244
+ // Otherwise we would have two events for a single cursor change interaction.
245
+ editor.onSelectionChanged(selection => this.onSelectionChanged(editor, selection)),
246
+ editor.onDocumentContentChanged(event => this.onDocumentContentChanged(editor, event))
247
+ ]);
248
+ this.locationStack.register(NavigationLocation.create(editor, editor.selection));
249
+ }
250
+ }
251
+
252
+ protected onCursorPositionChanged(editor: TextEditor, position: Position): void {
253
+ this.locationStack.register(NavigationLocation.create(editor, position));
254
+ }
255
+
256
+ protected onSelectionChanged(editor: TextEditor, selection: Range): void {
257
+ if (this.isZeroLengthRange(selection)) {
258
+ this.onCursorPositionChanged(editor, selection.start);
259
+ } else {
260
+ this.locationStack.register(NavigationLocation.create(editor, selection));
261
+ }
262
+ }
263
+
264
+ protected onDocumentContentChanged(editor: TextEditor, event: TextDocumentChangeEvent): void {
265
+ if (event.contentChanges.length > 0) {
266
+ this.locationStack.register(NavigationLocation.create(editor, event.contentChanges[0]));
267
+ }
268
+ }
269
+
270
+ /**
271
+ * `true` if the `range` argument has zero length. In other words, the `start` and the `end` positions are the same. Otherwise, `false`.
272
+ */
273
+ protected isZeroLengthRange(range: Range): boolean {
274
+ const { start, end } = range;
275
+ return start.line === end.line && start.character === end.character;
276
+ }
277
+
278
+ protected async storeState(): Promise<void> {
279
+ this.storageService.setData(EditorNavigationContribution.ID, {
280
+ locations: this.locationStack.locations().map(NavigationLocation.toObject)
281
+ });
282
+ this.storageService.setData(EditorNavigationContribution.CLOSED_EDITORS_KEY, {
283
+ closedEditors: this.shouldStoreClosedEditors() ? this.locationStack.closedEditorsStack.map(RecentlyClosedEditor.toObject) : []
284
+ });
285
+ }
286
+
287
+ protected async restoreState(): Promise<void> {
288
+ await this.restoreNavigationLocations();
289
+ await this.restoreClosedEditors();
290
+ }
291
+
292
+ protected async restoreNavigationLocations(): Promise<void> {
293
+ const raw: { locations?: ArrayLike<object> } | undefined = await this.storageService.getData(EditorNavigationContribution.ID);
294
+ if (raw && raw.locations) {
295
+ const locations: NavigationLocation[] = [];
296
+ for (let i = 0; i < raw.locations.length; i++) {
297
+ const location = NavigationLocation.fromObject(raw.locations[i]);
298
+ if (location) {
299
+ locations.push(location);
300
+ } else {
301
+ this.logger.warn('Could not restore the state of the editor navigation history.');
302
+ return;
303
+ }
304
+ }
305
+ this.locationStack.register(...locations);
306
+ }
307
+ }
308
+
309
+ protected async restoreClosedEditors(): Promise<void> {
310
+ const raw: { closedEditors?: ArrayLike<object> } | undefined = await this.storageService.getData(EditorNavigationContribution.CLOSED_EDITORS_KEY);
311
+ if (raw && raw.closedEditors) {
312
+ for (let i = 0; i < raw.closedEditors.length; i++) {
313
+ const editor = RecentlyClosedEditor.fromObject(raw.closedEditors[i]);
314
+ if (editor) {
315
+ this.locationStack.addClosedEditor(editor);
316
+ } else {
317
+ this.logger.warn('Could not restore the state of the closed editors stack.');
318
+ }
319
+ }
320
+ }
321
+ }
322
+
323
+ private isMinimapEnabled(): boolean {
324
+ return !!this.preferenceService.get('editor.minimap.enabled');
325
+ }
326
+
327
+ private isRenderWhitespaceEnabled(): boolean {
328
+ const renderWhitespace = this.preferenceService.get('editor.renderWhitespace');
329
+ return renderWhitespace === 'none' ? false : true;
330
+ }
331
+
332
+ private shouldStoreClosedEditors(): boolean {
333
+ return !!this.preferenceService.get('editor.history.persistClosedEditors');
334
+ }
335
+
336
+ private shouldNavigateWithMouse(): boolean {
337
+ return !!this.preferenceService.get(EditorNavigationContribution.MOUSE_NAVIGATION_PREFERENCE);
338
+ }
339
+
340
+ private isStickyScrollEnabled(): boolean {
341
+ return !!this.preferenceService.get('editor.stickyScroll.enabled');
342
+ }
343
+ }