hunks.vim 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. " MIT License. Copyright (c) 2013-2021 Bailey Ling et al.
  2. " Plugin: vim-gitgutter, vim-signify, changesPlugin, quickfixsigns, coc-git,
  3. " gitsigns.nvim
  4. " vim: et ts=2 sts=2 sw=2
  5. scriptencoding utf-8
  6. if !get(g:, 'loaded_signify', 0)
  7. \ && !get(g:, 'loaded_gitgutter', 0)
  8. \ && !get(g:, 'loaded_changes', 0)
  9. \ && !get(g:, 'loaded_quickfixsigns', 0)
  10. \ && !exists(':Gitsigns')
  11. \ && !exists("*CocAction")
  12. finish
  13. endif
  14. let s:non_zero_only = get(g:, 'airline#extensions#hunks#non_zero_only', 0)
  15. let s:hunk_symbols = get(g:, 'airline#extensions#hunks#hunk_symbols', ['+', '~', '-'])
  16. function! s:coc_git_enabled() abort
  17. if !exists("*CocAction") ||
  18. \ !get(g:, 'airline#extensions#hunks#coc_git', 0)
  19. " coc-git extension is disabled by default
  20. " unless specifically being enabled by the user
  21. " (as requested from coc maintainer)
  22. return 0
  23. endif
  24. return 1
  25. endfunction
  26. function! s:parse_hunk_status_dict(hunks) abort
  27. let result = [0, 0, 0]
  28. let result[0] = get(a:hunks, 'added', 0)
  29. let result[1] = get(a:hunks, 'changed', 0)
  30. let result[2] = get(a:hunks, 'removed', 0)
  31. return result
  32. endfunction
  33. function! s:parse_hunk_status_decorated(hunks) abort
  34. if empty(a:hunks)
  35. return []
  36. endif
  37. let result = [0, 0, 0]
  38. for val in split(a:hunks)
  39. if val[0] is# '+'
  40. let result[0] = val[1:] + 0
  41. elseif val[0] is# '~'
  42. let result[1] = val[1:] + 0
  43. elseif val[0] is# '-'
  44. let result[2] = val[1:] + 0
  45. endif
  46. endfor
  47. return result
  48. endfunction
  49. function! s:get_hunks_signify() abort
  50. let hunks = sy#repo#get_stats()
  51. if hunks[0] >= 0
  52. return hunks
  53. endif
  54. return []
  55. endfunction
  56. function! s:get_hunks_gitgutter() abort
  57. let hunks = GitGutterGetHunkSummary()
  58. return hunks == [0, 0, 0] ? [] : hunks
  59. endfunction
  60. function! s:get_hunks_changes() abort
  61. let hunks = changes#GetStats()
  62. return hunks == [0, 0, 0] ? [] : hunks
  63. endfunction
  64. function! s:get_hunks_gitsigns() abort
  65. let hunks = get(b:, 'gitsigns_status_dict', {})
  66. return s:parse_hunk_status_dict(hunks)
  67. endfunction
  68. function! s:get_hunks_coc() abort
  69. let hunks = get(b:, 'coc_git_status', '')
  70. return s:parse_hunk_status_decorated(hunks)
  71. endfunction
  72. function! s:get_hunks_empty() abort
  73. return ''
  74. endfunction
  75. function! airline#extensions#hunks#get_raw_hunks() abort
  76. if !exists('b:source_func') || get(b:, 'source_func', '') is# 's:get_hunks_empty'
  77. if get(g:, 'loaded_signify') && sy#buffer_is_active()
  78. let b:source_func = 's:get_hunks_signify'
  79. elseif exists('*GitGutterGetHunkSummary') && get(g:, 'gitgutter_enabled')
  80. let b:source_func = 's:get_hunks_gitgutter'
  81. elseif exists('*changes#GetStats')
  82. let b:source_func = 's:get_hunks_changes'
  83. elseif exists('*quickfixsigns#vcsdiff#GetHunkSummary')
  84. let b:source_func = 'quickfixsigns#vcsdiff#GetHunkSummary'
  85. elseif exists(':Gitsigns')
  86. let b:source_func = 's:get_hunks_gitsigns'
  87. elseif s:coc_git_enabled()
  88. let b:source_func = 's:get_hunks_coc'
  89. else
  90. let b:source_func = 's:get_hunks_empty'
  91. endif
  92. endif
  93. return {b:source_func}()
  94. endfunction
  95. function! airline#extensions#hunks#get_hunks() abort
  96. if !get(w:, 'airline_active', 0)
  97. return ''
  98. endif
  99. " Cache values, so that it isn't called too often
  100. if exists("b:airline_hunks") &&
  101. \ get(b:, 'airline_changenr', 0) == b:changedtick &&
  102. \ airline#util#winwidth() == get(s:, 'airline_winwidth', 0) &&
  103. \ get(b:, 'source_func', '') isnot# 's:get_hunks_signify' &&
  104. \ get(b:, 'source_func', '') isnot# 's:get_hunks_gitgutter' &&
  105. \ get(b:, 'source_func', '') isnot# 's:get_hunks_empty' &&
  106. \ get(b:, 'source_func', '') isnot# 's:get_hunks_changes' &&
  107. \ get(b:, 'source_func', '') isnot# 's:get_hunks_gitsigns' &&
  108. \ get(b:, 'source_func', '') isnot# 's:get_hunks_coc'
  109. return b:airline_hunks
  110. endif
  111. let hunks = airline#extensions#hunks#get_raw_hunks()
  112. let string = ''
  113. let winwidth = get(airline#parts#get('hunks'), 'minwidth', 100)
  114. if !empty(hunks)
  115. " hunks should contain [added, changed, deleted]
  116. for i in [0, 1, 2]
  117. if (s:non_zero_only == 0 && airline#util#winwidth() > winwidth) || hunks[i] > 0
  118. let string .= printf('%s%s ', s:hunk_symbols[i], hunks[i])
  119. endif
  120. endfor
  121. endif
  122. if index(airline#extensions#get_loaded_extensions(), 'branch') == -1 && string[-1:] == ' '
  123. " branch extension not loaded, skip trailing whitespace
  124. let string = string[0:-2]
  125. endif
  126. let b:airline_hunks = string
  127. let b:airline_changenr = b:changedtick
  128. let s:airline_winwidth = airline#util#winwidth()
  129. return string
  130. endfunction
  131. function! airline#extensions#hunks#init(ext) abort
  132. call airline#parts#define_function('hunks', 'airline#extensions#hunks#get_hunks')
  133. endfunction