Skip to content

Commit

Permalink
Add command for opening the containing folder of a file
Browse files Browse the repository at this point in the history
A new 'OPEN_CONTAINING_FOLDER' command was added for the electron
application. It opens the native file explorer at the path where the
selected resource (or resources) from the file navigator is located.

Signed-off-by: Alexandra Buzila <[email protected]>
  • Loading branch information
AlexandraBuzila committed Jan 10, 2022
1 parent e0359d4 commit 422a778
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
- [plugin-ext] `ViewContextKeyService#with` method removed. Use `ContextKeyService#with` instead. `PluginViewWidget` and `PluginTreeWidget` inject the `ContextKeyService` rather than `ViewContextKeyService`. [#10546](https://github.com/eclipse-theia/theia/pull/10546)


## v1.22.0 - 01/??/2022

[1.22.0 Milestone](https://github.com/eclipse-theia/theia/milestone/30)

- [navigator] added `Open Containing Folder` command [#10523](https://github.com/eclipse-theia/theia/pull/10523)

## v1.21.0 - 12/16/2021

[1.21.0 Milestone](https://github.com/eclipse-theia/theia/milestone/29)
Expand Down
3 changes: 3 additions & 0 deletions packages/navigator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"theiaExtensions": [
{
"frontend": "lib/browser/navigator-frontend-module"
},
{
"frontendElectron": "lib/electron-browser/electron-navigator-module"
}
],
"keywords": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/********************************************************************************
* Copyright (C) 2021 EclipseSource 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 { Command, CommandContribution, CommandRegistry, MenuContribution, MenuModelRegistry } from '@theia/core';
import { CommonCommands, KeybindingContribution, KeybindingRegistry } from '@theia/core/lib/browser';
import { WidgetManager } from '@theia/core/lib/browser/widget-manager';
import * as electron from '@theia/core/shared/electron';
import { inject, injectable } from '@theia/core/shared/inversify';
import { FileStatNode } from '@theia/filesystem/lib/browser';
import { FileNavigatorWidget, FILE_NAVIGATOR_ID } from '../browser';
import { NavigatorContextMenu } from '../browser/navigator-contribution';
import { isWindows, isOSX } from '@theia/core/lib/common/os';

export const OPEN_CONTAINING_FOLDER = Command.toDefaultLocalizedCommand({
id: 'revealFileInOS',
category: CommonCommands.FILE_CATEGORY,
label: isWindows ? 'Reveal File in File Explorer' :
isOSX ? 'Reveal in Finder' :
/* linux */ 'Open Containing Folder'
});

@injectable()
export class ElectronNavigatorMenuContribution implements MenuContribution, CommandContribution, KeybindingContribution {

@inject(WidgetManager)
protected readonly widgetManager: WidgetManager;

registerCommands(commands: CommandRegistry): void {
commands.registerCommand(OPEN_CONTAINING_FOLDER, {
isEnabled: () => this.getSelectedFileStatNodes().length > 0,
isVisible: () => this.getSelectedFileStatNodes().length > 0,
execute: () => {
this.getSelectedFileStatNodes().forEach(node => {
electron.shell.showItemInFolder(node.uri.path.toString());
});
}
});
}

registerMenus(menus: MenuModelRegistry): void {
menus.registerMenuAction(NavigatorContextMenu.NAVIGATION, {
commandId: OPEN_CONTAINING_FOLDER.id,
label: OPEN_CONTAINING_FOLDER.label
});
}

registerKeybindings(keybindings: KeybindingRegistry): void {
keybindings.registerKeybinding({
command: OPEN_CONTAINING_FOLDER.id,
keybinding: 'ctrlcmd+alt+p',
when: 'filesExplorerFocus'
});
}

protected getSelectedFileStatNodes(): FileStatNode[] {
const navigator = this.tryGetNavigatorWidget();
return navigator ? navigator.model.selectedNodes.filter(FileStatNode.is) : [];
}

tryGetNavigatorWidget(): FileNavigatorWidget | undefined {
return this.widgetManager.tryGetWidget(FILE_NAVIGATOR_ID);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/********************************************************************************
* Copyright (C) 2021 EclipseSource 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 { CommandContribution, MenuContribution } from '@theia/core';
import { ContainerModule } from '@theia/core/shared/inversify';
import { KeybindingContribution } from '@theia/core/lib/browser';
import { ElectronNavigatorMenuContribution } from './electron-navigator-menu-contribution';

export default new ContainerModule(bind => {
bind(MenuContribution).to(ElectronNavigatorMenuContribution).inSingletonScope();
bind(CommandContribution).to(ElectronNavigatorMenuContribution).inSingletonScope();
bind(KeybindingContribution).to(ElectronNavigatorMenuContribution).inSingletonScope();
});

0 comments on commit 422a778

Please sign in to comment.