From 88d396f1b49747fadbbde5c038a85067d94954e5 Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Wed, 28 Aug 2019 11:59:19 +0100 Subject: [PATCH] Add :GitGutterQuickFix command. It loads all hunks into the quickfix list. Closes #617. --- README.mkd | 3 +++ autoload/gitgutter.vim | 26 ++++++++++++++++++++++++++ doc/gitgutter.txt | 4 ++++ plugin/gitgutter.vim | 2 ++ test/test_gitgutter.vim | 16 ++++++++++++++++ 5 files changed, 51 insertions(+) diff --git a/README.mkd b/README.mkd index 4af6e47..a7fe4a7 100644 --- a/README.mkd +++ b/README.mkd @@ -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 GitGutterNextHunk nmap [h 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 `hs` or diff --git a/autoload/gitgutter.vim b/autoload/gitgutter.vim index 29b5865..ffc0966 100644 --- a/autoload/gitgutter.vim +++ b/autoload/gitgutter.vim @@ -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 diff --git a/doc/gitgutter.txt b/doc/gitgutter.txt index d17a3ce..c2affb4 100644 --- a/doc/gitgutter.txt +++ b/doc/gitgutter.txt @@ -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:~ diff --git a/plugin/gitgutter.vim b/plugin/gitgutter.vim index 14b413e..1758af2 100644 --- a/plugin/gitgutter.vim +++ b/plugin/gitgutter.vim @@ -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 {{{ diff --git a/test/test_gitgutter.vim b/test/test_gitgutter.vim index 20bf31e..f35be58 100644 --- a/test/test_gitgutter.vim +++ b/test/test_gitgutter.vim @@ -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