Skip to content

Commit f385213

Browse files
authored
feat: Add 'delete' into the view context menu (#343)
1 parent b1fc947 commit f385213

File tree

12 files changed

+127
-6
lines changed

12 files changed

+127
-6
lines changed

package.json

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@
139139
"command": "java.view.package.newPackage",
140140
"title": "%contributes.commands.java.view.package.newPackage%",
141141
"category": "Java"
142+
},
143+
{
144+
"command": "java.view.package.moveFileToTrash",
145+
"title": "%contributes.commands.java.view.package.moveFileToTrash%",
146+
"category": "Java"
142147
}
143148
],
144149
"configuration": {
@@ -176,6 +181,13 @@
176181
}
177182
}
178183
},
184+
"keybindings": [
185+
{
186+
"command": "java.view.package.moveFileToTrash",
187+
"key": "delete",
188+
"when": "java:projectManagerActivated && focusedView == javaProjectExplorer"
189+
}
190+
],
179191
"menus": {
180192
"commandPalette": [
181193
{
@@ -234,6 +246,10 @@
234246
"command": "java.view.package.newPackage",
235247
"when": "false"
236248
},
249+
{
250+
"command": "java.view.package.moveFileToTrash",
251+
"when": "false"
252+
},
237253
{
238254
"command": "java.project.build.workspace",
239255
"when": "false"
@@ -322,17 +338,32 @@
322338
{
323339
"command": "java.view.package.revealFileInOS",
324340
"when": "view == javaProjectExplorer && viewItem =~ /java:(?=.*?\\b\\+uri\\b)/",
325-
"group": "path@10"
341+
"group": "6_copypath@10"
326342
},
327343
{
328344
"command": "java.view.package.copyFilePath",
329345
"when": "view == javaProjectExplorer && viewItem =~ /java:(?=.*?\\b\\+uri\\b)/",
330-
"group": "path@20"
346+
"group": "6_copypath@20"
331347
},
332348
{
333349
"command": "java.view.package.copyRelativeFilePath",
334350
"when": "view == javaProjectExplorer && viewItem =~ /java:(?=.*?\\b\\+uri\\b)/",
335-
"group": "path@25"
351+
"group": "6_copypath@25"
352+
},
353+
{
354+
"command": "java.view.package.moveFileToTrash",
355+
"when": "view == javaProjectExplorer && viewItem =~ /java:(package|packageRoot)(?=.*?\\b\\+source\\b)(?=.*?\\b\\+uri\\b)/",
356+
"group": "7_modification@10"
357+
},
358+
{
359+
"command": "java.view.package.moveFileToTrash",
360+
"when": "view == javaProjectExplorer && viewItem =~ /java:file(?=.*?\\b\\+uri\\b)/",
361+
"group": "7_modification@10"
362+
},
363+
{
364+
"command": "java.view.package.moveFileToTrash",
365+
"when": "view == javaProjectExplorer && viewItem =~ /java:type(?=.*?\\b\\+uri\\b)/",
366+
"group": "7_modification@10"
336367
},
337368
{
338369
"command": "java.view.package.newJavaClass",

package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"contributes.commands.java.view.package.copyRelativeFilePath": "Copy Relative Path",
1818
"contributes.commands.java.view.package.newJavaClass": "New Java Class",
1919
"contributes.commands.java.view.package.newPackage": "New Package",
20+
"contributes.commands.java.view.package.moveFileToTrash": "Delete",
2021
"configuration.java.dependency.showMembers": "Show the members in the explorer",
2122
"configuration.java.dependency.syncWithFolderExplorer": "Synchronize Java Projects explorer selection with folder explorer",
2223
"configuration.java.dependency.autoRefresh": "Synchronize Java Projects explorer with changes",

package.nls.zh.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"contributes.commands.java.view.package.copyRelativeFilePath": "复制相对路径",
1818
"contributes.commands.java.view.package.newJavaClass": "创建 Java 类",
1919
"contributes.commands.java.view.package.newPackage": "创建包",
20+
"contributes.commands.java.view.package.moveFileToTrash": "删除",
2021
"configuration.java.dependency.showMembers": "在 Java 项目管理器中显示成员",
2122
"configuration.java.dependency.syncWithFolderExplorer": "在 Java 项目管理器中同步关联当前打开的文件",
2223
"configuration.java.dependency.autoRefresh": "在 Java 项目管理器中自动同步修改",

src/commands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export namespace Commands {
3636

3737
export const VIEW_PACKAGE_NEW_JAVA_PACKAGE = "java.view.package.newPackage";
3838

39+
export const VIEW_PACKAGE_MOVE_FILE_TO_TRASH = "java.view.package.moveFileToTrash";
40+
3941
export const VIEW_PACKAGE_REVEAL_IN_PROJECT_EXPLORER = "java.view.package.revealInProjectExplorer";
4042

4143
export const JAVA_PROJECT_CREATE = "java.project.create";

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export namespace Explorer {
1414
PackageRoot = "packageRoot",
1515
Package = "package",
1616
Jar = "jar",
17+
File = "file",
18+
Type = "type",
1719
}
1820
}
1921

src/explorerCommands/delete.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
import { Uri, window, workspace } from "vscode";
5+
import { DataNode } from "../views/dataNode";
6+
import { ExplorerNode } from "../views/explorerNode";
7+
import { isMutable } from "./utils";
8+
9+
const confirmMessage = "Move to Recycle Bin";
10+
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+
// avoid delete dependency files
16+
if (!isMutable(node)) {
17+
return;
18+
}
19+
}
20+
21+
const children = await node.getChildren();
22+
const isFolder = children && children.length !== 0;
23+
const message = getInformationMessage(node.name, isFolder);
24+
25+
const answer: string | undefined = await window.showInformationMessage(
26+
message,
27+
{ modal: true },
28+
confirmMessage,
29+
);
30+
31+
if (answer === confirmMessage) {
32+
workspace.fs.delete(Uri.parse(node.uri), {
33+
recursive: true,
34+
useTrash: true,
35+
});
36+
}
37+
}
38+
39+
function getInformationMessage(name: string, isFolder: boolean): string {
40+
const folderMsg = isFolder ? " and its contents" : "";
41+
const msg = `Are you sure you want to delete \'${name}\'${folderMsg}?\n\n`;
42+
const additionMsg = "You can restore from the Recycle Bin.";
43+
return msg + additionMsg;
44+
}

src/explorerCommands/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
import { DataNode } from "../views/dataNode";
5+
6+
export function isMutable(node: DataNode): boolean {
7+
const packageExp = /java:(package|packageRoot)(?=.*?\b\+source\b)(?=.*?\b\+uri\b)/;
8+
const fileExp = /java:file(?=.*?\b\+uri\b)/;
9+
const typeExp = /java:type(?=.*?\b\+uri\b)/;
10+
11+
const contextValue = node.computeContextValue();
12+
return packageExp.test(contextValue) || fileExp.test(contextValue) || typeExp.test(contextValue);
13+
}

src/views/PrimaryTypeNode.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { Command, commands, DocumentSymbol, SymbolInformation, SymbolKind, TextDocument, ThemeIcon, Uri, workspace } from "vscode";
55
import { createUuid, sendOperationEnd, sendOperationStart } from "vscode-extension-telemetry-wrapper";
66
import { Commands } from "../commands";
7+
import { Explorer } from "../constants";
78
import { INodeData, TypeKind } from "../java/nodeData";
89
import { Settings } from "../settings";
910
import { DataNode } from "./dataNode";
@@ -88,4 +89,17 @@ export class PrimaryTypeNode extends DataNode {
8889
arguments: [this.uri],
8990
};
9091
}
92+
93+
protected get contextValue(): string {
94+
const context = Explorer.ContextValueType.Type;
95+
const type = this.nodeData.metaData[PrimaryTypeNode.K_TYPE_KIND];
96+
97+
if (type === TypeKind.Enum) {
98+
return `${context}+enum`;
99+
} else if (type === TypeKind.Interface) {
100+
return `${context}+interface`;
101+
} else {
102+
return `${context}+class`;
103+
}
104+
}
91105
}

