-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathnavigator-contribution.ts
371 lines (331 loc) · 15.3 KB
/
navigator-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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/********************************************************************************
* Copyright (C) 2017-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 { injectable, inject, postConstruct } from 'inversify';
import { AbstractViewContribution } from '@theia/core/lib/browser/shell/view-contribution';
import {
Navigatable, SelectableTreeNode, Widget, KeybindingRegistry, CommonCommands,
OpenerService, FrontendApplicationContribution, FrontendApplication, CompositeTreeNode
} from '@theia/core/lib/browser';
import { FileDownloadCommands } from '@theia/filesystem/lib/browser/download/file-download-command-contribution';
import { CommandRegistry, MenuModelRegistry, MenuPath, isOSX, Command, DisposableCollection } from '@theia/core/lib/common';
import { SHELL_TABBAR_CONTEXT_MENU } from '@theia/core/lib/browser';
import { WorkspaceCommands, WorkspaceService, WorkspacePreferences } from '@theia/workspace/lib/browser';
import { FILE_NAVIGATOR_ID, FileNavigatorWidget } from './navigator-widget';
import { FileNavigatorPreferences } from './navigator-preferences';
import { NavigatorKeybindingContexts } from './navigator-keybinding-context';
import { FileNavigatorFilter } from './navigator-filter';
import { WorkspaceNode } from './navigator-tree';
import { NavigatorContextKeyService } from './navigator-context-key-service';
import { TabBarToolbarContribution, TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { FileSystemCommands } from '@theia/filesystem/lib/browser/filesystem-frontend-contribution';
import { NavigatorDiff, NavigatorDiffCommands } from './navigator-diff';
export namespace FileNavigatorCommands {
export const REVEAL_IN_NAVIGATOR: Command = {
id: 'navigator.reveal',
label: 'Reveal in Explorer'
};
export const TOGGLE_HIDDEN_FILES: Command = {
id: 'navigator.toggle.hidden.files',
label: 'Toggle Hidden Files'
};
export const COLLAPSE_ALL: Command = {
id: 'navigator.collapse.all',
category: 'File',
label: 'Collapse Folders in Explorer',
iconClass: 'collapse-all'
};
}
export const NAVIGATOR_CONTEXT_MENU: MenuPath = ['navigator-context-menu'];
/**
* Navigator context menu default groups should be aligned
* with VS Code default groups: https://code.visualstudio.com/api/references/contribution-points#contributes.menus
*/
export namespace NavigatorContextMenu {
export const NAVIGATION = [...NAVIGATOR_CONTEXT_MENU, 'navigation'];
/** @deprecated use NAVIGATION */
export const OPEN = NAVIGATION;
/** @deprecated use NAVIGATION */
export const NEW = NAVIGATION;
export const WORKSPACE = [...NAVIGATOR_CONTEXT_MENU, '2_workspace'];
export const COMPARE = [...NAVIGATOR_CONTEXT_MENU, '3_compare'];
/** @deprecated use COMPARE */
export const DIFF = COMPARE;
export const SEARCH = [...NAVIGATOR_CONTEXT_MENU, '4_search'];
export const CLIPBOARD = [...NAVIGATOR_CONTEXT_MENU, '5_cutcopypaste'];
export const MODIFICATION = [...NAVIGATOR_CONTEXT_MENU, '7_modification'];
/** @deprecated use MODIFICATION */
export const MOVE = MODIFICATION;
/** @deprecated use MODIFICATION */
export const ACTIONS = MODIFICATION;
export const OPEN_WITH = [...NAVIGATION, 'open_with'];
}
@injectable()
export class FileNavigatorContribution extends AbstractViewContribution<FileNavigatorWidget> implements FrontendApplicationContribution, TabBarToolbarContribution {
@inject(NavigatorContextKeyService)
protected readonly contextKeyService: NavigatorContextKeyService;
@inject(MenuModelRegistry)
protected readonly menuRegistry: MenuModelRegistry;
@inject(NavigatorDiff)
protected readonly navigatorDiff: NavigatorDiff;
constructor(
@inject(FileNavigatorPreferences) protected readonly fileNavigatorPreferences: FileNavigatorPreferences,
@inject(OpenerService) protected readonly openerService: OpenerService,
@inject(FileNavigatorFilter) protected readonly fileNavigatorFilter: FileNavigatorFilter,
@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService,
@inject(WorkspacePreferences) protected readonly workspacePreferences: WorkspacePreferences
) {
super({
widgetId: FILE_NAVIGATOR_ID,
widgetName: 'Explorer',
defaultWidgetOptions: {
area: 'left',
rank: 100
},
toggleCommandId: 'fileNavigator:toggle',
toggleKeybinding: 'ctrlcmd+shift+e'
});
}
@postConstruct()
protected async init() {
await this.fileNavigatorPreferences.ready;
this.shell.currentChanged.connect(() => this.onCurrentWidgetChangedHandler());
const updateFocusContextKeys = () => {
const hasFocus = this.shell.activeWidget instanceof FileNavigatorWidget;
this.contextKeyService.explorerViewletFocus.set(hasFocus);
this.contextKeyService.filesExplorerFocus.set(hasFocus);
};
updateFocusContextKeys();
this.shell.activeChanged.connect(updateFocusContextKeys);
this.updateAddRemoveFolderActions(this.menuRegistry);
this.workspacePreferences.onPreferenceChanged(change => {
if (change.preferenceName === 'workspace.supportMultiRootWorkspace') {
this.updateAddRemoveFolderActions(this.menuRegistry);
}
});
}
async initializeLayout(app: FrontendApplication): Promise<void> {
await this.openView();
}
registerCommands(registry: CommandRegistry): void {
super.registerCommands(registry);
registry.registerCommand(FileNavigatorCommands.REVEAL_IN_NAVIGATOR, {
execute: () => this.openView({ activate: true }).then(() => this.selectWidgetFileNode(this.shell.currentWidget)),
isEnabled: () => Navigatable.is(this.shell.currentWidget),
isVisible: () => Navigatable.is(this.shell.currentWidget)
});
registry.registerCommand(FileNavigatorCommands.TOGGLE_HIDDEN_FILES, {
execute: () => {
this.fileNavigatorFilter.toggleHiddenFiles();
},
isEnabled: () => true,
isVisible: () => true
});
registry.registerCommand(FileNavigatorCommands.COLLAPSE_ALL, {
execute: widget => this.withWidget(widget, () => this.collapseFileNavigatorTree()),
isEnabled: widget => this.withWidget(widget, () => this.workspaceService.opened),
isVisible: widget => this.withWidget(widget, () => this.workspaceService.opened)
});
registry.registerCommand(NavigatorDiffCommands.COMPARE_FIRST, {
execute: () => {
this.navigatorDiff.addFirstComparisonFile();
},
isEnabled: () => true,
isVisible: () => true
});
registry.registerCommand(NavigatorDiffCommands.COMPARE_SECOND, {
execute: () => {
this.navigatorDiff.compareFiles();
},
isEnabled: () => this.navigatorDiff.isFirstFileSelected,
isVisible: () => this.navigatorDiff.isFirstFileSelected
});
}
protected withWidget<T>(widget: Widget | undefined = this.tryGetWidget(), cb: (navigator: FileNavigatorWidget) => T): T | false {
if (widget instanceof FileNavigatorWidget && widget.id === FILE_NAVIGATOR_ID) {
return cb(widget);
}
return false;
}
registerMenus(registry: MenuModelRegistry): void {
super.registerMenus(registry);
registry.registerMenuAction(SHELL_TABBAR_CONTEXT_MENU, {
commandId: FileNavigatorCommands.REVEAL_IN_NAVIGATOR.id,
label: FileNavigatorCommands.REVEAL_IN_NAVIGATOR.label,
order: '5'
});
registry.registerMenuAction(NavigatorContextMenu.NAVIGATION, {
commandId: CommonCommands.OPEN.id
});
registry.registerSubmenu(NavigatorContextMenu.OPEN_WITH, 'Open With');
this.openerService.getOpeners().then(openers => {
for (const opener of openers) {
const openWithCommand = WorkspaceCommands.FILE_OPEN_WITH(opener);
registry.registerMenuAction(NavigatorContextMenu.OPEN_WITH, {
commandId: openWithCommand.id,
label: opener.label,
icon: opener.iconClass
});
}
});
// registry.registerMenuAction([CONTEXT_MENU_PATH, CUT_MENU_GROUP], {
// commandId: Commands.FILE_CUT
// });
registry.registerMenuAction(NavigatorContextMenu.CLIPBOARD, {
commandId: CommonCommands.COPY.id,
order: 'a'
});
registry.registerMenuAction(NavigatorContextMenu.CLIPBOARD, {
commandId: CommonCommands.PASTE.id,
order: 'b'
});
registry.registerMenuAction(NavigatorContextMenu.CLIPBOARD, {
commandId: FileDownloadCommands.COPY_DOWNLOAD_LINK.id,
order: 'z'
});
registry.registerMenuAction(NavigatorContextMenu.MODIFICATION, {
commandId: WorkspaceCommands.FILE_RENAME.id
});
registry.registerMenuAction(NavigatorContextMenu.MODIFICATION, {
commandId: WorkspaceCommands.FILE_DELETE.id
});
registry.registerMenuAction(NavigatorContextMenu.MODIFICATION, {
commandId: WorkspaceCommands.FILE_DUPLICATE.id
});
const downloadUploadMenu = [...NAVIGATOR_CONTEXT_MENU, '6_downloadupload'];
registry.registerMenuAction(downloadUploadMenu, {
commandId: FileSystemCommands.UPLOAD.id,
order: 'a'
});
registry.registerMenuAction(downloadUploadMenu, {
commandId: FileDownloadCommands.DOWNLOAD.id,
order: 'b'
});
registry.registerMenuAction(NavigatorContextMenu.NAVIGATION, {
commandId: WorkspaceCommands.NEW_FILE.id
});
registry.registerMenuAction(NavigatorContextMenu.NAVIGATION, {
commandId: WorkspaceCommands.NEW_FOLDER.id
});
registry.registerMenuAction(NavigatorContextMenu.COMPARE, {
commandId: WorkspaceCommands.FILE_COMPARE.id
});
registry.registerMenuAction(NavigatorContextMenu.MODIFICATION, {
commandId: FileNavigatorCommands.COLLAPSE_ALL.id,
label: 'Collapse All',
order: 'z2'
});
registry.registerMenuAction(NavigatorContextMenu.COMPARE, {
commandId: NavigatorDiffCommands.COMPARE_FIRST.id,
order: 'z'
});
registry.registerMenuAction(NavigatorContextMenu.COMPARE, {
commandId: NavigatorDiffCommands.COMPARE_SECOND.id,
order: 'z'
});
}
registerKeybindings(registry: KeybindingRegistry): void {
super.registerKeybindings(registry);
registry.registerKeybinding({
command: FileNavigatorCommands.REVEAL_IN_NAVIGATOR.id,
keybinding: 'alt+r'
});
registry.registerKeybinding({
command: WorkspaceCommands.FILE_DELETE.id,
keybinding: 'del',
context: NavigatorKeybindingContexts.navigatorActive
});
if (isOSX) {
registry.registerKeybinding({
command: WorkspaceCommands.FILE_DELETE.id,
keybinding: 'cmd+backspace',
context: NavigatorKeybindingContexts.navigatorActive
});
}
registry.registerKeybinding({
command: WorkspaceCommands.FILE_RENAME.id,
keybinding: 'f2',
context: NavigatorKeybindingContexts.navigatorActive
});
registry.registerKeybinding({
command: FileNavigatorCommands.TOGGLE_HIDDEN_FILES.id,
keybinding: 'ctrlcmd+i',
context: NavigatorKeybindingContexts.navigatorActive
});
}
async registerToolbarItems(toolbarRegistry: TabBarToolbarRegistry): Promise<void> {
toolbarRegistry.registerItem({
id: FileNavigatorCommands.COLLAPSE_ALL.id,
command: FileNavigatorCommands.COLLAPSE_ALL.id,
tooltip: 'Collapse All',
priority: 0,
});
}
/**
* Reveals and selects node in the file navigator to which given widget is related.
* Does nothing if given widget undefined or doesn't have related resource.
*
* @param widget widget file resource of which should be revealed and selected
*/
async selectWidgetFileNode(widget: Widget | undefined): Promise<void> {
if (Navigatable.is(widget)) {
const resourceUri = widget.getResourceUri();
if (resourceUri) {
const { model } = await this.widget;
const node = await model.revealFile(resourceUri);
if (SelectableTreeNode.is(node)) {
model.selectNode(node);
}
}
}
}
protected onCurrentWidgetChangedHandler(): void {
if (this.fileNavigatorPreferences['navigator.autoReveal']) {
this.selectWidgetFileNode(this.shell.currentWidget);
}
}
/**
* Collapse file navigator nodes and set focus on first visible node
* - single root workspace: collapse all nodes except root
* - multiple root workspace: collapse all nodes, even roots
*/
async collapseFileNavigatorTree(): Promise<void> {
const { model } = await this.widget;
// collapse all child nodes which are not the root (single root workspace)
// collapse all root nodes (multiple root workspace)
let root = model.root as CompositeTreeNode;
if (WorkspaceNode.is(root) && root.children.length === 1) {
root = root.children[0];
}
root.children.forEach(child => CompositeTreeNode.is(child) && model.collapseAll(child));
// select first visible node
const firstChild = WorkspaceNode.is(root) ? root.children[0] : root;
if (SelectableTreeNode.is(firstChild)) {
model.selectNode(firstChild);
}
}
private readonly toDisposeAddRemoveFolderActions = new DisposableCollection();
private updateAddRemoveFolderActions(registry: MenuModelRegistry): void {
this.toDisposeAddRemoveFolderActions.dispose();
if (this.workspacePreferences['workspace.supportMultiRootWorkspace']) {
this.toDisposeAddRemoveFolderActions.push(registry.registerMenuAction(NavigatorContextMenu.WORKSPACE, {
commandId: WorkspaceCommands.ADD_FOLDER.id
}));
this.toDisposeAddRemoveFolderActions.push(registry.registerMenuAction(NavigatorContextMenu.WORKSPACE, {
commandId: WorkspaceCommands.REMOVE_FOLDER.id
}));
}
}
}