hunk.vim 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. " number of lines [added, modified, removed]
  2. let s:summary = [0, 0, 0]
  3. let s:hunks = []
  4. function! gitgutter#hunk#set_hunks(hunks)
  5. let s:hunks = a:hunks
  6. endfunction
  7. function! gitgutter#hunk#hunks()
  8. return s:hunks
  9. endfunction
  10. function! gitgutter#hunk#summary()
  11. return s:summary
  12. endfunction
  13. function! gitgutter#hunk#reset()
  14. let s:summary = [0, 0, 0]
  15. endfunction
  16. function! gitgutter#hunk#increment_lines_added(count)
  17. let s:summary[0] += a:count
  18. endfunction
  19. function! gitgutter#hunk#increment_lines_modified(count)
  20. let s:summary[1] += a:count
  21. endfunction
  22. function! gitgutter#hunk#increment_lines_removed(count)
  23. let s:summary[2] += a:count
  24. endfunction
  25. function! gitgutter#hunk#next_hunk(count)
  26. if gitgutter#utility#is_active()
  27. let current_line = line('.')
  28. let hunk_count = 0
  29. for hunk in s:hunks
  30. if hunk[2] > current_line
  31. let hunk_count += 1
  32. if hunk_count == a:count
  33. execute 'normal!' hunk[2] . 'G'
  34. return
  35. endif
  36. endif
  37. endfor
  38. call gitgutter#utility#warn('No more hunks')
  39. endif
  40. endfunction
  41. function! gitgutter#hunk#prev_hunk(count)
  42. if gitgutter#utility#is_active()
  43. let current_line = line('.')
  44. let hunk_count = 0
  45. for hunk in reverse(copy(s:hunks))
  46. if hunk[2] < current_line
  47. let hunk_count += 1
  48. if hunk_count == a:count
  49. let target = hunk[2] == 0 ? 1 : hunk[2]
  50. execute 'normal!' target . 'G'
  51. return
  52. endif
  53. endif
  54. endfor
  55. call gitgutter#utility#warn('No previous hunks')
  56. endif
  57. endfunction
  58. " Returns the hunk the cursor is currently in or an empty list if the cursor
  59. " isn't in a hunk.
  60. function! gitgutter#hunk#current_hunk()
  61. let current_hunk = []
  62. for hunk in s:hunks
  63. if gitgutter#hunk#cursor_in_hunk(hunk)
  64. let current_hunk = hunk
  65. break
  66. endif
  67. endfor
  68. return current_hunk
  69. endfunction
  70. function! gitgutter#hunk#cursor_in_hunk(hunk)
  71. let current_line = line('.')
  72. if current_line == 1 && a:hunk[2] == 0
  73. return 1
  74. endif
  75. if current_line >= a:hunk[2] && current_line < a:hunk[2] + (a:hunk[3] == 0 ? 1 : a:hunk[3])
  76. return 1
  77. endif
  78. return 0
  79. endfunction
  80. " Returns the number of lines the current hunk is offset from where it would
  81. " be if any changes above it in the file didn't exist.
  82. function! gitgutter#hunk#line_adjustment_for_current_hunk()
  83. let adj = 0
  84. for hunk in s:hunks
  85. if gitgutter#hunk#cursor_in_hunk(hunk)
  86. break
  87. else
  88. let adj += hunk[1] - hunk[3]
  89. endif
  90. endfor
  91. return adj
  92. endfunction