Skip to content

Commit 2eb8d16

Browse files
refactor diagnostics under cursor to use the new internal diagnostics state (#997)
1 parent efd07d8 commit 2eb8d16

File tree

5 files changed

+54
-37
lines changed

5 files changed

+54
-37
lines changed

autoload/lsp/internal/diagnostics/echo.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ function! lsp#internal#diagnostics#echo#_enable() abort
99
let s:enabled = 1
1010

1111
let s:Dispose = lsp#callbag#pipe(
12-
\ lsp#callbag#fromEvent('CursorMoved'),
12+
\ lsp#callbag#fromEvent(['CursorMoved']),
1313
\ lsp#callbag#filter({_->g:lsp_diagnostics_echo_cursor}),
1414
\ lsp#callbag#debounceTime(g:lsp_diagnostics_echo_delay),
1515
\ lsp#callbag#map({_->{'bufnr': bufnr('%'), 'curpos': getcurpos()[0:2], 'changedtick': b:changedtick }}),
1616
\ lsp#callbag#distinctUntilChanged({a,b -> a['bufnr'] == b['bufnr'] && a['curpos'] == b['curpos'] && a['changedtick'] == b['changedtick']}),
1717
\ lsp#callbag#filter({_->mode() is# 'n'}),
1818
\ lsp#callbag#filter({_->getbufvar(bufnr('%'), '&buftype') !=# 'terminal' }),
19-
\ lsp#callbag#map({_->lsp#ui#vim#diagnostics#get_diagnostics_under_cursor()}),
19+
\ lsp#callbag#map({_->lsp#internal#diagnostics#under_cursor#get_diagnostic()}),
2020
\ lsp#callbag#subscribe({x->s:echo(x)}),
2121
\ )
2222
endfunction

autoload/lsp/internal/diagnostics/float.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function! lsp#internal#diagnostics#float#_enable() abort
1717
\ lsp#callbag#distinctUntilChanged({a,b -> a['bufnr'] == b['bufnr'] && a['curpos'] == b['curpos'] && a['changedtick'] == b['changedtick']}),
1818
\ lsp#callbag#filter({_->mode() is# 'n'}),
1919
\ lsp#callbag#filter({_->getbufvar(bufnr('%'), '&buftype') !=# 'terminal' }),
20-
\ lsp#callbag#map({_->lsp#ui#vim#diagnostics#get_diagnostics_under_cursor()}),
20+
\ lsp#callbag#map({_->lsp#internal#diagnostics#under_cursor#get_diagnostic()}),
2121
\ lsp#callbag#subscribe({x->s:show_float(x)}),
2222
\ )
2323
endfunction
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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

autoload/lsp/ui/vim/code_action.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function! lsp#ui#vim#code_action#do(option) abort
3939
let l:bufnr = bufnr('%')
4040
let l:command_id = lsp#_new_command()
4141
for l:server_name in l:server_names
42-
let l:diagnostic = lsp#ui#vim#diagnostics#get_diagnostics_under_cursor(l:server_name)
42+
let l:diagnostic = lsp#internal#diagnostics#under_cursor#get_diagnostic(l:server_name)
4343
call lsp#send_request(l:server_name, {
4444
\ 'method': 'textDocument/codeAction',
4545
\ 'params': {

autoload/lsp/ui/vim/diagnostics.vim

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,6 @@ function! lsp#ui#vim#diagnostics#get_document_diagnostics(bufnr) abort
1919
return get(s:diagnostics, lsp#utils#get_buffer_uri(a:bufnr), {})
2020
endfunction
2121

22-
" Returns a diagnostic object, or empty dictionary if no diagnostics are available.
23-
"
24-
" Note: Consider renaming this method (s/diagnostics/diagnostic) to make
25-
" it clear that it returns just one diagnostic, not a list.
26-
function! lsp#ui#vim#diagnostics#get_diagnostics_under_cursor(...) abort
27-
let l:target_server_name = get(a:000, 0, '')
28-
29-
let l:diagnostics = s:get_all_buffer_diagnostics(l:target_server_name)
30-
if !len(l:diagnostics)
31-
return
32-
endif
33-
34-
let l:line = line('.')
35-
let l:col = col('.')
36-
37-
let l:closest_diagnostic = {}
38-
let l:closest_distance = -1
39-
40-
for l:diagnostic in l:diagnostics
41-
let [l:start_line, l:start_col] = lsp#utils#position#lsp_to_vim('%', l:diagnostic['range']['start'])
42-
43-
if l:line == l:start_line
44-
let l:distance = abs(l:start_col - l:col)
45-
if l:closest_distance < 0 || l:distance < l:closest_distance
46-
let l:closest_diagnostic = l:diagnostic
47-
let l:closest_distance = l:distance
48-
endif
49-
endif
50-
endfor
51-
52-
return l:closest_diagnostic
53-
endfunction
54-
5522
function! s:severity_of(diagnostic) abort
5623
return get(a:diagnostic, 'severity', 1)
5724
endfunction

0 commit comments

Comments
 (0)