]> git.madduck.net Git - etc/vim.git/blob - autoload/lsp/internal/diagnostics/under_cursor.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/vim-lsp/' content from commit 04428c92
[etc/vim.git] / autoload / lsp / internal / diagnostics / under_cursor.vim
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     let l:diagnostics = []
19     if empty(l:server)
20         for l:item in values(l:diagnostics_by_server)
21             let l:diagnostics += lsp#utils#iteratable(l:item['params']['diagnostics'])
22         endfor
23     else
24         if has_key(l:diagnostics_by_server, l:server)
25             let l:diagnostics = lsp#utils#iteratable(l:diagnostics_by_server[l:server]['params']['diagnostics'])
26         endif
27     endif
28
29     let l:line = line('.')
30     let l:col = col('.')
31
32     let l:closest_diagnostic = {}
33     let l:closest_distance = -1
34
35     for l:diagnostic in l:diagnostics
36         let [l:start_line, l:start_col] = lsp#utils#position#lsp_to_vim('%', l:diagnostic['range']['start'])
37
38         if l:line == l:start_line
39             let l:distance = abs(l:start_col - l:col)
40             if l:closest_distance < 0 || l:distance < l:closest_distance
41                 let l:closest_diagnostic = l:diagnostic
42                 let l:closest_distance = l:distance
43             endif
44         endif
45     endfor
46
47     return l:closest_diagnostic
48 endfunction