1
0
Fork 0
mirror of synced 2024-12-25 08:13:20 -05:00

Update vim-gitgutter.

This commit is contained in:
Kurtis Moxley 2022-05-19 23:20:20 +08:00
parent 2538a00dff
commit ca2bc6ca9f
11 changed files with 254 additions and 118 deletions

View file

@ -18,6 +18,7 @@ Features:
* Stage partial hunks. * Stage partial hunks.
* Provides a hunk text object. * Provides a hunk text object.
* Diffs against index (default) or any commit. * Diffs against index (default) or any commit.
* Heeds git's "assume unchanged" bit.
* Allows folding all unchanged text. * Allows folding all unchanged text.
* Provides fold text showing whether folded lines have been changed. * Provides fold text showing whether folded lines have been changed.
* Can load all hunk locations into quickfix list or the current window's location list. * Can load all hunk locations into quickfix list or the current window's location list.
@ -238,6 +239,11 @@ Finally, you can force vim-gitgutter to update its signs across all visible buff
See the customisation section below for how to change the defaults. See the customisation section below for how to change the defaults.
### Vimdiff
Use the `GitGutterDiffOrig` command to open a vimdiff view of the current buffer, respecting `g:gitgutter_diff_relative_to` and `:gitgutter_diff_base`.
### Folding ### Folding
Use the `GitGutterFold` command to fold all unchanged lines, leaving just the hunks visible. Use `zr` to unfold 3 lines of context above and below a hunk. Use the `GitGutterFold` command to fold all unchanged lines, leaving just the hunks visible. Use `zr` to unfold 3 lines of context above and below a hunk.
@ -299,6 +305,7 @@ You can customise:
* Whether to clobber or preserve non-gitgutter signs * Whether to clobber or preserve non-gitgutter signs
* The priority of gitgutter's signs. * The priority of gitgutter's signs.
* Whether to use a floating/popup window for hunk previews * Whether to use a floating/popup window for hunk previews
* The appearance of a floating/popup window for hunk previews
* Whether to populate the quickfix list or a location list with all hunks * Whether to populate the quickfix list or a location list with all hunks
Please note that vim-gitgutter won't override any colours or highlights you've set in your colorscheme. Please note that vim-gitgutter won't override any colours or highlights you've set in your colorscheme.
@ -519,6 +526,11 @@ let g:gitgutter_async = 0
Add `let g:gitgutter_preview_win_floating = 1` to your `~/.vimrc`. Note that on Vim this prevents you staging (partial) hunks via the preview window. Add `let g:gitgutter_preview_win_floating = 1` to your `~/.vimrc`. Note that on Vim this prevents you staging (partial) hunks via the preview window.
#### The appearance of a floating/popup window for hunk previews
Set `g:gitgutter_floating_window_options` to a dictionary of the options you want. This dictionary is passed directly to `popup_create()` (Vim) / `nvim_open_win()` (Neovim).
#### To load all hunks into the current window's location list instead of the quickfix list #### To load all hunks into the current window's location list instead of the quickfix list
Add `let g:gitgutter_use_location_list = 1` to your `~/.vimrc`. Add `let g:gitgutter_use_location_list = 1` to your `~/.vimrc`.

View file

