@@ -513,37 +513,21 @@ function getNewFilePath(basePath: string, className: string): string {
513
513
return path . join ( basePath , ...className . split ( "." ) ) + ".java" ;
514
514
}
515
515
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 ) {
518
518
return ;
519
519
}
520
520
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 ) {
547
531
return ;
548
532
}
549
533
@@ -570,9 +554,58 @@ export async function newPackage(node?: DataNode): Promise<void> {
570
554
return ;
571
555
}
572
556
557
+ sendInfo ( "" , {
558
+ "packageNameContainsDot" : packageName . includes ( "." ) . toString ( ) ,
559
+ } ) ;
560
+
573
561
await fse . ensureDir ( getNewPackagePath ( packageRootPath , packageName ) ) ;
574
562
}
575
563
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
+
576
609
/**
577
610
* Check if the create package command is available for the given node.
578
611
* Currently the check logic is the same as the create class command.
0 commit comments