Skip to content

Commit

Permalink
Allow reuse of ShowHover preview window
Browse files Browse the repository at this point in the history
Check if a preview window is already open and switch to it instead of
splitting. Avoids `pclose` which can change layouts which were manually
adjusted.

Set `winfixheight` so that further splits with `equalalways` won't make
the preview window bigger than it needs to be - completion preview
windows already set this option.
  • Loading branch information
natebosch committed Aug 2, 2017
1 parent 7b6f3c5 commit 6429372
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# 0.2.4-dev

- Bug Fix: Handle completion items with empty detail.
- `LSClientShowHover` now reuses the window it already opened rather than
closing it and splitting again to allow for maintaining layout.

# 0.2.3

Expand Down
36 changes: 27 additions & 9 deletions autoload/lsc/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,39 @@ function! s:QuickFixFilename(item) abort
return bufname(a:item.bufnr)
endfunction

" Populate a hidden buffer with [lines] and show it as a preview window.
" Populate a buffer with [lines] and show it as a preview window.
"
" If the __lsc_preview__ buffer was already showing, reuse it's window,
" otherwise split a window with a max height of `&previewheight`.
function! lsc#util#displayAsPreview(lines) abort
let view = winsaveview()
let alternate=@#
silent! pclose
sp __lsc_preview__
execute 'resize '.min([len(a:lines), &previewheight])
call s:createOrJumpToPreview(len(a:lines))
%d
call setline(1, a:lines)
wincmd p
call winrestview(view)
let @#=alternate
endfunction

function! s:createOrJumpToPreview(line_count) abort
let want_height = min([a:line_count, &previewheight])
let windows = range(1, winnr('$'))
call filter(windows, 'getwinvar(v:val, "&previewwindow") == 1')
if len(windows) > 0
execute string(windows[0]).' wincmd W'
edit __lsc_preview__
if winheight(windows[0]) < want_height
execute 'resize '.want_height
endif
else
sp __lsc_preview__
execute 'resize '.want_height
endif
set previewwindow
set winfixheight
setlocal bufhidden=hide
setlocal nobuflisted
setlocal buftype=nofile
setlocal noswapfile
%d
call setline(1, a:lines)
execute "normal \<c-w>p"
call winrestview(view)
let @#=alternate
endfunction

0 comments on commit 6429372

Please sign in to comment.