src/views/dataNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export abstract class DataNode extends ExplorerNode {
7474
}
7575
}
7676

77-
protected computeContextValue(): string {
77+
public computeContextValue(): string {
7878
let contextValue = this.contextValue;
7979
if (this.uri && this.uri.startsWith("file:")) {
8080
contextValue = `${contextValue || ""}+uri`;

src/views/dependencyDataProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ export class DependencyDataProvider implements TreeDataProvider<ExplorerNode> {
4646
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_COPY_RELATIVE_FILE_PATH, (node: INodeData) =>
4747
commands.executeCommand("copyRelativeFilePath", Uri.parse(node.uri))));
4848
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_OPEN_FILE, (uri) =>
49-
commands.executeCommand(Commands.VSCODE_OPEN, Uri.parse(uri))));
49+
commands.executeCommand(Commands.VSCODE_OPEN, Uri.parse(uri), { preserveFocus: true })));
5050
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_OUTLINE, (uri, range) =>
51-
window.showTextDocument(Uri.parse(uri), { selection: range })));
51+
window.showTextDocument(Uri.parse(uri), { selection: range })));
5252
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_BUILD_WORKSPACE, () =>
5353
commands.executeCommand(Commands.JAVA_BUILD_WORKSPACE)));
5454
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_CLEAN_WORKSPACE, () =>

0 commit comments

Comments
 (0)