Skip to content

Commit bc52421

Browse files
committed
support rename
1 parent fc960ac commit bc52421

File tree

4 files changed

+95
-8
lines changed

4 files changed

+95
-8
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ for experienced user, here's some configs you may interest
168168
* `let g:ZFDirDiffKeymap_syncToThere = ['dp', 'DL']` : sync nodes from here to there
169169
* `let g:ZFDirDiffKeymap_add = ['a']` : add new node, end with `/` to add dir
170170
* `let g:ZFDirDiffKeymap_delete = ['dd']` : delete selected nodes
171+
* `let g:ZFDirDiffKeymap_rename = ['cc']` : rename selected node
171172
* `let g:ZFDirDiffKeymap_getPath = ['p']` : get relative path of node under cursor
172173
* `let g:ZFDirDiffKeymap_getFullPath = ['P']` : get absolute path of node under cursor
173174

doc/zfdirdiff.txt

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ For experienced user, here's some configs you may want:
156156
* `let g:ZFDirDiffKeymap_syncToThere = ['dp', 'DL']` : sync nodes from here to there
157157
* `let g:ZFDirDiffKeymap_add = ['a']` : add new node, end with `/` to add dir
158158
* `let g:ZFDirDiffKeymap_delete = ['dd']` : delete selected nodes
159+
* `let g:ZFDirDiffKeymap_rename = ['cc']` : rename selected node
159160
* `let g:ZFDirDiffKeymap_getPath = ['p']` : get relative path of node under cursor
160161
* `let g:ZFDirDiffKeymap_getFullPath = ['P']` : get absolute path of node under cursor
161162

plugin/api.vim

