Add :GitGutterQuickFix command.

It loads all hunks into the quickfix list.

Closes #617.
This commit is contained in:
Andy Stewart
2019-08-28 11:59:19 +01:00
parent 307caf6f39
commit 88d396f1b4
5 changed files with 51 additions and 0 deletions

View File

@@ -16,6 +16,7 @@ Features:
* Provides a hunk text object.
* Diffs against index (default) or any commit.
* Allows folding all unchanged text.
* Can load all hunk locations into quickfix list.
* Handles line endings correctly, even with repos that do CRLF conversion.
* Optional line highlighting.
* Optional line number highlighting. (Only available in Neovim 0.3.2 or higher)
@@ -183,6 +184,8 @@ nmap ]h <Plug>GitGutterNextHunk
nmap [h <Plug>GitGutterPrevHunk
```
You can load all your hunks into the quickfix list with `:GitGutterQuickFix`. Note this ignores any unsaved changes in your buffers.
You can stage or undo an individual hunk when your cursor is in it:
* stage the hunk with `<Leader>hs` or

View File

@@ -179,3 +179,29 @@ function! s:clear(bufnr)
call s:reset_tick(a:bufnr)
call gitgutter#utility#setbufvar(a:bufnr, 'path', '')
endfunction
" Note:
" - this runs synchronously
" - it ignores unsaved changes in buffers
" - it does not change to the repo root
function! gitgutter#quickfix()
let locations = []
let cmd = g:gitgutter_git_executable.' '.g:gitgutter_git_args.' --no-pager '.g:gitgutter_git_args.
\ ' diff --no-ext-diff --no-color -U0 '.g:gitgutter_diff_args
let diff = systemlist(cmd)
let lnum = 0
for line in diff
if line =~ '^diff --git [^"]'
let fname = matchlist(line, '^diff --git [abciow12]/\(\S\+\) ')[1]
elseif line =~ '^diff --git "'
let fname = matchlist(line, '^diff --git "[abciow12]/\(.\+\)" ')[1]
elseif line =~ '^@@'
let lnum = matchlist(line, '+\(\d\+\)')[1]
elseif lnum > 0
call add(locations, {'filename': fname, 'lnum': lnum, 'text': line})
let lnum = 0
endif
endfor
call setqflist(locations)
endfunction

View File

@@ -160,6 +160,10 @@ Commands for jumping between hunks:~
*gitgutter-:GitGutterPrevHunk*
:GitGutterPrevHunk Jump to the previous [count] hunk.
*gitgutter-:GitGutterQuickFix*
:GitGutterQuickFix Load all hunks into the |quickfix| list. Note this
ignores any unsaved changes in your buffers.
Commands for operating on a hunk:~

View File

@@ -109,6 +109,8 @@ command! -bar GitGutterBufferDisable call gitgutter#buffer_disable()
command! -bar GitGutterBufferEnable call gitgutter#buffer_enable()
command! -bar GitGutterBufferToggle call gitgutter#buffer_toggle()
command! -bar GitGutterQuickFix call gitgutter#quickfix()
" }}}
" Line highlights {{{

View File

@@ -894,3 +894,19 @@ function Test_empty_file()
set eol fixeol
endfunction
function Test_quickfix()
call setline(5, ['A', 'B'])
call setline(9, ['C', 'D'])
write
GitGutterQuickFix
let expected = [
\ {'lnum': 5, 'bufnr': bufnr(''), 'text': '-e'},
\ {'lnum': 9, 'bufnr': bufnr(''), 'text': '-i'}
\ ]
call s:assert_list_of_dicts(expected, getqflist())
endfunction