Skip to content

Commit 27500f2

Browse files
committed
autocmd events for open/close floats
1 parent 20404b4 commit 27500f2

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

autoload/lsp.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ augroup _lsp_silent_
2525
autocmd User lsp_unregister_server silent
2626
autocmd User lsp_server_init silent
2727
autocmd User lsp_server_exit silent
28+
autocmd User lsp_float_opened silent
29+
autocmd User lsp_float_closed silent
2830
augroup END
2931

3032
function! lsp#log_verbose(...) abort

autoload/lsp/ui/vim/output.vim

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function! lsp#ui#vim#output#closepreview() abort
1717
endif
1818
let s:winid = v:false
1919
autocmd! lsp_float_preview_close CursorMoved,CursorMovedI,VimResized *
20+
doautocmd User lsp_float_closed
2021
endfunction
2122

2223
function! lsp#ui#vim#output#focuspreview() abort
@@ -149,18 +150,27 @@ function! s:add_float_closing_hooks() abort
149150
endif
150151
endfunction
151152

153+
function! lsp#ui#vim#output#getpreviewwinid() abort
154+
return s:winid
155+
endfunction
156+
157+
function! s:open_preview(data) abort
158+
if s:supports_floating && g:lsp_preview_float
159+
let l:winid = lsp#ui#vim#output#floatingpreview(a:data)
160+
else
161+
execute &previewheight.'new'
162+
let l:winid = win_getid()
163+
endif
164+
return l:winid
165+
endfunction
166+
152167
function! lsp#ui#vim#output#preview(data) abort
153168
" Close any previously opened preview window
154169
pclose
155170

156171
let l:current_window_id = win_getid()
157172

158-
if s:supports_floating && g:lsp_preview_float
159-
let s:winid = lsp#ui#vim#output#floatingpreview(a:data)
160-
else
161-
execute &previewheight.'new'
162-
let s:winid = win_getid()
163-
endif
173+
let s:winid = s:open_preview(a:data)
164174

165175
let l:lines = []
166176
let l:ft = s:append(a:data, l:lines)
@@ -177,9 +187,12 @@ function! lsp#ui#vim#output#preview(data) abort
177187

178188
echo ''
179189

180-
if s:supports_floating && s:winid && g:lsp_preview_float && has('nvim')
181-
call s:adjust_float_placement(l:bufferlines, l:maxwidth)
182-
call s:add_float_closing_hooks()
190+
if s:supports_floating && s:winid && g:lsp_preview_float
191+
if has('nvim')
192+
call s:adjust_float_placement(l:bufferlines, l:maxwidth)
193+
call s:add_float_closing_hooks()
194+
endif
195+
doautocmd User lsp_float_opened
183196
endif
184197
return ''
185198
endfunction

doc/vim-lsp.txt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,16 @@ g:lsp_preview_float *g:lsp_preview_float*
158158
Type: |Number|
159159
Default: `1`
160160

161-
If set and nvim_win_open() is available, hover information are shown in a
162-
floating window as |preview-window| at the cursor position.
161+
If set and nvim_win_open() or popup_create is available, hover information
162+
are shown in a floating window as |preview-window| at the cursor position.
163163
The |preview-window| is closed automatically on cursor moves, unless it is
164164
focused. While focused it may be closed with <esc>.
165-
This feature requires neovim 0.4.0 (current master).
165+
After opening an autocmd User event lsp_float_opened is issued, as well as
166+
and lsp_float_closed upon closing. This can be used to alter the preview
167+
window (using lsp#ui#vim#output#getpreviewwinid() to get the window id), or
168+
setup custom bindings while a preview is open.
169+
This feature requires neovim 0.4.0 (current master) or
170+
Vim8.1 with has('patch-8.1.1517').
166171

167172
Example:
168173
" Opens preview windows as floating
@@ -171,6 +176,11 @@ g:lsp_preview_float *g:lsp_preview_float*
171176
" Opens preview windows as normal windows
172177
let g:lsp_preview_float = 0
173178

179+
" Close preview window with <esc>
180+
autocmd User lsp_float_opened nmap <buffer> <silent> <esc>
181+
\ <Plug>(lsp-preview-close)
182+
autocmd User lsp_float_closed nunmap <buffer> <esc>
183+
174184
g:lsp_preview_autoclose *g:lsp_preview_autoclose*
175185
Type: |Number|
176186
Default: `1`

0 commit comments

Comments
 (0)