Skip to content

Commit

Permalink
Show diagnostics in a floating window (#700)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tyru authored Feb 9, 2020
1 parent f33d624 commit 52539a5
Show file tree
Hide file tree
Showing 14 changed files with 322 additions and 245 deletions.
2 changes: 1 addition & 1 deletion .vintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmdargs:

policies:
ProhibitUnusedVariable:
enabled: true
enabled: false
ProhibitImplicitScopeVariable:
enabled: true
ProhibitNoAbortFunction:
Expand Down
12 changes: 7 additions & 5 deletions autoload/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ endfunction
" "exited", "starting", "failed", "running", "not running"
function! lsp#get_server_status(...) abort
if a:0 == 0
let l:strs = map(keys(s:servers), {k, v -> v . ": " . s:server_status(v)})
let l:strs = map(keys(s:servers), {k, v -> v . ': ' . s:server_status(v)})
return join(l:strs, "\n")
else
return s:server_status(a:1)
Expand Down Expand Up @@ -189,7 +189,7 @@ function! s:register_events() abort
if exists('##TextChangedP')
autocmd TextChangedP * call s:on_text_document_did_change()
endif
if g:lsp_diagnostics_echo_cursor || g:lsp_highlight_references_enabled
if g:lsp_diagnostics_echo_cursor || g:lsp_diagnostics_float_cursor || g:lsp_highlight_references_enabled
autocmd CursorMoved * call s:on_cursor_moved()
endif
autocmd BufWinEnter,BufWinLeave,InsertEnter * call lsp#ui#vim#references#clean_references()
Expand Down Expand Up @@ -243,6 +243,8 @@ function! s:on_cursor_moved() abort

if g:lsp_diagnostics_echo_cursor
call lsp#ui#vim#diagnostics#echo#cursor_moved()
elseif g:lsp_diagnostics_float_cursor && lsp#ui#vim#output#float_supported()
call lsp#ui#vim#diagnostics#float#cursor_moved()
endif

if g:lsp_highlight_references_enabled
Expand Down Expand Up @@ -517,7 +519,7 @@ function! s:ensure_init(buf, server_name, cb) abort
if has_key(l:server_info, 'capabilities')
let l:capabilities = l:server_info['capabilities']
else
let l:capabilities = call(g:lsp_get_supported_capabilities[0], [server_info])
let l:capabilities = call(g:lsp_get_supported_capabilities[0], [l:server_info])
endif

let l:request = {
Expand Down Expand Up @@ -886,8 +888,8 @@ function! s:add_didchange_queue(buf) abort
call add(s:didchange_queue, a:buf)
call lsp#log('s:send_didchange_queue() will be triggered')
call timer_stop(s:didchange_timer)
let lazy = &updatetime > 1000 ? &updatetime : 1000
let s:didchange_timer = timer_start(lazy, function('s:send_didchange_queue'))
let l:lazy = &updatetime > 1000 ? &updatetime : 1000
let s:didchange_timer = timer_start(l:lazy, function('s:send_didchange_queue'))
endfunction

function! s:send_didchange_queue(...) abort
Expand Down
12 changes: 6 additions & 6 deletions autoload/lsp/client.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let s:save_cpo = &cpo
set cpo&vim
let s:save_cpo = &cpoptions
set cpoptions&vim

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

Expand Down Expand Up @@ -268,10 +268,10 @@ function! s:lsp_get_last_request_id(id) abort
endfunction

function! s:lsp_is_error(obj_or_response) abort
let vt = type(a:obj_or_response)
if vt == type('')
let l:vt = type(a:obj_or_response)
if l:vt == type('')
return len(a:obj_or_response) > 0
elseif vt == type({})
elseif l:vt == type({})
return has_key(a:obj_or_response, 'error')
endif
return 0
Expand Down Expand Up @@ -330,6 +330,6 @@ endfunction

" }}}

let &cpo = s:save_cpo
let &cpoptions = s:save_cpo
unlet s:save_cpo
" vim sw=4 ts=4 et
40 changes: 20 additions & 20 deletions autoload/lsp/ui/vim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ function! lsp#ui#vim#document_format() abort
endfunction

function! lsp#ui#vim#stop_server(...) abort
let l:name = get(a:000, 0, '')
for l:server in lsp#get_whitelisted_servers()
if !empty(l:name) && l:server != l:name
continue
endif
echo 'Stopping' l:server 'server ...'
call lsp#stop_server(server)
endfor
let l:name = get(a:000, 0, '')
for l:server in lsp#get_whitelisted_servers()
if !empty(l:name) && l:server != l:name
continue
endif
echo 'Stopping' l:server 'server ...'
call lsp#stop_server(l:server)
endfor
endfunction

function! s:get_selection_pos(type) abort
Expand Down Expand Up @@ -439,15 +439,15 @@ function! s:handle_location(ctx, server, type, data) abort "ctx = {counter, list
if has_key(l:loc,'viewstart') " showing a locationLink
let l:view = l:lines[l:loc['viewstart'] : l:loc['viewend']]
call lsp#ui#vim#output#preview(a:server, l:view, {
\ 'statusline': ' LSP Peek ' . a:type,
\ 'filetype': &filetype
\ })
\ 'statusline': ' LSP Peek ' . a:type,
\ 'filetype': &filetype
\ })
else " showing a location
call lsp#ui#vim#output#preview(a:server, l:lines, {
\ 'statusline': ' LSP Peek ' . a:type,
\ 'cursor': { 'line': l:loc['lnum'], 'col': l:loc['col'], 'align': g:lsp_peek_alignment },
\ 'filetype': &filetype
\ })
\ 'statusline': ' LSP Peek ' . a:type,
\ 'cursor': { 'line': l:loc['lnum'], 'col': l:loc['col'], 'align': g:lsp_peek_alignment },
\ 'filetype': &filetype
\ })
endif
endif
endif
Expand Down Expand Up @@ -582,9 +582,9 @@ function! s:get_treeitem_for_tree_hierarchy(Callback, object) dict abort
endfunction

