Skip to content

Commit 0394fba

Browse files
committed
Make use of vim 8.2+ popup win
Ref #925
1 parent 5d0b152 commit 0394fba

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

autoload/LanguageClient.vim

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ let s:TYPE = {
88
\ 'dict': type({}),
99
\ 'funcref': type(function('call'))
1010
\ }
11-
let s:FLOAT_WINDOW_AVAILABLE = has('nvim') && exists('*nvim_open_win')
11+
let s:FLOAT_WINDOW_AVAILABLE = exists('*nvim_open_win')
12+
let s:POPUP_WINDOW_AVAILABLE = exists('*popup_atcursor')
1213

1314
function! s:AddPrefix(message) abort
1415
return '[LC] ' . a:message
@@ -268,8 +269,8 @@ function! s:GetVar(...) abort
268269
endfunction
269270

270271
function! s:ShouldUseFloatWindow() abort
271-
let use = s:GetVar('LanguageClient_useFloatingHover')
272-
return s:FLOAT_WINDOW_AVAILABLE && (use || use is v:null)
272+
let floatingHoverEnabled = s:GetVar('LanguageClient_useFloatingHover', v:true)
273+
return s:FLOAT_WINDOW_AVAILABLE && floatingHoverEnabled
273274
endfunction
274275

275276
function! s:CloseFloatingHover() abort
@@ -321,14 +322,23 @@ endfunction
321322

322323
" Open preview window. Window is open in:
323324
" - Floating window on Neovim (0.4.0 or later)
325+
" - popup window on vim (8.2 or later)
324326
" - Preview window on Neovim (0.3.0 or earlier) or Vim
325327
function! s:OpenHoverPreview(bufname, lines, filetype) abort
326328
" Use local variable since parameter is not modifiable
327329
let lines = a:lines
328330
let bufnr = bufnr('%')
329331

330-
let use_float_win = s:ShouldUseFloatWindow()
331-
if use_float_win
332+
let display_approach = ''
333+
if s:ShouldUseFloatWindow()
334+
let display_approach = 'float_win'
335+
elseif s:POPUP_WINDOW_AVAILABLE && s:GetVar('LanguageClient_usePopupHover', v:true)
336+
let display_approach = 'popup_win'
337+
else
338+
let display_approach = 'preview'
339+
endif
340+
341+
if display_approach ==# 'float_win'
332342
" When a language server takes a while to initialize and the user
333343
" calls hover multiple times during that time (for example, via an
334344
" automatic hover on cursor move setup), we will get a number of
@@ -391,23 +401,30 @@ function! s:OpenHoverPreview(bufname, lines, filetype) abort
391401
execute 'noswapfile edit!' a:bufname
392402

393403
setlocal winhl=Normal:CursorLine
394-
else
404+
elseif display_approach ==# 'popup_win'
405+
let pop_win_id = popup_atcursor(a:lines, {})
406+
call setbufvar(winbufnr(pop_win_id), '&filetype', a:filetype)
407+
elseif display_approach ==# 'preview'
395408
execute 'silent! noswapfile pedit!' a:bufname
396409
wincmd P
410+
else
411+
call s:Echoerr('Unknown display approach: ' . display_approach)
397412
endif
398413

399-
setlocal buftype=nofile nobuflisted bufhidden=wipe nonumber norelativenumber signcolumn=no modifiable
414+
if display_approach !=# 'popup_win'
415+
setlocal buftype=nofile nobuflisted bufhidden=wipe nonumber norelativenumber signcolumn=no modifiable
400416
401-
if a:filetype isnot v:null
402-
let &filetype = a:filetype
403-
endif
417+
if a:filetype isnot v:null
418+
let &filetype = a:filetype
419+
endif
404420

405-
call setline(1, lines)
406-
setlocal nomodified nomodifiable
421+
call setline(1, lines)
422+
setlocal nomodified nomodifiable
407423

408-
wincmd p
424+
wincmd p
425+
endif
409426

410-
if use_float_win
427+
if display_approach ==# 'float_win'
411428
" Unlike preview window, :pclose does not close window. Instead, close
412429
" hover window automatically when cursor is moved.
413430
let call_after_move = printf('<SID>CloseFloatingHoverOnCursorMove(%s)', string(pos))

doc/LanguageClient.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,23 @@ Valid Options: 1 | 0
352352
When the value is set to 1, |LanguageClient#textDocument_hover()| opens
353353
documentation in a floating window instead of preview.
354354
This variable is effective only when the floating window feature is
355-
supported.
355+
supported. Check by running >
356+
:echomsg exists('nvim_open_win')
356357
357358
Default: 1 when a floating window is supported, otherwise 0
358359
Valid Options: 1 | 0
359360

361+
2.26 g:LanguageClient_usePopupHover *g:LanguageClient_usePopupHover*
362+
363+
When the value is set to 1, |LanguageClient#textDocument_hover()| opens
364+
documentation in a popup window instead of preview.
365+
This variable is effective only when the popup window feature is
366+
supported, which is supported only in vim 8.2+. Check by running >
367+
:echomsg has('popupwin')
368+
369+
Default: 1 when a popup window is supported, otherwise 0
370+
Valid Options: 1 | 0
371+
360372
2.27 g:LanguageClient_virtualTextPrefix *g:LanguageClient_virtualTextPrefix*
361373

362374
When the value is set to a valid string and |g:LanguageClient_useVirtualText| is

0 commit comments

Comments
 (0)