Skip to content

Commit

Permalink
Merge pull request #216 from lambdalisue/fix-bd
Browse files Browse the repository at this point in the history
Fix behavior on 'bdelete' command
  • Loading branch information
lambdalisue authored Sep 21, 2020
2 parents 93d110c + 5ae1280 commit c7fbdc5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 15 deletions.
47 changes: 32 additions & 15 deletions autoload/fern/internal/drawer.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
let s:Lambda = vital#fern#import('Lambda')
let s:auto_quit_enabled = 0

function! fern#internal#drawer#open(fri, ...) abort
let options = extend({
\ 'toggle': 0,
Expand All @@ -20,15 +23,13 @@ function! fern#internal#drawer#init() abort
return
endif

augroup fern_drawer_internal
augroup fern_internal_drawer_init
autocmd! * <buffer>
autocmd BufEnter <buffer> call s:auto_quit()
autocmd BufEnter <buffer> call s:auto_resize(v:false)
autocmd BufLeave <buffer> call s:auto_resize(v:false)
autocmd BufEnter <buffer> call s:auto_winfixwidth(v:false)
if !g:fern#disable_drawer_auto_restore_focus
autocmd WinLeave <buffer> call s:auto_restore_focus_pre()
endif
autocmd WinLeave <buffer> call s:auto_restore_focus_pre()
augroup END

call s:auto_resize(v:true)
Expand Down Expand Up @@ -68,8 +69,13 @@ function! s:auto_resize(force) abort
execute 'vertical resize' width
endfunction

function! s:auto_quit_pre() abort
let s:auto_quit_enabled = 1
call timer_start(0, { -> s:Lambda.let(s:, 'auto_quit_enabled', 0)})
endfunction

function! s:auto_quit() abort
if g:fern#disable_drawer_auto_quit
if g:fern#disable_drawer_auto_quit || !s:auto_quit_enabled
return
endif
let fri = fern#fri#parse(bufname('%'))
Expand All @@ -80,15 +86,26 @@ function! s:auto_quit() abort
return
elseif keep
" Add a new window to avoid being a last window
silent! vertical botright new
keepjumps wincmd p
execute 'vertical resize' width
let winid = win_getid()
if has('nvim')
call s:auto_quit_post(winid, width)
else
" Use timer to avoid E242 in Vim
call timer_start(0, { -> s:auto_quit_post(winid, width) })
endif
else
" This window is a last window of a current tabpage
quit
endif
endfunction

function! s:auto_quit_post(winid, width) abort
keepjumps call win_gotoid(a:winid)
silent! vertical botright new
keepjumps call win_gotoid(a:winid)
execute 'vertical resize' a:width
endfunction

function! s:auto_restore_focus_pre() abort
let s:restore_focus = {
\ 'nwin': winnr('$'),
Expand All @@ -97,7 +114,8 @@ function! s:auto_restore_focus_pre() abort
endfunction

function! s:auto_restore_focus() abort
if !exists('s:restore_focus')
if g:fern#disable_drawer_auto_restore_focus
\ || !exists('s:restore_focus')
return
endif
if s:restore_focus.nwin > winnr('$')
Expand All @@ -106,9 +124,8 @@ function! s:auto_restore_focus() abort
silent! unlet! s:restore_focus
endfunction

if !g:fern#disable_drawer_auto_restore_focus
augroup fern_internal_drawer_internal
autocmd!
autocmd WinEnter * ++nested call s:auto_restore_focus()
augroup END
endif
augroup fern_internal_drawer
autocmd!
autocmd QuitPre * call s:auto_quit_pre()
autocmd WinEnter * ++nested call s:auto_restore_focus()
augroup END
43 changes: 43 additions & 0 deletions test/behavior/drawer-auto-quit.vimspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Describe drawer-auto-quit
After all
%bwipeout!
let g:fern#disable_drawer_auto_quit = 0
End

Before
%bwipeout!
let g:fern#disable_drawer_auto_quit = 0
End

Describe keep
It automatically create a new buffer to keep the number of windows on 'quit'
Fern . -drawer -keep -stay
Assert Equals(winnr('$'), 2)
quit
sleep 1m
Assert Equals(winnr('$'), 2)
End

It does nothing when g:fern#disable_drawer_auto_quit = 1
let g:fern#disable_drawer_auto_quit = 1
Fern . -drawer -keep -stay
Assert Equals(winnr('$'), 2)
quit
sleep 1m
Assert Equals(winnr('$'), 1)
End

It does nothing on 'bdelete'
" NOTE:
" The following two edit is required to prevent unconsistent
" behaviro of bdelete in Vim
edit foo
edit bar
Fern . -drawer -keep -stay
Assert Equals(winnr('$'), 2)
bdelete
sleep 1m
Assert Equals(winnr('$'), 1)
End
End
End

0 comments on commit c7fbdc5

Please sign in to comment.