Skip to content

Commit 07b673a

Browse files
authored
Merge pull request #135 from lambdalisue/rename-buffer
Apply buffer modification after move/rename/trash/remove action
2 parents b51b25a + 7455a73 commit 07b673a

File tree

4 files changed

+108
-6
lines changed

4 files changed

+108
-6
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function! fern#scheme#file#bufutil#removes(paths) abort
2+
for path in a:paths
3+
let bufnr = bufnr(path)
4+
if bufnr is# -1 || getbufvar(bufnr, '&modified')
5+
continue
6+
endif
7+
execute printf('silent! noautocmd %dbwipeout', bufnr)
8+
endfor
9+
endfunction
10+
11+
function! fern#scheme#file#bufutil#moves(pairs) abort
12+
let bufnr_saved = bufnr('%')
13+
let hidden_saved = &hidden
14+
set hidden
15+
try
16+
for [src, dst] in a:pairs
17+
let bufnr = bufnr(src)
18+
if bufnr is# -1
19+
return
20+
endif
21+
execute printf('silent! noautocmd keepjumps keepalt %dbuffer', bufnr)
22+
execute printf('silent! noautocmd keepalt file %s', fnameescape(dst))
23+
endfor
24+
finally
25+
execute printf('keepjumps keepalt %dbuffer', bufnr_saved)
26+
let &hidden = hidden_saved
27+
endtry
28+
endfunction

