Skip to content

Commit d7e644c

Browse files
thomasfaingnaertprabirshrestha
authored andcommitted
Nicer syntax highlighting for hover (#441)
* Implement syntax highlighting for hover * Fix signatureHelp
1 parent 5fafaa3 commit d7e644c

File tree

8 files changed

+91
-10
lines changed

8 files changed

+91
-10
lines changed

autoload/lsp/ui/vim.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ function! s:handle_location(ctx, server, type, data) abort "ctx = {counter, list
488488
botright copen
489489
else
490490
let l:lines = readfile(fnameescape(l:loc['filename']))
491-
call lsp#ui#vim#output#preview(l:lines, {
491+
call lsp#ui#vim#output#preview(a:server, l:lines, {
492492
\ 'statusline': ' LSP Peek ' . a:type,
493493
\ 'cursor': { 'line': l:loc['lnum'], 'col': l:loc['col'], 'align': g:lsp_peek_alignment },
494494
\ 'filetype': &filetype

autoload/lsp/ui/vim/hover.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function! s:handle_hover(server, data) abort
3535
endif
3636

3737
if !empty(a:data['response']['result']) && !empty(a:data['response']['result']['contents'])
38-
call lsp#ui#vim#output#preview(a:data['response']['result']['contents'], {'statusline': ' LSP Hover'})
38+
call lsp#ui#vim#output#preview(a:server, a:data['response']['result']['contents'], {'statusline': ' LSP Hover'})
3939
return
4040
else
4141
call lsp#utils#error('No hover information found')

autoload/lsp/ui/vim/output.vim

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ function! s:align_preview(options) abort
284284
endif
285285
endfunction
286286

287-
function! lsp#ui#vim#output#preview(data, options) abort
287+
function! lsp#ui#vim#output#preview(server, data, options) abort
288288
if s:winid && type(s:preview_data) == type(a:data)
289289
\ && s:preview_data == a:data
290290
\ && type(g:lsp_preview_doubletap) == 3
@@ -302,12 +302,22 @@ function! lsp#ui#vim#output#preview(data, options) abort
302302

303303
let s:preview_data = a:data
304304
let l:lines = []
305-
let l:ft = s:append(a:data, l:lines)
305+
let l:syntax_lines = []
306+
let l:ft = s:append(a:data, l:lines, l:syntax_lines)
306307

307308
if has_key(a:options, 'filetype')
308309
let l:ft = a:options['filetype']
309310
endif
310311

312+
let l:server_info = lsp#get_server_info(a:server)
313+
try
314+
let l:do_conceal = l:server_info['config']['hover_conceal']
315+
catch
316+
let l:do_conceal = g:lsp_hover_conceal
317+
endtry
318+
319+
call setbufvar(winbufnr(s:winid), 'lsp_syntax_highlights', l:syntax_lines)
320+
call setbufvar(winbufnr(s:winid), 'lsp_do_conceal', l:do_conceal)
311321
call s:setcontent(l:lines, l:ft)
312322

313323
" Get size information while still having the buffer active
@@ -361,10 +371,10 @@ function! lsp#ui#vim#output#preview(data, options) abort
361371
return ''
362372
endfunction
363373

364-
function! s:append(data, lines) abort
374+
function! s:append(data, lines, syntax_lines) abort
365375
if type(a:data) == type([])
366376
for l:entry in a:data
367-
call s:append(entry, a:lines)
377+
call s:append(entry, a:lines, a:syntax_lines)
368378
endfor
369379

370380
return 'markdown'
@@ -373,9 +383,15 @@ function! s:append(data, lines) abort
373383

374384
return 'markdown'
375385
elseif type(a:data) == type({}) && has_key(a:data, 'language')
376-
call add(a:lines, '```'.a:data.language)
377-
call extend(a:lines, split(a:data.value, '\n'))
378-
call add(a:lines, '```')
386+
let l:new_lines = split(a:data.value, '\n')
387+
388+
let l:i = 1
389+
while l:i <= len(l:new_lines)
390+
call add(a:syntax_lines, { 'line': len(a:lines) + l:i, 'language': a:data.language })
391+
let l:i += 1
392+
endwhile
393+
394+
call extend(a:lines, l:new_lines)
379395

380396
return 'markdown'
381397
elseif type(a:data) == type({}) && has_key(a:data, 'kind')

autoload/lsp/ui/vim/signature_help.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function! s:handle_signature_help(server, data) abort
4343
if has_key(l:signature, 'documentation')
4444
call add(l:contents, l:signature['documentation'])
4545
endif
46-
call lsp#ui#vim#output#preview(l:contents, {'statusline': ' LSP SignatureHelp'})
46+
call lsp#ui#vim#output#preview(a:server, l:contents, {'statusline': ' LSP SignatureHelp'})
4747
return
4848
else
4949
" signature help is used while inserting. So this must be graceful.

doc/vim-lsp.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ CONTENTS *vim-lsp-contents*
3434
g:lsp_preview_max_height |g:lsp_preview_max_height|
3535
g:lsp_signature_help_enabled |g:lsp_signature_help_enabled|
3636
g:lsp_fold_enabled |g:lsp_fold_enabled|
37+
g:lsp_hover_conceal |g:lsp_hover_conceal|
3738
Functions |vim-lsp-functions|
3839
enable |vim-lsp-enable|
3940
disable |vim-lsp-disable|
@@ -492,6 +493,18 @@ g:lsp_fold_enabled *g:lsp_fold_enabled*
492493
Determines whether or not folding is enabled globally. Set to `0` to
493494
disable sending requests.
494495

496+
g:lsp_hover_conceal *g:lsp_hover_conceal*
497+
Type: |Boolean|
498+
Default: `1`
499+
500+
If `true` (`1`), |conceallevel| is set to `2` for hover windows. This
501+
means that, for example, asterisks in markdown hovers are hidden, but the
502+
text is still displayed bold. You may want to disable this if the filetype
503+
of the popup has custom conceals which you don't want to use, or if
504+
you're using Vim in a terminal.
505+
506+
To override this setting per server, see |vim-lsp-hover_conceal|.
507+
495508
===============================================================================
496509
FUNCTIONS *vim-lsp-functions*
497510

@@ -642,6 +655,15 @@ The vim |dict| containing information about the server.
642655
'workspace_config': {'pyls': {'plugins': \
643656
{'pydocstyle': {'enabled': v:true}}}}
644657

658+
hover_conceal *vim-lsp_hover_conceal*
659+
Type: |Boolean|
660+
Default: |g:lsp_hover_conceal|
661+
662+
This takes precendence over the value of |g:lsp_hover_conceal|, to allow
663+
overriding this setting per server.
664+
665+
Example:
666+
'config': { 'hover_conceal': 1 }
645667

646668
lsp#stop_server *vim-lsp-stop_server*
647669

ftplugin/lsp-hover.vim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ if has('patch-8.1.1517') && g:lsp_preview_float && !has('nvim')
66
else
77
setlocal previewwindow buftype=nofile bufhidden=wipe noswapfile nobuflisted
88
endif
9+
10+
if has('conceal') && b:lsp_do_conceal
11+
setlocal conceallevel=2
12+
endif
13+
914
setlocal nocursorline nofoldenable nonumber norelativenumber
1015

1116
if has('syntax')
@@ -15,4 +20,5 @@ endif
1520
let b:undo_ftplugin = 'setlocal pvw< bt< bh< swf< bl< cul< fen<' .
1621
\ (has('syntax') ? ' spell<' : '') .
1722
\ ' number< relativenumber<' .
23+
\ (has('conceal') && b:lsp_do_conceal ? ' conceallevel<' : '') .
1824
\ ' | unlet! g:markdown_fenced_languages'

plugin/lsp.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ let g:lsp_preview_max_width = get(g:, 'lsp_preview_max_width', -1)
3636
let g:lsp_preview_max_height = get(g:, 'lsp_preview_max_height', -1)
3737
let g:lsp_signature_help_enabled = get(g:, 'lsp_signature_help_enabled', 1)
3838
let g:lsp_fold_enabled = get(g:, 'lsp_fold_enabled', 1)
39+
let g:lsp_hover_conceal = get(g:, 'lsp_hover_conceal', 1)
3940

4041
let g:lsp_get_vim_completion_item = get(g:, 'lsp_get_vim_completion_item', [function('lsp#omni#default_get_vim_completion_item')])
4142
let g:lsp_get_supported_capabilities = get(g:, 'lsp_get_supported_capabilities', [function('lsp#default_get_supported_capabilities')])

syntax/lsp-hover.vim

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
" Converts a markdown language (```foo) to the corresponding Vim filetype
2+
function! s:get_vim_filetype(lang_markdown) abort
3+
" If the user hasn't customised markdown fenced languages, just return the
4+
" markdown language
5+
if !exists('g:markdown_fenced_languages')
6+
return a:lang_markdown
7+
endif
8+
9+
" Otherwise, check if it has an entry for the given language
10+
for l:type in g:markdown_fenced_languages
11+
let l:vim_type = substitute(matchstr(l:type, '[^=]*$'), '\..*', '', '')
12+
let l:markdown_type = matchstr(l:type, '[^=]*')
13+
14+
if l:markdown_type ==# a:lang_markdown
15+
" Found entry
16+
return l:vim_type
17+
endif
18+
endfor
19+
20+
" Not found
21+
return a:lang_markdown
22+
endfunction
23+
24+
function! s:do_highlight() abort
25+
if exists('b:lsp_syntax_highlights')
26+
for l:entry in b:lsp_syntax_highlights
27+
let l:line = l:entry['line']
28+
let l:lang = l:entry['language']
29+
let l:ft = s:get_vim_filetype(l:lang)
30+
31+
execute printf('syntax match markdownHighlight%s contains=@markdownHighlight%s /^\%%%sl.*$/', l:ft, l:ft, l:line)
32+
endfor
33+
endif
34+
endfunction
35+
36+
call s:do_highlight()

0 commit comments

Comments
 (0)