Skip to content

Commit a63de42

Browse files
committed
feat - Support add new package from file explorer
1 parent 0904e07 commit a63de42

File tree

5 files changed

+91
-38
lines changed

5 files changed

+91
-38
lines changed

package.json

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@
257257
"title": "%contributes.commands.java.view.package.newPackage%",
258258
"category": "Java"
259259
},
260+
{
261+
"command": "java.view.explorer.newPackage",
262+
"title": "%contributes.commands.java.view.explorer.newPackage%",
263+
"category": "Java"
264+
},
260265
{
261266
"command": "java.view.package.newFile",
262267
"title": "%contributes.commands.java.view.package.newFile%",
@@ -502,6 +507,10 @@
502507
"command": "java.view.package.deleteFilePermanently",
503508
"when": "false"
504509
},
510+
{
511+
"command": "java.view.explorer.newPackage",
512+
"when": "false"
513+
},
505514
{
506515
"command": "java.project.build.workspace",
507516
"when": "false"
@@ -545,24 +554,29 @@
545554
],
546555
"explorer/context": [
547556
{
548-
"command": "java.view.package.revealInProjectExplorer",
549-
"when": "resourceFilename =~ /(.*\\.gradle)|(.*\\.gradle\\.kts)|(pom\\.xml)$/ && java:serverMode == Standard",
550-
"group": "1_javaactions"
557+
"submenu": "javaProject.newJavaFile",
558+
"when": "explorerResourceIsFolder",
559+
"group": "1_javaactions@10"
551560
},
552561
{
553-
"command": "java.view.package.revealInProjectExplorer",
554-
"when": "resourceExtname == .java && java:serverMode == Standard",
555-
"group": "1_javaactions"
562+
"command": "java.view.explorer.newPackage",
563+
"when": "explorerResourceIsFolder",
564+
"group": "1_javaactions@20"
556565
},
557566
{
558567
"command": "_java.project.create.from.fileexplorer.menu",
559568
"when": "explorerResourceIsFolder",
560-
"group": "1_javaactions"
569+
"group": "1_javaactions@30"
561570
},
562571
{
563-
"submenu": "javaProject.newJavaFile",
564-
"when": "explorerResourceIsFolder",
565-
"group": "1_javaactions"
572+
"command": "java.view.package.revealInProjectExplorer",
573+
"when": "resourceFilename =~ /(.*\\.gradle)|(.*\\.gradle\\.kts)|(pom\\.xml)$/ && java:serverMode == Standard",
574+
"group": "1_javaactions@40"
575+
},
576+
{
577+
"command": "java.view.package.revealInProjectExplorer",
578+
"when": "resourceExtname == .java && java:serverMode == Standard",
579+
"group": "1_javaactions@40"
566580
}
567581
],
568582
"editor/title": [

package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"contributes.commands.java.view.package.renameFile": "Rename",
3636
"contributes.commands.java.view.package.moveFileToTrash": "Delete",
3737
"contributes.commands.java.view.package.deleteFilePermanently": "Delete Permanently",
38+
"contributes.commands.java.view.explorer.newPackage": "New Java Package...",
3839
"contributes.submenus.javaProject.new": "New",
3940
"contributes.commands.java.view.menus.file.newJavaClass": "New Java File",
4041
"configuration.java.dependency.showMembers": "Show the members in the explorer",

src/commands.ts

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

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

57+
export const VIEW_EXPLORER_NEW_PACKAGE = "java.view.explorer.newPackage";
58+
5759
export const VIEW_PACKAGE_RENAME_FILE = "java.view.package.renameFile";
5860

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

src/explorerCommands/new.ts

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -513,37 +513,21 @@ function getNewFilePath(basePath: string, className: string): string {
513513
return path.join(basePath, ...className.split(".")) + ".java";
514514
}
515515

516-
export async function newPackage(node?: DataNode): Promise<void> {
517-
if (!node?.uri || !canCreatePackage(node)) {
516+
export async function newPackage(node: DataNode | Uri | undefined): Promise<void> {
517+
if (!node) {
518518
return;
519519
}
520520

521-
let defaultValue: string;
522-
let packageRootPath: string;
523-
const nodeKind = node.nodeData.kind;
524-
if (nodeKind === NodeKind.Project) {
525-
defaultValue = "";
526-
packageRootPath = await getPackageFsPath(node) || "";
527-
} else if (nodeKind === NodeKind.PackageRoot) {
528-
defaultValue = "";
529-
packageRootPath = Uri.parse(node.uri).fsPath;
530-
} else if (nodeKind === NodeKind.Package) {
531-
defaultValue = node.nodeData.name + ".";
532-
packageRootPath = getPackageRootPath(Uri.parse(node.uri).fsPath, node.nodeData.name);
533-
} else if (nodeKind === NodeKind.PrimaryType) {
534-
const primaryTypeNode = <PrimaryTypeNode> node;
535-
packageRootPath = primaryTypeNode.getPackageRootPath();
536-
if (packageRootPath === "") {
537-
window.showErrorMessage("Failed to get the package root path.");
538-
return;
539-
}
540-
const packagePath = await getPackageFsPath(node);
541-
if (!packagePath) {
542-
window.showErrorMessage("Failed to get the package path.");
543-
return;
544-
}
545-
defaultValue = path.relative(packageRootPath, packagePath).replace(/[\\\/]/g, ".") + ".";
546-
} else {
521+
if (node instanceof DataNode && (!node.uri || !canCreatePackage(node))) {
522+
return;
523+
}
524+
525+
sendInfo("", {
526+
"triggernewpackagefrom": node instanceof Uri ? "fileExplorer" : "javaProjectExplorer",
527+
});
528+
529+
const {defaultValue, packageRootPath} = await getPackagePromptInformation(node) || {};
530+
if (defaultValue === undefined|| packageRootPath === undefined) {
547531
return;
548532
}
549533

@@ -570,9 +554,58 @@ export async function newPackage(node?: DataNode): Promise<void> {
570554
return;
571555
}
572556

557+
sendInfo("", {
558+
"packageNameContainsDot": packageName.includes(".").toString(),
559+
});
560+
573561
await fse.ensureDir(getNewPackagePath(packageRootPath, packageName));
574562
}
575563

564+
async function getPackagePromptInformation(node: DataNode | Uri): Promise<Record<string, string> | undefined> {
565+
if (node instanceof Uri) {
566+
return {
567+
packageRootPath: node.fsPath,
568+
defaultValue: "",
569+
}
570+
} else if (node instanceof DataNode) {
571+
const nodeKind = node.nodeData.kind;
572+
if (nodeKind === NodeKind.Project) {
573+
return {
574+
packageRootPath: await getPackageFsPath(node) || "",
575+
defaultValue: "",
576+
}
577+
} else if (nodeKind === NodeKind.PackageRoot) {
578+
return {
579+
packageRootPath: Uri.parse(node.uri!).fsPath,
580+
defaultValue: "",
581+
}
582+
} else if (nodeKind === NodeKind.Package) {
583+
return {
584+
packageRootPath: getPackageRootPath(Uri.parse(node.uri!).fsPath, node.nodeData.name),
585+
defaultValue: node.nodeData.name + ".",
586+
}
587+
} else if (nodeKind === NodeKind.PrimaryType) {
588+
const primaryTypeNode = <PrimaryTypeNode> node;
589+
const packageRootPath = primaryTypeNode.getPackageRootPath();
590+
if (packageRootPath === "") {
591+
window.showErrorMessage("Failed to get the package root path.");
592+
return undefined;
593+
}
594+
const packagePath = await getPackageFsPath(node);
595+
if (!packagePath) {
596+
window.showErrorMessage("Failed to get the package path.");
597+
return undefined;
598+
}
599+
return {
600+
packageRootPath: packageRootPath,
601+
defaultValue: path.relative(packageRootPath, packagePath).replace(/[\\\/]/g, ".") + ".",
602+
}
603+
}
604+
}
605+
606+
return undefined;
607+
}
608+
576609
/**
577610
* Check if the create package command is available for the given node.
578611
* Currently the check logic is the same as the create class command.

src/views/dependencyExplorer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ export class DependencyExplorer implements Disposable {
147147
}
148148
newPackage(cmdNode);
149149
}),
150+
instrumentOperationAsVsCodeCommand(Commands.VIEW_EXPLORER_NEW_PACKAGE, (node: Uri) => {
151+
newPackage(node);
152+
}),
150153
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_REVEAL_FILE_OS, (node?: DataNode) => {
151154
const cmdNode = getCmdNode(this._dependencyViewer.selection, node);
152155
if (cmdNode?.uri) {

0 commit comments

Comments
 (0)