@ -21,6 +21,10 @@ endfunction
function! gitgutter#process_buffer(bufnr, force) abort function! gitgutter#process_buffer(bufnr, force) abort
" NOTE a:bufnr is not necessarily the current buffer. " NOTE a:bufnr is not necessarily the current buffer.
if gitgutter#utility#getbufvar(a:bufnr, 'enabled', -1) == -1
call gitgutter#utility#setbufvar(a:bufnr, 'enabled', g:gitgutter_enabled)
endif
if gitgutter#utility#is_active(a:bufnr) if gitgutter#utility#is_active(a:bufnr)
if has('patch-7.4.1559') if has('patch-7.4.1559')
@ -40,6 +44,8 @@ function! gitgutter#process_buffer(bufnr, force) abort
let diff = gitgutter#diff#run_diff(a:bufnr, g:gitgutter_diff_relative_to, 0) let diff = gitgutter#diff#run_diff(a:bufnr, g:gitgutter_diff_relative_to, 0)
catch /gitgutter not tracked/ catch /gitgutter not tracked/
call gitgutter#debug#log('Not tracked: '.gitgutter#utility#file(a:bufnr)) call gitgutter#debug#log('Not tracked: '.gitgutter#utility#file(a:bufnr))
catch /gitgutter assume unchanged/
call gitgutter#debug#log('Assume unchanged: '.gitgutter#utility#file(a:bufnr))
catch /gitgutter diff failed/ catch /gitgutter diff failed/
call gitgutter#debug#log('Diff failed: '.gitgutter#utility#file(a:bufnr)) call gitgutter#debug#log('Diff failed: '.gitgutter#utility#file(a:bufnr))
call gitgutter#hunk#reset(a:bufnr) call gitgutter#hunk#reset(a:bufnr)
@ -55,22 +61,28 @@ endfunction
function! gitgutter#disable() abort function! gitgutter#disable() abort
" get list of all buffers (across all tabs) call s:toggle_each_buffer(0)
for bufnr in range(1, bufnr('$') + 1)
if buflisted(bufnr)
let file = expand('#'.bufnr.':p')
if !empty(file)
call s:clear(bufnr)
endif
endif
endfor
let g:gitgutter_enabled = 0 let g:gitgutter_enabled = 0
endfunction endfunction
function! gitgutter#enable() abort function! gitgutter#enable() abort
call s:toggle_each_buffer(1)
let g:gitgutter_enabled = 1 let g:gitgutter_enabled = 1
call gitgutter#all(1) endfunction
function s:toggle_each_buffer(enable)
for bufnr in range(1, bufnr('$') + 1)
if buflisted(bufnr)
let file = expand('#'.bufnr.':p')
if !empty(file)
if a:enable
call gitgutter#buffer_enable(bufnr)
else
call gitgutter#buffer_disable(bufnr)
end
endif
endif
endfor
endfunction endfunction
function! gitgutter#toggle() abort function! gitgutter#toggle() abort
@ -82,23 +94,24 @@ function! gitgutter#toggle() abort
endfunction endfunction
function! gitgutter#buffer_disable() abort function! gitgutter#buffer_disable(...) abort
let bufnr = bufnr('') let bufnr = a:0 ? a:1 : bufnr('')
call gitgutter#utility#setbufvar(bufnr, 'enabled', 0) call gitgutter#utility#setbufvar(bufnr, 'enabled', 0)
call s:clear(bufnr) call s:clear(bufnr)
endfunction endfunction
function! gitgutter#buffer_enable() abort function! gitgutter#buffer_enable(...) abort
let bufnr = bufnr('') let bufnr = a:0 ? a:1 : bufnr('')
call gitgutter#utility#setbufvar(bufnr, 'enabled', 1) call gitgutter#utility#setbufvar(bufnr, 'enabled', 1)
call gitgutter#process_buffer(bufnr, 1) call gitgutter#process_buffer(bufnr, 1)
endfunction endfunction
function! gitgutter#buffer_toggle() abort function! gitgutter#buffer_toggle(...) abort
if gitgutter#utility#getbufvar(bufnr(''), 'enabled', 1) let bufnr = a:0 ? a:1 : bufnr('')
call gitgutter#buffer_disable() if gitgutter#utility#getbufvar(bufnr, 'enabled', 1)
call gitgutter#buffer_disable(bufnr)
else else
call gitgutter#buffer_enable() call gitgutter#buffer_enable(bufnr)
endif endif
endfunction endfunction
@ -222,3 +235,31 @@ function! gitgutter#quickfix(current_file)
call setloclist(0, locations) call setloclist(0, locations)
endif endif
endfunction endfunction
function! gitgutter#difforig()
let bufnr = bufnr('')
let path = gitgutter#utility#repo_path(bufnr, 1)
let filetype = &filetype
vertical new
set buftype=nofile
let &filetype = filetype
if g:gitgutter_diff_relative_to ==# 'index'
let index_name = gitgutter#utility#get_diff_base(bufnr).':'.path
let cmd = gitgutter#utility#cd_cmd(bufnr,
\ g:gitgutter_git_executable.' '.g:gitgutter_git_args.' --no-pager show '.index_name
\ )
" NOTE: this uses &shell to execute cmd. Perhaps we should use instead
" gitgutter#utility's use_known_shell() / restore_shell() functions.
silent! execute "read ++edit !" cmd
else
silent! execute "read ++edit" path
endif
0d_
diffthis
wincmd p
diffthis
endfunction

View file

@ -77,6 +77,10 @@ function! gitgutter#diff#run_diff(bufnr, from, preserve_full_diff) abort
throw 'gitgutter not tracked' throw 'gitgutter not tracked'
endif endif
if gitgutter#utility#repo_path(a:bufnr, 0) == -3
throw 'gitgutter assume unchanged'
endif
" Wrap compound commands in parentheses to make Windows happy. " Wrap compound commands in parentheses to make Windows happy.
" bash doesn't mind the parentheses. " bash doesn't mind the parentheses.
let cmd = '(' let cmd = '('

View file

