|
| 1 | +let s:supports_hl = exists('*prop_add') |
| 2 | +let s:enabled = 0 |
| 3 | +let s:prop_type_prefix = 'vim_lsp_hl_' |
| 4 | + |
| 5 | +let s:severity_sign_names_mapping = { |
| 6 | + \ 1: 'LspError', |
| 7 | + \ 2: 'LspWarning', |
| 8 | + \ 3: 'LspInformation', |
| 9 | + \ 4: 'LspHint', |
| 10 | + \ } |
| 11 | + |
| 12 | +if !hlexists('LspErrorHighlight') |
| 13 | + highlight link LspErrorHighlight Error |
| 14 | +endif |
| 15 | + |
| 16 | +if !hlexists('LspWarningHighlight') |
| 17 | + highlight link LspWarningHighlight Todo |
| 18 | +endif |
| 19 | + |
| 20 | +if !hlexists('LspInformationHighlight') |
| 21 | + highlight link LspInformationHighlight Normal |
| 22 | +endif |
| 23 | + |
| 24 | +if !hlexists('LspHintHighlight') |
| 25 | + highlight link LspHintHighlight Normal |
| 26 | +endif |
| 27 | + |
| 28 | +function! lsp#ui#vim#diagnostics#textprop#enable() abort |
| 29 | + if !s:supports_hl |
| 30 | + call lsp#log('vim-lsp highlighting requires vim with +textprop') |
| 31 | + return |
| 32 | + endif |
| 33 | + if !s:enabled |
| 34 | + let s:enabled = 1 |
| 35 | + call lsp#log('vim-lsp highlighting enabled (textprop)') |
| 36 | + endif |
| 37 | +endfunction |
| 38 | + |
| 39 | +function! lsp#ui#vim#diagnostics#textprop#disable() abort |
| 40 | + if s:enabled |
| 41 | + call s:clear_all_highlights() |
| 42 | + let s:enabled = 0 |
| 43 | + call lsp#log('vim-lsp highlighting disabled') |
| 44 | + endif |
| 45 | +endfunction |
| 46 | + |
| 47 | +function! lsp#ui#vim#diagnostics#textprop#set(server_name, data) abort |
| 48 | + if !s:enabled | return | endif |
| 49 | + |
| 50 | + if lsp#client#is_error(a:data['response']) |
| 51 | + return |
| 52 | + endif |
| 53 | + |
| 54 | + let l:uri = a:data['response']['params']['uri'] |
| 55 | + let l:diagnostics = a:data['response']['params']['diagnostics'] |
| 56 | + let l:path = lsp#utils#uri_to_path(l:uri) |
| 57 | + |
| 58 | + call s:clear_highlights(a:server_name, l:path) |
| 59 | + call s:place_highlights(a:server_name, l:path, l:diagnostics) |
| 60 | +endfunction |
| 61 | + |
| 62 | +function! s:get_prop_type(server_name, severity) abort |
| 63 | + let l:severity = has_key(s:severity_sign_names_mapping, a:severity) ? a:severity : 0 |
| 64 | + let l:name = s:prop_type_prefix . l:severity . '_' . a:server_name |
| 65 | + if empty(prop_type_get(l:name)) |
| 66 | + call prop_type_add(l:name, { |
| 67 | + \ 'highlight': s:severity_sign_names_mapping[l:severity] . 'Highlight', |
| 68 | + \ 'combine': v:true, |
| 69 | + \ }) |
| 70 | + endif |
| 71 | + return l:name |
| 72 | +endfunction |
| 73 | + |
| 74 | +function! s:clear_all_highlights() abort |
| 75 | + for l:prop_type in prop_type_list() |
| 76 | + if l:prop_type !~# '^' . s:prop_type_prefix |
| 77 | + continue |
| 78 | + endif |
| 79 | + |
| 80 | + for l:bufnr in range(1, bufnr('$')) |
| 81 | + if bufexists(l:bufnr) |
| 82 | + call prop_remove({ |
| 83 | + \ 'type': l:prop_type, |
| 84 | + \ 'bufnr': l:bufnr, |
| 85 | + \ 'all': v:true, |
| 86 | + \ }) |
| 87 | + endif |
| 88 | + endfor |
| 89 | + |
| 90 | + call prop_type_delete(l:prop_type) |
| 91 | + endfor |
| 92 | +endfunction |
| 93 | + |
| 94 | +function! s:clear_highlights(server_name, path) abort |
| 95 | + if !s:enabled | return | endif |
| 96 | + |
| 97 | + let l:bufnr = bufnr(a:path) |
| 98 | + for l:severity in keys(s:severity_sign_names_mapping) |
| 99 | + let l:prop_type = s:get_prop_type(a:server_name, l:severity) |
| 100 | + call prop_remove({ |
| 101 | + \ 'type': l:prop_type, |
| 102 | + \ 'bufnr': l:bufnr, |
| 103 | + \ 'all': v:true, |
| 104 | + \ }) |
| 105 | + endfor |
| 106 | +endfunction |
| 107 | + |
| 108 | +function! s:place_highlights(server_name, path, diagnostics) abort |
| 109 | + if !s:enabled | return | endif |
| 110 | + |
| 111 | + let l:bufnr = bufnr(a:path) |
| 112 | + if !empty(a:diagnostics) && l:bufnr >= 0 |
| 113 | + for l:item in a:diagnostics |
| 114 | + let l:start_line = l:item['range']['start']['line'] + 1 |
| 115 | + let l:start_col = l:item['range']['start']['character'] + 1 |
| 116 | + let l:end_line = l:item['range']['end']['line'] + 1 |
| 117 | + let l:end_col = l:item['range']['end']['character'] + 1 |
| 118 | + |
| 119 | + let l:prop_type = s:get_prop_type(a:server_name, l:item['severity']) |
| 120 | + call prop_add(l:start_line, l:start_col, { |
| 121 | + \ 'end_lnum': l:end_line, |
| 122 | + \ 'end_col': l:end_col, |
| 123 | + \ 'bufnr': l:bufnr, |
| 124 | + \ 'type': l:prop_type, |
| 125 | + \ }) |
| 126 | + endfor |
| 127 | + endif |
| 128 | +endfunction |
0 commit comments