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