From fd3df08248413b6cdb75781a8abc02a329d45066 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Wed, 12 Jun 2019 11:38:41 +0900 Subject: [PATCH 1/2] Use popup window for hover preview --- autoload/lsp/ui/vim.vim | 4 +-- autoload/lsp/ui/vim/output.vim | 61 ++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/autoload/lsp/ui/vim.vim b/autoload/lsp/ui/vim.vim index f6e677f1c..b2b7c230c 100644 --- a/autoload/lsp/ui/vim.vim +++ b/autoload/lsp/ui/vim.vim @@ -440,9 +440,9 @@ function! s:handle_location(ctx, server, type, data) abort "ctx = {counter, list let l:loc = a:ctx['list'][0] let l:buffer = bufnr(l:loc['filename']) if &modified && !&hidden - let l:cmd = l:buffer !=# -1 ? 'sb ' . l:buffer : 'split ' . l:loc['filename'] + let l:cmd = l:buffer !=# -1 ? 'sb ' . l:buffer : 'split ' . fnameescape(l:loc['filename']) else - let l:cmd = l:buffer !=# -1 ? 'b ' . l:buffer : 'edit ' . l:loc['filename'] + let l:cmd = l:buffer !=# -1 ? 'b ' . l:buffer : 'edit ' . fnameescape(l:loc['filename']) endif execute l:cmd . ' | call cursor('.l:loc['lnum'].','.l:loc['col'].')' echo 'Retrieved ' . a:type diff --git a/autoload/lsp/ui/vim/output.vim b/autoload/lsp/ui/vim/output.vim index 159c97372..722b94606 100644 --- a/autoload/lsp/ui/vim/output.vim +++ b/autoload/lsp/ui/vim/output.vim @@ -1,49 +1,52 @@ function! lsp#ui#vim#output#preview(data) abort - " Close any previously opened preview window - pclose + if has('patch-8.1.1517') + let l:winid = popup_atcursor('...', { + \ 'moved': 'any', + \ 'border': [1, 1, 1, 1], + \}) + let l:buf = winbufnr(l:winid) + let [l:ft, l:lines] = s:tolines(a:data) + call setbufline(l:buf, 1, split(l:lines, "\n")) + call win_execute(l:winid, 'setlocal filetype=' . l:ft . '.lsp-hover') + else + " Close any previously opened preview window + pclose - let l:current_window_id = win_getid() + let l:current_window_id = win_getid() - execute &previewheight.'new' + execute &previewheight.'new' - let l:ft = s:append(a:data) - " Delete first empty line - 0delete _ + let [l:ft, l:lines] = s:tolines(a:data) + call setline(1, split(l:lines, "\n")) - setlocal readonly nomodifiable + setlocal readonly nomodifiable - let &l:filetype = l:ft . '.lsp-hover' + let &l:filetype = l:ft . '.lsp-hover' - if g:lsp_preview_keep_focus - " restore focus to the previous window - call win_gotoid(l:current_window_id) - endif + if g:lsp_preview_keep_focus + " restore focus to the previous window + call win_gotoid(l:current_window_id) + endif - echo '' + echo '' + endif return '' endfunction -function! s:append(data) abort +function! s:tolines(data) abort if type(a:data) == type([]) + let l:lines = '' for l:entry in a:data - call s:append(entry) + let l:lines .= s:tolines(l:entry)[1] . "\n" endfor - - return 'markdown' + return ['markdown', l:lines] elseif type(a:data) == type('') - silent put =a:data - - return 'markdown' + return ['markdown', a:data] elseif type(a:data) == type({}) && has_key(a:data, 'language') - silent put ='```'.a:data.language - silent put =a:data.value - silent put ='```' - - return 'markdown' + return ['markdown', '```' . a:data.language . "\n" . a:data.value . "\n```\n"] elseif type(a:data) == type({}) && has_key(a:data, 'kind') - silent put =a:data.value - - return a:data.kind ==? 'plaintext' ? 'text' : a:data.kind + return [a:data.kind ==? 'plaintext' ? 'text' : a:data.kind, a:data.value] endif + return ['', ''] endfunction From dddff34c3bd267f682d5d1d5764df37775cb43bf Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Wed, 12 Jun 2019 11:42:57 +0900 Subject: [PATCH 2/2] Revert change for #167 --- autoload/lsp/ui/vim.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autoload/lsp/ui/vim.vim b/autoload/lsp/ui/vim.vim index b2b7c230c..f6e677f1c 100644 --- a/autoload/lsp/ui/vim.vim +++ b/autoload/lsp/ui/vim.vim @@ -440,9 +440,9 @@ function! s:handle_location(ctx, server, type, data) abort "ctx = {counter, list let l:loc = a:ctx['list'][0] let l:buffer = bufnr(l:loc['filename']) if &modified && !&hidden - let l:cmd = l:buffer !=# -1 ? 'sb ' . l:buffer : 'split ' . fnameescape(l:loc['filename']) + let l:cmd = l:buffer !=# -1 ? 'sb ' . l:buffer : 'split ' . l:loc['filename'] else - let l:cmd = l:buffer !=# -1 ? 'b ' . l:buffer : 'edit ' . fnameescape(l:loc['filename']) + let l:cmd = l:buffer !=# -1 ? 'b ' . l:buffer : 'edit ' . l:loc['filename'] endif execute l:cmd . ' | call cursor('.l:loc['lnum'].','.l:loc['col'].')' echo 'Retrieved ' . a:type