hunks.vim 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. " MIT License. Copyright (c) 2013-2015 Bailey Ling.
  2. " vim: et ts=2 sts=2 sw=2
  3. if !get(g:, 'loaded_signify', 0) && !get(g:, 'loaded_gitgutter', 0) && !get(g:, 'loaded_changes', 0) && !get(g:, 'loaded_quickfixsigns', 0)
  4. finish
  5. endif
  6. let s:non_zero_only = get(g:, 'airline#extensions#hunks#non_zero_only', 0)
  7. let s:hunk_symbols = get(g:, 'airline#extensions#hunks#hunk_symbols', ['+', '~', '-'])
  8. function! s:get_hunks_signify()
  9. let hunks = sy#repo#get_stats()
  10. if hunks[0] >= 0
  11. return hunks
  12. endif
  13. return []
  14. endfunction
  15. function! s:is_branch_empty()
  16. return exists('*airline#extensions#branch#head') && empty(airline#extensions#branch#head())
  17. endfunction
  18. function! s:get_hunks_gitgutter()
  19. if !get(g:, 'gitgutter_enabled', 0) || s:is_branch_empty()
  20. return ''
  21. endif
  22. return GitGutterGetHunkSummary()
  23. endfunction
  24. function! s:get_hunks_changes()
  25. if !get(b:, 'changes_view_enabled', 0) || s:is_branch_empty()
  26. return []
  27. endif
  28. let hunks = changes#GetStats()
  29. for i in hunks
  30. if i > 0
  31. return hunks
  32. endif
  33. endfor
  34. return []
  35. endfunction
  36. function! s:get_hunks_empty()
  37. return ''
  38. endfunction
  39. let s:source_func = ''
  40. function! s:get_hunks()
  41. if empty(s:source_func)
  42. if get(g:, 'loaded_signify', 0)
  43. let s:source_func = 's:get_hunks_signify'
  44. elseif exists('*GitGutterGetHunkSummary')
  45. let s:source_func = 's:get_hunks_gitgutter'
  46. elseif exists('*changes#GetStats')
  47. let s:source_func = 's:get_hunks_changes'
  48. elseif exists('*quickfixsigns#vcsdiff#GetHunkSummary')
  49. let s:source_func = 'quickfixsigns#vcsdiff#GetHunkSummary'
  50. else
  51. let s:source_func = 's:get_hunks_empty'
  52. endif
  53. endif
  54. return {s:source_func}()
  55. endfunction
  56. function! airline#extensions#hunks#get_hunks()
  57. if !get(w:, 'airline_active', 0)
  58. return ''
  59. endif
  60. let hunks = s:get_hunks()
  61. let string = ''
  62. if !empty(hunks)
  63. for i in [0, 1, 2]
  64. if s:non_zero_only == 0 || hunks[i] > 0
  65. let string .= printf('%s%s ', s:hunk_symbols[i], hunks[i])
  66. endif
  67. endfor
  68. endif
  69. return string
  70. endfunction
  71. function! airline#extensions#hunks#init(ext)
  72. call airline#parts#define_function('hunks', 'airline#extensions#hunks#get_hunks')
  73. endfunction