Skip to content

Commit 52539a5

Browse files
authored
Show diagnostics in a floating window (#700)
* fix comment * fix indent * support diagnostics in a floating window * doc: add g:lsp_diagnostics_float_{cursor,delay} * fix vint errors except unused variables * suppress vint unused variable errors * lint: use robust operator
1 parent f33d624 commit 52539a5

14 files changed

+322
-245
lines changed

.vintrc.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cmdargs:
33

44
policies:
55
ProhibitUnusedVariable:
6-
enabled: true
6+
enabled: false
77
ProhibitImplicitScopeVariable:
88
enabled: true
99
ProhibitNoAbortFunction:

autoload/lsp.vim

+7-5
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ endfunction
115115
" "exited", "starting", "failed", "running", "not running"
116116
function! lsp#get_server_status(...) abort
117117
if a:0 == 0
118-
let l:strs = map(keys(s:servers), {k, v -> v . ": " . s:server_status(v)})
118+
let l:strs = map(keys(s:servers), {k, v -> v . ': ' . s:server_status(v)})
119119
return join(l:strs, "\n")
120120
else
121121
return s:server_status(a:1)
@@ -189,7 +189,7 @@ function! s:register_events() abort
189189
if exists('##TextChangedP')
190190
autocmd TextChangedP * call s:on_text_document_did_change()
191191
endif
192-
if g:lsp_diagnostics_echo_cursor || g:lsp_highlight_references_enabled
192+
if g:lsp_diagnostics_echo_cursor || g:lsp_diagnostics_float_cursor || g:lsp_highlight_references_enabled
193193
autocmd CursorMoved * call s:on_cursor_moved()
194194
endif
195195
autocmd BufWinEnter,BufWinLeave,InsertEnter * call lsp#ui#vim#references#clean_references()
@@ -243,6 +243,8 @@ function! s:on_cursor_moved() abort
243243

244244
if g:lsp_diagnostics_echo_cursor
245245
call lsp#ui#vim#diagnostics#echo#cursor_moved()
246+
elseif g:lsp_diagnostics_float_cursor && lsp#ui#vim#output#float_supported()
247+
call lsp#ui#vim#diagnostics#float#cursor_moved()
246248
endif
247249

248250
if g:lsp_highlight_references_enabled
@@ -517,7 +519,7 @@ function! s:ensure_init(buf, server_name, cb) abort
517519
if has_key(l:server_info, 'capabilities')
518520
let l:capabilities = l:server_info['capabilities']
519521
else
520-
let l:capabilities = call(g:lsp_get_supported_capabilities[0], [server_info])
522+
let l:capabilities = call(g:lsp_get_supported_capabilities[0], [l:server_info])
521523
endif
522524

523525
let l:request = {
@@ -886,8 +888,8 @@ function! s:add_didchange_queue(buf) abort
886888
call add(s:didchange_queue, a:buf)
887889
call lsp#log('s:send_didchange_queue() will be triggered')
888890
call timer_stop(s:didchange_timer)
889-
let lazy = &updatetime > 1000 ? &updatetime : 1000
890-
let s:didchange_timer = timer_start(lazy, function('s:send_didchange_queue'))
891+
let l:lazy = &updatetime > 1000 ? &updatetime : 1000
892+
let s:didchange_timer = timer_start(l:lazy, function('s:send_didchange_queue'))
891893
endfunction
892894

893895
function! s:send_didchange_queue(...) abort

autoload/lsp/client.vim

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
let s:save_cpo = &cpo
2-
set cpo&vim
1+
let s:save_cpo = &cpoptions
2+
set cpoptions&vim
33

44
let s:clients = {} " { client_id: ctx }
55

@@ -268,10 +268,10 @@ function! s:lsp_get_last_request_id(id) abort
268268
endfunction
269269

270270
function! s:lsp_is_error(obj_or_response) abort
271-
let vt = type(a:obj_or_response)
272-
if vt == type('')
271+
let l:vt = type(a:obj_or_response)
272+
if l:vt == type('')
273273
return len(a:obj_or_response) > 0
274-
elseif vt == type({})
274+
elseif l:vt == type({})
275275
return has_key(a:obj_or_response, 'error')
276276
endif
277277
return 0
@@ -330,6 +330,6 @@ endfunction
330330

331331
" }}}
332332

333-
let &cpo = s:save_cpo
333+
let &cpoptions = s:save_cpo
334334
unlet s:save_cpo
335335
" vim sw=4 ts=4 et

autoload/lsp/ui/vim.vim

+20-20
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,14 @@ function! lsp#ui#vim#document_format() abort
249249
endfunction
250250

251251
function! lsp#ui#vim#stop_server(...) abort
252-
let l:name = get(a:000, 0, '')
253-
for l:server in lsp#get_whitelisted_servers()
254-
if !empty(l:name) && l:server != l:name
255-
continue
256-
endif
257-
echo 'Stopping' l:server 'server ...'
258-
call lsp#stop_server(server)
259-
endfor
252+
let l:name = get(a:000, 0, '')
253+
for l:server in lsp#get_whitelisted_servers()
254+
if !empty(l:name) && l:server != l:name
255+
continue
256+
endif
257+
echo 'Stopping' l:server 'server ...'
258+
call lsp#stop_server(l:server)
259+
endfor
260260
endfunction
261261

262262
function! s:get_selection_pos(type) abort
@@ -439,15 +439,15 @@ function! s:handle_location(ctx, server, type, data) abort "ctx = {counter, list
439439
if has_key(l:loc,'viewstart') " showing a locationLink
440440
let l:view = l:lines[l:loc['viewstart'] : l:loc['viewend']]
441441
call lsp#ui#vim#output#preview(a:server, l:view, {
442-
\ 'statusline': ' LSP Peek ' . a:type,
443-
\ 'filetype': &filetype
444-
\ })
442+
\ 'statusline': ' LSP Peek ' . a:type,
443+
\ 'filetype': &filetype
444+
\ })
445445
else " showing a location
446446
call lsp#ui#vim#output#preview(a:server, l:lines, {
447-
\ 'statusline': ' LSP Peek ' . a:type,
448-
\ 'cursor': { 'line': l:loc['lnum'], 'col': l:loc['col'], 'align': g:lsp_peek_alignment },
449-
\ 'filetype': &filetype
450-
\ })
447+
\ 'statusline': ' LSP Peek ' . a:type,
448+
\ 'cursor': { 'line': l:loc['lnum'], 'col': l:loc['col'], 'align': g:lsp_peek_alignment },
449+
\ 'filetype': &filetype
450+
\ })
451451
endif
452452
endif
453453
endif
@@ -582,9 +582,9 @@ function! s:get_treeitem_for_tree_hierarchy(Callback, object) dict abort
582582
endfunction
583583

