From 714d4683447183e81e77ab274ea3edc4130f3d66 Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Tue, 15 Jan 2019 11:33:48 +0000 Subject: [PATCH] Enable unfolding 3 lines of context around each hunk. --- README.mkd | 4 +++- autoload/gitgutter/fold.vim | 28 +++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/README.mkd b/README.mkd index 45cf212..5236104 100644 --- a/README.mkd +++ b/README.mkd @@ -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 diff --git a/autoload/gitgutter/fold.vim b/autoload/gitgutter/fold.vim index a3fb886..3feeb7f 100644 --- a/autoload/gitgutter/fold.vim +++ b/autoload/gitgutter/fold.vim @@ -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