Skip to content

Commit d63389f

Browse files
committed
Add test for closing floating window on WinEnter
1 parent 1a7748f commit d63389f

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

autoload/LanguageClient.vim

+10-11
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,15 @@ function! s:GetVar(...) abort
262262
endif
263263
endfunction
264264

265-
function! s:CloseFloatingHoverAfterCursorMove(bufname, opened) abort
265+
function! s:CloseFloatingHoverAfterCursorMove(win_id, opened) abort
266266
if getpos('.') == a:opened
267267
" Just after opening floating window, CursorMoved event is run.
268268
" To avoid closing floating window immediately, check the cursor
269269
" was really moved
270270
return
271271
endif
272272
autocmd! plugin-LC-neovim-close-hover
273-
let winnr = bufwinnr(bufnr(a:bufname))
273+
let winnr = win_id2win(a:win_id)
274274
if winnr == -1
275275
return
276276
endif
@@ -279,7 +279,7 @@ endfunction
279279

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

305-
" Unlike preview window, :pclose does not close window. Instead, close
306-
" hover window automatically when cursor is moved.
307-
let call_after_move = printf('<SID>CloseFloatingHoverAfterCursorMove("%s", %s)', a:bufname, string(pos))
308-
augroup plugin-LC-neovim-close-hover
309-
execute 'autocmd CursorMoved,CursorMovedI,InsertEnter <buffer> call ' . call_after_move
310-
augroup END
311-
312305
" Calculate width and height and give margin to lines
313306
let width = 0
314307
for index in range(len(lines))
@@ -371,7 +364,13 @@ function! s:OpenHoverPreview(bufname, lines, filetype) abort
371364
wincmd p
372365

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

tests/LanguageClient_test.py

+23-4
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,20 @@ def test_textDocument_hover_float_window(nvim):
214214
if not nvim.funcs.exists('*nvim_open_win'):
215215
pytest.skip('Neovim 0.3.0 or earlier does not support floating window')
216216

217+
def _open_float_window():
218+
nvim.funcs.cursor(13, 19)
219+
pos = nvim.funcs.getpos('.')
220+
nvim.funcs.LanguageClient_textDocument_hover()
221+
time.sleep(1)
222+
return pos
223+
217224
nvim.command("edit! {}".format(PATH_INDEXJS))
218225
time.sleep(1)
219226

220227
buf = nvim.current.buffer
221228

222-
nvim.funcs.cursor(13, 19)
223-
pos = nvim.funcs.getpos('.')
224-
nvim.funcs.LanguageClient_textDocument_hover()
225-
time.sleep(1)
229+
pos = _open_float_window()
230+
226231
float_buf = next(
227232
b for b in nvim.buffers if b.name.endswith('__LanguageClient__'))
228233

@@ -240,3 +245,17 @@ def test_textDocument_hover_float_window(nvim):
240245
# Check float window buffer was closed by CursorMoved
241246
assert all(
242247
b for b in nvim.buffers if not b.name.endswith('__LanguageClient__'))
248+
249+
win_id = nvim.funcs.win_getid()
250+
nvim.command('split')
251+
assert win_id != nvim.funcs.win_getid()
252+
253+
_open_float_window()
254+
255+
# Move to another window
256+
nvim.funcs.win_gotoid(win_id)
257+
assert win_id == nvim.funcs.win_getid()
258+
259+
# Check float window buffer was closed by WinEnter
260+
assert all(
261+
b for b in nvim.buffers if not b.name.endswith('__LanguageClient__'))

0 commit comments

Comments
 (0)