@@ -262,22 +262,33 @@ function! s:GetVar(...) abort
262262 endif
263263endfunction
264264
265- function ! s: CloseFloatingHover (bufname , opened) abort
265+ function ! s: CloseFloatingHoverAfterCursorMove (bufname , 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 bufnr = bufnr (a: bufname )
274- if bufnr == -1
273+ let winnr = bufwinnr ( bufnr (a: bufname) )
274+ if winnr == -1
275275 return
276276 endif
277- let winnr = bufwinnr (bufnr )
278- if winnr == -1
277+ execute winnr . ' wincmd c'
278+ endfunction
279+
280+ function ! s: CloseFloatingHoverAfterEnterAnotherWin (win_id) abort
281+ let winnr = win_id2win (a: win_id )
282+ if winnr == 0
283+ " Float window was already closed
284+ autocmd ! plugin - LC- neovim- close - hover
279285 return
280286 endif
287+ if winnr == winnr ()
288+ " Cursor is moving into floating window. Do not close it
289+ return
290+ endif
291+ autocmd ! plugin - LC- neovim- close - hover
281292 execute winnr . ' wincmd c'
282293endfunction
283294
@@ -293,9 +304,9 @@ function! s:OpenHoverPreview(bufname, lines, filetype) abort
293304
294305 " Unlike preview window, :pclose does not close window. Instead, close
295306 " hover window automatically when cursor is moved.
296- let params = printf (' "%s", %s' , a: bufname , string (pos))
307+ let call_after_move = printf (' <SID>CloseFloatingHoverAfterCursorMove( "%s", %s) ' , a: bufname , string (pos))
297308 augroup plugin - LC- neovim- close - hover
298- execute ' autocmd CursorMoved,CursorMovedI,InsertEnter <buffer> call <SID>CloseFloatingHover( ' . params . ' ) '
309+ execute ' autocmd CursorMoved,CursorMovedI,InsertEnter <buffer> call ' . call_after_move
299310 augroup END
300311
301312 " Calculate width and height and give margin to lines
@@ -333,7 +344,7 @@ function! s:OpenHoverPreview(bufname, lines, filetype) abort
333344 let col = 1
334345 endif
335346
336- call nvim_open_win (bufnr (' %' ), v: true , width, height, {
347+ let float_win_id = nvim_open_win (bufnr (' %' ), v: true , width, height, {
337348 \ ' relative' : ' cursor' ,
338349 \ ' anchor' : vert . hor ,
339350 \ ' row' : row,
@@ -358,6 +369,10 @@ function! s:OpenHoverPreview(bufname, lines, filetype) abort
358369 setlocal nomodified nomodifiable
359370
360371 wincmd p
372+
373+ if s: FLOAT_WINDOW_AVAILABLE
374+ execute ' autocmd WinEnter * call <SID>CloseFloatingHoverAfterEnterAnotherWin(' . float_win_id . ' )'
375+ endif
361376endfunction
362377
363378let s: id = 1
0 commit comments