diff --git a/package.json b/package.json index ad0195c7..1bf3f7b4 100644 --- a/package.json +++ b/package.json @@ -211,6 +211,27 @@ } }, "keybindings": [ + { + "command": "java.view.package.revealFileInOS", + "key": "ctrl+alt+r", + "win": "shift+alt+r", + "mac": "cmd+alt+r", + "when": "java:projectManagerActivated && focusedView == javaProjectExplorer" + }, + { + "command": "java.view.package.copyFilePath", + "key": "ctrl+alt+c", + "win": "shift+alt+c", + "mac": "cmd+alt+c", + "when": "java:projectManagerActivated && focusedView == javaProjectExplorer" + }, + { + "command": "java.view.package.copyRelativeFilePath", + "key": "ctrl+shift+alt+c", + "win": "ctrl+k ctrl+shift+c", + "mac": "cmd+shift+alt+c", + "when": "java:projectManagerActivated && focusedView == javaProjectExplorer" + }, { "command": "java.view.package.renameFile", "key": "F2", diff --git a/src/explorerCommands/delete.ts b/src/explorerCommands/delete.ts index cf5cda47..036c8c96 100644 --- a/src/explorerCommands/delete.ts +++ b/src/explorerCommands/delete.ts @@ -3,18 +3,13 @@ import { Uri, window, workspace } from "vscode"; import { DataNode } from "../views/dataNode"; -import { ExplorerNode } from "../views/explorerNode"; import { isMutable } from "./utility"; const confirmMessage = "Move to Recycle Bin"; -export async function deleteFiles(node: DataNode, selectedNode: ExplorerNode): Promise { - // if command not invoked by context menu, use selected node in explorer - if (!node) { - node = selectedNode as DataNode; - if (!isMutable(node)) { - return; - } +export async function deleteFiles(node: DataNode): Promise { + if (!isMutable(node) || !node.uri) { + return; } const children = await node.getChildren(); diff --git a/src/explorerCommands/rename.ts b/src/explorerCommands/rename.ts index 356f1142..d5433042 100644 --- a/src/explorerCommands/rename.ts +++ b/src/explorerCommands/rename.ts @@ -6,16 +6,11 @@ import * as path from "path"; import { Uri, window, workspace, WorkspaceEdit } from "vscode"; import { NodeKind } from "../java/nodeData"; import { DataNode } from "../views/dataNode"; -import { ExplorerNode } from "../views/explorerNode"; import { checkJavaQualifiedName, isMutable } from "./utility"; -export async function renameFile(node: DataNode, selectedNode: ExplorerNode): Promise { - // if command not invoked by context menu, use selected node in explorer - if (!node) { - node = selectedNode as DataNode; - if (!isMutable(node)) { - return; - } +export async function renameFile(node: DataNode): Promise { + if (!isMutable(node) || !node.uri) { + return; } const oldFsPath = Uri.parse(node.uri).fsPath; @@ -64,7 +59,7 @@ function getPrefillValue(node: DataNode): string { if (nodeKind === NodeKind.PrimaryType) { return node.name; } - return path.basename(node.uri); + return path.basename(node.uri!); } function getValueSelection(uri: string): [number, number] | undefined { diff --git a/src/explorerCommands/utility.ts b/src/explorerCommands/utility.ts index c7829a84..b08668cd 100644 --- a/src/explorerCommands/utility.ts +++ b/src/explorerCommands/utility.ts @@ -3,6 +3,7 @@ import { isJavaIdentifier, isKeyword } from "../utility"; import { DataNode } from "../views/dataNode"; +import { ExplorerNode } from "../views/explorerNode"; export function isMutable(node: DataNode): boolean { // avoid modify dependency files @@ -30,3 +31,8 @@ export function checkJavaQualifiedName(value: string): string { return ""; } + +export function getCmdNode(selectedNode: ExplorerNode, node?: DataNode): DataNode { + // if command not invoked by context menu, use selected node in explorer + return node ? node : selectedNode as DataNode; +} diff --git a/src/views/dependencyDataProvider.ts b/src/views/dependencyDataProvider.ts index a2724928..a948a64a 100644 --- a/src/views/dependencyDataProvider.ts +++ b/src/views/dependencyDataProvider.ts @@ -41,12 +41,6 @@ export class DependencyDataProvider implements TreeDataProvider { })); context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_CLASS, (node: DataNode) => newJavaClass(node))); context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_PACKAGE, (node: DataNode) => newPackage(node))); - context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_REVEAL_FILE_OS, (node?: INodeData) => - commands.executeCommand("revealFileInOS", Uri.parse(node.uri)))); - context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_COPY_FILE_PATH, (node: INodeData) => - commands.executeCommand("copyFilePath", Uri.parse(node.uri)))); - context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_COPY_RELATIVE_FILE_PATH, (node: INodeData) => - commands.executeCommand("copyRelativeFilePath", Uri.parse(node.uri)))); context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_OPEN_FILE, (uri) => commands.executeCommand(Commands.VSCODE_OPEN, Uri.parse(uri), { preserveFocus: true }))); context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_OUTLINE, (uri, range) => diff --git a/src/views/dependencyExplorer.ts b/src/views/dependencyExplorer.ts index 32f5ba3d..4b4c2e19 100644 --- a/src/views/dependencyExplorer.ts +++ b/src/views/dependencyExplorer.ts @@ -9,6 +9,7 @@ import { Commands } from "../commands"; import { Build } from "../constants"; import { deleteFiles } from "../explorerCommands/delete"; import { renameFile } from "../explorerCommands/rename"; +import { getCmdNode } from "../explorerCommands/utility"; import { isStandardServerReady } from "../extension"; import { Jdtls } from "../java/jdtls"; import { INodeData } from "../java/nodeData"; @@ -84,15 +85,43 @@ export class DependencyExplorer implements Disposable { }), ); + // register keybinding commands context.subscriptions.push( - instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_RENAME_FILE, (node: DataNode) => { - renameFile(node, this._dependencyViewer.selection[0]); + instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_REVEAL_FILE_OS, (node?: DataNode) => { + const cmdNode = getCmdNode(this._dependencyViewer.selection[0], node); + if (cmdNode.uri) { + commands.executeCommand("revealFileInOS", Uri.parse(cmdNode.uri)); + } + }), + ); + + context.subscriptions.push( + instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_COPY_FILE_PATH, (node?: DataNode) => { + const cmdNode = getCmdNode(this._dependencyViewer.selection[0], node); + if (cmdNode.uri) { + commands.executeCommand("copyFilePath", Uri.parse(cmdNode.uri)); + } + }), + ); + + context.subscriptions.push( + instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_COPY_RELATIVE_FILE_PATH, (node?: DataNode) => { + const cmdNode = getCmdNode(this._dependencyViewer.selection[0], node); + if (cmdNode.uri) { + commands.executeCommand("copyRelativeFilePath", Uri.parse(cmdNode.uri)); + } + }), + ); + + context.subscriptions.push( + instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_RENAME_FILE, (node?: DataNode) => { + renameFile(getCmdNode(this._dependencyViewer.selection[0], node)); }), ); context.subscriptions.push( - instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_MOVE_FILE_TO_TRASH, (node: DataNode) => { - deleteFiles(node, this._dependencyViewer.selection[0]); + instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_MOVE_FILE_TO_TRASH, (node?: DataNode) => { + deleteFiles(getCmdNode(this._dependencyViewer.selection[0], node)); }), ); }