ale.vim 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. " MIT License. Copyright (c) 2013-2021 Bjorn Neergaard, w0rp et al.
  2. " Plugin: https://github.com/dense-analysis/ale
  3. " vim: et ts=2 sts=2 sw=2
  4. scriptencoding utf-8
  5. if !get(g:, 'loaded_ale_dont_use_this_in_other_plugins_please', 0)
  6. finish
  7. endif
  8. function! s:airline_ale_count(cnt, symbol)
  9. return a:cnt ? a:symbol. a:cnt : ''
  10. endfunction
  11. function! s:legacy_airline_ale_get_line_number(cnt, type) abort
  12. " Before ALE introduced the FirstProblem API function, this is how
  13. " airline would get the line numbers:
  14. " 1. Get the whole loclist; 2. Filter it for the desired problem type.
  15. " 3. Return the line number of the first element in the filtered list.
  16. if a:cnt == 0
  17. return ''
  18. endif
  19. let buffer = bufnr('')
  20. let problem_type = (a:type ==# 'error') ? 'E' : 'W'
  21. let problems = copy(ale#engine#GetLoclist(buffer))
  22. call filter(problems, 'v:val.bufnr is buffer && v:val.type is# problem_type')
  23. if empty(problems)
  24. return ''
  25. endif
  26. let open_lnum_symbol = get(g:, 'airline#extensions#ale#open_lnum_symbol', '(L')
  27. let close_lnum_symbol = get(g:, 'airline#extensions#ale#close_lnum_symbol', ')')
  28. return open_lnum_symbol . problems[0].lnum . close_lnum_symbol
  29. endfunction
  30. function! s:new_airline_ale_get_line_number(cnt, type) abort
  31. " The FirstProblem call in ALE is a far more efficient way
  32. " of obtaining line number data. If the installed ALE supports
  33. " it, we should use this method of getting line data.
  34. if a:cnt == 0
  35. return ''
  36. endif
  37. let l:buffer = bufnr('')
  38. " Try to get the first error from ALE.
  39. let l:result = ale#statusline#FirstProblem(l:buffer, a:type)
  40. if empty(l:result)
  41. " If there are no errors then try and check for style errors.
  42. let l:result = ale#statusline#FirstProblem(l:buffer, 'style_' . a:type)
  43. endif
  44. if empty(l:result)
  45. return ''
  46. endif
  47. let l:open_lnum_symbol =
  48. \ get(g:, 'airline#extensions#ale#open_lnum_symbol', '(L')
  49. let l:close_lnum_symbol =
  50. \ get(g:, 'airline#extensions#ale#close_lnum_symbol', ')')
  51. return open_lnum_symbol . l:result.lnum . close_lnum_symbol
  52. endfunction
  53. function! s:airline_ale_get_line_number(cnt, type) abort
  54. " Use the new ALE statusline API function if it is available.
  55. if exists("*ale#statusline#FirstProblem")
  56. return s:new_airline_ale_get_line_number(a:cnt, a:type)
  57. endif
  58. return s:legacy_airline_ale_get_line_number(a:cnt, a:type)
  59. endfunction
  60. function! airline#extensions#ale#get(type)
  61. if !exists(':ALELint')
  62. return ''
  63. endif
  64. let error_symbol = get(g:, 'airline#extensions#ale#error_symbol', 'E:')
  65. let warning_symbol = get(g:, 'airline#extensions#ale#warning_symbol', 'W:')
  66. let checking_symbol = get(g:, 'airline#extensions#ale#checking_symbol', '...')
  67. let show_line_numbers = get(g:, 'airline#extensions#ale#show_line_numbers', 1)
  68. let is_err = a:type ==# 'error'
  69. if ale#engine#IsCheckingBuffer(bufnr('')) == 1
  70. return is_err ? '' : checking_symbol
  71. endif
  72. let symbol = is_err ? error_symbol : warning_symbol
  73. let counts = ale#statusline#Count(bufnr(''))
  74. if type(counts) == type({}) && has_key(counts, 'error')
  75. " Use the current Dictionary format.
  76. let errors = counts.error + counts.style_error
  77. let num = is_err ? errors : counts.total - errors
  78. else
  79. " Use the old List format.
  80. let num = is_err ? counts[0] : counts[1]
  81. endif
  82. if show_line_numbers == 1
  83. return s:airline_ale_count(num, symbol) . <sid>airline_ale_get_line_number(num, a:type)
  84. else
  85. return s:airline_ale_count(num, symbol)
  86. endif
  87. endfunction
  88. function! airline#extensions#ale#get_warning()
  89. return airline#extensions#ale#get('warning')
  90. endfunction
  91. function! airline#extensions#ale#get_error()
  92. return airline#extensions#ale#get('error')
  93. endfunction
  94. function! airline#extensions#ale#init(ext)
  95. call airline#parts#define_function('ale_error_count', 'airline#extensions#ale#get_error')
  96. call airline#parts#define_function('ale_warning_count', 'airline#extensions#ale#get_warning')
  97. augroup airline_ale
  98. autocmd!
  99. autocmd CursorHold,BufWritePost * call <sid>ale_refresh()
  100. autocmd User ALEJobStarted,ALELintPost call <sid>ale_refresh()
  101. augroup END
  102. endfunction
  103. function! s:ale_refresh()
  104. if !exists('#airline')
  105. " airline disabled
  106. return
  107. endif
  108. if get(g:, 'airline_skip_empty_sections', 0)
  109. exe ':AirlineRefresh!'
  110. endif
  111. endfunction