Skip to content

Commit

Permalink
autocmd events for open/close floats
Browse files Browse the repository at this point in the history
  • Loading branch information
jerdna-regeiz committed Jun 16, 2019
1 parent 20404b4 commit 27500f2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
2 changes: 2 additions & 0 deletions autoload/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ augroup _lsp_silent_
autocmd User lsp_unregister_server silent
autocmd User lsp_server_init silent
autocmd User lsp_server_exit silent
autocmd User lsp_float_opened silent
autocmd User lsp_float_closed silent
augroup END

function! lsp#log_verbose(...) abort
Expand Down
31 changes: 22 additions & 9 deletions autoload/lsp/ui/vim/output.vim
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function! lsp#ui#vim#output#closepreview() abort
endif
let s:winid = v:false
autocmd! lsp_float_preview_close CursorMoved,CursorMovedI,VimResized *
doautocmd User lsp_float_closed
endfunction

function! lsp#ui#vim#output#focuspreview() abort
Expand Down Expand Up @@ -149,18 +150,27 @@ function! s:add_float_closing_hooks() abort
endif
endfunction

function! lsp#ui#vim#output#getpreviewwinid() abort
return s:winid
endfunction

function! s:open_preview(data) abort
if s:supports_floating && g:lsp_preview_float
let l:winid = lsp#ui#vim#output#floatingpreview(a:data)
else
execute &previewheight.'new'
let l:winid = win_getid()
endif
return l:winid
endfunction

function! lsp#ui#vim#output#preview(data) abort
" Close any previously opened preview window
pclose

let l:current_window_id = win_getid()

if s:supports_floating && g:lsp_preview_float
let s:winid = lsp#ui#vim#output#floatingpreview(a:data)
else
execute &previewheight.'new'
let s:winid = win_getid()
endif
let s:winid = s:open_preview(a:data)

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

echo ''

if s:supports_floating && s:winid && g:lsp_preview_float && has('nvim')
call s:adjust_float_placement(l:bufferlines, l:maxwidth)
call s:add_float_closing_hooks()
if s:supports_floating && s:winid && g:lsp_preview_float
if has('nvim')
call s:adjust_float_placement(l:bufferlines, l:maxwidth)
call s:add_float_closing_hooks()
endif
doautocmd User lsp_float_opened
endif
return ''
endfunction
Expand Down
16 changes: 13 additions & 3 deletions doc/vim-lsp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,16 @@ g:lsp_preview_float *g:lsp_preview_float*
Type: |Number|
Default: `1`

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

Example:
" Opens preview windows as floating
Expand All @@ -171,6 +176,11 @@ g:lsp_preview_float *g:lsp_preview_float*
" Opens preview windows as normal windows
let g:lsp_preview_float = 0

" Close preview window with <esc>
autocmd User lsp_float_opened nmap <buffer> <silent> <esc>
\ <Plug>(lsp-preview-close)
autocmd User lsp_float_closed nunmap <buffer> <esc>

g:lsp_preview_autoclose *g:lsp_preview_autoclose*
Type: |Number|
Default: `1`
Expand Down

0 comments on commit 27500f2

Please sign in to comment.