Skip to content

Commit 2f1888d

Browse files
authored
feat: Add more key bindings copy path actions (#393)
1 parent 70cd176 commit 2f1888d

File tree

6 files changed

+67
-27
lines changed

6 files changed

+67
-27
lines changed

package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,27 @@
211211
}
212212
},
213213
"keybindings": [
214+
{
215+
"command": "java.view.package.revealFileInOS",
216+
"key": "ctrl+alt+r",
217+
"win": "shift+alt+r",
218+
"mac": "cmd+alt+r",
219+
"when": "java:projectManagerActivated && focusedView == javaProjectExplorer"
220+
},
221+
{
222+
"command": "java.view.package.copyFilePath",
223+
"key": "ctrl+alt+c",
224+
"win": "shift+alt+c",
225+
"mac": "cmd+alt+c",
226+
"when": "java:projectManagerActivated && focusedView == javaProjectExplorer"
227+
},
228+
{
229+
"command": "java.view.package.copyRelativeFilePath",
230+
"key": "ctrl+shift+alt+c",
231+
"win": "ctrl+k ctrl+shift+c",
232+
"mac": "cmd+shift+alt+c",
233+
"when": "java:projectManagerActivated && focusedView == javaProjectExplorer"
234+
},
214235
{
215236
"command": "java.view.package.renameFile",
216237
"key": "F2",

src/explorerCommands/delete.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33

44
import { Uri, window, workspace } from "vscode";
55
import { DataNode } from "../views/dataNode";
6-
import { ExplorerNode } from "../views/explorerNode";
76
import { isMutable } from "./utility";
87

98
const confirmMessage = "Move to Recycle Bin";
109

11-
export async function deleteFiles(node: DataNode, selectedNode: ExplorerNode): Promise<void> {
12-
// if command not invoked by context menu, use selected node in explorer
13-
if (!node) {
14-
node = selectedNode as DataNode;
15-
if (!isMutable(node)) {
16-
return;
17-
}
10+
export async function deleteFiles(node: DataNode): Promise<void> {
11+
if (!isMutable(node) || !node.uri) {
12+
return;
1813
}
1914

2015
const children = await node.getChildren();

src/explorerCommands/rename.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@ import * as path from "path";
66
import { Uri, window, workspace, WorkspaceEdit } from "vscode";
77
import { NodeKind } from "../java/nodeData";
88
import { DataNode } from "../views/dataNode";
9-
import { ExplorerNode } from "../views/explorerNode";
109
import { checkJavaQualifiedName, isMutable } from "./utility";
1110

12-
export async function renameFile(node: DataNode, selectedNode: ExplorerNode): Promise<void> {
13-
// if command not invoked by context menu, use selected node in explorer
14-
if (!node) {
15-
node = selectedNode as DataNode;
16-
if (!isMutable(node)) {
17-
return;
18-
}
11+
export async function renameFile(node: DataNode): Promise<void> {
12+
if (!isMutable(node) || !node.uri) {
13+
return;
1914
}
2015

2116
const oldFsPath = Uri.parse(node.uri).fsPath;
@@ -64,7 +59,7 @@ function getPrefillValue(node: DataNode): string {
6459
if (nodeKind === NodeKind.PrimaryType) {
6560
return node.name;
6661
}
67-
return path.basename(node.uri);
62+
return path.basename(node.uri!);
6863
}
6964

7065
function getValueSelection(uri: string): [number, number] | undefined {

src/explorerCommands/utility.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import { isJavaIdentifier, isKeyword } from "../utility";
55
import { DataNode } from "../views/dataNode";
6+
import { ExplorerNode } from "../views/explorerNode";
67

78
export function isMutable(node: DataNode): boolean {
89
// avoid modify dependency files
@@ -30,3 +31,8 @@ export function checkJavaQualifiedName(value: string): string {
3031

3132
return "";
3233
}
34+
35+
export function getCmdNode(selectedNode: ExplorerNode, node?: DataNode): DataNode {
36+
// if command not invoked by context menu, use selected node in explorer
37+
return node ? node : selectedNode as DataNode;
38+
}

src/views/dependencyDataProvider.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ export class DependencyDataProvider implements TreeDataProvider<ExplorerNode> {
4141
}));
4242
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_CLASS, (node: DataNode) => newJavaClass(node)));
4343
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_PACKAGE, (node: DataNode) => newPackage(node)));
44-
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_REVEAL_FILE_OS, (node?: INodeData) =>
45-
commands.executeCommand("revealFileInOS", Uri.parse(node.uri))));
46-
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_COPY_FILE_PATH, (node: INodeData) =>
47-
commands.executeCommand("copyFilePath", Uri.parse(node.uri))));
48-
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_COPY_RELATIVE_FILE_PATH, (node: INodeData) =>
49-
commands.executeCommand("copyRelativeFilePath", Uri.parse(node.uri))));
5044
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_OPEN_FILE, (uri) =>
5145
commands.executeCommand(Commands.VSCODE_OPEN, Uri.parse(uri), { preserveFocus: true })));
5246
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_OUTLINE, (uri, range) =>

src/views/dependencyExplorer.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Commands } from "../commands";
99
import { Build } from "../constants";
1010
import { deleteFiles } from "../explorerCommands/delete";
1111
import { renameFile } from "../explorerCommands/rename";
12+
import { getCmdNode } from "../explorerCommands/utility";
1213
import { isStandardServerReady } from "../extension";
1314
import { Jdtls } from "../java/jdtls";
1415
import { INodeData } from "../java/nodeData";
@@ -84,15 +85,43 @@ export class DependencyExplorer implements Disposable {
8485
}),
8586
);
8687

88+
// register keybinding commands
8789
context.subscriptions.push(
88-
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_RENAME_FILE, (node: DataNode) => {
89-
renameFile(node, this._dependencyViewer.selection[0]);
90+
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_REVEAL_FILE_OS, (node?: DataNode) => {
91+
const cmdNode = getCmdNode(this._dependencyViewer.selection[0], node);
92+
if (cmdNode.uri) {
93+
commands.executeCommand("revealFileInOS", Uri.parse(cmdNode.uri));
94+
}
95+
}),
96+
);
97+
98+
context.subscriptions.push(
99+
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_COPY_FILE_PATH, (node?: DataNode) => {
100+
const cmdNode = getCmdNode(this._dependencyViewer.selection[0], node);
101+
if (cmdNode.uri) {
102+
commands.executeCommand("copyFilePath", Uri.parse(cmdNode.uri));
103+
}
104+
}),
105+
);
106+
107+
context.subscriptions.push(
108+
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_COPY_RELATIVE_FILE_PATH, (node?: DataNode) => {
109+
const cmdNode = getCmdNode(this._dependencyViewer.selection[0], node);
110+
if (cmdNode.uri) {
111+
commands.executeCommand("copyRelativeFilePath", Uri.parse(cmdNode.uri));
112+
}
113+
}),
114+
);
115+
116+
context.subscriptions.push(
117+
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_RENAME_FILE, (node?: DataNode) => {
118+
renameFile(getCmdNode(this._dependencyViewer.selection[0], node));
90119
}),
91120
);
92121

93122
context.subscriptions.push(
94-
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_MOVE_FILE_TO_TRASH, (node: DataNode) => {
95-
deleteFiles(node, this._dependencyViewer.selection[0]);
123+
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_MOVE_FILE_TO_TRASH, (node?: DataNode) => {
124+
deleteFiles(getCmdNode(this._dependencyViewer.selection[0], node));
96125
}),
97126
);
98127
}

0 commit comments

Comments
 (0)