Skip to content

Commit 920f2f8

Browse files
committed
autocmd events for open/close floats
1 parent e71ae18 commit 920f2f8

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
@@ -26,6 +26,8 @@ augroup _lsp_silent_
2626
autocmd User lsp_server_init silent
2727
autocmd User lsp_server_exit silent
2828
autocmd User lsp_complete_done silent
29+
autocmd User lsp_float_opened silent
30+
autocmd User lsp_float_closed silent
2931
augroup END
3032

3133
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
@@ -163,11 +163,16 @@ g:lsp_preview_float *g:lsp_preview_float*
163163
Type: |Number|
164164
Default: `1`
165165

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

172177
Example:
173178
" Opens preview windows as floating
@@ -176,6 +181,11 @@ g:lsp_preview_float *g:lsp_preview_float*
176181
" Opens preview windows as normal windows
177182
let g:lsp_preview_float = 0
178183

184+
" Close preview window with <esc>
185+
autocmd User lsp_float_opened nmap <buffer> <silent> <esc>
186+
\ <Plug>(lsp-preview-close)
187+
autocmd User lsp_float_closed nunmap <buffer> <esc>
188+
179189
g:lsp_preview_autoclose *g:lsp_preview_autoclose*
180190
Type: |Number|
181191
Default: `1`

0 commit comments

Comments
 (0)