Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test for closing floating window on WinEnter
  • Loading branch information
rhysd committed Mar 14, 2019
commit d63389f4d52705bddd33d486b28cbc580721caad
21 changes: 10 additions & 11 deletions autoload/LanguageClient.vim
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,15 @@ function! s:GetVar(...) abort
endif
endfunction

function! s:CloseFloatingHoverAfterCursorMove(bufname, opened) abort
function! s:CloseFloatingHoverAfterCursorMove(win_id, opened) abort
if getpos('.') == a:opened
" Just after opening floating window, CursorMoved event is run.
" To avoid closing floating window immediately, check the cursor
" was really moved
return
endif
autocmd! plugin-LC-neovim-close-hover
let winnr = bufwinnr(bufnr(a:bufname))
let winnr = win_id2win(a:win_id)
if winnr == -1
return
endif
Expand All @@ -279,7 +279,7 @@ endfunction

function! s:CloseFloatingHoverAfterEnterAnotherWin(win_id) abort
let winnr = win_id2win(a:win_id)
if winnr == 0
if winnr == -1
" Float window was already closed
autocmd! plugin-LC-neovim-close-hover
return
Expand All @@ -302,13 +302,6 @@ function! s:OpenHoverPreview(bufname, lines, filetype) abort
if s:FLOAT_WINDOW_AVAILABLE
let pos = getpos('.')

" Unlike preview window, :pclose does not close window. Instead, close
" hover window automatically when cursor is moved.
let call_after_move = printf('<SID>CloseFloatingHoverAfterCursorMove("%s", %s)', a:bufname, string(pos))
augroup plugin-LC-neovim-close-hover
execute 'autocmd CursorMoved,CursorMovedI,InsertEnter <buffer> call ' . call_after_move
augroup END

" Calculate width and height and give margin to lines
let width = 0
for index in range(len(lines))
Expand Down Expand Up @@ -371,7 +364,13 @@ function! s:OpenHoverPreview(bufname, lines, filetype) abort
wincmd p

if s:FLOAT_WINDOW_AVAILABLE
execute 'autocmd WinEnter * call <SID>CloseFloatingHoverAfterEnterAnotherWin(' . float_win_id . ')'
" Unlike preview window, :pclose does not close window. Instead, close
" hover window automatically when cursor is moved.
let call_after_move = printf('<SID>CloseFloatingHoverAfterCursorMove(%d, %s)', float_win_id, string(pos))
augroup plugin-LC-neovim-close-hover
execute 'autocmd CursorMoved,CursorMovedI,InsertEnter <buffer> call ' . call_after_move
execute 'autocmd WinEnter * call <SID>CloseFloatingHoverAfterEnterAnotherWin(' . float_win_id . ')'
augroup END
endif
endfunction

Expand Down
27 changes: 23 additions & 4 deletions tests/LanguageClient_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,20 @@ def test_textDocument_hover_float_window(nvim):
if not nvim.funcs.exists('*nvim_open_win'):
pytest.skip('Neovim 0.3.0 or earlier does not support floating window')

def _open_float_window():
nvim.funcs.cursor(13, 19)
pos = nvim.funcs.getpos('.')
nvim.funcs.LanguageClient_textDocument_hover()
time.sleep(1)
return pos

nvim.command("edit! {}".format(PATH_INDEXJS))
time.sleep(1)

buf = nvim.current.buffer

nvim.funcs.cursor(13, 19)
pos = nvim.funcs.getpos('.')
nvim.funcs.LanguageClient_textDocument_hover()
time.sleep(1)
pos = _open_float_window()

float_buf = next(
b for b in nvim.buffers if b.name.endswith('__LanguageClient__'))

Expand All @@ -240,3 +245,17 @@ def test_textDocument_hover_float_window(nvim):
# Check float window buffer was closed by CursorMoved
assert all(
b for b in nvim.buffers if not b.name.endswith('__LanguageClient__'))

win_id = nvim.funcs.win_getid()
nvim.command('split')
assert win_id != nvim.funcs.win_getid()

_open_float_window()

# Move to another window
nvim.funcs.win_gotoid(win_id)
assert win_id == nvim.funcs.win_getid()

# Check float window buffer was closed by WinEnter
assert all(
b for b in nvim.buffers if not b.name.endswith('__LanguageClient__'))