LanguageClient-neovim: How to reliably get number of errors from after diagnositcs?

I want to display the number of errors reported by LanguageClient-neovim using vim-arline. What I have right now is:

function! AirlineInit()
  let g:airline_section_x = airline#section#create(['LC_status'])
  let g:airline_section_error = airline#section#create(['LC_error_count'])
endfunction

call airline#parts#define_function('LC_error_count', 'LC_error_count')
call airline#parts#define_function('LC_status', 'LC_status')

function! LC_error_count()
  let count = len(getqflist())
  return count > 0 && g:LanguageClient_loaded ? 'E: ' . count : ''
endfunction

function! LC_status()
  return g:LanguageClient_loaded ? LanguageClient#statusLine() : ''
endfunction

autocmd User AirlineAfterInit call AirlineInit()

This is great for the LanguageClient#statusLine(), however I am currently relying on len(getqflist()) to get the number of errors from LS.

Is there a better way to get this number of diagnostic errors in order to use them in the status line?

Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

To @blahgeek and anyone else interested in this, I have this working by parsing over the QuickFix list for warnings and errors. This code will display separate counts for warnings and errors on Airline using the built in airline_section_warning and airline_section_error sections. Thanks to @fedeDev for the initial code and inspiration!

function! AirlineInit()
  let g:airline_section_warning = airline#section#create(['LC_warning_count'])
  let g:airline_section_error = airline#section#create(['LC_error_count'])
endfunction

call airline#parts#define_function('LC_warning_count', 'LC_warning_count')
call airline#parts#define_function('LC_error_count', 'LC_error_count')

function! LC_warning_count()
  let current_buf_number = bufnr('%')
  let qflist = getqflist()
  let current_buf_diagnostics = filter(qflist, {index, dict -> dict['bufnr'] == current_buf_number && dict['type'] == 'W'})
  let count = len(current_buf_diagnostics)
  return count > 0 && g:LanguageClient_loaded ? 'W: ' . count : ''
endfunction

function! LC_error_count()
  let current_buf_number = bufnr('%')
  let qflist = getqflist()
  let current_buf_diagnostics = filter(qflist, {index, dict -> dict['bufnr'] == current_buf_number && dict['type'] == 'E'})
  let count = len(current_buf_diagnostics)
  return count > 0 && g:LanguageClient_loaded ? 'E: ' . count : ''
endfunction

autocmd User AirlineAfterInit call AirlineInit()

Counting the elements of the quickfix is only useful if I’m only using the quickfix for LanguageClient-neovim. Any other plugin populating the quickfix would make my hack useless. Furthremore, I can no longer use the option of changing diagnostics report to display on the location list in the future.

Airline/lightline are very popular plugins, and I’m sure people will want to have this functionality in the future (YCM, Syntasic, ale, etc. already have this as part of airline). Simply exposing the number of diagnostic errors through a global variable (as done with LanguageClient#statusLine(), for example) would be a great start.

I am sorry not to be able to easily help with a PR, but I have never used Rust before 😦