1
0
Fork 0
mirror of synced 2024-11-18 06:55:33 -05:00
ultimate-vim/sources_non_forked/vim-gitgutter/autoload/gitgutter.vim

266 lines
7.8 KiB
VimL
Raw Normal View History

" Primary functions {{{
2018-03-31 10:56:26 -04:00
function! gitgutter#all(force) abort
2018-07-19 08:52:53 -04:00
let visible = tabpagebuflist()
for bufnr in range(1, bufnr('$') + 1)
if buflisted(bufnr)
let file = expand('#'.bufnr.':p')
if !empty(file)
if index(visible, bufnr) != -1
call gitgutter#process_buffer(bufnr, a:force)
elseif a:force
call s:reset_tick(bufnr)
endif
endif
endif
endfor
endfunction
2018-03-31 10:56:26 -04:00
function! gitgutter#process_buffer(bufnr, force) abort
" NOTE a:bufnr is not necessarily the current buffer.
2022-08-08 09:45:56 -04:00
if gitgutter#utility#getbufvar(a:bufnr, 'enabled', -1) == -1
call gitgutter#utility#setbufvar(a:bufnr, 'enabled', g:gitgutter_enabled)
endif
2018-03-31 10:56:26 -04:00
if gitgutter#utility#is_active(a:bufnr)
2019-08-22 11:36:17 -04:00
if has('patch-7.4.1559')
let l:Callback = function('gitgutter#process_buffer', [a:bufnr, a:force])
else
let l:Callback = {'function': 'gitgutter#process_buffer', 'arguments': [a:bufnr, a:force]}
endif
let how = s:setup_path(a:bufnr, l:Callback)
if [how] == ['async'] " avoid string-to-number conversion if how is a number
return
endif
2018-03-31 10:56:26 -04:00
if a:force || s:has_fresh_changes(a:bufnr)
2020-04-25 21:56:16 -04:00
let diff = 'NOT SET'
2018-03-31 10:56:26 -04:00
try
2019-11-16 10:28:42 -05:00
let diff = gitgutter#diff#run_diff(a:bufnr, g:gitgutter_diff_relative_to, 0)
2018-03-31 10:56:26 -04:00
catch /gitgutter not tracked/
call gitgutter#debug#log('Not tracked: '.gitgutter#utility#file(a:bufnr))
2022-08-08 09:45:56 -04:00
catch /gitgutter assume unchanged/
call gitgutter#debug#log('Assume unchanged: '.gitgutter#utility#file(a:bufnr))
2018-03-31 10:56:26 -04:00
catch /gitgutter diff failed/
call gitgutter#debug#log('Diff failed: '.gitgutter#utility#file(a:bufnr))
call gitgutter#hunk#reset(a:bufnr)
endtry
2020-04-25 21:56:16 -04:00
if diff != 'async' && diff != 'NOT SET'
2018-03-31 10:56:26 -04:00
call gitgutter#diff#handler(a:bufnr, diff)
endif
2018-03-31 10:56:26 -04:00
endif
endif
endfunction
2018-03-31 10:56:26 -04:00
function! gitgutter#disable() abort
2022-08-08 09:45:56 -04:00
call s:toggle_each_buffer(0)
let g:gitgutter_enabled = 0
endfunction
function! gitgutter#enable() abort
call s:toggle_each_buffer(1)
let g:gitgutter_enabled = 1
endfunction
function s:toggle_each_buffer(enable)
2018-07-19 08:52:53 -04:00
for bufnr in range(1, bufnr('$') + 1)
if buflisted(bufnr)
let file = expand('#'.bufnr.':p')
if !empty(file)
2022-08-08 09:45:56 -04:00
if a:enable
call gitgutter#buffer_enable(bufnr)
else
call gitgutter#buffer_disable(bufnr)
end
2018-07-19 08:52:53 -04:00
endif
endif
endfor
endfunction
function! gitgutter#toggle() abort
if g:gitgutter_enabled
call gitgutter#disable()
else
call gitgutter#enable()
endif
endfunction
2019-03-08 06:04:56 -05:00
2022-08-08 09:45:56 -04:00
function! gitgutter#buffer_disable(...) abort
let bufnr = a:0 ? a:1 : bufnr('')
2019-03-08 06:04:56 -05:00
call gitgutter#utility#setbufvar(bufnr, 'enabled', 0)
call s:clear(bufnr)
endfunction
2022-08-08 09:45:56 -04:00
function! gitgutter#buffer_enable(...) abort
let bufnr = a:0 ? a:1 : bufnr('')
2019-03-08 06:04:56 -05:00
call gitgutter#utility#setbufvar(bufnr, 'enabled', 1)
call gitgutter#process_buffer(bufnr, 1)
endfunction
2022-08-08 09:45:56 -04:00
function! gitgutter#buffer_toggle(...) abort
let bufnr = a:0 ? a:1 : bufnr('')
if gitgutter#utility#getbufvar(bufnr, 'enabled', 1)
call gitgutter#buffer_disable(bufnr)
2019-03-08 06:04:56 -05:00
else
2022-08-08 09:45:56 -04:00
call gitgutter#buffer_enable(bufnr)
2019-03-08 06:04:56 -05:00
endif
endfunction
" }}}
2019-08-22 11:36:17 -04:00
function! gitgutter#setup_maps()
2018-06-14 06:31:12 -04:00
if !g:gitgutter_map_keys
return
endif
2019-08-22 11:36:17 -04:00
" Note hasmapto() and maparg() operate on the current buffer.
let bufnr = bufnr('')
if gitgutter#utility#getbufvar(bufnr, 'mapped', 0)
return
endif
2019-11-16 10:28:42 -05:00
if !hasmapto('<Plug>(GitGutterPrevHunk)') && maparg('[c', 'n') ==# ''
nmap <buffer> [c <Plug>(GitGutterPrevHunk)
2018-06-14 06:31:12 -04:00
endif
2019-11-16 10:28:42 -05:00
if !hasmapto('<Plug>(GitGutterNextHunk)') && maparg(']c', 'n') ==# ''
nmap <buffer> ]c <Plug>(GitGutterNextHunk)
2018-06-14 06:31:12 -04:00
endif
2019-11-16 10:28:42 -05:00
if !hasmapto('<Plug>(GitGutterStageHunk)', 'v') && maparg('<Leader>hs', 'x') ==# ''
xmap <buffer> <Leader>hs <Plug>(GitGutterStageHunk)
2019-08-22 11:36:17 -04:00
endif
2019-11-16 10:28:42 -05:00
if !hasmapto('<Plug>(GitGutterStageHunk)', 'n') && maparg('<Leader>hs', 'n') ==# ''
nmap <buffer> <Leader>hs <Plug>(GitGutterStageHunk)
2018-06-14 06:31:12 -04:00
endif
2019-11-16 10:28:42 -05:00
if !hasmapto('<Plug>(GitGutterUndoHunk)') && maparg('<Leader>hu', 'n') ==# ''
nmap <buffer> <Leader>hu <Plug>(GitGutterUndoHunk)
2018-06-14 06:31:12 -04:00
endif
2019-11-16 10:28:42 -05:00
if !hasmapto('<Plug>(GitGutterPreviewHunk)') && maparg('<Leader>hp', 'n') ==# ''
nmap <buffer> <Leader>hp <Plug>(GitGutterPreviewHunk)
2018-06-14 06:31:12 -04:00
endif
2019-11-16 10:28:42 -05:00
if !hasmapto('<Plug>(GitGutterTextObjectInnerPending)') && maparg('ic', 'o') ==# ''
omap <buffer> ic <Plug>(GitGutterTextObjectInnerPending)
2018-06-14 06:31:12 -04:00
endif
2019-11-16 10:28:42 -05:00
if !hasmapto('<Plug>(GitGutterTextObjectOuterPending)') && maparg('ac', 'o') ==# ''
omap <buffer> ac <Plug>(GitGutterTextObjectOuterPending)
2018-06-14 06:31:12 -04:00
endif
2019-11-16 10:28:42 -05:00
if !hasmapto('<Plug>(GitGutterTextObjectInnerVisual)') && maparg('ic', 'x') ==# ''
xmap <buffer> ic <Plug>(GitGutterTextObjectInnerVisual)
2018-06-14 06:31:12 -04:00
endif
2019-11-16 10:28:42 -05:00
if !hasmapto('<Plug>(GitGutterTextObjectOuterVisual)') && maparg('ac', 'x') ==# ''
xmap <buffer> ac <Plug>(GitGutterTextObjectOuterVisual)
2018-06-14 06:31:12 -04:00
endif
2019-08-22 11:36:17 -04:00
call gitgutter#utility#setbufvar(bufnr, 'mapped', 1)
endfunction
function! s:setup_path(bufnr, continuation)
2020-04-25 21:56:16 -04:00
if gitgutter#utility#has_repo_path(a:bufnr) | return | endif
2019-08-22 11:36:17 -04:00
return gitgutter#utility#set_repo_path(a:bufnr, a:continuation)
2018-06-14 06:31:12 -04:00
endfunction
2018-03-31 10:56:26 -04:00
function! s:has_fresh_changes(bufnr) abort
return getbufvar(a:bufnr, 'changedtick') != gitgutter#utility#getbufvar(a:bufnr, 'tick')
endfunction
2018-03-31 10:56:26 -04:00
function! s:reset_tick(bufnr) abort
call gitgutter#utility#setbufvar(a:bufnr, 'tick', 0)
endfunction
2018-03-31 10:56:26 -04:00
function! s:clear(bufnr)
call gitgutter#sign#clear_signs(a:bufnr)
call gitgutter#hunk#reset(a:bufnr)
call s:reset_tick(a:bufnr)
2019-08-22 11:36:17 -04:00
call gitgutter#utility#setbufvar(a:bufnr, 'path', '')
endfunction
2019-11-16 10:28:42 -05:00
" Note:
" - this runs synchronously
" - it ignores unsaved changes in buffers
" - it does not change to the repo root
2021-07-30 16:52:54 -04:00
function! gitgutter#quickfix(current_file)
2020-12-04 16:15:32 -05:00
let cmd = g:gitgutter_git_executable.' '.g:gitgutter_git_args.' rev-parse --show-cdup'
let path_to_repo = get(systemlist(cmd), 0, '')
if !empty(path_to_repo) && path_to_repo[-1:] != '/'
let path_to_repo .= '/'
endif
2019-11-16 10:28:42 -05:00
let locations = []
2020-12-04 16:15:32 -05:00
let cmd = g:gitgutter_git_executable.' '.g:gitgutter_git_args.' --no-pager'.
\ ' diff --no-ext-diff --no-color -U0'.
\ ' --src-prefix=a/'.path_to_repo.' --dst-prefix=b/'.path_to_repo.' '.
\ g:gitgutter_diff_args. ' '. g:gitgutter_diff_base
2021-07-30 16:52:54 -04:00
if a:current_file
let cmd = cmd.' -- '.expand('%:p')
endif
2019-11-16 10:28:42 -05:00
let diff = systemlist(cmd)
let lnum = 0
for line in diff
if line =~ '^diff --git [^"]'
let paths = line[11:]
let mid = (len(paths) - 1) / 2
let [fnamel, fnamer] = [paths[:mid-1], paths[mid+1:]]
let fname = fnamel ==# fnamer ? fnamel : fnamel[2:]
elseif line =~ '^diff --git "'
let [_, fnamel, _, fnamer] = split(line, '"')
let fname = fnamel ==# fnamer ? fnamel : fnamel[2:]
2021-10-11 05:30:43 -04:00
elseif line =~ '^diff --cc [^"]'
let fname = line[10:]
elseif line =~ '^diff --cc "'
let [_, fname] = split(line, '"')
2019-11-16 10:28:42 -05:00
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
2019-11-30 07:06:56 -05:00
if !g:gitgutter_use_location_list
call setqflist(locations)
else
call setloclist(0, locations)
endif
2019-11-16 10:28:42 -05:00
endfunction
2022-08-08 09:45:56 -04:00
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