Nicer syntax highlighting for hover (#441)

* Implement syntax highlighting for hover

* Fix signatureHelp
This commit is contained in:
Thomas Faingnaert
2019-09-08 20:26:22 +02:00
committed by Prabir Shrestha
parent 5fafaa3d72
commit d7e644c2a7
8 changed files with 91 additions and 10 deletions

View File

@@ -488,7 +488,7 @@ function! s:handle_location(ctx, server, type, data) abort "ctx = {counter, list
botright copen
else
let l:lines = readfile(fnameescape(l:loc['filename']))
call lsp#ui#vim#output#preview(l:lines, {
call lsp#ui#vim#output#preview(a:server, l:lines, {
\ 'statusline': ' LSP Peek ' . a:type,
\ 'cursor': { 'line': l:loc['lnum'], 'col': l:loc['col'], 'align': g:lsp_peek_alignment },
\ 'filetype': &filetype

View File

@@ -35,7 +35,7 @@ function! s:handle_hover(server, data) abort
endif
if !empty(a:data['response']['result']) && !empty(a:data['response']['result']['contents'])
call lsp#ui#vim#output#preview(a:data['response']['result']['contents'], {'statusline': ' LSP Hover'})
call lsp#ui#vim#output#preview(a:server, a:data['response']['result']['contents'], {'statusline': ' LSP Hover'})
return
else
call lsp#utils#error('No hover information found')

View File

@@ -284,7 +284,7 @@ function! s:align_preview(options) abort
endif
endfunction
function! lsp#ui#vim#output#preview(data, options) abort
function! lsp#ui#vim#output#preview(server, data, options) abort
if s:winid && type(s:preview_data) == type(a:data)
\ && s:preview_data == a:data
\ && type(g:lsp_preview_doubletap) == 3
@@ -302,12 +302,22 @@ function! lsp#ui#vim#output#preview(data, options) abort
let s:preview_data = a:data
let l:lines = []
let l:ft = s:append(a:data, l:lines)
let l:syntax_lines = []
let l:ft = s:append(a:data, l:lines, l:syntax_lines)
if has_key(a:options, 'filetype')
let l:ft = a:options['filetype']
endif
let l:server_info = lsp#get_server_info(a:server)
try
let l:do_conceal = l:server_info['config']['hover_conceal']
catch
let l:do_conceal = g:lsp_hover_conceal
endtry
call setbufvar(winbufnr(s:winid), 'lsp_syntax_highlights', l:syntax_lines)
call setbufvar(winbufnr(s:winid), 'lsp_do_conceal', l:do_conceal)
call s:setcontent(l:lines, l:ft)
" Get size information while still having the buffer active
@@ -361,10 +371,10 @@ function! lsp#ui#vim#output#preview(data, options) abort
return ''
endfunction
function! s:append(data, lines) abort
function! s:append(data, lines, syntax_lines) abort
if type(a:data) == type([])
for l:entry in a:data
call s:append(entry, a:lines)
call s:append(entry, a:lines, a:syntax_lines)
endfor
return 'markdown'
@@ -373,9 +383,15 @@ function! s:append(data, lines) abort
return 'markdown'
elseif type(a:data) == type({}) && has_key(a:data, 'language')
call add(a:lines, '```'.a:data.language)
call extend(a:lines, split(a:data.value, '\n'))
call add(a:lines, '```')
let l:new_lines = split(a:data.value, '\n')
let l:i = 1
while l:i <= len(l:new_lines)
call add(a:syntax_lines, { 'line': len(a:lines) + l:i, 'language': a:data.language })
let l:i += 1
endwhile
call extend(a:lines, l:new_lines)
return 'markdown'
elseif type(a:data) == type({}) && has_key(a:data, 'kind')

View File

@@ -43,7 +43,7 @@ function! s:handle_signature_help(server, data) abort
if has_key(l:signature, 'documentation')
call add(l:contents, l:signature['documentation'])
endif
call lsp#ui#vim#output#preview(l:contents, {'statusline': ' LSP SignatureHelp'})
call lsp#ui#vim#output#preview(a:server, l:contents, {'statusline': ' LSP SignatureHelp'})
return
else
" signature help is used while inserting. So this must be graceful.

View File

@@ -34,6 +34,7 @@ CONTENTS *vim-lsp-contents*
g:lsp_preview_max_height |g:lsp_preview_max_height|
g:lsp_signature_help_enabled |g:lsp_signature_help_enabled|
g:lsp_fold_enabled |g:lsp_fold_enabled|
g:lsp_hover_conceal |g:lsp_hover_conceal|
Functions |vim-lsp-functions|
enable |vim-lsp-enable|
disable |vim-lsp-disable|
@@ -492,6 +493,18 @@ g:lsp_fold_enabled *g:lsp_fold_enabled*
Determines whether or not folding is enabled globally. Set to `0` to
disable sending requests.
g:lsp_hover_conceal *g:lsp_hover_conceal*
Type: |Boolean|
Default: `1`
If `true` (`1`), |conceallevel| is set to `2` for hover windows. This
means that, for example, asterisks in markdown hovers are hidden, but the
text is still displayed bold. You may want to disable this if the filetype
of the popup has custom conceals which you don't want to use, or if
you're using Vim in a terminal.
To override this setting per server, see |vim-lsp-hover_conceal|.
===============================================================================
FUNCTIONS *vim-lsp-functions*
@@ -642,6 +655,15 @@ The vim |dict| containing information about the server.
'workspace_config': {'pyls': {'plugins': \
{'pydocstyle': {'enabled': v:true}}}}
hover_conceal *vim-lsp_hover_conceal*
Type: |Boolean|
Default: |g:lsp_hover_conceal|
This takes precendence over the value of |g:lsp_hover_conceal|, to allow
overriding this setting per server.
Example:
'config': { 'hover_conceal': 1 }
lsp#stop_server *vim-lsp-stop_server*

View File

@@ -6,6 +6,11 @@ if has('patch-8.1.1517') && g:lsp_preview_float && !has('nvim')
else
setlocal previewwindow buftype=nofile bufhidden=wipe noswapfile nobuflisted
endif
if has('conceal') && b:lsp_do_conceal
setlocal conceallevel=2
endif
setlocal nocursorline nofoldenable nonumber norelativenumber
if has('syntax')
@@ -15,4 +20,5 @@ endif
let b:undo_ftplugin = 'setlocal pvw< bt< bh< swf< bl< cul< fen<' .
\ (has('syntax') ? ' spell<' : '') .
\ ' number< relativenumber<' .
\ (has('conceal') && b:lsp_do_conceal ? ' conceallevel<' : '') .
\ ' | unlet! g:markdown_fenced_languages'

View File

@@ -36,6 +36,7 @@ let g:lsp_preview_max_width = get(g:, 'lsp_preview_max_width', -1)
let g:lsp_preview_max_height = get(g:, 'lsp_preview_max_height', -1)
let g:lsp_signature_help_enabled = get(g:, 'lsp_signature_help_enabled', 1)
let g:lsp_fold_enabled = get(g:, 'lsp_fold_enabled', 1)
let g:lsp_hover_conceal = get(g:, 'lsp_hover_conceal', 1)
let g:lsp_get_vim_completion_item = get(g:, 'lsp_get_vim_completion_item', [function('lsp#omni#default_get_vim_completion_item')])
let g:lsp_get_supported_capabilities = get(g:, 'lsp_get_supported_capabilities', [function('lsp#default_get_supported_capabilities')])

36
syntax/lsp-hover.vim Normal file
View File

@@ -0,0 +1,36 @@
" Converts a markdown language (```foo) to the corresponding Vim filetype
function! s:get_vim_filetype(lang_markdown) abort
" If the user hasn't customised markdown fenced languages, just return the
" markdown language
if !exists('g:markdown_fenced_languages')
return a:lang_markdown
endif
" Otherwise, check if it has an entry for the given language
for l:type in g:markdown_fenced_languages
let l:vim_type = substitute(matchstr(l:type, '[^=]*$'), '\..*', '', '')
let l:markdown_type = matchstr(l:type, '[^=]*')
if l:markdown_type ==# a:lang_markdown
" Found entry
return l:vim_type
endif
endfor
" Not found
return a:lang_markdown
endfunction
function! s:do_highlight() abort
if exists('b:lsp_syntax_highlights')
for l:entry in b:lsp_syntax_highlights
let l:line = l:entry['line']
let l:lang = l:entry['language']
let l:ft = s:get_vim_filetype(l:lang)
execute printf('syntax match markdownHighlight%s contains=@markdownHighlight%s /^\%%%sl.*$/', l:ft, l:ft, l:line)
endfor
endif
endfunction
call s:do_highlight()