@ -150,12 +150,6 @@ function! s:lcs(s1, s2)
return a:s1[endindex - maxlength + 1 : endindex] return a:s1[endindex - maxlength + 1 : endindex]
endfunction endfunction
if $VIM_GITGUTTER_TEST
function! gitgutter#diff_highlight#lcs(s1, s2)
return s:lcs(a:s1, a:s2)
endfunction
endif
" Returns 0-based index of last character of common prefix " Returns 0-based index of last character of common prefix
" If there is no common prefix, returns -1. " If there is no common prefix, returns -1.
@ -175,12 +169,6 @@ function! s:common_prefix(a, b)
return i return i
endfunction endfunction
if $VIM_GITGUTTER_TEST
function! gitgutter#diff_highlight#common_prefix(a, b)
return s:common_prefix(a:a, a:b)
endfunction
endif
" Returns 0-based indices of start of common suffix " Returns 0-based indices of start of common suffix
" "
@ -199,12 +187,6 @@ function! s:common_suffix(a, b, start)
return [sa+1, sb+1] return [sa+1, sb+1]
endfunction endfunction
if $VIM_GITGUTTER_TEST
function! gitgutter#diff_highlight#common_suffix(a, b, start)
return s:common_suffix(a:a, a:b, a:start)
endfunction
endif
" Split a string on another string. " Split a string on another string.
" Assumes 1 occurrence of the delimiter. " Assumes 1 occurrence of the delimiter.
@ -217,9 +199,3 @@ function! s:split(str, delimiter)
return [a:str[:i-1], a:str[i+len(a:delimiter):]] return [a:str[:i-1], a:str[i+len(a:delimiter):]]
endfunction endfunction
if $VIM_GITGUTTER_TEST
function! gitgutter#diff_highlight#split(str, delimiter)
return s:split(a:str, a:delimiter)
endfunction
endif

View file

@ -175,12 +175,12 @@ function! s:define_sign_line_highlights() abort
sign define GitGutterLineRemovedAboveAndBelow linehl=GitGutterDeleteLine sign define GitGutterLineRemovedAboveAndBelow linehl=GitGutterDeleteLine
sign define GitGutterLineModifiedRemoved linehl=GitGutterChangeDeleteLine sign define GitGutterLineModifiedRemoved linehl=GitGutterChangeDeleteLine
else else
sign define GitGutterLineAdded linehl= sign define GitGutterLineAdded linehl=NONE
sign define GitGutterLineModified linehl= sign define GitGutterLineModified linehl=NONE
sign define GitGutterLineRemoved linehl= sign define GitGutterLineRemoved linehl=NONE
sign define GitGutterLineRemovedFirstLine linehl= sign define GitGutterLineRemovedFirstLine linehl=NONE
sign define GitGutterLineRemovedAboveAndBelow linehl= sign define GitGutterLineRemovedAboveAndBelow linehl=NONE
sign define GitGutterLineModifiedRemoved linehl= sign define GitGutterLineModifiedRemoved linehl=NONE
endif endif
endfunction endfunction
@ -195,12 +195,12 @@ function! s:define_sign_linenr_highlights() abort
sign define GitGutterLineRemovedAboveAndBelow numhl=GitGutterDeleteLineNr sign define GitGutterLineRemovedAboveAndBelow numhl=GitGutterDeleteLineNr
sign define GitGutterLineModifiedRemoved numhl=GitGutterChangeDeleteLineNr sign define GitGutterLineModifiedRemoved numhl=GitGutterChangeDeleteLineNr
else else
sign define GitGutterLineAdded numhl= sign define GitGutterLineAdded numhl=NONE
sign define GitGutterLineModified numhl= sign define GitGutterLineModified numhl=NONE
sign define GitGutterLineRemoved numhl= sign define GitGutterLineRemoved numhl=NONE
sign define GitGutterLineRemovedFirstLine numhl= sign define GitGutterLineRemovedFirstLine numhl=NONE
sign define GitGutterLineRemovedAboveAndBelow numhl= sign define GitGutterLineRemovedAboveAndBelow numhl=NONE
sign define GitGutterLineModifiedRemoved numhl= sign define GitGutterLineModifiedRemoved numhl=NONE
endif endif
catch /E475/ catch /E475/
endtry endtry

View file

