Skip to content

Commit

Permalink
Avoid invoking on_closed twice
Browse files Browse the repository at this point in the history
  • Loading branch information
hrsh7th committed Jan 17, 2021
1 parent b956d45 commit 67120a2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
57 changes: 37 additions & 20 deletions autoload/vital/__vital__/VS/Vim/Window/FloatingWindow.vim
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ endfunction
"
function! s:_notify_opened(winid, floating_window) abort
let s:floating_windows[a:winid] = a:floating_window
call a:floating_window._on_opened(a:floating_window)
call a:floating_window._on_opened()
endfunction

"
" _notify_closed
"
function! s:_notify_closed() abort
for [l:win, l:floating_window] in items(s:floating_windows)
if winheight(l:win) == -1
call l:floating_window._on_closed(l:floating_window)
unlet s:floating_windows[l:win]
for [l:winid, l:floating_window] in items(s:floating_windows)
if winheight(l:winid) == -1
call l:floating_window._on_closed()
unlet s:floating_windows[l:winid]
endif
endfor
endfunction
Expand All @@ -76,22 +76,13 @@ function! s:FloatingWindow.new(args) abort
\ })
endfunction

"
" set_bufnr
"
" @param {number} bufnr
"
function! s:FloatingWindow.set_bufnr(bufnr) abort
let self._bufnr = a:bufnr
endfunction

"
" get_size
"
" @param {number} args.minwidth
" @param {number} args.maxwidth
" @param {number} args.minheight
" @param {number} args.maxheight
" @param {number?} args.minwidth
" @param {number?} args.maxwidth
" @param {number?} args.minheight
" @param {number?} args.maxheight
"
function! s:FloatingWindow.get_size(args) abort
if self._bufnr is# v:null
Expand Down Expand Up @@ -127,6 +118,32 @@ function! s:FloatingWindow.get_size(args) abort
\ }
endfunction

"
" set_bufnr
"
" @param {number} bufnr
"
function! s:FloatingWindow.set_bufnr(bufnr) abort
let self._bufnr = a:bufnr
endfunction

"
" get_bufnr
"
function! s:FloatingWindow.get_bufnr() abort
return self._bufnr
endfunction

"
" get_winid
"
function! s:FloatingWindow.get_winid() abort
if self.is_visible()
return self._winid
endif
return v:null
endfunction

"
" open
"
Expand All @@ -146,7 +163,7 @@ function! s:FloatingWindow.open(args) abort
if self.is_visible()
call s:_move(self._winid, l:style)
else
let self._winid = s:_open(self._bufnr, l:style, { -> self._on_closed(self) })
let self._winid = s:_open(self._bufnr, l:style, { -> self._on_closed() })
call s:_notify_opened(self._winid, self)
endif
endfunction
Expand All @@ -157,7 +174,6 @@ endfunction
function! s:FloatingWindow.close() abort
if self.is_visible()
call s:_close(self._winid)
call s:_notify_closed()
endif
let self._winid = v:null
endfunction
Expand Down Expand Up @@ -197,6 +213,7 @@ endif
if has('nvim')
function! s:_close(win) abort
call nvim_win_close(a:win, v:true)
call s:_notify_closed()
endfunction
else
function! s:_close(win) abort
Expand Down
18 changes: 9 additions & 9 deletions autoload/vital/__vital__/VS/Vim/Window/FloatingWindow.vimspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ Describe vital#__vital__#VS#Vim#Window#FloatingWindow

Describe #on_close
It should callback on closed by method
let s:closed = v:false
let s:closed = 0
let l:win = s:FloatingWindow.new({
\ 'on_closed': { win -> execute('let s:closed = v:true') }
\ 'on_closed': { -> execute('let s:closed += 1') }
\ })
call l:win.set_bufnr(s:Buffer.create())
call l:win.open({
Expand All @@ -40,17 +40,17 @@ Describe vital#__vital__#VS#Vim#Window#FloatingWindow
\ 'height': 10,
\ })
call l:win.close()
call s:expect(s:closed).to_equal(v:true)
call s:expect(s:closed).to_equal(1)
End

It should callback on closed by popup_close (vim only)
if has('nvim')
return
endif

let s:closed = v:false
let s:closed = 0
let l:win = s:FloatingWindow.new({
\ 'on_closed': { win -> execute('let s:closed = v:true') }
\ 'on_closed': { -> execute('let s:closed += 1') }
\ })
call l:win.set_bufnr(s:Buffer.create())
call l:win.open({
Expand All @@ -60,17 +60,17 @@ Describe vital#__vital__#VS#Vim#Window#FloatingWindow
\ 'height': 10,
\ })
call popup_close(l:win._winid)
call s:expect(s:closed).to_equal(v:true)
call s:expect(s:closed).to_equal(1)
End

It should callback on closed by command (nvim only)
if !has('nvim')
return
endif

let s:closed = v:false
let s:closed = 0
let l:win = s:FloatingWindow.new({
\ 'on_closed': { win -> execute('let s:closed = v:true') }
\ 'on_closed': { -> execute('let s:closed += 1') }
\ })
call l:win.set_bufnr(s:Buffer.create())
call l:win.open({
Expand All @@ -81,7 +81,7 @@ Describe vital#__vital__#VS#Vim#Window#FloatingWindow
\ })
call l:win.enter()
call execute('close')
call s:expect(s:closed).to_equal(v:true)
call s:expect(s:closed).to_equal(1)
End
End

Expand Down

0 comments on commit 67120a2

Please sign in to comment.