|
| 1 | +" Returns a diagnostic object, or empty dictionary if no diagnostics are |
| 2 | +" available. |
| 3 | +" options = { |
| 4 | +" 'server': '', " optional |
| 5 | +" } |
| 6 | +function! lsp#internal#diagnostics#under_cursor#get_diagnostic() abort |
| 7 | + let l:options = get(a:000, 0, {}) |
| 8 | + let l:server = get(l:options, 'server', '') |
| 9 | + let l:bufnr = bufnr('%') |
| 10 | + |
| 11 | + if !lsp#internal#diagnostics#state#_is_enabled_for_buffer(l:bufnr) |
| 12 | + return {} |
| 13 | + endif |
| 14 | + |
| 15 | + let l:uri = lsp#utils#get_buffer_uri(l:bufnr) |
| 16 | + |
| 17 | + let l:diagnostics_by_server = lsp#internal#diagnostics#state#_get_all_diagnostics_grouped_by_server_for_uri(l:uri) |
| 18 | + if empty(l:server) |
| 19 | + let l:diagnostics = [] |
| 20 | + for l:item in values(l:diagnostics_by_server) |
| 21 | + let l:diagnostics += l:item['params']['diagnostics'] |
| 22 | + endfor |
| 23 | + else |
| 24 | + if has_key(l:diagnostics_by_server, l:server) |
| 25 | + let l:diagnostics = l:diagnostics_by_server[l:server]['params']['diagnostics'] |
| 26 | + else |
| 27 | + let l:diagnostics = [] |
| 28 | + endif |
| 29 | + endif |
| 30 | + |
| 31 | + let l:line = line('.') |
| 32 | + let l:col = col('.') |
| 33 | + |
| 34 | + let l:closest_diagnostic = {} |
| 35 | + let l:closest_distance = -1 |
| 36 | + |
| 37 | + for l:diagnostic in l:diagnostics |
| 38 | + let [l:start_line, l:start_col] = lsp#utils#position#lsp_to_vim('%', l:diagnostic['range']['start']) |
| 39 | + |
| 40 | + if l:line == l:start_line |
| 41 | + let l:distance = abs(l:start_col - l:col) |
| 42 | + if l:closest_distance < 0 || l:distance < l:closest_distance |
| 43 | + let l:closest_diagnostic = l:diagnostic |
| 44 | + let l:closest_distance = l:distance |
| 45 | + endif |
| 46 | + endif |
| 47 | + endfor |
| 48 | + |
| 49 | + return l:closest_diagnostic |
| 50 | +endfunction |
0 commit comments