+28-8
Original file line numberDiff line numberDiff line change
@@ -698,9 +698,9 @@ if !exists('*ZFDirDiffAPI_mkdir')
698698
if exists("*mkdir")
699699
call mkdir(a:path, 'p')
700700
elseif (has('win32') || has('win64')) && !has('unix')
701-
silent execute '!mkdir "' . substitute(a:path, '/', '\', 'g') . '"'
701+
silent execute printf('!mkdir "%s"', substitute(a:path, '/', '\', 'g'))
702702
else
703-
silent execute '!mkdir -p "' . a:path . '"'
703+
silent execute printf('!mkdir -p "%s"', a:path)
704704
endif
705705
endif
706706
endfunction
@@ -710,29 +710,49 @@ if !exists('*ZFDirDiffAPI_cpfile')
710710
function! ZFDirDiffAPI_cpfile(from, to)
711711
call ZFDirDiffAPI_mkdir(fnamemodify(a:to, ":h"))
712712
if (has('win32') || has('win64')) && !has('unix')
713-
silent execute '!copy "' . substitute(a:from, '/', '\', 'g') . '" "' . substitute(a:to, '/', '\', 'g') . '"'
713+
silent execute printf('!copy "%s" "%s"', substitute(a:from, '/', '\', 'g'), substitute(a:to, '/', '\', 'g'))
714714
else
715-
silent execute '!cp -rf "' . a:from . '" "' . a:to . '"'
715+
silent execute printf('!cp -rf "%s" "%s"', a:from, a:to)
716716
endif
717717
endfunction
718718
endif
719719

720720
if !exists('*ZFDirDiffAPI_rmdir')
721721
function! ZFDirDiffAPI_rmdir(path)
722722
if (has('win32') || has('win64')) && !has('unix')
723-
silent execute '!rmdir /s/q "' . substitute(a:path, '/', '\', 'g') . '"'
723+
silent execute printf('!rmdir /s/q "%s"', substitute(a:path, '/', '\', 'g'))
724724
else
725-
silent execute '!rm -rf "' . a:path . '"'
725+
silent execute printf('!rm -rf "%s"', a:path)
726726
endif
727727
endfunction
728728
endif
729729

730730
if !exists('*ZFDirDiffAPI_rmfile')
731731
function! ZFDirDiffAPI_rmfile(path)
732732
if (has('win32') || has('win64')) && !has('unix')
733-
silent execute '!del /f/q "' . substitute(a:path, '/', '\', 'g') . '"'
733+
silent execute printf('!del /f/q "%s"', substitute(a:path, '/', '\', 'g'))
734734
else
735-
silent execute '!rm -f "' . a:path . '"'
735+
silent execute printf('!rm -f "%s"', a:path)
736+
endif
737+
endfunction
738+
endif
739+
740+
if !exists('*ZFDirDiffAPI_mvdir')
741+
function! ZFDirDiffAPI_mvdir(from, to)
742+
if (has('win32') || has('win64')) && !has('unix')
743+
silent execute printf('!move "%s" "%s"', a:from, a:to)
744+
else
745+
silent execute printf('!mv "%s" "%s"', a:from, a:to)
746+
endif
747+
endfunction
748+
endif
749+
750+
if !exists('*ZFDirDiffAPI_mvfile')
751+
function! ZFDirDiffAPI_mvfile(from, to)
752+
if (has('win32') || has('win64')) && !has('unix')
753+
silent execute printf('!move "%s" "%s"', a:from, a:to)
754+
else
755+
silent execute printf('!mv "%s" "%s"', a:from, a:to)
736756
endif
737757
endfunction
738758
endif

plugin/ui.vim

+65
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@ function! s:diffUI_makeKeymap()
716716
call s:makeKeymap('ZFDirDiffUIAction_syncToThere', 'ZFDirDiffKeymap_syncToThere', ['dp', 'DL'], 1)
717717
call s:makeKeymap('ZFDirDiffUIAction_add', 'ZFDirDiffKeymap_add', ['a'])
718718
call s:makeKeymap('ZFDirDiffUIAction_delete', 'ZFDirDiffKeymap_delete', ['dd'], 1)
719+
call s:makeKeymap('ZFDirDiffUIAction_rename', 'ZFDirDiffKeymap_rename', ['cc'])
719720
call s:makeKeymap('ZFDirDiffUIAction_getPath', 'ZFDirDiffKeymap_getPath', ['p'])
720721
call s:makeKeymap('ZFDirDiffUIAction_getFullPath', 'ZFDirDiffKeymap_getFullPath', ['P'])
721722
endfunction
@@ -1256,6 +1257,70 @@ function! ZFDirDiffUIAction_delete() range
12561257
call s:diffUI_op(t:ZFDirDiff_taskData, diffNodes, op)
12571258
endfunction
12581259

1260+
function! ZFDirDiffUIAction_rename()
1261+
if !exists('t:ZFDirDiff_taskData')
1262+
echomsg '[ZFDirDiff] no previous diff task'
1263+
return
1264+
endif
1265+
silent let path = s:ZFDirDiffUIAction_getPathOrFullPath(1)
1266+
let diffNode = ZFDirDiffUI_diffNodeUnderCursor()
1267+
if empty(path) || empty(diffNode)
1268+
echo '[ZFDirDiff] no file or dir under cursor'
1269+
return
1270+
endif
1271+
1272+
let nameNew = input(isdirectory(path) ? 'rename dir: ' : 'rename file: ', fnamemodify(path, ':t'), 'file')
1273+
" ^[ \t]+|[ \t]+$
1274+
let nameNew = substitute(nameNew, '^[ \t]\+\|[ \t]\+$', '', '')
1275+
if empty(nameNew)
1276+
return
1277+
endif
1278+
1279+
let pathNew = fnamemodify(path, ':h') . '/' . nameNew
1280+
if isdirectory(pathNew) || filereadable(pathNew)
1281+
let hint = "target already exists:"
1282+
let relPath = fnamemodify(pathNew, ':.')
1283+
let hint .= "\n " . pathNew
1284+
if relPath != pathNew
1285+
let hint .= "\n (" . pathNew . ")"
1286+
endif
1287+
let hint .= "\nconfirm override?"
1288+
let hint .= "\n"
1289+
let hint .= "\n(y)es / (n)o: "
1290+
redraw
1291+
echo hint
1292+
let confirm = nr2char(getchar())
1293+
redraw
1294+
if confirm != 'y'
1295+
echo 'canceled'
1296+
return
1297+
endif
1298+
1299+
if isdirectory(pathNew)
1300+
call ZFDirDiffAPI_rmdir(pathNew)
1301+
elseif filereadable(pathNew)
1302+
call ZFDirDiffAPI_rmfile(pathNew)
1303+
endif
1304+
endif
1305+
1306+
call ZFDirDiffAPI_mkdir(fnamemodify(pathNew, ':h'))
1307+
1308+
if isdirectory(path)
1309+
call ZFDirDiffAPI_mvdir(path, pathNew)
1310+
let hint = printf('dir renamed: %s => %s', fnamemodify(path, ':.'), fnamemodify(pathNew, ':.'))
1311+
elseif filereadable(path)
1312+
call ZFDirDiffAPI_mvfile(path, pathNew)
1313+
let hint = printf('file renamed: %s => %s', fnamemodify(path, ':.'), fnamemodify(pathNew, ':.'))
1314+
else
1315+
echo 'failed to move: ' . path
1316+
return
1317+
endif
1318+
1319+
call ZFDirDiffAPI_update(t:ZFDirDiff_taskData, diffNode['parent'])
1320+
redraw
1321+
echo hint
1322+
endfunction
1323+
12591324
function! s:ZFDirDiffUIAction_getPathOrFullPath(isFullPath)
12601325
let diffNode = ZFDirDiffUI_diffNodeUnderCursor()
12611326
if empty(diffNode)

0 commit comments

Comments
 (0)