Skip to content

Commit b242af3

Browse files
committed
Closing and focusing floating preview (nvim)
1 parent 36e9f25 commit b242af3

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

autoload/lsp/ui/vim/output.vim

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
11
let s:supports_floating = exists('*nvim_open_win') || has('patch-8.1.1517')
22
let s:win = v:false
3+
let s:prevwin = v:false
34

45
function! lsp#ui#vim#output#closepreview() abort
56
if win_getid() == s:win
67
" Don't close if window got focus
78
return
89
endif
9-
pclose
10+
"closing floats in vim8.1 must use popup_close() (nvim could use nvim_win_close but pclose
11+
"works)
12+
if s:supports_floating && s:win && g:lsp_preview_float && !has('nvim')
13+
" TODO:
14+
call popup_close(s:win)
15+
else
16+
pclose
17+
endif
1018
let s:win = v:false
1119
autocmd! lsp_float_preview_close CursorMoved,CursorMovedI,VimResized *
1220
endfunction
1321

22+
function! lsp#ui#vim#output#focuspreview() abort
23+
" This does not work for vim8.1 popup but will work for nvim and old preview
24+
if s:win
25+
if win_getid() != s:win
26+
let s:prevwin = win_getid()
27+
call win_gotoid(s:win)
28+
elseif s:prevwin
29+
" Temporarily disable hooks
30+
" TODO: remove this when closing logic is able to distinguish different move directions
31+
autocmd! lsp_float_preview_close CursorMoved,CursorMovedI,VimResized *
32+
call win_gotoid(s:prevwin)
33+
call s:add_float_closing_hooks()
34+
let s:prevwin = v:false
35+
endif
36+
endif
37+
endfunction
38+
1439
function! s:bufwidth() abort
1540
let width = winwidth(0)
1641
let numberwidth = max([&numberwidth, strlen(line('$'))+1])
@@ -83,13 +108,13 @@ function! lsp#ui#vim#output#floatingpreview(data) abort
83108
call nvim_win_set_option(s:win, 'cursorline', v:false)
84109
" Enable closing the preview with esc, but map only in the scratch buffer
85110
nmap <buffer><silent> <esc> :pclose<cr>
86-
return s:win
87111
else
88-
return popup_atcursor('...', {
112+
let s:win = popup_atcursor('...', {
89113
\ 'moved': 'any',
90114
\ 'border': [1, 1, 1, 1],
91115
\})
92116
endif
117+
return s:win
93118
endfunction
94119

95120
function! s:setcontent(lines, ft) abort
@@ -116,10 +141,12 @@ function! s:adjust_float_placement(bufferlines, maxwidth) abort
116141
endfunction
117142

118143
function! s:add_float_closing_hooks() abort
144+
if g:lsp_preview_autoclose
119145
augroup lsp_float_preview_close
120146
autocmd! lsp_float_preview_close CursorMoved,CursorMovedI,VimResized *
121147
autocmd CursorMoved,CursorMovedI,VimResized * call lsp#ui#vim#output#closepreview()
122148
augroup END
149+
endif
123150
endfunction
124151

125152
function! lsp#ui#vim#output#preview(data) abort

doc/vim-lsp.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CONTENTS *vim-lsp-contents*
1414
g:lsp_auto_enable |g:lsp_auto_enable|
1515
g:lsp_preview_keep_focus |g:lsp_preview_keep_focus|
1616
g:lsp_preview_float |g:lsp_preview_float|
17+
g:lsp_preview_autoclose |g:lsp_preview_autoclose|
1718
g:lsp_insert_text_enabled |g:lsp_insert_text_enabled|
1819
g:lsp_text_edit_enabled |g:lsp_text_edit_enabled|
1920
g:lsp_diagnostics_echo_cursor |g:lsp_diagnostics_echo_cursor|
@@ -54,6 +55,9 @@ CONTENTS *vim-lsp-contents*
5455
Autocommands |vim-lsp-autocommands|
5556
lsp_complete_done |lsp_complete_done|
5657
Mappings |vim-lsp-mappings|
58+
<plug>(lsp-preview-close) |<plug>(lsp-preview-close)|
59+
<plug>(lsp-preview-focus) |<plug>(lsp-preview-focus)|
60+
5761
Autocomplete |vim-lsp-autocomplete|
5862
omnifunc |vim-lsp-omnifunc|
5963
asyncomplete.vim |vim-lsp-asyncomplete|
@@ -172,6 +176,23 @@ g:lsp_preview_float *g:lsp_preview_float*
172176
" Opens preview windows as normal windows
173177
let g:lsp_preview_float = 0
174178

179+
g:lsp_preview_autoclose *g:lsp_preview_autoclose*
180+
Type: |Number|
181+
Default: `1`
182+
183+
Indicates if an opened floating preview shall be automatically closed upon
184+
movement of the cursor. If set to 1, the window will close automatically if
185+
the cursor is moved and the preview is not focused. If set to 0, it will
186+
remain open until explicitly closed (e.g. with <plug>(lsp-preview-close),
187+
or <ESC> when focused).
188+
189+
Example:
190+
" Preview closes on cursor move
191+
let g:lsp_preview_autoclose = 1
192+
193+
" Preview remains open and waits for an explicit call
194+
let g:lsp_preview_autoclose = 0
195+
175196
g:lsp_insert_text_enabled *g:lsp_insert_text_enabled*
176197
Type: |Number|
177198
Default: `1`
@@ -661,6 +682,8 @@ Available plug mappings are following:
661682
(lsp-hover)
662683
(lsp-next-error)
663684
(lsp-next-reference)
685+
(lsp-preview-close)
686+
(lsp-preview-focus)
664687
(lsp-previous-error)
665688
(lsp-previous-reference)
666689
(lsp-references)
@@ -674,6 +697,16 @@ Available plug mappings are following:
674697

675698
See also |vim-lsp-commands|
676699

700+
<plug>(lsp-preview-close) *<plug>(lsp-preview-close)*
701+
702+
Closes an opened preview window
703+
704+
<plug>(lsp-preview-focus) *<plug>(lsp-preview-focus)*
705+
706+
Transfers focus to an opened preview window or back to the previous window if
707+
focus is already on the preview window.
708+
709+
677710
===============================================================================
678711
Autocomplete *vim-lsp-autocomplete*
679712

plugin/lsp.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ let g:lsp_insert_text_enabled= get(g:, 'lsp_insert_text_enabled', 1)
2626
let g:lsp_text_edit_enabled = get(g:, 'lsp_text_edit_enabled', has('patch-8.0.1493'))
2727
let g:lsp_highlight_references_enabled = get(g:, 'lsp_highlight_references_enabled', 1)
2828
let g:lsp_preview_float = get(g:, 'lsp_preview_float', 1)
29+
let g:lsp_preview_autoclose = get(g:, 'lsp_preview_autoclose', 1)
2930

3031
let g:lsp_get_vim_completion_item = get(g:, 'lsp_get_vim_completion_item', [function('lsp#omni#default_get_vim_completion_item')])
3132
let g:lsp_get_supported_capabilities = get(g:, 'lsp_get_supported_capabilities', [function('lsp#default_get_supported_capabilities')])
@@ -65,6 +66,8 @@ nnoremap <plug>(lsp-definition) :<c-u>call lsp#ui#vim#definition()<cr>
6566
nnoremap <plug>(lsp-document-symbol) :<c-u>call lsp#ui#vim#document_symbol()<cr>
6667
nnoremap <plug>(lsp-document-diagnostics) :<c-u>call lsp#ui#vim#diagnostics#document_diagnostics()<cr>
6768
nnoremap <plug>(lsp-hover) :<c-u>call lsp#ui#vim#hover#get_hover_under_cursor()<cr>
69+
nnoremap <plug>(lsp-preview-close) :<c-u>call lsp#ui#vim#output#closepreview()<cr>
70+
nnoremap <plug>(lsp-preview-focus) :<c-u>call lsp#ui#vim#output#focuspreview()<cr>
6871
nnoremap <plug>(lsp-next-error) :<c-u>call lsp#ui#vim#diagnostics#next_error()<cr>
6972
nnoremap <plug>(lsp-previous-error) :<c-u>call lsp#ui#vim#diagnostics#previous_error()<cr>
7073
nnoremap <plug>(lsp-references) :<c-u>call lsp#ui#vim#references()<cr>

0 commit comments

Comments
 (0)