function! lsp#ui#vim#code_action() abort
call lsp#ui#vim#code_action#do({
\ 'sync': v:false,
\ 'selection': v:false,
\ 'query': '',
\ })
call lsp#ui#vim#code_action#do({
\ 'sync': v:false,
\ 'selection': v:false,
\ 'query': '',
\ })
endfunction
2 changes: 1 addition & 1 deletion autoload/lsp/ui/vim/diagnostics.vim
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function! lsp#ui#vim#diagnostics#document_diagnostics() abort
endif

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

Expand Down
2 changes: 1 addition & 1 deletion autoload/lsp/ui/vim/diagnostics/echo.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function! lsp#ui#vim#diagnostics#echo#cursor_moved() abort
if mode() isnot# 'n'
" dont' show echo only in normal mode
" show echo only in normal mode
return
endif

Expand Down
32 changes: 32 additions & 0 deletions autoload/lsp/ui/vim/diagnostics/float.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function! lsp#ui#vim#diagnostics#float#cursor_moved() abort
call s:stop_cursor_moved_timer()

let l:current_pos = getcurpos()[0:2]

" use timer to avoid recalculation
if !exists('s:last_pos') || l:current_pos != s:last_pos
let s:last_pos = l:current_pos
let s:cursor_moved_timer = timer_start(g:lsp_diagnostics_float_delay, function('s:float_diagnostics_under_cursor'))
endif
endfunction

function! s:float_diagnostics_under_cursor(...) abort
let l:diagnostic = lsp#ui#vim#diagnostics#get_diagnostics_under_cursor()
if !empty(l:diagnostic) && has_key(l:diagnostic, 'message')
let l:lines = split(l:diagnostic['message'], '\n', 1)
call lsp#ui#vim#output#preview('', l:lines, {
\ 'statusline': ' LSP Diagnostics'
\})
let s:displaying_message = 1
elseif get(s:, 'displaying_message', 0)
call lsp#ui#vim#output#closepreview()
let s:displaying_message = 0
endif
endfunction

function! s:stop_cursor_moved_timer() abort
if exists('s:cursor_moved_timer')
call timer_stop(s:cursor_moved_timer)
unlet s:cursor_moved_timer
endif
endfunction
Loading

0 comments on commit 52539a5

Please sign in to comment.