@ -382,12 +382,6 @@ function! s:fix_file_references(filepath, hunk_diff)
return join(lines, "\n")."\n" return join(lines, "\n")."\n"
endfunction endfunction
if $VIM_GITGUTTER_TEST
function! gitgutter#hunk#fix_file_references(filepath, hunk_diff)
return s:fix_file_references(a:filepath, a:hunk_diff)
endfunction
endif
function! s:adjust_hunk_summary(hunk_diff) abort function! s:adjust_hunk_summary(hunk_diff) abort
let line_adjustment = s:line_adjustment_for_current_hunk() let line_adjustment = s:line_adjustment_for_current_hunk()
@ -431,14 +425,7 @@ function! s:open_hunk_preview_window()
let buf = nvim_create_buf(v:false, v:false) let buf = nvim_create_buf(v:false, v:false)
" Set default width and height for now. " Set default width and height for now.
let s:winid = nvim_open_win(buf, v:false, { let s:winid = nvim_open_win(buf, v:false, g:gitgutter_floating_window_options)
\ 'relative': 'cursor',
\ 'row': 1,
\ 'col': 0,
\ 'width': 42,
\ 'height': &previewheight,
\ 'style': 'minimal'
\ })
call nvim_buf_set_option(buf, 'filetype', 'diff') call nvim_buf_set_option(buf, 'filetype', 'diff')
call nvim_buf_set_option(buf, 'buftype', 'acwrite') call nvim_buf_set_option(buf, 'buftype', 'acwrite')
call nvim_buf_set_option(buf, 'bufhidden', 'delete') call nvim_buf_set_option(buf, 'bufhidden', 'delete')
@ -461,16 +448,11 @@ function! s:open_hunk_preview_window()
endif endif
if exists('*popup_create') if exists('*popup_create')
let opts = {
\ 'line': 'cursor+1',
\ 'col': 'cursor',
\ 'moved': 'any',
\ }
if g:gitgutter_close_preview_on_escape if g:gitgutter_close_preview_on_escape
let opts.filter = function('s:close_popup_on_escape') let g:gitgutter_floating_window_options.filter = function('s:close_popup_on_escape')
endif endif
let s:winid = popup_create('', opts) let s:winid = popup_create('', g:gitgutter_floating_window_options)
call setbufvar(winbufnr(s:winid), '&filetype', 'diff') call setbufvar(winbufnr(s:winid), '&filetype', 'diff')
@ -478,6 +460,10 @@ function! s:open_hunk_preview_window()
endif endif
endif endif
if exists('&previewpopup')
let [previewpopup, &previewpopup] = [&previewpopup, '']
endif
" Specifying where to open the preview window can lead to the cursor going " Specifying where to open the preview window can lead to the cursor going
" to an unexpected window when the preview window is closed (#769). " to an unexpected window when the preview window is closed (#769).
silent! noautocmd execute g:gitgutter_preview_win_location 'pedit gitgutter://hunk-preview' silent! noautocmd execute g:gitgutter_preview_win_location 'pedit gitgutter://hunk-preview'
@ -496,6 +482,10 @@ function! s:open_hunk_preview_window()
" Ensure cursor goes to the expected window. " Ensure cursor goes to the expected window.
nnoremap <buffer> <silent> <Esc> :<C-U>wincmd p<Bar>pclose<CR> nnoremap <buffer> <silent> <Esc> :<C-U>wincmd p<Bar>pclose<CR>
endif endif
if exists('&previewpopup')
let &previewpopup=previewpopup
endif
endfunction endfunction
@ -515,7 +505,7 @@ function! s:populate_hunk_preview_window(header, body)
if g:gitgutter_preview_win_floating if g:gitgutter_preview_win_floating
if exists('*nvim_open_win') if exists('*nvim_open_win')
let height = min([body_length, &previewheight]) let height = min([body_length, g:gitgutter_floating_window_options.height])
" Assumes cursor is not in previewing window. " Assumes cursor is not in previewing window.
call nvim_buf_set_var(winbufnr(s:winid), 'hunk_header', a:header) call nvim_buf_set_var(winbufnr(s:winid), 'hunk_header', a:header)

View file

@ -48,8 +48,7 @@ endfunction
" Returns truthy when the buffer's file should be processed; and falsey when it shouldn't. " Returns truthy when the buffer's file should be processed; and falsey when it shouldn't.
" This function does not and should not make any system calls. " This function does not and should not make any system calls.
function! gitgutter#utility#is_active(bufnr) abort function! gitgutter#utility#is_active(bufnr) abort
return g:gitgutter_enabled && return gitgutter#utility#getbufvar(a:bufnr, 'enabled') &&
\ gitgutter#utility#getbufvar(a:bufnr, 'enabled', 1) &&
\ !pumvisible() && \ !pumvisible() &&
\ s:is_file_buffer(a:bufnr) && \ s:is_file_buffer(a:bufnr) &&
\ s:exists_file(a:bufnr) && \ s:exists_file(a:bufnr) &&
@ -109,6 +108,7 @@ endfunction
" * non-empty string - path " * non-empty string - path
" * -1 - pending " * -1 - pending
" * -2 - not tracked by git " * -2 - not tracked by git
" * -3 - assume unchanged
function! gitgutter#utility#repo_path(bufnr, shellesc) abort function! gitgutter#utility#repo_path(bufnr, shellesc) abort
let p = gitgutter#utility#getbufvar(a:bufnr, 'path', '') let p = gitgutter#utility#getbufvar(a:bufnr, 'path', '')
return a:shellesc ? gitgutter#utility#shellescape(p) : p return a:shellesc ? gitgutter#utility#shellescape(p) : p
@ -117,9 +117,14 @@ endfunction
let s:set_path_handler = {} let s:set_path_handler = {}
function! s:set_path_handler.out(buffer, path) abort function! s:set_path_handler.out(buffer, listing) abort
let path = s:strip_trailing_new_line(a:path) let listing = s:strip_trailing_new_line(a:listing)
call gitgutter#utility#setbufvar(a:buffer, 'path', path) let [status, path] = [listing[0], listing[2:]]
if status =~# '[a-z]'
call gitgutter#utility#setbufvar(a:buffer, 'path', -3)
else
call gitgutter#utility#setbufvar(a:buffer, 'path', path)
endif
if type(self.continuation) == type(function('tr')) if type(self.continuation) == type(function('tr'))
call self.continuation() call self.continuation()
@ -141,9 +146,13 @@ function! gitgutter#utility#set_repo_path(bufnr, continuation) abort
" * non-empty string - path " * non-empty string - path
" * -1 - pending " * -1 - pending
" * -2 - not tracked by git " * -2 - not tracked by git
" * -3 - assume unchanged
call gitgutter#utility#setbufvar(a:bufnr, 'path', -1) call gitgutter#utility#setbufvar(a:bufnr, 'path', -1)
let cmd = gitgutter#utility#cd_cmd(a:bufnr, g:gitgutter_git_executable.' '.g:gitgutter_git_args.' ls-files --error-unmatch --full-name -z -- '.gitgutter#utility#shellescape(s:filename(a:bufnr))) let cmd = gitgutter#utility#cd_cmd(a:bufnr,
\ g:gitgutter_git_executable.' '.g:gitgutter_git_args.
\ ' ls-files -v --error-unmatch --full-name -z -- '.
\ gitgutter#utility#shellescape(s:filename(a:bufnr)))
if g:gitgutter_async && gitgutter#async#available() && !has('vim_starting') if g:gitgutter_async && gitgutter#async#available() && !has('vim_starting')
let handler = copy(s:set_path_handler) let handler = copy(s:set_path_handler)
@ -152,11 +161,19 @@ function! gitgutter#utility#set_repo_path(bufnr, continuation) abort
return 'async' return 'async'
endif endif
let path = gitgutter#utility#system(cmd) let listing = gitgutter#utility#system(cmd)
if v:shell_error if v:shell_error
call gitgutter#utility#setbufvar(a:bufnr, 'path', -2) call gitgutter#utility#setbufvar(a:bufnr, 'path', -2)
return
endif
let listing = s:strip_trailing_new_line(listing)
let [status, path] = [listing[0], listing[2:]]
if status =~# '[a-z]'
call gitgutter#utility#setbufvar(a:bufnr, 'path', -3)
else else
call gitgutter#utility#setbufvar(a:bufnr, 'path', s:strip_trailing_new_line(path)) call gitgutter#utility#setbufvar(a:bufnr, 'path', path)
endif endif
endfunction endfunction

View file

@ -93,6 +93,15 @@ Commands for turning vim-gitgutter on and off:~
*gitgutter-:GitGutterToggle* *gitgutter-:GitGutterToggle*
:GitGutterToggle Toggle vim-gitgutter on or off for all buffers. :GitGutterToggle Toggle vim-gitgutter on or off for all buffers.
*gitgutter-:GitGutterBufferDisable*
:GitGutterBufferDisable Turn vim-gitgutter off for current buffer.
*gitgutter-:GitGutterBufferEnable*
:GitGutterBufferEnable Turn vim-gitgutter on for current buffer.
*gitgutter-:GitGutterBufferToggle*
:GitGutterBufferToggle Toggle vim-gitgutter on or off for current buffer.
*gitgutter-:GitGutter* *gitgutter-:GitGutter*
:GitGutter Update signs for the current buffer. You shouldn't :GitGutter Update signs for the current buffer. You shouldn't
need to run this. need to run this.
@ -212,6 +221,12 @@ Commands for folds:~
:GitGutterFold Fold all unchanged lines. Execute again to undo. :GitGutterFold Fold all unchanged lines. Execute again to undo.
Other commands:~
*gitgutter-:GitGutterDiffOrig*
:GitGutterDiffOrig Similar to |:DiffOrig| but shows gitgutter's diff.
=============================================================================== ===============================================================================
AUTOCOMMANDS *gitgutter-autocommands* AUTOCOMMANDS *gitgutter-autocommands*
@ -348,6 +363,7 @@ Hunk jumping:~
Hunk previews:~ Hunk previews:~
|g:gitgutter_preview_win_floating| |g:gitgutter_preview_win_floating|
|g:gitgutter_floating_window_options|
|g:gitgutter_close_preview_on_escape| |g:gitgutter_close_preview_on_escape|
Terminal:~ Terminal:~
@ -508,6 +524,29 @@ Whether to use floating/popup windows for hunk previews. Note that if you use
popup windows on Vim you will not be able to stage partial hunks via the popup windows on Vim you will not be able to stage partial hunks via the
preview window. preview window.
*g:gitgutter_floating_window_options*
Default:
>
" Vim
{
\ 'line': 'cursor+1',
\ 'col': 'cursor',
\ 'moved': 'any'
}
" Neovim
{
\ 'relative': 'cursor',
\ 'row': 1,
\ 'col': 0,
\ 'width': 42,
\ 'height': &previewheight,
\ 'style': 'minimal'
}
<
This dictionary is passed directly to |popup_create()| (Vim) or
|nvim_open_win()| (Neovim).
*g:gitgutter_close_preview_on_escape* *g:gitgutter_close_preview_on_escape*
Default: 0 Default: 0

View file

@ -24,9 +24,22 @@ endfunction
let g:gitgutter_preview_win_location = get(g:, 'gitgutter_preview_win_location', 'bo') let g:gitgutter_preview_win_location = get(g:, 'gitgutter_preview_win_location', 'bo')
if exists('*nvim_open_win') if exists('*nvim_open_win')
let g:gitgutter_preview_win_floating = get(g:, 'gitgutter_preview_win_floating', 1) let g:gitgutter_preview_win_floating = get(g:, 'gitgutter_preview_win_floating', 1)
let g:gitgutter_floating_window_options = get(g:, 'gitgutter_floating_window_options', {
\ 'relative': 'cursor',
\ 'row': 1,
\ 'col': 0,
\ 'width': 42,
\ 'height': &previewheight,
\ 'style': 'minimal'
\ })
else else
let default = exists('&previewpopup') ? !empty(&previewpopup) : 0 let default = exists('&previewpopup') ? !empty(&previewpopup) : 0
let g:gitgutter_preview_win_floating = get(g:, 'gitgutter_preview_win_floating', default) let g:gitgutter_preview_win_floating = get(g:, 'gitgutter_preview_win_floating', default)
let g:gitgutter_floating_window_options = get(g:, 'gitgutter_floating_window_options', {
\ 'line': 'cursor+1',
\ 'col': 'cursor',
\ 'moved': 'any'
\ })
endif endif
let g:gitgutter_enabled = get(g:, 'gitgutter_enabled', 1) let g:gitgutter_enabled = get(g:, 'gitgutter_enabled', 1)
if exists('*sign_unplace') if exists('*sign_unplace')
@ -121,6 +134,8 @@ command! -bar GitGutterBufferToggle call gitgutter#buffer_toggle()
command! -bar GitGutterQuickFix call gitgutter#quickfix(0) command! -bar GitGutterQuickFix call gitgutter#quickfix(0)
command! -bar GitGutterQuickFixCurrentFile call gitgutter#quickfix(1) command! -bar GitGutterQuickFixCurrentFile call gitgutter#quickfix(1)
command! -bar GitGutterDiffOrig call gitgutter#difforig()
" }}} " }}}
" Line highlights {{{ " Line highlights {{{
@ -248,6 +263,10 @@ function! GitGutterCursorHold(timer)
execute 'doautocmd' s:nomodeline 'gitgutter CursorHold' execute 'doautocmd' s:nomodeline 'gitgutter CursorHold'
endfunction endfunction
function! s:next_tick(cmd)
call timer_start(1, {-> execute(a:cmd)})
endfunction
" Autocommands {{{ " Autocommands {{{
augroup gitgutter augroup gitgutter
@ -264,9 +283,9 @@ augroup gitgutter
autocmd CursorHold,CursorHoldI * call gitgutter#process_buffer(bufnr(''), 0) autocmd CursorHold,CursorHoldI * call gitgutter#process_buffer(bufnr(''), 0)
if exists('*timer_start') && has('lambda') if exists('*timer_start') && has('lambda')
autocmd FileChangedShellPost * call timer_start(1, {-> gitgutter#process_buffer(bufnr(''), 1)}) autocmd FileChangedShellPost * call s:next_tick("call gitgutter#process_buffer(+".expand('<abuf>').", 1)")
else else
autocmd FileChangedShellPost * call gitgutter#process_buffer(bufnr(''), 1) autocmd FileChangedShellPost * call gitgutter#process_buffer(+expand('<abuf>'), 1)
endif endif
" Ensure that all buffers are processed when opening vim with multiple files, e.g.: " Ensure that all buffers are processed when opening vim with multiple files, e.g.:

0
sources_non_forked/vim-gitgutter/test/test Normal file → Executable file
View file

View file

@ -205,6 +205,20 @@ function Test_filename_with_square_brackets()
endfunction endfunction
function Test_filename_with_space()
call system('touch fix\ ture.txt && git add fix\ ture.txt')
edit fix\ ture.txt
normal ggo*
call s:trigger_gitgutter()
let expected = [
\ {'lnum': 1, 'name': 'GitGutterLineAdded'},
\ {'lnum': 2, 'name': 'GitGutterLineAdded'}
\ ]
call s:assert_signs(expected, 'fix\ ture.txt')
endfunction
function Test_filename_leading_dash() function Test_filename_leading_dash()
call system('touch -- -fixture.txt && git add -- -fixture.txt') call system('touch -- -fixture.txt && git add -- -fixture.txt')
edit -fixture.txt edit -fixture.txt
@ -835,6 +849,9 @@ endfunction
function Test_fix_file_references() function Test_fix_file_references()
let sid = matchstr(execute('filter autoload/gitgutter/hunk.vim scriptnames'), '\d\+')
let FixFileReferences = function("<SNR>".sid."_fix_file_references")
" No special characters " No special characters
let hunk_diff = join([ let hunk_diff = join([
\ 'diff --git a/fixture.txt b/fixture.txt', \ 'diff --git a/fixture.txt b/fixture.txt',
@ -855,7 +872,7 @@ function Test_fix_file_references()
\ '+x' \ '+x'
\ ], "\n")."\n" \ ], "\n")."\n"
call assert_equal(expected, gitgutter#hunk#fix_file_references(filepath, hunk_diff)) call assert_equal(expected, FixFileReferences(filepath, hunk_diff))
" diff.mnemonicPrefix; spaces in filename " diff.mnemonicPrefix; spaces in filename
let hunk_diff = join([ let hunk_diff = join([
@ -877,7 +894,7 @@ function Test_fix_file_references()
\ '+x' \ '+x'
\ ], "\n")."\n" \ ], "\n")."\n"
call assert_equal(expected, gitgutter#hunk#fix_file_references(filepath, hunk_diff)) call assert_equal(expected, FixFileReferences(filepath, hunk_diff))
" Backslashes in filename; quotation marks " Backslashes in filename; quotation marks
let hunk_diff = join([ let hunk_diff = join([
@ -899,7 +916,7 @@ function Test_fix_file_references()
\ '+x' \ '+x'
\ ], "\n")."\n" \ ], "\n")."\n"
call assert_equal(expected, gitgutter#hunk#fix_file_references(filepath, hunk_diff)) call assert_equal(expected, FixFileReferences(filepath, hunk_diff))
endfunction endfunction
@ -975,39 +992,45 @@ endfunction
function Test_common_prefix() function Test_common_prefix()
let sid = matchstr(execute('filter autoload/gitgutter/diff_highlight.vim scriptnames'), '\d\+')
let CommonPrefix = function("<SNR>".sid."_common_prefix")
" zero length " zero length
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('', 'foo')) call assert_equal(-1, CommonPrefix('', 'foo'))
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('foo', '')) call assert_equal(-1, CommonPrefix('foo', ''))
" nothing in common " nothing in common
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abcde', '+pqrst')) call assert_equal(-1, CommonPrefix('-abcde', '+pqrst'))
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('abcde', 'pqrst')) call assert_equal(-1, CommonPrefix('abcde', 'pqrst'))
" something in common " something in common
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abcde', '+abcpq')) call assert_equal(-1, CommonPrefix('-abcde', '+abcpq'))
call assert_equal(2, gitgutter#diff_highlight#common_prefix('abcde', 'abcpq')) call assert_equal(2, CommonPrefix('abcde', 'abcpq'))
call assert_equal(0, gitgutter#diff_highlight#common_prefix('abc', 'apq')) call assert_equal(0, CommonPrefix('abc', 'apq'))
" everything in common " everything in common
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abcde', '+abcde')) call assert_equal(-1, CommonPrefix('-abcde', '+abcde'))
call assert_equal(4, gitgutter#diff_highlight#common_prefix('abcde', 'abcde')) call assert_equal(4, CommonPrefix('abcde', 'abcde'))
" different lengths " different lengths
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abcde', '+abx')) call assert_equal(-1, CommonPrefix('-abcde', '+abx'))
call assert_equal(1, gitgutter#diff_highlight#common_prefix('abcde', 'abx')) call assert_equal(1, CommonPrefix('abcde', 'abx'))
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abx', '+abcde')) call assert_equal(-1, CommonPrefix('-abx', '+abcde'))
call assert_equal(1, gitgutter#diff_highlight#common_prefix('abx', 'abcde')) call assert_equal(1, CommonPrefix('abx', 'abcde'))
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abcde', '+abc')) call assert_equal(-1, CommonPrefix('-abcde', '+abc'))
call assert_equal(2, gitgutter#diff_highlight#common_prefix('abcde', 'abc')) call assert_equal(2, CommonPrefix('abcde', 'abc'))
endfunction endfunction
function Test_common_suffix() function Test_common_suffix()
let sid = matchstr(execute('filter autoload/gitgutter/diff_highlight.vim scriptnames'), '\d\+')
let CommonSuffix = function("<SNR>".sid."_common_suffix")
" nothing in common " nothing in common
call assert_equal([6,6], gitgutter#diff_highlight#common_suffix('-abcde', '+pqrst', 0)) call assert_equal([6,6], CommonSuffix('-abcde', '+pqrst', 0))
" something in common " something in common
call assert_equal([3,3], gitgutter#diff_highlight#common_suffix('-abcde', '+pqcde', 0)) call assert_equal([3,3], CommonSuffix('-abcde', '+pqcde', 0))
" everything in common " everything in common
call assert_equal([5,5], gitgutter#diff_highlight#common_suffix('-abcde', '+abcde', 5)) call assert_equal([5,5], CommonSuffix('-abcde', '+abcde', 5))
" different lengths " different lengths
call assert_equal([4,2], gitgutter#diff_highlight#common_suffix('-abcde', '+xde', 0)) call assert_equal([4,2], CommonSuffix('-abcde', '+xde', 0))
call assert_equal([2,4], gitgutter#diff_highlight#common_suffix('-xde', '+abcde', 0)) call assert_equal([2,4], CommonSuffix('-xde', '+abcde', 0))
endfunction endfunction
@ -1096,18 +1119,24 @@ endfunction
function Test_lcs() function Test_lcs()
call assert_equal('', gitgutter#diff_highlight#lcs('', 'foo')) let sid = matchstr(execute('filter autoload/gitgutter/diff_highlight.vim scriptnames'), '\d\+')
call assert_equal('', gitgutter#diff_highlight#lcs('foo', '')) let Lcs = function("<SNR>".sid."_lcs")
call assert_equal('bar', gitgutter#diff_highlight#lcs('foobarbaz', 'bbart'))
call assert_equal('transaction', gitgutter#diff_highlight#lcs('transaction.unexplained_amount', 'amount(transaction)')) call assert_equal('', Lcs('', 'foo'))
call assert_equal('', Lcs('foo', ''))
call assert_equal('bar', Lcs('foobarbaz', 'bbart'))
call assert_equal('transaction', Lcs('transaction.unexplained_amount', 'amount(transaction)'))
endfunction endfunction
function Test_split() function Test_split()
call assert_equal(['foo', 'baz'], gitgutter#diff_highlight#split('foobarbaz', 'bar')) let sid = matchstr(execute('filter autoload/gitgutter/diff_highlight.vim scriptnames'), '\d\+')
call assert_equal(['', 'barbaz'], gitgutter#diff_highlight#split('foobarbaz', 'foo')) let Split = function("<SNR>".sid."_split")
call assert_equal(['foobar', ''], gitgutter#diff_highlight#split('foobarbaz', 'baz'))
call assert_equal(['1', '2'], gitgutter#diff_highlight#split('1~2', '~')) call assert_equal(['foo', 'baz'], Split('foobarbaz', 'bar'))
call assert_equal(['', 'barbaz'], Split('foobarbaz', 'foo'))
call assert_equal(['foobar', ''], Split('foobarbaz', 'baz'))
call assert_equal(['1', '2'], Split('1~2', '~'))
endfunction endfunction
@ -1126,3 +1155,12 @@ function Test_foldtext()
call assert_equal(0, gitgutter#fold#is_changed()) call assert_equal(0, gitgutter#fold#is_changed())
call assert_equal('+- 3 lines: a', gitgutter#fold#foldtext()) call assert_equal('+- 3 lines: a', gitgutter#fold#foldtext())
endfunction endfunction
function Test_assume_unchanged()
call system("git update-index --assume-unchanged fixture.txt")
unlet b:gitgutter.path " it was already set when fixture.txt was loaded in SetUp()
normal ggo*
call s:trigger_gitgutter()
call s:assert_signs([], 'fixture.txt')
endfunction