584584
function! lsp#ui#vim#code_action() abort
585-
call lsp#ui#vim#code_action#do({
586-
\ 'sync': v:false,
587-
\ 'selection': v:false,
588-
\ 'query': '',
589-
\ })
585+
call lsp#ui#vim#code_action#do({
586+
\ 'sync': v:false,
587+
\ 'selection': v:false,
588+
\ 'query': '',
589+
\ })
590590
endfunction

autoload/lsp/ui/vim/diagnostics.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function! lsp#ui#vim#diagnostics#document_diagnostics() abort
3535
endif
3636

3737
let l:result = []
38-
for [l:server_name, l:data] in items(l:diagnostics)
38+
for l:data in values(l:diagnostics)
3939
let l:result += lsp#ui#vim#utils#diagnostics_to_loc_list(l:data)
4040
endfor
4141

autoload/lsp/ui/vim/diagnostics/echo.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function! lsp#ui#vim#diagnostics#echo#cursor_moved() abort
22
if mode() isnot# 'n'
3-
" dont' show echo only in normal mode
3+
" show echo only in normal mode
44
return
55
endif
66

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function! lsp#ui#vim#diagnostics#float#cursor_moved() abort
2+
call s:stop_cursor_moved_timer()
3+
4+
let l:current_pos = getcurpos()[0:2]
5+
6+
" use timer to avoid recalculation
7+
if !exists('s:last_pos') || l:current_pos != s:last_pos
8+
let s:last_pos = l:current_pos
9+
let s:cursor_moved_timer = timer_start(g:lsp_diagnostics_float_delay, function('s:float_diagnostics_under_cursor'))
10+
endif
11+
endfunction
12+
13+
function! s:float_diagnostics_under_cursor(...) abort
14+
let l:diagnostic = lsp#ui#vim#diagnostics#get_diagnostics_under_cursor()
15+
if !empty(l:diagnostic) && has_key(l:diagnostic, 'message')
16+
let l:lines = split(l:diagnostic['message'], '\n', 1)
17+
call lsp#ui#vim#output#preview('', l:lines, {
18+
\ 'statusline': ' LSP Diagnostics'
19+
\})
20+
let s:displaying_message = 1
21+
elseif get(s:, 'displaying_message', 0)
22+
call lsp#ui#vim#output#closepreview()
23+
let s:displaying_message = 0
24+
endif
25+
endfunction
26+
27+
function! s:stop_cursor_moved_timer() abort
28+
if exists('s:cursor_moved_timer')
29+
call timer_stop(s:cursor_moved_timer)
30+
unlet s:cursor_moved_timer
31+
endif
32+
endfunction

0 commit comments

Comments
 (0)