lightlineにvim-lspのdiagnosticの情報を載せる
August 08, 2020
vim-lspのPRを探っているとlightlineにdiagnosticsの情報を投げたいというPRがあったので、それを参考に設定してみました。
function! LightlineLSPWarnings() abort
let l:counts = lsp#ui#vim#diagnostics#get_buffer_diagnostics_counts()
return l:counts.warning == 0 ? '' : printf('W:%d', l:counts.warning)
endfunction
function! LightlineLSPErrors() abort
let l:counts = lsp#ui#vim#diagnostics#get_buffer_diagnostics_counts()
return l:counts.error == 0 ? '' : printf('E:%d', l:counts.error)
endfunction
function! LightlineLSPOk() abort
let l:counts = lsp#ui#vim#diagnostics#get_buffer_diagnostics_counts()
let l:total = l:counts.error + l:counts.warning
return l:total == 0 ? 'OK' : ''
endfunction
augroup LightLineOnLSP
autocmd!
autocmd User lsp_diagnostics_updated call lightline#update()
augroup END
let g:lightline = {
\ 'colorscheme': 'wombat',
\ 'active': {
\ 'left': [
\ [ 'mode', 'paste' ],
\ ],
\ 'right': [
\ [ 'lsp_errors', 'lsp_warnings', 'lsp_ok' ],
\ [ 'percent' ],
\ [ 'fileformat', 'fileencoding', 'filetype' ],
\ ],
\ },
\ 'component_expand': {
\ 'lsp_warnings': 'LightlineLSPWarnings',
\ 'lsp_errors': 'LightlineLSPErrors',
\ 'lsp_ok': 'LightlineLSPOk',
\ },
\ 'component_type': {
\ 'lsp_warnings': 'warning',
\ 'lsp_errors': 'error',
\ 'lsp_ok': 'middle',
\ },
\ }少し長くなったので説明すると、lsp#ui#vim#diagnostics#get_buffer_diagnostics_countsを使うことによってDiagnosticsの数を取得することが出来ます。
let l:count = lsp#ui#vim#diagnostics#get_buffer_diagnostics_counts()
l:count.error " errorの数
l:count.warning " warningの数これを使い、errorの数を数えるメソッド・warningの数を数えるメソッド・errorもwarningもないことを数えるメソッドの3つを作成します。
function! LightlineLSPWarnings() abort
let l:counts = lsp#ui#vim#diagnostics#get_buffer_diagnostics_counts()
return l:counts.warning == 0 ? '' : printf('W:%d', l:counts.warning)
endfunction
function! LightlineLSPErrors() abort
let l:counts = lsp#ui#vim#diagnostics#get_buffer_diagnostics_counts()
return l:counts.error == 0 ? '' : printf('E:%d', l:counts.error)
endfunction
function! LightlineLSPOk() abort
let l:counts = lsp#ui#vim#diagnostics#get_buffer_diagnostics_counts()
let l:total = l:counts.error + l:counts.warning
return l:total == 0 ? 'OK' : ''
endfunctionこれら3つのメソッドをlightlineに設定することによってエラーが何個あるかなどを設定することが出来ます。
最後にLSPのdiagnosticsの変更が起きるたびに、lightline#update()を呼び出すようにします。
augroup LightLineOnLSP
autocmd!
autocmd User lsp_diagnostics_updated call lightline#update()
augroup ENDということで設定するとこんな感じになります。
