Skip to content

Commit bb26de6

Browse files
committed
Closing and focusing floating preview (nvim)
1 parent 96af1b7 commit bb26de6

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|
@@ -50,6 +51,9 @@ CONTENTS *vim-lsp-contents*
5051
LspTypeDefinition |LspTypeDefinition|
5152
LspWorkspaceSymbol |LspWorkspaceSymbol|
5253
Mappings |vim-lsp-mappings|
54+
<plug>(lsp-preview-close) |<plug>(lsp-preview-close)|
55+
<plug>(lsp-preview-focus) |<plug>(lsp-preview-focus)|
56+
5357
Autocomplete |vim-lsp-autocomplete|
5458
omnifunc |vim-lsp-omnifunc|
5559
asyncomplete.vim |vim-lsp-asyncomplete|
@@ -167,6 +171,23 @@ g:lsp_preview_float *g:lsp_preview_float*
167171
" Opens preview windows as normal windows
168172
let g:lsp_preview_float = 0
169173

174+
g:lsp_preview_autoclose *g:lsp_preview_autoclose*
175+
Type: |Number|
176+
Default: `1`
177+
178+
Indicates if an opened floating preview shall be automatically closed upon
179+
movement of the cursor. If set to 1, the window will close automatically if
180+
the cursor is moved and the preview is not focused. If set to 0, it will
181+
remain open until explicitly closed (e.g. with <plug>(lsp-preview-close),
182+
or <ESC> when focused).
183+
184+
Example:
185+
" Preview closes on cursor move
186+
let g:lsp_preview_autoclose = 1
187+
188+
" Preview remains open and waits for an explicit call
189+
let g:lsp_preview_autoclose = 0
190+
170191
g:lsp_insert_text_enabled *g:lsp_insert_text_enabled*
171192
Type: |Number|
172193
Default: `1`
@@ -615,6 +636,8 @@ Available plug mappings are following:
615636
(lsp-hover)
616637
(lsp-next-error)
617638
(lsp-next-reference)
639+
(lsp-preview-close)
640+
(lsp-preview-focus)
618641
(lsp-previous-error)
619642
(lsp-previous-reference)
620643
(lsp-references)
@@ -628,6 +651,16 @@ Available plug mappings are following:
628651

629652
See also |vim-lsp-commands|
630653

654+
<plug>(lsp-preview-close) *<plug>(lsp-preview-close)*
655+
656+
Closes an opened preview window
657+
658+
<plug>(lsp-preview-focus) *<plug>(lsp-preview-focus)*
659+
660+
Transfers focus to an opened preview window or back to the previous window if
661+
focus is already on the preview window.
662+
663+
631664
===============================================================================
632665
Autocomplete *vim-lsp-autocomplete*
633666

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
if g:lsp_auto_enable
3132
augroup lsp_auto_enable
@@ -62,6 +63,8 @@ nnoremap <plug>(lsp-definition) :<c-u>call lsp#ui#vim#definition()<cr>
6263
nnoremap <plug>(lsp-document-symbol) :<c-u>call lsp#ui#vim#document_symbol()<cr>
6364
nnoremap <plug>(lsp-document-diagnostics) :<c-u>call lsp#ui#vim#diagnostics#document_diagnostics()<cr>
6465
nnoremap <plug>(lsp-hover) :<c-u>call lsp#ui#vim#hover#get_hover_under_cursor()<cr>
66+
nnoremap <plug>(lsp-preview-close) :<c-u>call lsp#ui#vim#output#closepreview()<cr>
67+
nnoremap <plug>(lsp-preview-focus) :<c-u>call lsp#ui#vim#output#focuspreview()<cr>
6568
nnoremap <plug>(lsp-next-error) :<c-u>call lsp#ui#vim#diagnostics#next_error()<cr>
6669
nnoremap <plug>(lsp-previous-error) :<c-u>call lsp#ui#vim#diagnostics#previous_error()<cr>
6770
nnoremap <plug>(lsp-references) :<c-u>call lsp#ui#vim#references()<cr>

0 commit comments

Comments
 (0)