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