import * as fse from "fs-extra" ; import * as path from "path" ; import { Uri , window , workspace , WorkspaceEdit } from "vscode" ; import { NodeKind } from "../java/nodeData" ; import { DataNode } from "../views/dataNode" ; import { checkJavaQualifiedName , isMutable } from "./utility" ; export async function renameFile ( node ? ) { if ( ! node ?. uri || ! isMutable ( node ) ) { return ; } const oldFsPath = Uri . parse ( node . uri ) . fsPath ; const newName = await window . showInputBox ( { placeHolder : "Input new file name" , value : getPrefillValue ( node ) , ignoreFocusOut : true , valueSelection : getValueSelection ( node . uri ) , validateInput : async ( value ) : Promise < string > => { const checkMessage = CheckQualifiedInputName ( value , node . nodeData . kind ) ; if ( checkMessage ) { return checkMessage ; } const inputFsPath = getRenamedFsPath ( oldFsPath , value ) ; if ( await fse . pathExists ( inputFsPath ) ) { return `File path: ${ inputFsPath } already exists.` ; } return "" ; } , } ) ; if ( ! newName ) { return ; } const newFsPath = getRenamedFsPath ( oldFsPath , newName ) ; const workspaceEdit = new WorkspaceEdit ( ) ; workspaceEdit . renameFile ( Uri . file ( oldFsPath ) , Uri . file ( newFsPath ) ) ; workspace . applyEdit ( workspaceEdit ) ; } function getRenamedFsPath ( oldUri , newName ) { if ( ! path . extname ( newName ) ) { newName += path . extname ( oldUri ) ; } const dirname = path . dirname ( oldUri ) ; return path . join ( dirname , newName ) ; } function getPrefillValue ( node ) { const nodeKind = node . nodeData . kind ; if ( nodeKind === NodeKind . PrimaryType ) { return node . name ; } return path . basename ( node . uri ! ) ; } function getValueSelection ( uri ) { const pos = path . basename ( uri ) . lastIndexOf ( "." ) ; if ( pos !== - 1 ) { return [ 0 , pos ] ; } return undefined ; } function CheckQualifiedInputName ( value , nodeKind ) { const javaValidateMessage = checkJavaQualifiedName ( value ) ; if ( javaValidateMessage ) { return javaValidateMessage ; } if ( nodeKind === NodeKind . Package || nodeKind === NodeKind . PackageRoot ) { if ( value . indexOf ( "." ) !== - 1 ) { return "Rename is only applicable to innermost package." ; } } return "" ; }