Refresh buffers after m, m operation on a folder (#888)

* WIP: Wow! That's an ugly diff. I'm not done yet, though.

* Fix the ugliness of the previous commit, and finish the work.

If the node being renamed is a directory, get a list of all open buffers
whose file lives in the old directory. If that list is not empty, ask
the user if he wants to replace the old buffers with the new files. The
logic to handle renaming a file node stays the same, although the code
has been refactored a bit.
This commit is contained in:
Phil Runninger 2018-10-24 22:20:59 -04:00 committed by GitHub
parent 388623e702
commit c372911c4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -83,31 +83,32 @@ function! s:promptToDelBuffer(bufnum, msg)
endif endif
endfunction endfunction
"FUNCTION: s:promptToRenameBuffer(bufnum, msg){{{1 "FUNCTION: s:renameBuffer(bufNum, newNodeName, isDirectory){{{1
"prints out the given msg and, if the user responds by pushing 'y' then the "The buffer with the given bufNum is replaced with a new one
"buffer with the given bufnum is replaced with a new one
" "
"Args: "Args:
"bufnum: the buffer that may be deleted "bufNum: the buffer that may be deleted
"msg: a message that will be echoed to the user asking them if they wish to "newNodeName: the name given to the renamed node
" del the buffer "isDirectory: determines how to do the create the new filenames
function! s:promptToRenameBuffer(bufnum, msg, newFileName) function! s:renameBuffer(bufNum, newNodeName, isDirectory)
echo a:msg if a:isDirectory
if g:NERDTreeAutoDeleteBuffer || nr2char(getchar()) ==# 'y' let quotedFileName = fnameescape(a:newNodeName . '/' . fnamemodify(bufname(a:bufNum),':t'))
let quotedFileName = fnameescape(a:newFileName) let editStr = g:NERDTreePath.New(a:newNodeName . '/' . fnamemodify(bufname(a:bufNum),':t')).str({'format': 'Edit'})
" 1. ensure that a new buffer is loaded else
exec "badd " . quotedFileName let quotedFileName = fnameescape(a:newNodeName)
" 2. ensure that all windows which display the just deleted filename let editStr = g:NERDTreePath.New(a:newNodeName).str({'format': 'Edit'})
" display a buffer for a new filename.
let s:originalTabNumber = tabpagenr()
let s:originalWindowNumber = winnr()
let editStr = g:NERDTreePath.New(a:newFileName).str({'format': 'Edit'})
exec "tabdo windo if winbufnr(0) == " . a:bufnum . " | exec ':e! " . editStr . "' | endif"
exec "tabnext " . s:originalTabNumber
exec s:originalWindowNumber . "wincmd w"
" 3. We don't need a previous buffer anymore
exec "bwipeout! " . a:bufnum
endif endif
" 1. ensure that a new buffer is loaded
exec "badd " . quotedFileName
" 2. ensure that all windows which display the just deleted filename
" display a buffer for a new filename.
let s:originalTabNumber = tabpagenr()
let s:originalWindowNumber = winnr()
exec "tabdo windo if winbufnr(0) == " . a:bufNum . " | exec ':e! " . editStr . "' | endif"
exec "tabnext " . s:originalTabNumber
exec s:originalWindowNumber . "wincmd w"
" 3. We don't need a previous buffer anymore
exec "bwipeout! " . a:bufNum
endfunction endfunction
"FUNCTION: NERDTreeAddNode(){{{1 "FUNCTION: NERDTreeAddNode(){{{1
function! NERDTreeAddNode() function! NERDTreeAddNode()
@ -158,7 +159,11 @@ function! NERDTreeMoveNode()
endif endif
try try
let bufnum = bufnr("^".curNode.path.str()."$") if curNode.path.isDirectory
let l:openBuffers = filter(range(1,bufnr("$")),'bufexists(v:val) && fnamemodify(bufname(v:val),":p") =~# curNode.path.str() . "/.*"')
else
let l:openBuffers = filter(range(1,bufnr("$")),'bufexists(v:val) && fnamemodify(bufname(v:val),":p") ==# curNode.path.str()')
endif
call curNode.rename(newNodePath) call curNode.rename(newNodePath)
" Emptying g:NERDTreeOldSortOrder forces the sort to " Emptying g:NERDTreeOldSortOrder forces the sort to
@ -167,11 +172,20 @@ function! NERDTreeMoveNode()
call b:NERDTree.root.refresh() call b:NERDTree.root.refresh()
call NERDTreeRender() call NERDTreeRender()
"if the node is open in a buffer, ask the user if they want to " If the file node is open, or files under the directory node are
"close that buffer " open, ask the user if they want to replace the file(s) with the
if bufnum != -1 " renamed files.
let prompt = "\nNode renamed.\n\nThe old file is open in buffer ". bufnum . (bufwinnr(bufnum) ==# -1 ? " (hidden)" : "") .". Replace this buffer with the new file? (yN)" if !empty(l:openBuffers)
call s:promptToRenameBuffer(bufnum, prompt, newNodePath) if curNode.path.isDirectory
echo "\nDirectory renamed.\n\nFiles with the old directory name are open in buffers " . join(l:openBuffers, ', ') . ". Replace these buffers with the new files? (yN)"
else
echo "\nFile renamed.\n\nThe old file is open in buffer " . l:openBuffers[0] . ". Replace this buffer with the new file? (yN)"
endif
if g:NERDTreeAutoDeleteBuffer || nr2char(getchar()) ==# 'y'
for bufNum in l:openBuffers
call s:renameBuffer(bufNum, newNodePath, curNode.path.isDirectory)
endfor
endif
endif endif
call curNode.putCursorHere(1, 0) call curNode.putCursorHere(1, 0)