Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add 'delete' into the view context menu #343

Merged
merged 16 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@
"command": "java.view.package.newPackage",
"title": "%contributes.commands.java.view.package.newPackage%",
"category": "Java"
},
{
"command": "java.view.package.moveFileToTrash",
"title": "%contributes.commands.java.view.package.moveFileToTrash%",
"category": "Java"
}
],
"configuration": {
Expand Down Expand Up @@ -176,6 +181,13 @@
}
}
},
"keybindings": [
{
"command": "java.view.package.moveFileToTrash",
"key": "delete",
"when": "java:projectManagerActivated && focusedView == javaProjectExplorer"
}
],
"menus": {
"commandPalette": [
{
Expand Down Expand Up @@ -234,6 +246,10 @@
"command": "java.view.package.newPackage",
"when": "false"
},
{
"command": "java.view.package.moveFileToTrash",
"when": "false"
},
{
"command": "java.project.build.workspace",
"when": "false"
Expand Down Expand Up @@ -322,17 +338,32 @@
{
"command": "java.view.package.revealFileInOS",
"when": "view == javaProjectExplorer && viewItem =~ /java:(?=.*?\\b\\+uri\\b)/",
"group": "path@10"
"group": "6_copypath@10"
},
{
"command": "java.view.package.copyFilePath",
"when": "view == javaProjectExplorer && viewItem =~ /java:(?=.*?\\b\\+uri\\b)/",
"group": "path@20"
"group": "6_copypath@20"
},
{
"command": "java.view.package.copyRelativeFilePath",
"when": "view == javaProjectExplorer && viewItem =~ /java:(?=.*?\\b\\+uri\\b)/",
"group": "path@25"
"group": "6_copypath@25"
},
{
"command": "java.view.package.moveFileToTrash",
"when": "view == javaProjectExplorer && viewItem =~ /java:(package|packageRoot)(?=.*?\\b\\+source\\b)(?=.*?\\b\\+uri\\b)/",
"group": "7_modification@10"
},
{
"command": "java.view.package.moveFileToTrash",
"when": "view == javaProjectExplorer && viewItem =~ /java:file(?=.*?\\b\\+uri\\b)/",
"group": "7_modification@10"
},
{
"command": "java.view.package.moveFileToTrash",
"when": "view == javaProjectExplorer && viewItem =~ /java:type(?=.*?\\b\\+uri\\b)/",
"group": "7_modification@10"
},
{
"command": "java.view.package.newJavaClass",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"contributes.commands.java.view.package.copyRelativeFilePath": "Copy Relative Path",
"contributes.commands.java.view.package.newJavaClass": "New Java Class",
"contributes.commands.java.view.package.newPackage": "New Package",
"contributes.commands.java.view.package.moveFileToTrash": "Delete",
"configuration.java.dependency.showMembers": "Show the members in the explorer",
"configuration.java.dependency.syncWithFolderExplorer": "Synchronize Java Projects explorer selection with folder explorer",
"configuration.java.dependency.autoRefresh": "Synchronize Java Projects explorer with changes",
Expand Down
1 change: 1 addition & 0 deletions package.nls.zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"contributes.commands.java.view.package.copyRelativeFilePath": "复制相对路径",
"contributes.commands.java.view.package.newJavaClass": "创建 Java 类",
"contributes.commands.java.view.package.newPackage": "创建包",
"contributes.commands.java.view.package.moveFileToTrash": "删除",
"configuration.java.dependency.showMembers": "在 Java 项目管理器中显示成员",
"configuration.java.dependency.syncWithFolderExplorer": "在 Java 项目管理器中同步关联当前打开的文件",
"configuration.java.dependency.autoRefresh": "在 Java 项目管理器中自动同步修改",
Expand Down
2 changes: 2 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export namespace Commands {

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

export const VIEW_PACKAGE_MOVE_FILE_TO_TRASH = "java.view.package.moveFileToTrash";

export const VIEW_PACKAGE_REVEAL_IN_PROJECT_EXPLORER = "java.view.package.revealInProjectExplorer";

export const JAVA_PROJECT_CREATE = "java.project.create";
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export namespace Explorer {
PackageRoot = "packageRoot",
Package = "package",
Jar = "jar",
File = "file",
Type = "type",
}
}

Expand Down
45 changes: 45 additions & 0 deletions src/explorerCommands/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import { commands, Uri, window, workspace } from "vscode";
import { Commands } from "../commands";
xqzlgy2 marked this conversation as resolved.
Show resolved Hide resolved
import { DataNode } from "../views/dataNode";
import { ExplorerNode } from "../views/explorerNode";
import { isMutable } from "./utils";

const confirmMessage = "Move to Recycle Bin";

export async function deleteFiles(node: DataNode, selectedNode: ExplorerNode): Promise<void> {
// if command not invoked by context menu, use selected node in explorer
if (!node) {
node = selectedNode as DataNode;
// avoid delete dependency files
if (!isMutable(node)) {
return;
}
}

const children = await node.getChildren();
const isFolder = children && children.length !== 0;
const message = getInformationMessage(node.name, isFolder);

const answer: string | undefined = await window.showInformationMessage(
message,
{ modal: true },
confirmMessage,
);

if (answer === confirmMessage) {
workspace.fs.delete(Uri.parse(node.uri), {
recursive: true,
useTrash: true,
});
}
}

function getInformationMessage(name: string, isFolder: boolean): string {
const folderMsg = isFolder ? " and its contents" : "";
const msg = `Are you sure you want to delete \'${name}\'${folderMsg}?\n\n`;
const additionMsg = "You can restore from the Recycle Bin.";
return msg + additionMsg;
}
13 changes: 13 additions & 0 deletions src/explorerCommands/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import { DataNode } from "../views/dataNode";

export function isMutable(node: DataNode): boolean {
const packageExp = /java:(package|packageRoot)(?=.*?\b\+source\b)(?=.*?\b\+uri\b)/;
const fileExp = /java:file(?=.*?\b\+uri\b)/;
const typeExp = /java:type(?=.*?\b\+uri\b)/;

const contextValue = node.computeContextValue();
return packageExp.test(contextValue) || fileExp.test(contextValue) || typeExp.test(contextValue);
}
14 changes: 14 additions & 0 deletions src/views/PrimaryTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { Command, commands, DocumentSymbol, SymbolInformation, SymbolKind, TextDocument, ThemeIcon, Uri, workspace } from "vscode";
import { createUuid, sendOperationEnd, sendOperationStart } from "vscode-extension-telemetry-wrapper";
import { Commands } from "../commands";
import { Explorer } from "../constants";
import { INodeData, TypeKind } from "../java/nodeData";
import { Settings } from "../settings";
import { DataNode } from "./dataNode";
Expand Down Expand Up @@ -88,4 +89,17 @@ export class PrimaryTypeNode extends DataNode {
arguments: [this.uri],
};
}

protected get contextValue(): string {
const context = Explorer.ContextValueType.Type;
const type = this.nodeData.metaData[PrimaryTypeNode.K_TYPE_KIND];

if (type === TypeKind.Enum) {
return `${context}+enum`;
} else if (type === TypeKind.Interface) {
return `${context}+interface`;
} else {
return `${context}+class`;
}
}
}
2 changes: 1 addition & 1 deletion src/views/dataNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export abstract class DataNode extends ExplorerNode {
}
}

protected computeContextValue(): string {
public computeContextValue(): string {
let contextValue = this.contextValue;
if (this.uri && this.uri.startsWith("file:")) {
contextValue = `${contextValue || ""}+uri`;
Expand Down
4 changes: 2 additions & 2 deletions src/views/dependencyDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export class DependencyDataProvider implements TreeDataProvider<ExplorerNode> {
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))));
commands.executeCommand(Commands.VSCODE_OPEN, Uri.parse(uri), { preserveFocus: true })));
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_OUTLINE, (uri, range) =>
window.showTextDocument(Uri.parse(uri), { selection: range })));
window.showTextDocument(Uri.parse(uri), { selection: range })));
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_BUILD_WORKSPACE, () =>
commands.executeCommand(Commands.JAVA_BUILD_WORKSPACE)));
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_CLEAN_WORKSPACE, () =>
Expand Down
8 changes: 8 additions & 0 deletions src/views/dependencyExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { commands, Disposable, ExtensionContext, TextEditor, TreeView, TreeViewV
import { instrumentOperationAsVsCodeCommand } from "vscode-extension-telemetry-wrapper";
import { Commands } from "../commands";
import { Build } from "../constants";
import { deleteFiles } from "../explorerCommands/delete";
import { isStandardServerReady } from "../extension";
import { Jdtls } from "../java/jdtls";
import { INodeData } from "../java/nodeData";
Expand Down Expand Up @@ -72,6 +73,13 @@ export class DependencyExplorer implements Disposable {
this.reveal(uri);
}),
);

context.subscriptions.push(
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_MOVE_FILE_TO_TRASH, (node: DataNode) => {
const firstSelectedNode = this._dependencyViewer.selection[0];
deleteFiles(node, firstSelectedNode);
}),
);
}

public dispose(): void {
Expand Down
5 changes: 5 additions & 0 deletions src/views/fileNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { Command, ThemeColor, ThemeIcon, TreeItem, TreeItemCollapsibleState } from "vscode";
import { Commands } from "../commands";
import { Explorer } from "../constants";
import { INodeData } from "../java/nodeData";
import { DataNode } from "./dataNode";
import { ExplorerNode } from "./explorerNode";
Expand Down Expand Up @@ -35,4 +36,8 @@ export class FileNode extends DataNode {
arguments: [this.uri],
};
}

protected get contextValue(): string {
return Explorer.ContextValueType.File;
}
}