1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT license.
3
3
4
+ import * as fse from "fs-extra" ;
4
5
import * as path from "path" ;
5
6
import { Uri , window , workspace , WorkspaceEdit } from "vscode" ;
7
+ import { NodeKind } from "../java/nodeData" ;
6
8
import { DataNode } from "../views/dataNode" ;
7
9
import { ExplorerNode } from "../views/explorerNode" ;
8
- import { isMutable } from "./utils" ;
10
+ import { checkJavaQualifiedName , isMutable } from "./utils" ;
9
11
10
12
export async function renameFile ( node : DataNode , selectedNode : ExplorerNode ) : Promise < void > {
11
13
// if command not invoked by context menu, use selected node in explorer
@@ -16,29 +18,75 @@ export async function renameFile(node: DataNode, selectedNode: ExplorerNode): Pr
16
18
}
17
19
}
18
20
21
+ const oldFsPath = Uri . parse ( node . uri ) . fsPath ;
22
+
19
23
const newName : string | undefined = await window . showInputBox ( {
20
24
placeHolder : "Input new file name" ,
21
- value : node . name ,
25
+ value : getPrefillValue ( node ) ,
22
26
ignoreFocusOut : true ,
27
+ valueSelection : getValueSelection ( node . uri ) ,
28
+ validateInput : async ( value : string ) : Promise < string > => {
29
+ const checkMessage = CheckQualifiedInputName ( value , node . nodeData . kind ) ;
30
+ if ( checkMessage ) {
31
+ return checkMessage ;
32
+ }
33
+
34
+ const inputFsPath = getRenamedFsPath ( oldFsPath , value ) ;
35
+ if ( await fse . pathExists ( inputFsPath ) ) {
36
+ return `File path: ${ inputFsPath } already exists.` ;
37
+ }
38
+
39
+ return "" ;
40
+ } ,
23
41
} ) ;
24
42
25
43
if ( ! newName ) {
26
44
return ;
27
45
}
28
46
29
- const oldFsPath = Uri . parse ( node . uri ) . fsPath ;
30
- const renamedFilePath = getRenamedFilePath ( oldFsPath , newName ) ;
31
-
47
+ const newFsPath = getRenamedFsPath ( oldFsPath , newName ) ;
32
48
const workspaceEdit : WorkspaceEdit = new WorkspaceEdit ( ) ;
33
- workspaceEdit . renameFile ( Uri . file ( oldFsPath ) , Uri . file ( renamedFilePath ) ) ;
49
+ workspaceEdit . renameFile ( Uri . file ( oldFsPath ) , Uri . file ( newFsPath ) ) ;
34
50
workspace . applyEdit ( workspaceEdit ) ;
35
51
}
36
52
37
- function getRenamedFilePath ( oldUri : string , newName : string ) : string {
53
+ function getRenamedFsPath ( oldUri : string , newName : string ) : string {
38
54
// preserve default file extension if not provided
39
55
if ( ! path . extname ( newName ) ) {
40
56
newName += path . extname ( oldUri ) ;
41
57
}
42
58
const dirname = path . dirname ( oldUri ) ;
43
59
return path . join ( dirname , newName ) ;
44
60
}
61
+
62
+ function getPrefillValue ( node : DataNode ) : string {
63
+ const nodeKind = node . nodeData . kind ;
64
+ if ( nodeKind === NodeKind . PrimaryType ) {
65
+ return node . name ;
66
+ }
67
+ return path . basename ( node . uri ) ;
68
+ }
69
+
70
+ function getValueSelection ( uri : string ) : [ number , number ] | undefined {
71
+ const pos = path . basename ( uri ) . lastIndexOf ( "." ) ;
72
+ if ( pos !== - 1 ) {
73
+ return [ 0 , pos ] ;
74
+ }
75
+ return undefined ;
76
+ }
77
+
78
+ function CheckQualifiedInputName ( value : string , nodeKind : NodeKind ) : string {
79
+ const javaValidateMessage = checkJavaQualifiedName ( value ) ;
80
+
81
+ if ( javaValidateMessage ) {
82
+ return javaValidateMessage ;
83
+ }
84
+
85
+ if ( nodeKind === NodeKind . Package || nodeKind === NodeKind . PackageRoot ) {
86
+ if ( value . indexOf ( "." ) !== - 1 ) {
87
+ return "Rename is only applicable to innermost package." ;
88
+ }
89
+ }
90
+
91
+ return "" ;
92
+ }
0 commit comments