From a63de4210c00597dbb019ab7f8d7f29a0728ac56 Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Mon, 1 Jul 2024 16:43:34 +0800 Subject: [PATCH] feat - Support add new package from file explorer --- package.json | 34 +++++++++---- package.nls.json | 1 + src/commands.ts | 2 + src/explorerCommands/new.ts | 89 ++++++++++++++++++++++----------- src/views/dependencyExplorer.ts | 3 ++ 5 files changed, 91 insertions(+), 38 deletions(-) diff --git a/package.json b/package.json index 9bdb4752..d23f141a 100644 --- a/package.json +++ b/package.json @@ -257,6 +257,11 @@ "title": "%contributes.commands.java.view.package.newPackage%", "category": "Java" }, + { + "command": "java.view.explorer.newPackage", + "title": "%contributes.commands.java.view.explorer.newPackage%", + "category": "Java" + }, { "command": "java.view.package.newFile", "title": "%contributes.commands.java.view.package.newFile%", @@ -502,6 +507,10 @@ "command": "java.view.package.deleteFilePermanently", "when": "false" }, + { + "command": "java.view.explorer.newPackage", + "when": "false" + }, { "command": "java.project.build.workspace", "when": "false" @@ -545,24 +554,29 @@ ], "explorer/context": [ { - "command": "java.view.package.revealInProjectExplorer", - "when": "resourceFilename =~ /(.*\\.gradle)|(.*\\.gradle\\.kts)|(pom\\.xml)$/ && java:serverMode == Standard", - "group": "1_javaactions" + "submenu": "javaProject.newJavaFile", + "when": "explorerResourceIsFolder", + "group": "1_javaactions@10" }, { - "command": "java.view.package.revealInProjectExplorer", - "when": "resourceExtname == .java && java:serverMode == Standard", - "group": "1_javaactions" + "command": "java.view.explorer.newPackage", + "when": "explorerResourceIsFolder", + "group": "1_javaactions@20" }, { "command": "_java.project.create.from.fileexplorer.menu", "when": "explorerResourceIsFolder", - "group": "1_javaactions" + "group": "1_javaactions@30" }, { - "submenu": "javaProject.newJavaFile", - "when": "explorerResourceIsFolder", - "group": "1_javaactions" + "command": "java.view.package.revealInProjectExplorer", + "when": "resourceFilename =~ /(.*\\.gradle)|(.*\\.gradle\\.kts)|(pom\\.xml)$/ && java:serverMode == Standard", + "group": "1_javaactions@40" + }, + { + "command": "java.view.package.revealInProjectExplorer", + "when": "resourceExtname == .java && java:serverMode == Standard", + "group": "1_javaactions@40" } ], "editor/title": [ diff --git a/package.nls.json b/package.nls.json index adf5b5cb..12cabf89 100644 --- a/package.nls.json +++ b/package.nls.json @@ -35,6 +35,7 @@ "contributes.commands.java.view.package.renameFile": "Rename", "contributes.commands.java.view.package.moveFileToTrash": "Delete", "contributes.commands.java.view.package.deleteFilePermanently": "Delete Permanently", + "contributes.commands.java.view.explorer.newPackage": "New Java Package...", "contributes.submenus.javaProject.new": "New", "contributes.commands.java.view.menus.file.newJavaClass": "New Java File", "configuration.java.dependency.showMembers": "Show the members in the explorer", diff --git a/src/commands.ts b/src/commands.ts index e84a5fe9..f6b07c58 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -54,6 +54,8 @@ export namespace Commands { export const VIEW_PACKAGE_NEW_JAVA_PACKAGE = "java.view.package.newPackage"; + export const VIEW_EXPLORER_NEW_PACKAGE = "java.view.explorer.newPackage"; + export const VIEW_PACKAGE_RENAME_FILE = "java.view.package.renameFile"; export const VIEW_PACKAGE_MOVE_FILE_TO_TRASH = "java.view.package.moveFileToTrash"; diff --git a/src/explorerCommands/new.ts b/src/explorerCommands/new.ts index 736c49c7..470305a3 100644 --- a/src/explorerCommands/new.ts +++ b/src/explorerCommands/new.ts @@ -513,37 +513,21 @@ function getNewFilePath(basePath: string, className: string): string { return path.join(basePath, ...className.split(".")) + ".java"; } -export async function newPackage(node?: DataNode): Promise { - if (!node?.uri || !canCreatePackage(node)) { +export async function newPackage(node: DataNode | Uri | undefined): Promise { + if (!node) { return; } - let defaultValue: string; - let packageRootPath: string; - const nodeKind = node.nodeData.kind; - if (nodeKind === NodeKind.Project) { - defaultValue = ""; - packageRootPath = await getPackageFsPath(node) || ""; - } else if (nodeKind === NodeKind.PackageRoot) { - defaultValue = ""; - packageRootPath = Uri.parse(node.uri).fsPath; - } else if (nodeKind === NodeKind.Package) { - defaultValue = node.nodeData.name + "."; - packageRootPath = getPackageRootPath(Uri.parse(node.uri).fsPath, node.nodeData.name); - } else if (nodeKind === NodeKind.PrimaryType) { - const primaryTypeNode = node; - packageRootPath = primaryTypeNode.getPackageRootPath(); - if (packageRootPath === "") { - window.showErrorMessage("Failed to get the package root path."); - return; - } - const packagePath = await getPackageFsPath(node); - if (!packagePath) { - window.showErrorMessage("Failed to get the package path."); - return; - } - defaultValue = path.relative(packageRootPath, packagePath).replace(/[\\\/]/g, ".") + "."; - } else { + if (node instanceof DataNode && (!node.uri || !canCreatePackage(node))) { + return; + } + + sendInfo("", { + "triggernewpackagefrom": node instanceof Uri ? "fileExplorer" : "javaProjectExplorer", + }); + + const {defaultValue, packageRootPath} = await getPackagePromptInformation(node) || {}; + if (defaultValue === undefined|| packageRootPath === undefined) { return; } @@ -570,9 +554,58 @@ export async function newPackage(node?: DataNode): Promise { return; } + sendInfo("", { + "packageNameContainsDot": packageName.includes(".").toString(), + }); + await fse.ensureDir(getNewPackagePath(packageRootPath, packageName)); } +async function getPackagePromptInformation(node: DataNode | Uri): Promise | undefined> { + if (node instanceof Uri) { + return { + packageRootPath: node.fsPath, + defaultValue: "", + } + } else if (node instanceof DataNode) { + const nodeKind = node.nodeData.kind; + if (nodeKind === NodeKind.Project) { + return { + packageRootPath: await getPackageFsPath(node) || "", + defaultValue: "", + } + } else if (nodeKind === NodeKind.PackageRoot) { + return { + packageRootPath: Uri.parse(node.uri!).fsPath, + defaultValue: "", + } + } else if (nodeKind === NodeKind.Package) { + return { + packageRootPath: getPackageRootPath(Uri.parse(node.uri!).fsPath, node.nodeData.name), + defaultValue: node.nodeData.name + ".", + } + } else if (nodeKind === NodeKind.PrimaryType) { + const primaryTypeNode = node; + const packageRootPath = primaryTypeNode.getPackageRootPath(); + if (packageRootPath === "") { + window.showErrorMessage("Failed to get the package root path."); + return undefined; + } + const packagePath = await getPackageFsPath(node); + if (!packagePath) { + window.showErrorMessage("Failed to get the package path."); + return undefined; + } + return { + packageRootPath: packageRootPath, + defaultValue: path.relative(packageRootPath, packagePath).replace(/[\\\/]/g, ".") + ".", + } + } + } + + return undefined; +} + /** * Check if the create package command is available for the given node. * Currently the check logic is the same as the create class command. diff --git a/src/views/dependencyExplorer.ts b/src/views/dependencyExplorer.ts index 8841b4d8..9afb27b8 100644 --- a/src/views/dependencyExplorer.ts +++ b/src/views/dependencyExplorer.ts @@ -147,6 +147,9 @@ export class DependencyExplorer implements Disposable { } newPackage(cmdNode); }), + instrumentOperationAsVsCodeCommand(Commands.VIEW_EXPLORER_NEW_PACKAGE, (node: Uri) => { + newPackage(node); + }), instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_REVEAL_FILE_OS, (node?: DataNode) => { const cmdNode = getCmdNode(this._dependencyViewer.selection, node); if (cmdNode?.uri) {