Enable unfolding 3 lines of context around each hunk.

This commit is contained in:
Andy Stewart
2019-01-15 11:33:48 +00:00
parent ac787afa02
commit 714d468344
2 changed files with 30 additions and 2 deletions

View File

@@ -213,7 +213,9 @@ See the customisation section below for how to change the defaults.
### Folding
Use the `GitGutterFold` command to fold all unchanged lines, leaving just the hunks visible. Use the command again to restore the previous view.
Use the `GitGutterFold` command to fold all unchanged lines, leaving just the hunks visible. Use `zo` to unfold 3 lines of context above and below a hunk.
Execute `GitGutterFold` a second time to restore the previous view.
### Customisation

View File

@@ -1,6 +1,7 @@
function! gitgutter#fold#enable()
call s:save_fold_state()
call s:set_fold_levels()
setlocal foldexpr=gitgutter#fold#level(v:lnum)
setlocal foldmethod=expr
setlocal foldlevel=0
@@ -26,7 +27,32 @@ endfunction
function! gitgutter#fold#level(lnum)
return gitgutter#hunk#in_hunk(a:lnum) ? 0 : 1
return gitgutter#utility#getbufvar(bufnr(''), 'fold_levels')[a:lnum]
endfunction
" A line in a hunk has a fold level of 0.
" A line within 3 lines of a hunk has a fold level of 1.
" All other lines have a fold level of 2.
function! s:set_fold_levels()
let fold_levels = ['']
for lnum in range(1, line('$'))
let in_hunk = gitgutter#hunk#in_hunk(lnum)
call add(fold_levels, (in_hunk ? 0 : 2))
endfor
for lnum in range(1, line('$'))
if fold_levels[lnum] == 2
let pre = lnum >= 3 ? lnum - 3 : 0
let post = lnum + 3
if index(fold_levels[pre:post], 0) != -1
let fold_levels[lnum] = 1
endif
endif
endfor
call gitgutter#utility#setbufvar(bufnr(''), 'fold_levels', fold_levels)
endfunction