lsp.vim 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. " MIT License. Copyright (c) 2013-2021 François-Xavier Carton et al.
  2. " Plugin: https://github.com/prabirshrestha/vim-lsp
  3. " vim: et ts=2 sts=2 sw=2
  4. scriptencoding utf-8
  5. if !get(g:, 'lsp_loaded', 0)
  6. finish
  7. endif
  8. function! s:airline_lsp_count(cnt, symbol) abort
  9. return a:cnt ? a:symbol. a:cnt : ''
  10. endfunction
  11. function! s:airline_lsp_get_line_number(cnt, type) abort
  12. let result = ''
  13. if a:type ==# 'error'
  14. let result = lsp#get_buffer_first_error_line()
  15. endif
  16. if empty(result)
  17. return ''
  18. endif
  19. let open_lnum_symbol =
  20. \ get(g:, 'airline#extensions#lsp#open_lnum_symbol', '(L')
  21. let close_lnum_symbol =
  22. \ get(g:, 'airline#extensions#lsp#close_lnum_symbol', ')')
  23. return open_lnum_symbol . result . close_lnum_symbol
  24. endfunction
  25. function! airline#extensions#lsp#get(type) abort
  26. if !exists(':LspDeclaration')
  27. return ''
  28. endif
  29. let error_symbol = get(g:, 'airline#extensions#lsp#error_symbol', 'E:')
  30. let warning_symbol = get(g:, 'airline#extensions#lsp#warning_symbol', 'W:')
  31. let show_line_numbers = get(g:, 'airline#extensions#lsp#show_line_numbers', 1)
  32. let is_err = a:type ==# 'error'
  33. let symbol = is_err ? error_symbol : warning_symbol
  34. let num = lsp#get_buffer_diagnostics_counts()[a:type]
  35. if show_line_numbers == 1
  36. return s:airline_lsp_count(num, symbol) . <sid>airline_lsp_get_line_number(num, a:type)
  37. else
  38. return s:airline_lsp_count(num, symbol)
  39. endif
  40. endfunction
  41. function! airline#extensions#lsp#get_warning() abort
  42. return airline#extensions#lsp#get('warning')
  43. endfunction
  44. function! airline#extensions#lsp#get_error() abort
  45. return airline#extensions#lsp#get('error')
  46. endfunction
  47. let s:lsp_progress = []
  48. function! airline#extensions#lsp#progress() abort
  49. if get(w:, 'airline_active', 0)
  50. if exists('*lsp#get_progress')
  51. let s:lsp_progress = lsp#get_progress()
  52. if len(s:lsp_progress) == 0 | return '' | endif
  53. " show only most new progress
  54. let s:lsp_progress = s:lsp_progress[0]
  55. if s:lsp_progress['message'] !=# ''
  56. let percent = ''
  57. if has_key(s:lsp_progress, 'percentage') && s:lsp_progress['percentage'] >= 0
  58. let percent = ' ' . string(s:lsp_progress['percentage']) . '%'
  59. endif
  60. let s:title = s:lsp_progress['title']
  61. let message = airline#util#shorten(s:lsp_progress['message'] . percent, 91, 9)
  62. return s:lsp_progress['server'] . ': ' . s:title . ' ' . message
  63. endif
  64. endif
  65. endif
  66. return ''
  67. endfunction
  68. let s:timer = 0
  69. let s:ignore_time = 0
  70. function! airline#extensions#lsp#update() abort
  71. if !exists('#airline')
  72. " airline disabled
  73. return
  74. endif
  75. if reltimefloat(reltime()) - s:ignore_time >=
  76. \ get(g:, 'airline#extensions#lsp#progress_skip_time', 0.3)
  77. \ || len(s:lsp_progress) == 0
  78. call airline#update_statusline()
  79. let s:ignore_time = reltimefloat(reltime())
  80. endif
  81. endfunction
  82. function! airline#extensions#lsp#init(ext) abort
  83. call airline#parts#define_function('lsp_error_count', 'airline#extensions#lsp#get_error')
  84. call airline#parts#define_function('lsp_warning_count', 'airline#extensions#lsp#get_warning')
  85. call airline#parts#define_function('lsp_progress', 'airline#extensions#lsp#progress')
  86. augroup airline_lsp_progress
  87. autocmd!
  88. autocmd User lsp_progress_updated call airline#extensions#lsp#update()
  89. augroup END
  90. endfunction