autoload/fern/scheme/file/mapping.vim

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ function! s:map_move(helper) abort
107107
let nodes = a:helper.sync.get_selected_nodes()
108108
let token = a:helper.fern.source.token
109109
let ps = []
110+
let bufutil_pairs = []
110111
for node in nodes
111112
let src = node._path
112113
let dst = input(
@@ -118,9 +119,11 @@ function! s:map_move(helper) abort
118119
continue
119120
endif
120121
call add(ps, fern#scheme#file#shutil#move(src, dst, token))
122+
call add(bufutil_pairs, [src, dst])
121123
endfor
122124
let root = a:helper.sync.get_root_node()
123125
return s:Promise.all(ps)
126+
\.then({ -> fern#scheme#file#bufutil#moves(bufutil_pairs) })
124127
\.then({ -> a:helper.async.collapse_modified_nodes(nodes) })
125128
\.then({ -> a:helper.async.reload_node(root.__key) })
126129
\.then({ -> a:helper.async.redraw() })
@@ -143,12 +146,16 @@ function! s:map_trash(helper) abort
143146
endif
144147
let token = a:helper.fern.source.token
145148
let ps = []
149+
let bufutil_paths = []
146150
for node in nodes
147-
echo printf('Trash %s', node._path)
148-
call add(ps, fern#scheme#file#shutil#trash(node._path, token))
151+
let path = node._path
152+
echo printf('Trash %s', path)
153+
call add(ps, fern#scheme#file#shutil#trash(path, token))
154+
call add(bufutil_paths, path)
149155
endfor
150156
let root = a:helper.sync.get_root_node()
151157
return s:Promise.all(ps)
158+
\.then({ -> fern#scheme#file#bufutil#removes(bufutil_paths) })
152159
\.then({ -> a:helper.async.collapse_modified_nodes(nodes) })
153160
\.then({ -> a:helper.async.reload_node(root.__key) })
154161
\.then({ -> a:helper.async.redraw() })
@@ -171,12 +178,16 @@ function! s:map_remove(helper) abort
171178
endif
172179
let token = a:helper.fern.source.token
173180
let ps = []
181+
let bufutil_paths = []
174182
for node in nodes
175-
echo printf('Remove %s', node._path)
176-
call add(ps, fern#scheme#file#shutil#remove(node._path, token))
183+
let path = node._path
184+
echo printf('Remove %s', path)
185+
call add(ps, fern#scheme#file#shutil#remove(path, token))
186+
call add(bufutil_paths, path)
177187
endfor
178188
let root = a:helper.sync.get_root_node()
179189
return s:Promise.all(ps)
190+
\.then({ -> fern#scheme#file#bufutil#removes(bufutil_paths) })
180191
\.then({ -> a:helper.async.collapse_modified_nodes(nodes) })
181192
\.then({ -> a:helper.async.reload_node(root.__key) })
182193
\.then({ -> a:helper.async.redraw() })

autoload/fern/scheme/file/mapping/rename.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ endfunction
5757
function! s:_map_rename(helper, result) abort
5858
let token = a:helper.fern.source.token
5959
let ps = []
60-
for pair in a:result
61-
let [src, dst] = pair
60+
for [src, dst] in a:result
6261
if !filereadable(src) && !isdirectory(src)
6362
echohl WarningMsg
6463
echo printf('%s does not exist', src)
@@ -68,5 +67,6 @@ function! s:_map_rename(helper, result) abort
6867
call add(ps, fern#scheme#file#shutil#move(src, dst, token))
6968
endfor
7069
return s:Promise.all(ps)
70+
\.then({ -> fern#scheme#file#bufutil#moves(a:result) })
7171
\.then({ -> len(ps) })
7272
endfunction
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Describe buffer-rename
2+
Before
3+
let workdir = tempname()
4+
lockvar workdir
5+
call mkdir(workdir, 'p')
6+
call writefile([], fern#internal#filepath#join([workdir, 'alpha']))
7+
call writefile([], fern#internal#filepath#join([workdir, 'beta']))
8+
call writefile([], fern#internal#filepath#join([workdir, 'gamma']))
9+
%bwipeout!
10+
End
11+
12+
After
13+
%bwipeout!
14+
call delete(workdir, 'rf')
15+
set hidden&
16+
End
17+
18+
It "move" action will update bufname of a corresponding opened buffer
19+
execute printf('edit %s', fnameescape(fern#internal#filepath#join([workdir, 'alpha'])))
20+
execute printf('Fern %s -wait', fnameescape(workdir))
21+
Assert Match(execute('ls'), '\<alpha\>')
22+
call timer_start(0, { -> feedkeys("\<C-w>OMEGA\<CR>") })
23+
execute "normal ggj\<Plug>(fern-action-move)\<Plug>(fern-wait)"
24+
Assert Equals(getline(1, '$'), [
25+
\ printf('%s ', fnamemodify(workdir, ':t')),
26+
\ '| OMEGA ',
27+
\ '| beta ',
28+
\ '| gamma ',
29+
\])
30+
Assert NotMatch(execute('ls'), '\<alpha\>')
31+
Assert Match(execute('ls'), '\<OMEGA\>')
32+
End
33+
34+
It "remove" action will remove a corresponding opened buffer
35+
execute printf('edit %s', fnameescape(fern#internal#filepath#join([workdir, 'alpha'])))
36+
execute printf('Fern %s -wait', fnameescape(workdir))
37+
Assert Match(execute('ls'), '\<alpha\>')
38+
call timer_start(0, { -> feedkeys("y\<CR>") })
39+
execute "normal ggj\<Plug>(fern-action-remove)\<Plug>(fern-wait)"
40+
Assert Equals(getline(1, '$'), [
41+
\ printf('%s ', fnamemodify(workdir, ':t')),
42+
\ '| beta ',
43+
\ '| gamma ',
44+
\])
45+
Assert NotMatch(execute('ls'), '\<alpha\>')
46+
End
47+
48+
It "remove" action does NOT remove a corresponding opened buffer if modified
49+
set hidden
50+
execute printf('edit %s', fnameescape(fern#internal#filepath#join([workdir, 'alpha'])))
51+
call append(0, ["Hello World"])
52+
execute printf('Fern %s -wait', fnameescape(workdir))
53+
Assert Match(execute('ls'), '\<alpha\>')
54+
call timer_start(0, { -> feedkeys("y\<CR>") })
55+
execute "normal ggj\<Plug>(fern-action-remove)\<Plug>(fern-wait)"
56+
Assert Equals(getline(1, '$'), [
57+
\ printf('%s ', fnamemodify(workdir, ':t')),
58+
\ '| beta ',
59+
\ '| gamma ',
60+
\])
61+
Assert Match(execute('ls'), '\<alpha\>')
62+
End
63+
End

0 commit comments

Comments
 (0)