mirror of
https://github.com/amix/vimrc
synced 2025-06-16 01:25:00 +08:00
Updated plugins
This commit is contained in:
@ -1,67 +1,54 @@
|
||||
let s:nomodeline = (v:version > 703 || (v:version == 703 && has('patch442'))) ? '<nomodeline>' : ''
|
||||
let s:t_string = type('')
|
||||
|
||||
" Primary functions {{{
|
||||
|
||||
function! gitgutter#all() abort
|
||||
for buffer_id in gitgutter#utility#dedup(tabpagebuflist())
|
||||
let file = expand('#' . buffer_id . ':p')
|
||||
function! gitgutter#all(force) abort
|
||||
for bufnr in s:uniq(tabpagebuflist())
|
||||
let file = expand('#'.bufnr.':p')
|
||||
if !empty(file)
|
||||
call gitgutter#process_buffer(buffer_id, 0)
|
||||
call gitgutter#init_buffer(bufnr)
|
||||
call gitgutter#process_buffer(bufnr, a:force)
|
||||
endif
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
" bufnr: (integer) the buffer to process.
|
||||
" realtime: (boolean) when truthy, do a realtime diff; otherwise do a disk-based diff.
|
||||
function! gitgutter#process_buffer(bufnr, realtime) abort
|
||||
call gitgutter#utility#use_known_shell()
|
||||
|
||||
call gitgutter#utility#set_buffer(a:bufnr)
|
||||
if gitgutter#utility#is_active()
|
||||
if g:gitgutter_sign_column_always
|
||||
call gitgutter#sign#add_dummy_sign()
|
||||
" Finds the file's path relative to the repo root.
|
||||
function! gitgutter#init_buffer(bufnr)
|
||||
if gitgutter#utility#is_active(a:bufnr)
|
||||
let p = gitgutter#utility#repo_path(a:bufnr, 0)
|
||||
if type(p) != s:t_string || empty(p)
|
||||
call gitgutter#utility#set_repo_path(a:bufnr)
|
||||
endif
|
||||
try
|
||||
if !a:realtime || gitgutter#utility#has_fresh_changes()
|
||||
let diff = gitgutter#diff#run_diff(a:realtime || gitgutter#utility#has_unsaved_changes(), 0)
|
||||
if diff != 'async'
|
||||
call gitgutter#handle_diff(diff)
|
||||
endif
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#process_buffer(bufnr, force) abort
|
||||
" NOTE a:bufnr is not necessarily the current buffer.
|
||||
|
||||
if gitgutter#utility#is_active(a:bufnr)
|
||||
if a:force || s:has_fresh_changes(a:bufnr)
|
||||
|
||||
let diff = ''
|
||||
try
|
||||
let diff = gitgutter#diff#run_diff(a:bufnr, 0)
|
||||
catch /gitgutter not tracked/
|
||||
call gitgutter#debug#log('Not tracked: '.gitgutter#utility#file(a:bufnr))
|
||||
catch /gitgutter diff failed/
|
||||
call gitgutter#debug#log('Diff failed: '.gitgutter#utility#file(a:bufnr))
|
||||
call gitgutter#hunk#reset(a:bufnr)
|
||||
endtry
|
||||
|
||||
if diff != 'async'
|
||||
call gitgutter#diff#handler(a:bufnr, diff)
|
||||
endif
|
||||
catch /diff failed/
|
||||
call gitgutter#debug#log('diff failed')
|
||||
call gitgutter#hunk#reset()
|
||||
endtry
|
||||
execute "silent doautocmd" s:nomodeline "User GitGutter"
|
||||
else
|
||||
call gitgutter#hunk#reset()
|
||||
endif
|
||||
|
||||
call gitgutter#utility#restore_shell()
|
||||
endif
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#handle_diff(diff) abort
|
||||
call gitgutter#debug#log(a:diff)
|
||||
|
||||
call gitgutter#utility#setbufvar(gitgutter#utility#bufnr(), 'tracked', 1)
|
||||
|
||||
call gitgutter#hunk#set_hunks(gitgutter#diff#parse_diff(a:diff))
|
||||
let modified_lines = gitgutter#diff#process_hunks(gitgutter#hunk#hunks())
|
||||
|
||||
if len(modified_lines) > g:gitgutter_max_signs
|
||||
call gitgutter#utility#warn_once('exceeded maximum number of signs (configured by g:gitgutter_max_signs).', 'max_signs')
|
||||
call gitgutter#sign#clear_signs()
|
||||
return
|
||||
endif
|
||||
|
||||
if g:gitgutter_signs || g:gitgutter_highlight_lines
|
||||
call gitgutter#sign#update_signs(modified_lines)
|
||||
endif
|
||||
|
||||
call gitgutter#utility#save_last_seen_change()
|
||||
endfunction
|
||||
|
||||
function! gitgutter#disable() abort
|
||||
" get list of all buffers (across all tabs)
|
||||
let buflist = []
|
||||
@ -69,13 +56,10 @@ function! gitgutter#disable() abort
|
||||
call extend(buflist, tabpagebuflist(i + 1))
|
||||
endfor
|
||||
|
||||
for buffer_id in gitgutter#utility#dedup(buflist)
|
||||
let file = expand('#' . buffer_id . ':p')
|
||||
for bufnr in s:uniq(buflist)
|
||||
let file = expand('#'.bufnr.':p')
|
||||
if !empty(file)
|
||||
call gitgutter#utility#set_buffer(buffer_id)
|
||||
call gitgutter#sign#clear_signs()
|
||||
call gitgutter#sign#remove_dummy_sign(1)
|
||||
call gitgutter#hunk#reset()
|
||||
call s:clear(bufnr)
|
||||
endif
|
||||
endfor
|
||||
|
||||
@ -84,7 +68,7 @@ endfunction
|
||||
|
||||
function! gitgutter#enable() abort
|
||||
let g:gitgutter_enabled = 1
|
||||
call gitgutter#all()
|
||||
call gitgutter#all(1)
|
||||
endfunction
|
||||
|
||||
function! gitgutter#toggle() abort
|
||||
@ -97,163 +81,33 @@ endfunction
|
||||
|
||||
" }}}
|
||||
|
||||
" Line highlights {{{
|
||||
|
||||
function! gitgutter#line_highlights_disable() abort
|
||||
let g:gitgutter_highlight_lines = 0
|
||||
call gitgutter#highlight#define_sign_line_highlights()
|
||||
|
||||
if !g:gitgutter_signs
|
||||
call gitgutter#sign#clear_signs()
|
||||
call gitgutter#sign#remove_dummy_sign(0)
|
||||
endif
|
||||
|
||||
redraw!
|
||||
function! s:has_fresh_changes(bufnr) abort
|
||||
return getbufvar(a:bufnr, 'changedtick') != gitgutter#utility#getbufvar(a:bufnr, 'tick')
|
||||
endfunction
|
||||
|
||||
function! gitgutter#line_highlights_enable() abort
|
||||
let old_highlight_lines = g:gitgutter_highlight_lines
|
||||
|
||||
let g:gitgutter_highlight_lines = 1
|
||||
call gitgutter#highlight#define_sign_line_highlights()
|
||||
|
||||
if !old_highlight_lines && !g:gitgutter_signs
|
||||
call gitgutter#all()
|
||||
endif
|
||||
|
||||
redraw!
|
||||
function! s:reset_tick(bufnr) abort
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'tick', 0)
|
||||
endfunction
|
||||
|
||||
function! gitgutter#line_highlights_toggle() abort
|
||||
if g:gitgutter_highlight_lines
|
||||
call gitgutter#line_highlights_disable()
|
||||
else
|
||||
call gitgutter#line_highlights_enable()
|
||||
endif
|
||||
function! s:clear(bufnr)
|
||||
call gitgutter#sign#clear_signs(a:bufnr)
|
||||
call gitgutter#sign#remove_dummy_sign(a:bufnr, 1)
|
||||
call gitgutter#hunk#reset(a:bufnr)
|
||||
call s:reset_tick(a:bufnr)
|
||||
endfunction
|
||||
|
||||
" }}}
|
||||
|
||||
" Signs {{{
|
||||
|
||||
function! gitgutter#signs_enable() abort
|
||||
let old_signs = g:gitgutter_signs
|
||||
|
||||
let g:gitgutter_signs = 1
|
||||
call gitgutter#highlight#define_sign_text_highlights()
|
||||
|
||||
if !old_signs && !g:gitgutter_highlight_lines
|
||||
call gitgutter#all()
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#signs_disable() abort
|
||||
let g:gitgutter_signs = 0
|
||||
call gitgutter#highlight#define_sign_text_highlights()
|
||||
|
||||
if !g:gitgutter_highlight_lines
|
||||
call gitgutter#sign#clear_signs()
|
||||
call gitgutter#sign#remove_dummy_sign(0)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#signs_toggle() abort
|
||||
if g:gitgutter_signs
|
||||
call gitgutter#signs_disable()
|
||||
else
|
||||
call gitgutter#signs_enable()
|
||||
endif
|
||||
endfunction
|
||||
|
||||
" }}}
|
||||
|
||||
" Hunks {{{
|
||||
|
||||
function! gitgutter#stage_hunk() abort
|
||||
call gitgutter#utility#use_known_shell()
|
||||
if gitgutter#utility#is_active()
|
||||
" Ensure the working copy of the file is up to date.
|
||||
" It doesn't make sense to stage a hunk otherwise.
|
||||
noautocmd silent write
|
||||
let diff = gitgutter#diff#run_diff(0, 1)
|
||||
call gitgutter#handle_diff(diff)
|
||||
|
||||
if empty(gitgutter#hunk#current_hunk())
|
||||
call gitgutter#utility#warn('cursor is not in a hunk')
|
||||
else
|
||||
let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk(diff, 'stage')
|
||||
call gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file(g:gitgutter_git_executable.' apply --cached --unidiff-zero - '), diff_for_hunk)
|
||||
|
||||
" refresh gitgutter's view of buffer
|
||||
silent execute "GitGutter"
|
||||
endif
|
||||
|
||||
silent! call repeat#set("\<Plug>GitGutterStageHunk", -1)<CR>
|
||||
endif
|
||||
call gitgutter#utility#restore_shell()
|
||||
endfunction
|
||||
|
||||
function! gitgutter#undo_hunk() abort
|
||||
call gitgutter#utility#use_known_shell()
|
||||
if gitgutter#utility#is_active()
|
||||
" Ensure the working copy of the file is up to date.
|
||||
" It doesn't make sense to stage a hunk otherwise.
|
||||
noautocmd silent write
|
||||
let diff = gitgutter#diff#run_diff(0, 1)
|
||||
call gitgutter#handle_diff(diff)
|
||||
|
||||
if empty(gitgutter#hunk#current_hunk())
|
||||
call gitgutter#utility#warn('cursor is not in a hunk')
|
||||
else
|
||||
let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk(diff, 'undo')
|
||||
call gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file(g:gitgutter_git_executable.' apply --reverse --unidiff-zero - '), diff_for_hunk)
|
||||
|
||||
" reload file preserving screen line position
|
||||
" CTRL-Y and CTRL-E treat negative counts as positive counts.
|
||||
let x = line('w0')
|
||||
silent edit
|
||||
let y = line('w0')
|
||||
let z = x - y
|
||||
if z > 0
|
||||
execute "normal! ".z."\<C-E>"
|
||||
else
|
||||
execute "normal! ".z."\<C-Y>"
|
||||
if exists('*uniq') " Vim 7.4.218
|
||||
function! s:uniq(list)
|
||||
return uniq(sort(a:list))
|
||||
endfunction
|
||||
else
|
||||
function! s:uniq(list)
|
||||
let processed = []
|
||||
for e in a:list
|
||||
if index(processed, e) == -1
|
||||
call add(processed, e)
|
||||
endif
|
||||
endif
|
||||
|
||||
silent! call repeat#set("\<Plug>GitGutterUndoHunk", -1)<CR>
|
||||
endif
|
||||
call gitgutter#utility#restore_shell()
|
||||
endfunction
|
||||
|
||||
function! gitgutter#preview_hunk() abort
|
||||
call gitgutter#utility#use_known_shell()
|
||||
if gitgutter#utility#is_active()
|
||||
" Ensure the working copy of the file is up to date.
|
||||
" It doesn't make sense to stage a hunk otherwise.
|
||||
noautocmd silent write
|
||||
let diff = gitgutter#diff#run_diff(0, 1)
|
||||
call gitgutter#handle_diff(diff)
|
||||
|
||||
if empty(gitgutter#hunk#current_hunk())
|
||||
call gitgutter#utility#warn('cursor is not in a hunk')
|
||||
else
|
||||
let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk(diff, 'preview')
|
||||
|
||||
silent! wincmd P
|
||||
if !&previewwindow
|
||||
noautocmd execute 'bo' &previewheight 'new'
|
||||
set previewwindow
|
||||
endif
|
||||
|
||||
setlocal noro modifiable filetype=diff buftype=nofile bufhidden=delete noswapfile
|
||||
execute "%delete_"
|
||||
call append(0, split(diff_for_hunk, "\n"))
|
||||
|
||||
noautocmd wincmd p
|
||||
endif
|
||||
endif
|
||||
call gitgutter#utility#restore_shell()
|
||||
endfunction
|
||||
|
||||
" }}}
|
||||
endfor
|
||||
return processed
|
||||
endfunction
|
||||
endif
|
||||
|
@ -11,10 +11,13 @@ function! gitgutter#async#available()
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#async#execute(cmd) abort
|
||||
function! gitgutter#async#execute(cmd, bufnr, handler) abort
|
||||
call gitgutter#debug#log('[async] '.a:cmd)
|
||||
|
||||
let options = {
|
||||
\ 'stdoutbuffer': [],
|
||||
\ 'buffer': gitgutter#utility#bufnr()
|
||||
\ 'buffer': a:bufnr,
|
||||
\ 'handler': a:handler
|
||||
\ }
|
||||
let command = s:build_command(a:cmd)
|
||||
|
||||
@ -58,33 +61,13 @@ function! s:on_stdout_nvim(_job_id, data, _event) dict abort
|
||||
endfunction
|
||||
|
||||
function! s:on_stderr_nvim(_job_id, _data, _event) dict abort
|
||||
" Backward compatibility for nvim < 0.2.0
|
||||
if !has('nvim-0.2.0')
|
||||
let current_buffer = gitgutter#utility#bufnr()
|
||||
call gitgutter#utility#set_buffer(self.buffer)
|
||||
if gitgutter#utility#is_active()
|
||||
call gitgutter#hunk#reset()
|
||||
endif
|
||||
call gitgutter#utility#set_buffer(current_buffer)
|
||||
return
|
||||
endif
|
||||
|
||||
call s:buffer_exec(self.buffer, function('gitgutter#hunk#reset'))
|
||||
call self.handler.err(self.buffer)
|
||||
endfunction
|
||||
|
||||
function! s:on_exit_nvim(_job_id, _data, _event) dict abort
|
||||
" Backward compatibility for nvim < 0.2.0
|
||||
if !has('nvim-0.2.0')
|
||||
let current_buffer = gitgutter#utility#bufnr()
|
||||
call gitgutter#utility#set_buffer(self.buffer)
|
||||
if gitgutter#utility#is_active()
|
||||
call gitgutter#handle_diff(gitgutter#utility#stringify(self.stdoutbuffer))
|
||||
endif
|
||||
call gitgutter#utility#set_buffer(current_buffer)
|
||||
return
|
||||
function! s:on_exit_nvim(_job_id, exit_code, _event) dict abort
|
||||
if !a:exit_code
|
||||
call self.handler.out(self.buffer, join(self.stdoutbuffer, "\n"))
|
||||
endif
|
||||
|
||||
call s:buffer_exec(self.buffer, function('gitgutter#handle_diff', [gitgutter#utility#stringify(self.stdoutbuffer)]))
|
||||
endfunction
|
||||
|
||||
|
||||
@ -92,22 +75,15 @@ function! s:on_stdout_vim(_channel, data) dict abort
|
||||
call add(self.stdoutbuffer, a:data)
|
||||
endfunction
|
||||
|
||||
function! s:on_stderr_vim(_channel, _data) dict abort
|
||||
call s:buffer_exec(self.buffer, function('gitgutter#hunk#reset'))
|
||||
function! s:on_stderr_vim(channel, _data) dict abort
|
||||
call self.handler.err(self.buffer)
|
||||
try
|
||||
call ch_close(a:channel) " so close_cb and its 'out' handler are not triggered
|
||||
catch /E906/
|
||||
" noop
|
||||
endtry
|
||||
endfunction
|
||||
|
||||
function! s:on_exit_vim(_channel) dict abort
|
||||
call s:buffer_exec(self.buffer, function('gitgutter#handle_diff', [gitgutter#utility#stringify(self.stdoutbuffer)]))
|
||||
endfunction
|
||||
|
||||
|
||||
function! s:buffer_exec(buffer, fn)
|
||||
let current_buffer = gitgutter#utility#bufnr()
|
||||
call gitgutter#utility#set_buffer(a:buffer)
|
||||
|
||||
if gitgutter#utility#is_active()
|
||||
call a:fn()
|
||||
endif
|
||||
|
||||
call gitgutter#utility#set_buffer(current_buffer)
|
||||
call self.handler.out(self.buffer, join(self.stdoutbuffer, "\n"))
|
||||
endfunction
|
||||
|
@ -12,67 +12,67 @@ function! gitgutter#debug#debug()
|
||||
setlocal bufhidden=delete
|
||||
setlocal noswapfile
|
||||
|
||||
call gitgutter#debug#vim_version()
|
||||
call gitgutter#debug#separator()
|
||||
call s:vim_version()
|
||||
call s:separator()
|
||||
|
||||
call gitgutter#debug#git_version()
|
||||
call gitgutter#debug#separator()
|
||||
call s:git_version()
|
||||
call s:separator()
|
||||
|
||||
call gitgutter#debug#grep_version()
|
||||
call gitgutter#debug#separator()
|
||||
call s:grep_version()
|
||||
call s:separator()
|
||||
|
||||
call gitgutter#debug#option('updatetime')
|
||||
call gitgutter#debug#option('shell')
|
||||
call gitgutter#debug#option('shellcmdflag')
|
||||
call gitgutter#debug#option('shellpipe')
|
||||
call gitgutter#debug#option('shellquote')
|
||||
call gitgutter#debug#option('shellredir')
|
||||
call gitgutter#debug#option('shellslash')
|
||||
call gitgutter#debug#option('shelltemp')
|
||||
call gitgutter#debug#option('shelltype')
|
||||
call gitgutter#debug#option('shellxescape')
|
||||
call gitgutter#debug#option('shellxquote')
|
||||
call s:option('updatetime')
|
||||
call s:option('shell')
|
||||
call s:option('shellcmdflag')
|
||||
call s:option('shellpipe')
|
||||
call s:option('shellquote')
|
||||
call s:option('shellredir')
|
||||
call s:option('shellslash')
|
||||
call s:option('shelltemp')
|
||||
call s:option('shelltype')
|
||||
call s:option('shellxescape')
|
||||
call s:option('shellxquote')
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#debug#separator()
|
||||
call gitgutter#debug#output('')
|
||||
function! s:separator()
|
||||
call s:output('')
|
||||
endfunction
|
||||
|
||||
function! gitgutter#debug#vim_version()
|
||||
function! s:vim_version()
|
||||
redir => version_info
|
||||
silent execute 'version'
|
||||
redir END
|
||||
call gitgutter#debug#output(split(version_info, '\n')[0:2])
|
||||
call s:output(split(version_info, '\n')[0:2])
|
||||
endfunction
|
||||
|
||||
function! gitgutter#debug#git_version()
|
||||
function! s:git_version()
|
||||
let v = system(g:gitgutter_git_executable.' --version')
|
||||
call gitgutter#debug#output( substitute(v, '\n$', '', '') )
|
||||
call s:output( substitute(v, '\n$', '', '') )
|
||||
endfunction
|
||||
|
||||
function! gitgutter#debug#grep_version()
|
||||
function! s:grep_version()
|
||||
let v = system('grep --version')
|
||||
call gitgutter#debug#output( substitute(v, '\n$', '', '') )
|
||||
call s:output( substitute(v, '\n$', '', '') )
|
||||
|
||||
let v = system('grep --help')
|
||||
call gitgutter#debug#output( substitute(v, '\%x00', '', 'g') )
|
||||
call s:output( substitute(v, '\%x00', '', 'g') )
|
||||
endfunction
|
||||
|
||||
function! gitgutter#debug#option(name)
|
||||
function! s:option(name)
|
||||
if exists('+' . a:name)
|
||||
let v = eval('&' . a:name)
|
||||
call gitgutter#debug#output(a:name . '=' . v)
|
||||
call s:output(a:name . '=' . v)
|
||||
" redir => output
|
||||
" silent execute "verbose set " . a:name . "?"
|
||||
" redir END
|
||||
" call gitgutter#debug#output(a:name . '=' . output)
|
||||
" call s:output(a:name . '=' . output)
|
||||
else
|
||||
call gitgutter#debug#output(a:name . ' [n/a]')
|
||||
call s:output(a:name . ' [n/a]')
|
||||
end
|
||||
endfunction
|
||||
|
||||
function! gitgutter#debug#output(text)
|
||||
function! s:output(text)
|
||||
call append(line('$'), a:text)
|
||||
endfunction
|
||||
|
||||
|
@ -1,152 +1,176 @@
|
||||
if exists('g:gitgutter_grep_command')
|
||||
let s:grep_available = 1
|
||||
let s:grep_command = g:gitgutter_grep_command
|
||||
else
|
||||
let s:grep_available = executable('grep')
|
||||
if s:grep_available
|
||||
let s:grep_command = 'grep'
|
||||
if $GREP_OPTIONS =~# '--color=always'
|
||||
let s:grep_command .= ' --color=never'
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
let s:nomodeline = (v:version > 703 || (v:version == 703 && has('patch442'))) ? '<nomodeline>' : ''
|
||||
|
||||
let s:hunk_re = '^@@ -\(\d\+\),\?\(\d*\) +\(\d\+\),\?\(\d*\) @@'
|
||||
|
||||
let s:c_flag = gitgutter#utility#git_supports_command_line_config_override()
|
||||
" True for git v1.7.2+.
|
||||
function! s:git_supports_command_line_config_override() abort
|
||||
call system(g:gitgutter_git_executable.' -c foo.bar=baz --version')
|
||||
return !v:shell_error
|
||||
endfunction
|
||||
|
||||
let s:c_flag = s:git_supports_command_line_config_override()
|
||||
|
||||
|
||||
let s:temp_index = tempname()
|
||||
let s:temp_buffer = tempname()
|
||||
|
||||
" Returns a diff of the buffer.
|
||||
"
|
||||
" The way to get the diff depends on whether the buffer is saved or unsaved.
|
||||
" The buffer contents is not the same as the file on disk so we need to pass
|
||||
" two instances of the file to git-diff:
|
||||
"
|
||||
" * Saved: the buffer contents is the same as the file on disk in the working
|
||||
" tree so we simply do:
|
||||
" git diff myfileA myfileB
|
||||
"
|
||||
" git diff myfile
|
||||
" where myfileA comes from
|
||||
"
|
||||
" * Unsaved: the buffer contents is not the same as the file on disk so we
|
||||
" need to pass two instances of the file to git-diff:
|
||||
" git show :myfile > myfileA
|
||||
"
|
||||
" git diff myfileA myfileB
|
||||
"
|
||||
" The first instance is the file in the index which we obtain with:
|
||||
"
|
||||
" git show :myfile > myfileA
|
||||
"
|
||||
" The second instance is the buffer contents. Ideally we would pass this to
|
||||
" git-diff on stdin via the second argument to vim's system() function.
|
||||
" Unfortunately git-diff does not do CRLF conversion for input received on
|
||||
" stdin, and git-show never performs CRLF conversion, so repos with CRLF
|
||||
" conversion report that every line is modified due to mismatching EOLs.
|
||||
"
|
||||
" Instead, we write the buffer contents to a temporary file - myfileB in this
|
||||
" example. Note the file extension must be preserved for the CRLF
|
||||
" conversion to work.
|
||||
"
|
||||
" Before diffing a buffer for the first time, we check whether git knows about
|
||||
" the file:
|
||||
"
|
||||
" git ls-files --error-unmatch myfile
|
||||
" and myfileB is the buffer contents.
|
||||
"
|
||||
" After running the diff we pass it through grep where available to reduce
|
||||
" subsequent processing by the plugin. If grep is not available the plugin
|
||||
" does the filtering instead.
|
||||
function! gitgutter#diff#run_diff(realtime, preserve_full_diff) abort
|
||||
"
|
||||
"
|
||||
" Regarding line endings:
|
||||
"
|
||||
" git-show does not convert line endings.
|
||||
" git-diff FILE FILE does convert line endings for the given files.
|
||||
"
|
||||
" If a file has CRLF line endings and git's core.autocrlf is true,
|
||||
" the file in git's object store will have LF line endings. Writing
|
||||
" it out via git-show will produce a file with LF line endings.
|
||||
"
|
||||
" If this last file is one of the files passed to git-diff, git-diff will
|
||||
" convert its line endings to CRLF before diffing -- which is what we want --
|
||||
" but also by default output a warning on stderr.
|
||||
"
|
||||
" warning: LF will be replace by CRLF in <temp file>.
|
||||
" The file will have its original line endings in your working directory.
|
||||
"
|
||||
" When running the diff asynchronously, the warning message triggers the stderr
|
||||
" callbacks which assume the overall command has failed and reset all the
|
||||
" signs. As this is not what we want, and we can safely ignore the warning,
|
||||
" we turn it off by passing the '-c "core.safecrlf=false"' argument to
|
||||
" git-diff.
|
||||
"
|
||||
" When writing the temporary files we preserve the original file's extension
|
||||
" so that repos using .gitattributes to control EOL conversion continue to
|
||||
" convert correctly.
|
||||
function! gitgutter#diff#run_diff(bufnr, preserve_full_diff) abort
|
||||
while gitgutter#utility#repo_path(a:bufnr, 0) == -1
|
||||
sleep 5m
|
||||
endwhile
|
||||
|
||||
if gitgutter#utility#repo_path(a:bufnr, 0) == -2
|
||||
throw 'gitgutter not tracked'
|
||||
endif
|
||||
|
||||
|
||||
" Wrap compound commands in parentheses to make Windows happy.
|
||||
" bash doesn't mind the parentheses.
|
||||
let cmd = '('
|
||||
|
||||
let bufnr = gitgutter#utility#bufnr()
|
||||
let tracked = gitgutter#utility#getbufvar(bufnr, 'tracked', 0) " i.e. tracked by git
|
||||
if !tracked
|
||||
" Don't bother trying to realtime-diff an untracked file.
|
||||
" NOTE: perhaps we should pull this guard up to the caller?
|
||||
if a:realtime
|
||||
throw 'diff failed'
|
||||
else
|
||||
let cmd .= g:gitgutter_git_executable.' ls-files --error-unmatch '.gitgutter#utility#shellescape(gitgutter#utility#filename()).' && ('
|
||||
endif
|
||||
" Append buffer number to avoid race conditions between writing and reading
|
||||
" the files when asynchronously processing multiple buffers.
|
||||
"
|
||||
" Without the buffer number, index_file would have a race in the shell
|
||||
" between the second process writing it (with git-show) and the first
|
||||
" reading it (with git-diff).
|
||||
let index_file = s:temp_index.'.'.a:bufnr
|
||||
|
||||
" Without the buffer number, buff_file would have a race between the
|
||||
" second gitgutter#process_buffer() writing the file (synchronously, below)
|
||||
" and the first gitgutter#process_buffer()'s async job reading it (with
|
||||
" git-diff).
|
||||
let buff_file = s:temp_buffer.'.'.a:bufnr
|
||||
|
||||
let extension = gitgutter#utility#extension(a:bufnr)
|
||||
if !empty(extension)
|
||||
let index_file .= '.'.extension
|
||||
let buff_file .= '.'.extension
|
||||
endif
|
||||
|
||||
if a:realtime
|
||||
let blob_name = g:gitgutter_diff_base.':'.gitgutter#utility#shellescape(gitgutter#utility#file_relative_to_repo_root())
|
||||
let blob_file = s:temp_index
|
||||
let buff_file = s:temp_buffer
|
||||
let extension = gitgutter#utility#extension()
|
||||
if !empty(extension)
|
||||
let blob_file .= '.'.extension
|
||||
let buff_file .= '.'.extension
|
||||
endif
|
||||
let cmd .= g:gitgutter_git_executable.' show '.blob_name.' > '.blob_file.' && '
|
||||
" Write file from index to temporary file.
|
||||
let index_name = g:gitgutter_diff_base.':'.gitgutter#utility#repo_path(a:bufnr, 1)
|
||||
let cmd .= g:gitgutter_git_executable.' --no-pager show '.index_name.' > '.index_file.' && '
|
||||
|
||||
" Writing the whole buffer resets the '[ and '] marks and also the
|
||||
" 'modified' flag (if &cpoptions includes '+'). These are unwanted
|
||||
" side-effects so we save and restore the values ourselves.
|
||||
let modified = getbufvar(bufnr, "&mod")
|
||||
let op_mark_start = getpos("'[")
|
||||
let op_mark_end = getpos("']")
|
||||
" Write buffer to temporary file.
|
||||
" Note: this is synchronous.
|
||||
call s:write_buffer(a:bufnr, buff_file)
|
||||
|
||||
let current_buffer = bufnr('')
|
||||
execute 'buffer '.bufnr
|
||||
execute 'keepalt noautocmd silent write!' buff_file
|
||||
execute 'buffer '.current_buffer
|
||||
|
||||
call setbufvar(bufnr, "&mod", modified)
|
||||
call setpos("'[", op_mark_start)
|
||||
call setpos("']", op_mark_end)
|
||||
endif
|
||||
|
||||
let cmd .= g:gitgutter_git_executable
|
||||
" Call git-diff with the temporary files.
|
||||
let cmd .= g:gitgutter_git_executable.' --no-pager'
|
||||
if s:c_flag
|
||||
let cmd .= ' -c "diff.autorefreshindex=0"'
|
||||
let cmd .= ' -c "diff.noprefix=false"'
|
||||
let cmd .= ' -c "core.safecrlf=false"'
|
||||
endif
|
||||
let cmd .= ' diff --no-ext-diff --no-color -U0 '.g:gitgutter_diff_args.' '
|
||||
let cmd .= ' diff --no-ext-diff --no-color -U0 '.g:gitgutter_diff_args.' -- '.index_file.' '.buff_file
|
||||
|
||||
if a:realtime
|
||||
let cmd .= ' -- '.blob_file.' '.buff_file
|
||||
else
|
||||
let cmd .= g:gitgutter_diff_base.' -- '.gitgutter#utility#shellescape(gitgutter#utility#filename())
|
||||
" Pipe git-diff output into grep.
|
||||
if !a:preserve_full_diff && !empty(g:gitgutter_grep)
|
||||
let cmd .= ' | '.g:gitgutter_grep.' '.gitgutter#utility#shellescape('^@@ ')
|
||||
endif
|
||||
|
||||
if !a:preserve_full_diff && s:grep_available
|
||||
let cmd .= ' | '.s:grep_command.' '.gitgutter#utility#shellescape('^@@ ')
|
||||
endif
|
||||
|
||||
if (!a:preserve_full_diff && s:grep_available) || a:realtime
|
||||
" grep exits with 1 when no matches are found; diff exits with 1 when
|
||||
" differences are found. However we want to treat non-matches and
|
||||
" differences as non-erroneous behaviour; so we OR the command with one
|
||||
" which always exits with success (0).
|
||||
let cmd .= ' || exit 0'
|
||||
endif
|
||||
" grep exits with 1 when no matches are found; git-diff exits with 1 when
|
||||
" differences are found. However we want to treat non-matches and
|
||||
" differences as non-erroneous behaviour; so we OR the command with one
|
||||
" which always exits with success (0).
|
||||
let cmd .= ' || exit 0'
|
||||
|
||||
let cmd .= ')'
|
||||
|
||||
if !tracked
|
||||
let cmd .= ')'
|
||||
endif
|
||||
let cmd = gitgutter#utility#cd_cmd(a:bufnr, cmd)
|
||||
|
||||
let cmd = gitgutter#utility#command_in_directory_of_file(cmd)
|
||||
|
||||
if g:gitgutter_async && gitgutter#async#available() && !a:preserve_full_diff
|
||||
call gitgutter#async#execute(cmd)
|
||||
if g:gitgutter_async && gitgutter#async#available()
|
||||
call gitgutter#async#execute(cmd, a:bufnr, {
|
||||
\ 'out': function('gitgutter#diff#handler'),
|
||||
\ 'err': function('gitgutter#hunk#reset'),
|
||||
\ })
|
||||
return 'async'
|
||||
|
||||
else
|
||||
let diff = gitgutter#utility#system(cmd)
|
||||
|
||||
if gitgutter#utility#shell_error()
|
||||
" A shell error indicates the file is not tracked by git (unless something bizarre is going on).
|
||||
throw 'diff failed'
|
||||
if v:shell_error
|
||||
call gitgutter#debug#log(diff)
|
||||
throw 'gitgutter diff failed'
|
||||
endif
|
||||
|
||||
return diff
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#diff#handler(bufnr, diff) abort
|
||||
call gitgutter#debug#log(a:diff)
|
||||
|
||||
call gitgutter#hunk#set_hunks(a:bufnr, gitgutter#diff#parse_diff(a:diff))
|
||||
let modified_lines = gitgutter#diff#process_hunks(a:bufnr, gitgutter#hunk#hunks(a:bufnr))
|
||||
|
||||
let signs_count = len(modified_lines)
|
||||
if signs_count > g:gitgutter_max_signs
|
||||
call gitgutter#utility#warn_once(a:bufnr, printf(
|
||||
\ 'exceeded maximum number of signs (%d > %d, configured by g:gitgutter_max_signs).',
|
||||
\ signs_count, g:gitgutter_max_signs), 'max_signs')
|
||||
call gitgutter#sign#clear_signs(a:bufnr)
|
||||
|
||||
else
|
||||
if g:gitgutter_signs || g:gitgutter_highlight_lines
|
||||
call gitgutter#sign#update_signs(a:bufnr, modified_lines)
|
||||
endif
|
||||
endif
|
||||
|
||||
call s:save_last_seen_change(a:bufnr)
|
||||
if exists('#User#GitGutter')
|
||||
let g:gitgutter_hook_context = {'bufnr': a:bufnr}
|
||||
execute 'doautocmd' s:nomodeline 'User GitGutter'
|
||||
unlet g:gitgutter_hook_context
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#diff#parse_diff(diff) abort
|
||||
let hunks = []
|
||||
for line in split(a:diff, '\n')
|
||||
@ -171,69 +195,71 @@ function! gitgutter#diff#parse_hunk(line) abort
|
||||
end
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#process_hunks(hunks) abort
|
||||
" This function is public so it may be used by other plugins
|
||||
" e.g. vim-signature.
|
||||
function! gitgutter#diff#process_hunks(bufnr, hunks) abort
|
||||
let modified_lines = []
|
||||
for hunk in a:hunks
|
||||
call extend(modified_lines, gitgutter#diff#process_hunk(hunk))
|
||||
call extend(modified_lines, s:process_hunk(a:bufnr, hunk))
|
||||
endfor
|
||||
return modified_lines
|
||||
endfunction
|
||||
|
||||
" Returns [ [<line_number (number)>, <name (string)>], ...]
|
||||
function! gitgutter#diff#process_hunk(hunk) abort
|
||||
function! s:process_hunk(bufnr, hunk) abort
|
||||
let modifications = []
|
||||
let from_line = a:hunk[0]
|
||||
let from_count = a:hunk[1]
|
||||
let to_line = a:hunk[2]
|
||||
let to_count = a:hunk[3]
|
||||
|
||||
if gitgutter#diff#is_added(from_count, to_count)
|
||||
call gitgutter#diff#process_added(modifications, from_count, to_count, to_line)
|
||||
call gitgutter#hunk#increment_lines_added(to_count)
|
||||
if s:is_added(from_count, to_count)
|
||||
call s:process_added(modifications, from_count, to_count, to_line)
|
||||
call gitgutter#hunk#increment_lines_added(a:bufnr, to_count)
|
||||
|
||||
elseif gitgutter#diff#is_removed(from_count, to_count)
|
||||
call gitgutter#diff#process_removed(modifications, from_count, to_count, to_line)
|
||||
call gitgutter#hunk#increment_lines_removed(from_count)
|
||||
elseif s:is_removed(from_count, to_count)
|
||||
call s:process_removed(modifications, from_count, to_count, to_line)
|
||||
call gitgutter#hunk#increment_lines_removed(a:bufnr, from_count)
|
||||
|
||||
elseif gitgutter#diff#is_modified(from_count, to_count)
|
||||
call gitgutter#diff#process_modified(modifications, from_count, to_count, to_line)
|
||||
call gitgutter#hunk#increment_lines_modified(to_count)
|
||||
elseif s:is_modified(from_count, to_count)
|
||||
call s:process_modified(modifications, from_count, to_count, to_line)
|
||||
call gitgutter#hunk#increment_lines_modified(a:bufnr, to_count)
|
||||
|
||||
elseif gitgutter#diff#is_modified_and_added(from_count, to_count)
|
||||
call gitgutter#diff#process_modified_and_added(modifications, from_count, to_count, to_line)
|
||||
call gitgutter#hunk#increment_lines_added(to_count - from_count)
|
||||
call gitgutter#hunk#increment_lines_modified(from_count)
|
||||
elseif s:is_modified_and_added(from_count, to_count)
|
||||
call s:process_modified_and_added(modifications, from_count, to_count, to_line)
|
||||
call gitgutter#hunk#increment_lines_added(a:bufnr, to_count - from_count)
|
||||
call gitgutter#hunk#increment_lines_modified(a:bufnr, from_count)
|
||||
|
||||
elseif gitgutter#diff#is_modified_and_removed(from_count, to_count)
|
||||
call gitgutter#diff#process_modified_and_removed(modifications, from_count, to_count, to_line)
|
||||
call gitgutter#hunk#increment_lines_modified(to_count)
|
||||
call gitgutter#hunk#increment_lines_removed(from_count - to_count)
|
||||
elseif s:is_modified_and_removed(from_count, to_count)
|
||||
call s:process_modified_and_removed(modifications, from_count, to_count, to_line)
|
||||
call gitgutter#hunk#increment_lines_modified(a:bufnr, to_count)
|
||||
call gitgutter#hunk#increment_lines_removed(a:bufnr, from_count - to_count)
|
||||
|
||||
endif
|
||||
return modifications
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#is_added(from_count, to_count) abort
|
||||
function! s:is_added(from_count, to_count) abort
|
||||
return a:from_count == 0 && a:to_count > 0
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#is_removed(from_count, to_count) abort
|
||||
function! s:is_removed(from_count, to_count) abort
|
||||
return a:from_count > 0 && a:to_count == 0
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#is_modified(from_count, to_count) abort
|
||||
function! s:is_modified(from_count, to_count) abort
|
||||
return a:from_count > 0 && a:to_count > 0 && a:from_count == a:to_count
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#is_modified_and_added(from_count, to_count) abort
|
||||
function! s:is_modified_and_added(from_count, to_count) abort
|
||||
return a:from_count > 0 && a:to_count > 0 && a:from_count < a:to_count
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#is_modified_and_removed(from_count, to_count) abort
|
||||
function! s:is_modified_and_removed(from_count, to_count) abort
|
||||
return a:from_count > 0 && a:to_count > 0 && a:from_count > a:to_count
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#process_added(modifications, from_count, to_count, to_line) abort
|
||||
function! s:process_added(modifications, from_count, to_count, to_line) abort
|
||||
let offset = 0
|
||||
while offset < a:to_count
|
||||
let line_number = a:to_line + offset
|
||||
@ -242,7 +268,7 @@ function! gitgutter#diff#process_added(modifications, from_count, to_count, to_l
|
||||
endwhile
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#process_removed(modifications, from_count, to_count, to_line) abort
|
||||
function! s:process_removed(modifications, from_count, to_count, to_line) abort
|
||||
if a:to_line == 0
|
||||
call add(a:modifications, [1, 'removed_first_line'])
|
||||
else
|
||||
@ -250,7 +276,7 @@ function! gitgutter#diff#process_removed(modifications, from_count, to_count, to
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#process_modified(modifications, from_count, to_count, to_line) abort
|
||||
function! s:process_modified(modifications, from_count, to_count, to_line) abort
|
||||
let offset = 0
|
||||
while offset < a:to_count
|
||||
let line_number = a:to_line + offset
|
||||
@ -259,7 +285,7 @@ function! gitgutter#diff#process_modified(modifications, from_count, to_count, t
|
||||
endwhile
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#process_modified_and_added(modifications, from_count, to_count, to_line) abort
|
||||
function! s:process_modified_and_added(modifications, from_count, to_count, to_line) abort
|
||||
let offset = 0
|
||||
while offset < a:from_count
|
||||
let line_number = a:to_line + offset
|
||||
@ -273,7 +299,7 @@ function! gitgutter#diff#process_modified_and_added(modifications, from_count, t
|
||||
endwhile
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#process_modified_and_removed(modifications, from_count, to_count, to_line) abort
|
||||
function! s:process_modified_and_removed(modifications, from_count, to_count, to_line) abort
|
||||
let offset = 0
|
||||
while offset < a:to_count
|
||||
let line_number = a:to_line + offset
|
||||
@ -283,28 +309,14 @@ function! gitgutter#diff#process_modified_and_removed(modifications, from_count,
|
||||
let a:modifications[-1] = [a:to_line + offset - 1, 'modified_removed']
|
||||
endfunction
|
||||
|
||||
" Generates a zero-context diff for the current hunk.
|
||||
"
|
||||
" diff - the full diff for the buffer
|
||||
" type - stage | undo | preview
|
||||
function! gitgutter#diff#generate_diff_for_hunk(diff, type) abort
|
||||
let diff_for_hunk = gitgutter#diff#discard_hunks(a:diff, a:type == 'stage' || a:type == 'undo')
|
||||
|
||||
if a:type == 'stage' || a:type == 'undo'
|
||||
let diff_for_hunk = gitgutter#diff#adjust_hunk_summary(diff_for_hunk, a:type == 'stage')
|
||||
endif
|
||||
|
||||
return diff_for_hunk
|
||||
endfunction
|
||||
|
||||
" Returns the diff with all hunks discarded except the current.
|
||||
"
|
||||
" diff - the diff to process
|
||||
" keep_header - truthy to keep the diff header and hunk summary, falsy to discard it
|
||||
function! gitgutter#diff#discard_hunks(diff, keep_header) abort
|
||||
" Returns a diff for the current hunk.
|
||||
function! gitgutter#diff#hunk_diff(bufnr, full_diff)
|
||||
let modified_diff = []
|
||||
let keep_line = a:keep_header
|
||||
for line in split(a:diff, '\n')
|
||||
let keep_line = 1
|
||||
" Don't keepempty when splitting because the diff we want may not be the
|
||||
" final one. Instead add trailing NL at end of function.
|
||||
for line in split(a:full_diff, '\n')
|
||||
let hunk_info = gitgutter#diff#parse_hunk(line)
|
||||
if len(hunk_info) == 4 " start of new hunk
|
||||
let keep_line = gitgutter#hunk#cursor_in_hunk(hunk_info)
|
||||
@ -313,36 +325,32 @@ function! gitgutter#diff#discard_hunks(diff, keep_header) abort
|
||||
call add(modified_diff, line)
|
||||
endif
|
||||
endfor
|
||||
return join(modified_diff, "\n")."\n"
|
||||
endfunction
|
||||
|
||||
if a:keep_header
|
||||
return gitgutter#utility#stringify(modified_diff)
|
||||
else
|
||||
" Discard hunk summary too.
|
||||
return gitgutter#utility#stringify(modified_diff[1:])
|
||||
|
||||
function! s:write_buffer(bufnr, file)
|
||||
let bufcontents = getbufline(a:bufnr, 1, '$')
|
||||
|
||||
if getbufvar(a:bufnr, '&fileformat') ==# 'dos'
|
||||
call map(bufcontents, 'v:val."\r"')
|
||||
endif
|
||||
|
||||
let fenc = getbufvar(a:bufnr, '&fileencoding')
|
||||
if fenc !=# &encoding
|
||||
call map(bufcontents, 'iconv(v:val, &encoding, "'.fenc.'")')
|
||||
endif
|
||||
|
||||
if getbufvar(a:bufnr, '&bomb')
|
||||
let bufcontents[0]=''.bufcontents[0]
|
||||
endif
|
||||
|
||||
call writefile(bufcontents, a:file)
|
||||
endfunction
|
||||
|
||||
" Adjust hunk summary (from's / to's line number) to ignore changes above/before this one.
|
||||
"
|
||||
" diff_for_hunk - a diff containing only the hunk of interest
|
||||
" staging - truthy if the hunk is to be staged, falsy if it is to be undone
|
||||
"
|
||||
" TODO: push this down to #discard_hunks?
|
||||
function! gitgutter#diff#adjust_hunk_summary(diff_for_hunk, staging) abort
|
||||
let line_adjustment = gitgutter#hunk#line_adjustment_for_current_hunk()
|
||||
let adj_diff = []
|
||||
for line in split(a:diff_for_hunk, '\n')
|
||||
if match(line, s:hunk_re) != -1
|
||||
if a:staging
|
||||
" increment 'to' line number
|
||||
let line = substitute(line, '+\@<=\(\d\+\)', '\=submatch(1)+line_adjustment', '')
|
||||
else
|
||||
" decrement 'from' line number
|
||||
let line = substitute(line, '-\@<=\(\d\+\)', '\=submatch(1)-line_adjustment', '')
|
||||
endif
|
||||
endif
|
||||
call add(adj_diff, line)
|
||||
endfor
|
||||
return gitgutter#utility#stringify(adj_diff)
|
||||
|
||||
function! s:save_last_seen_change(bufnr) abort
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'tick', getbufvar(a:bufnr, 'changedtick'))
|
||||
endfunction
|
||||
|
||||
|
||||
|
@ -1,3 +1,37 @@
|
||||
function! gitgutter#highlight#line_disable() abort
|
||||
let g:gitgutter_highlight_lines = 0
|
||||
call s:define_sign_line_highlights()
|
||||
|
||||
if !g:gitgutter_signs
|
||||
call gitgutter#sign#clear_signs(bufnr(''))
|
||||
call gitgutter#sign#remove_dummy_sign(bufnr(''), 0)
|
||||
endif
|
||||
|
||||
redraw!
|
||||
endfunction
|
||||
|
||||
function! gitgutter#highlight#line_enable() abort
|
||||
let old_highlight_lines = g:gitgutter_highlight_lines
|
||||
|
||||
let g:gitgutter_highlight_lines = 1
|
||||
call s:define_sign_line_highlights()
|
||||
|
||||
if !old_highlight_lines && !g:gitgutter_signs
|
||||
call gitgutter#all(1)
|
||||
endif
|
||||
|
||||
redraw!
|
||||
endfunction
|
||||
|
||||
function! gitgutter#highlight#line_toggle() abort
|
||||
if g:gitgutter_highlight_lines
|
||||
call gitgutter#highlight#line_disable()
|
||||
else
|
||||
call gitgutter#highlight#line_enable()
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#highlight#define_sign_column_highlight() abort
|
||||
if g:gitgutter_override_sign_column_highlight
|
||||
highlight! link SignColumn LineNr
|
||||
@ -7,7 +41,7 @@ function! gitgutter#highlight#define_sign_column_highlight() abort
|
||||
endfunction
|
||||
|
||||
function! gitgutter#highlight#define_highlights() abort
|
||||
let [guibg, ctermbg] = gitgutter#highlight#get_background_colors('SignColumn')
|
||||
let [guibg, ctermbg] = s:get_background_colors('SignColumn')
|
||||
|
||||
" Highlights used by the signs.
|
||||
|
||||
@ -42,12 +76,12 @@ function! gitgutter#highlight#define_signs() abort
|
||||
sign define GitGutterLineModifiedRemoved
|
||||
sign define GitGutterDummy
|
||||
|
||||
call gitgutter#highlight#define_sign_text()
|
||||
call s:define_sign_text()
|
||||
call gitgutter#highlight#define_sign_text_highlights()
|
||||
call gitgutter#highlight#define_sign_line_highlights()
|
||||
call s:define_sign_line_highlights()
|
||||
endfunction
|
||||
|
||||
function! gitgutter#highlight#define_sign_text() abort
|
||||
function! s:define_sign_text() abort
|
||||
execute "sign define GitGutterLineAdded text=" . g:gitgutter_sign_added
|
||||
execute "sign define GitGutterLineModified text=" . g:gitgutter_sign_modified
|
||||
execute "sign define GitGutterLineRemoved text=" . g:gitgutter_sign_removed
|
||||
@ -75,7 +109,7 @@ function! gitgutter#highlight#define_sign_text_highlights() abort
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#highlight#define_sign_line_highlights() abort
|
||||
function! s:define_sign_line_highlights() abort
|
||||
if g:gitgutter_highlight_lines
|
||||
sign define GitGutterLineAdded linehl=GitGutterAddLine
|
||||
sign define GitGutterLineModified linehl=GitGutterChangeLine
|
||||
@ -91,22 +125,22 @@ function! gitgutter#highlight#define_sign_line_highlights() abort
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#highlight#get_background_colors(group) abort
|
||||
function! s:get_background_colors(group) abort
|
||||
redir => highlight
|
||||
silent execute 'silent highlight ' . a:group
|
||||
redir END
|
||||
|
||||
let link_matches = matchlist(highlight, 'links to \(\S\+\)')
|
||||
if len(link_matches) > 0 " follow the link
|
||||
return gitgutter#highlight#get_background_colors(link_matches[1])
|
||||
return s:get_background_colors(link_matches[1])
|
||||
endif
|
||||
|
||||
let ctermbg = gitgutter#highlight#match_highlight(highlight, 'ctermbg=\([0-9A-Za-z]\+\)')
|
||||
let guibg = gitgutter#highlight#match_highlight(highlight, 'guibg=\([#0-9A-Za-z]\+\)')
|
||||
let ctermbg = s:match_highlight(highlight, 'ctermbg=\([0-9A-Za-z]\+\)')
|
||||
let guibg = s:match_highlight(highlight, 'guibg=\([#0-9A-Za-z]\+\)')
|
||||
return [guibg, ctermbg]
|
||||
endfunction
|
||||
|
||||
function! gitgutter#highlight#match_highlight(highlight, pattern) abort
|
||||
function! s:match_highlight(highlight, pattern) abort
|
||||
let matches = matchlist(a:highlight, a:pattern)
|
||||
if len(matches) == 0
|
||||
return 'NONE'
|
||||
|
@ -1,15 +1,15 @@
|
||||
function! gitgutter#hunk#set_hunks(hunks) abort
|
||||
call gitgutter#utility#setbufvar(gitgutter#utility#bufnr(), 'hunks', a:hunks)
|
||||
call s:reset_summary()
|
||||
function! gitgutter#hunk#set_hunks(bufnr, hunks) abort
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'hunks', a:hunks)
|
||||
call s:reset_summary(a:bufnr)
|
||||
endfunction
|
||||
|
||||
function! gitgutter#hunk#hunks() abort
|
||||
return gitgutter#utility#getbufvar(gitgutter#utility#bufnr(), 'hunks', [])
|
||||
function! gitgutter#hunk#hunks(bufnr) abort
|
||||
return gitgutter#utility#getbufvar(a:bufnr, 'hunks', [])
|
||||
endfunction
|
||||
|
||||
function! gitgutter#hunk#reset() abort
|
||||
call gitgutter#utility#setbufvar(gitgutter#utility#bufnr(), 'hunks', [])
|
||||
call s:reset_summary()
|
||||
function! gitgutter#hunk#reset(bufnr) abort
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'hunks', [])
|
||||
call s:reset_summary(a:bufnr)
|
||||
endfunction
|
||||
|
||||
|
||||
@ -17,37 +17,35 @@ function! gitgutter#hunk#summary(bufnr) abort
|
||||
return gitgutter#utility#getbufvar(a:bufnr, 'summary', [0,0,0])
|
||||
endfunction
|
||||
|
||||
function! s:reset_summary() abort
|
||||
call gitgutter#utility#setbufvar(gitgutter#utility#bufnr(), 'summary', [0,0,0])
|
||||
function! s:reset_summary(bufnr) abort
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'summary', [0,0,0])
|
||||
endfunction
|
||||
|
||||
function! gitgutter#hunk#increment_lines_added(count) abort
|
||||
let bufnr = gitgutter#utility#bufnr()
|
||||
let summary = gitgutter#hunk#summary(bufnr)
|
||||
function! gitgutter#hunk#increment_lines_added(bufnr, count) abort
|
||||
let summary = gitgutter#hunk#summary(a:bufnr)
|
||||
let summary[0] += a:count
|
||||
call gitgutter#utility#setbufvar(bufnr, 'summary', summary)
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'summary', summary)
|
||||
endfunction
|
||||
|
||||
function! gitgutter#hunk#increment_lines_modified(count) abort
|
||||
let bufnr = gitgutter#utility#bufnr()
|
||||
let summary = gitgutter#hunk#summary(bufnr)
|
||||
function! gitgutter#hunk#increment_lines_modified(bufnr, count) abort
|
||||
let summary = gitgutter#hunk#summary(a:bufnr)
|
||||
let summary[1] += a:count
|
||||
call gitgutter#utility#setbufvar(bufnr, 'summary', summary)
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'summary', summary)
|
||||
endfunction
|
||||
|
||||
function! gitgutter#hunk#increment_lines_removed(count) abort
|
||||
let bufnr = gitgutter#utility#bufnr()
|
||||
let summary = gitgutter#hunk#summary(bufnr)
|
||||
function! gitgutter#hunk#increment_lines_removed(bufnr, count) abort
|
||||
let summary = gitgutter#hunk#summary(a:bufnr)
|
||||
let summary[2] += a:count
|
||||
call gitgutter#utility#setbufvar(bufnr, 'summary', summary)
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'summary', summary)
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#hunk#next_hunk(count) abort
|
||||
if gitgutter#utility#is_active()
|
||||
let bufnr = bufnr('')
|
||||
if gitgutter#utility#is_active(bufnr)
|
||||
let current_line = line('.')
|
||||
let hunk_count = 0
|
||||
for hunk in gitgutter#hunk#hunks()
|
||||
for hunk in gitgutter#hunk#hunks(bufnr)
|
||||
if hunk[2] > current_line
|
||||
let hunk_count += 1
|
||||
if hunk_count == a:count
|
||||
@ -61,10 +59,11 @@ function! gitgutter#hunk#next_hunk(count) abort
|
||||
endfunction
|
||||
|
||||
function! gitgutter#hunk#prev_hunk(count) abort
|
||||
if gitgutter#utility#is_active()
|
||||
let bufnr = bufnr('')
|
||||
if gitgutter#utility#is_active(bufnr)
|
||||
let current_line = line('.')
|
||||
let hunk_count = 0
|
||||
for hunk in reverse(copy(gitgutter#hunk#hunks()))
|
||||
for hunk in reverse(copy(gitgutter#hunk#hunks(bufnr)))
|
||||
if hunk[2] < current_line
|
||||
let hunk_count += 1
|
||||
if hunk_count == a:count
|
||||
@ -80,10 +79,11 @@ endfunction
|
||||
|
||||
" Returns the hunk the cursor is currently in or an empty list if the cursor
|
||||
" isn't in a hunk.
|
||||
function! gitgutter#hunk#current_hunk() abort
|
||||
function! s:current_hunk() abort
|
||||
let bufnr = bufnr('')
|
||||
let current_hunk = []
|
||||
|
||||
for hunk in gitgutter#hunk#hunks()
|
||||
for hunk in gitgutter#hunk#hunks(bufnr)
|
||||
if gitgutter#hunk#cursor_in_hunk(hunk)
|
||||
let current_hunk = hunk
|
||||
break
|
||||
@ -107,22 +107,8 @@ function! gitgutter#hunk#cursor_in_hunk(hunk) abort
|
||||
return 0
|
||||
endfunction
|
||||
|
||||
" Returns the number of lines the current hunk is offset from where it would
|
||||
" be if any changes above it in the file didn't exist.
|
||||
function! gitgutter#hunk#line_adjustment_for_current_hunk() abort
|
||||
let adj = 0
|
||||
for hunk in gitgutter#hunk#hunks()
|
||||
if gitgutter#hunk#cursor_in_hunk(hunk)
|
||||
break
|
||||
else
|
||||
let adj += hunk[1] - hunk[3]
|
||||
endif
|
||||
endfor
|
||||
return adj
|
||||
endfunction
|
||||
|
||||
function! gitgutter#hunk#text_object(inner) abort
|
||||
let hunk = gitgutter#hunk#current_hunk()
|
||||
let hunk = s:current_hunk()
|
||||
|
||||
if empty(hunk)
|
||||
return
|
||||
@ -141,3 +127,155 @@ function! gitgutter#hunk#text_object(inner) abort
|
||||
|
||||
execute 'normal! 'first_line.'GV'.last_line.'G'
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#hunk#stage() abort
|
||||
call s:hunk_op(function('s:stage'))
|
||||
silent! call repeat#set("\<Plug>GitGutterStageHunk", -1)<CR>
|
||||
endfunction
|
||||
|
||||
function! gitgutter#hunk#undo() abort
|
||||
call s:hunk_op(function('s:undo'))
|
||||
silent! call repeat#set("\<Plug>GitGutterUndoHunk", -1)<CR>
|
||||
endfunction
|
||||
|
||||
function! gitgutter#hunk#preview() abort
|
||||
call s:hunk_op(function('s:preview'))
|
||||
silent! call repeat#set("\<Plug>GitGutterPreviewHunk", -1)<CR>
|
||||
endfunction
|
||||
|
||||
|
||||
function! s:hunk_op(op)
|
||||
let bufnr = bufnr('')
|
||||
|
||||
if gitgutter#utility#is_active(bufnr)
|
||||
" Get a (synchronous) diff.
|
||||
let [async, g:gitgutter_async] = [g:gitgutter_async, 0]
|
||||
let diff = gitgutter#diff#run_diff(bufnr, 1)
|
||||
let g:gitgutter_async = async
|
||||
|
||||
call gitgutter#hunk#set_hunks(bufnr, gitgutter#diff#parse_diff(diff))
|
||||
|
||||
if empty(s:current_hunk())
|
||||
call gitgutter#utility#warn('cursor is not in a hunk')
|
||||
else
|
||||
call a:op(gitgutter#diff#hunk_diff(bufnr, diff))
|
||||
endif
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
function! s:stage(hunk_diff)
|
||||
let bufnr = bufnr('')
|
||||
let diff = s:adjust_header(bufnr, a:hunk_diff)
|
||||
" Apply patch to index.
|
||||
call gitgutter#utility#system(
|
||||
\ gitgutter#utility#cd_cmd(bufnr, g:gitgutter_git_executable.' apply --cached --unidiff-zero - '),
|
||||
\ diff)
|
||||
|
||||
" Refresh gitgutter's view of buffer.
|
||||
call gitgutter#process_buffer(bufnr, 1)
|
||||
endfunction
|
||||
|
||||
|
||||
function! s:undo(hunk_diff)
|
||||
" Apply reverse patch to buffer.
|
||||
let hunk = gitgutter#diff#parse_hunk(split(a:hunk_diff, '\n')[4])
|
||||
let lines = map(split(a:hunk_diff, '\n')[5:], 'v:val[1:]')
|
||||
let lnum = hunk[2]
|
||||
let added_only = hunk[1] == 0 && hunk[3] > 0
|
||||
let removed_only = hunk[1] > 0 && hunk[3] == 0
|
||||
|
||||
if removed_only
|
||||
call append(lnum, lines)
|
||||
elseif added_only
|
||||
execute lnum .','. (lnum+len(lines)-1) .'d'
|
||||
else
|
||||
call append(lnum-1, lines[0:hunk[1]])
|
||||
execute (lnum+hunk[1]) .','. (lnum+hunk[1]+hunk[3]) .'d'
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
function! s:preview(hunk_diff)
|
||||
let hunk_lines = split(s:discard_header(a:hunk_diff), "\n")
|
||||
let hunk_lines_length = len(hunk_lines)
|
||||
let previewheight = min([hunk_lines_length, &previewheight])
|
||||
|
||||
silent! wincmd P
|
||||
if !&previewwindow
|
||||
noautocmd execute 'bo' previewheight 'new'
|
||||
set previewwindow
|
||||
else
|
||||
execute 'resize' previewheight
|
||||
endif
|
||||
|
||||
setlocal noreadonly modifiable filetype=diff buftype=nofile bufhidden=delete noswapfile
|
||||
execute "%delete_"
|
||||
call append(0, hunk_lines)
|
||||
normal! gg
|
||||
setlocal readonly nomodifiable
|
||||
|
||||
noautocmd wincmd p
|
||||
endfunction
|
||||
|
||||
|
||||
function! s:adjust_header(bufnr, hunk_diff)
|
||||
let filepath = gitgutter#utility#repo_path(a:bufnr, 0)
|
||||
return s:adjust_hunk_summary(s:fix_file_references(filepath, a:hunk_diff))
|
||||
endfunction
|
||||
|
||||
|
||||
" Replaces references to temp files with the actual file.
|
||||
function! s:fix_file_references(filepath, hunk_diff)
|
||||
let lines = split(a:hunk_diff, '\n')
|
||||
|
||||
let left_prefix = matchstr(lines[2], '[abciow12]').'/'
|
||||
let right_prefix = matchstr(lines[3], '[abciow12]').'/'
|
||||
let quote = lines[0][11] == '"' ? '"' : ''
|
||||
|
||||
let left_file = quote.left_prefix.a:filepath.quote
|
||||
let right_file = quote.right_prefix.a:filepath.quote
|
||||
|
||||
let lines[0] = 'diff --git '.left_file.' '.right_file
|
||||
let lines[2] = '--- '.left_file
|
||||
let lines[3] = '+++ '.right_file
|
||||
|
||||
return join(lines, "\n")."\n"
|
||||
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
|
||||
let line_adjustment = s:line_adjustment_for_current_hunk()
|
||||
let diff = split(a:hunk_diff, '\n', 1)
|
||||
let diff[4] = substitute(diff[4], '+\@<=\(\d\+\)', '\=submatch(1)+line_adjustment', '')
|
||||
return join(diff, "\n")
|
||||
endfunction
|
||||
|
||||
|
||||
function! s:discard_header(hunk_diff)
|
||||
return join(split(a:hunk_diff, '\n', 1)[5:], "\n")
|
||||
endfunction
|
||||
|
||||
|
||||
" Returns the number of lines the current hunk is offset from where it would
|
||||
" be if any changes above it in the file didn't exist.
|
||||
function! s:line_adjustment_for_current_hunk() abort
|
||||
let bufnr = bufnr('')
|
||||
let adj = 0
|
||||
for hunk in gitgutter#hunk#hunks(bufnr)
|
||||
if gitgutter#hunk#cursor_in_hunk(hunk)
|
||||
break
|
||||
else
|
||||
let adj += hunk[1] - hunk[3]
|
||||
endif
|
||||
endfor
|
||||
return adj
|
||||
endfunction
|
||||
|
||||
|
@ -10,14 +10,43 @@ let s:dummy_sign_id = s:first_sign_id - 1
|
||||
let s:supports_star = v:version > 703 || (v:version == 703 && has("patch596"))
|
||||
|
||||
|
||||
" Removes gitgutter's signs (excluding dummy sign) from the buffer being processed.
|
||||
function! gitgutter#sign#clear_signs() abort
|
||||
let bufnr = gitgutter#utility#bufnr()
|
||||
call gitgutter#sign#find_current_signs()
|
||||
function! gitgutter#sign#enable() abort
|
||||
let old_signs = g:gitgutter_signs
|
||||
|
||||
let sign_ids = map(values(gitgutter#utility#getbufvar(bufnr, 'gitgutter_signs')), 'v:val.id')
|
||||
call gitgutter#sign#remove_signs(sign_ids, 1)
|
||||
call gitgutter#utility#setbufvar(bufnr, 'gitgutter_signs', {})
|
||||
let g:gitgutter_signs = 1
|
||||
call gitgutter#highlight#define_sign_text_highlights()
|
||||
|
||||
if !old_signs && !g:gitgutter_highlight_lines
|
||||
call gitgutter#all(1)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#sign#disable() abort
|
||||
let g:gitgutter_signs = 0
|
||||
call gitgutter#highlight#define_sign_text_highlights()
|
||||
|
||||
if !g:gitgutter_highlight_lines
|
||||
call gitgutter#sign#clear_signs(bufnr(''))
|
||||
call gitgutter#sign#remove_dummy_sign(bufnr(''), 0)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#sign#toggle() abort
|
||||
if g:gitgutter_signs
|
||||
call gitgutter#sign#disable()
|
||||
else
|
||||
call gitgutter#sign#enable()
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
" Removes gitgutter's signs (excluding dummy sign) from the buffer being processed.
|
||||
function! gitgutter#sign#clear_signs(bufnr) abort
|
||||
call s:find_current_signs(a:bufnr)
|
||||
|
||||
let sign_ids = map(values(gitgutter#utility#getbufvar(a:bufnr, 'gitgutter_signs')), 'v:val.id')
|
||||
call s:remove_signs(a:bufnr, sign_ids, 1)
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'gitgutter_signs', {})
|
||||
endfunction
|
||||
|
||||
|
||||
@ -25,39 +54,37 @@ endfunction
|
||||
"
|
||||
" modified_lines: list of [<line_number (number)>, <name (string)>]
|
||||
" where name = 'added|removed|modified|modified_removed'
|
||||
function! gitgutter#sign#update_signs(modified_lines) abort
|
||||
call gitgutter#sign#find_current_signs()
|
||||
function! gitgutter#sign#update_signs(bufnr, modified_lines) abort
|
||||
call s:find_current_signs(a:bufnr)
|
||||
|
||||
let new_gitgutter_signs_line_numbers = map(copy(a:modified_lines), 'v:val[0]')
|
||||
let obsolete_signs = gitgutter#sign#obsolete_gitgutter_signs_to_remove(new_gitgutter_signs_line_numbers)
|
||||
let obsolete_signs = s:obsolete_gitgutter_signs_to_remove(a:bufnr, new_gitgutter_signs_line_numbers)
|
||||
|
||||
let flicker_possible = s:remove_all_old_signs && !empty(a:modified_lines)
|
||||
if flicker_possible
|
||||
call gitgutter#sign#add_dummy_sign()
|
||||
call s:add_dummy_sign(a:bufnr)
|
||||
endif
|
||||
|
||||
call gitgutter#sign#remove_signs(obsolete_signs, s:remove_all_old_signs)
|
||||
call gitgutter#sign#upsert_new_gitgutter_signs(a:modified_lines)
|
||||
call s:remove_signs(a:bufnr, obsolete_signs, s:remove_all_old_signs)
|
||||
call s:upsert_new_gitgutter_signs(a:bufnr, a:modified_lines)
|
||||
|
||||
if flicker_possible
|
||||
call gitgutter#sign#remove_dummy_sign(0)
|
||||
call gitgutter#sign#remove_dummy_sign(a:bufnr, 0)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#sign#add_dummy_sign() abort
|
||||
let bufnr = gitgutter#utility#bufnr()
|
||||
if !gitgutter#utility#getbufvar(bufnr, 'dummy_sign')
|
||||
execute "sign place" s:dummy_sign_id "line=" . 9999 "name=GitGutterDummy buffer=" . bufnr
|
||||
call gitgutter#utility#setbufvar(bufnr, 'dummy_sign', 1)
|
||||
function! s:add_dummy_sign(bufnr) abort
|
||||
if !gitgutter#utility#getbufvar(a:bufnr, 'dummy_sign')
|
||||
execute "sign place" s:dummy_sign_id "line=" . 9999 "name=GitGutterDummy buffer=" . a:bufnr
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'dummy_sign', 1)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#sign#remove_dummy_sign(force) abort
|
||||
let bufnr = gitgutter#utility#bufnr()
|
||||
if gitgutter#utility#getbufvar(bufnr, 'dummy_sign') && (a:force || !g:gitgutter_sign_column_always)
|
||||
execute "sign unplace" s:dummy_sign_id "buffer=" . bufnr
|
||||
call gitgutter#utility#setbufvar(bufnr, 'dummy_sign', 0)
|
||||
function! gitgutter#sign#remove_dummy_sign(bufnr, force) abort
|
||||
if gitgutter#utility#getbufvar(a:bufnr, 'dummy_sign') && (a:force || !g:gitgutter_sign_column_always)
|
||||
execute "sign unplace" s:dummy_sign_id "buffer=" . a:bufnr
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'dummy_sign', 0)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
@ -67,14 +94,13 @@ endfunction
|
||||
"
|
||||
|
||||
|
||||
function! gitgutter#sign#find_current_signs() abort
|
||||
let bufnr = gitgutter#utility#bufnr()
|
||||
function! s:find_current_signs(bufnr) abort
|
||||
let gitgutter_signs = {} " <line_number (string)>: {'id': <id (number)>, 'name': <name (string)>}
|
||||
let other_signs = [] " [<line_number (number),...]
|
||||
let dummy_sign_placed = 0
|
||||
|
||||
redir => signs
|
||||
silent execute "sign place buffer=" . bufnr
|
||||
silent execute "sign place buffer=" . a:bufnr
|
||||
redir END
|
||||
|
||||
for sign_line in filter(split(signs, '\n')[2:], 'v:val =~# "="')
|
||||
@ -101,19 +127,18 @@ function! gitgutter#sign#find_current_signs() abort
|
||||
end
|
||||
endfor
|
||||
|
||||
call gitgutter#utility#setbufvar(bufnr, 'dummy_sign', dummy_sign_placed)
|
||||
call gitgutter#utility#setbufvar(bufnr, 'gitgutter_signs', gitgutter_signs)
|
||||
call gitgutter#utility#setbufvar(bufnr, 'other_signs', other_signs)
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'dummy_sign', dummy_sign_placed)
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'gitgutter_signs', gitgutter_signs)
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'other_signs', other_signs)
|
||||
endfunction
|
||||
|
||||
|
||||
" Returns a list of [<id (number)>, ...]
|
||||
" Sets `s:remove_all_old_signs` as a side-effect.
|
||||
function! gitgutter#sign#obsolete_gitgutter_signs_to_remove(new_gitgutter_signs_line_numbers) abort
|
||||
let bufnr = gitgutter#utility#bufnr()
|
||||
function! s:obsolete_gitgutter_signs_to_remove(bufnr, new_gitgutter_signs_line_numbers) abort
|
||||
let signs_to_remove = [] " list of [<id (number)>, ...]
|
||||
let remove_all_signs = 1
|
||||
let old_gitgutter_signs = gitgutter#utility#getbufvar(bufnr, 'gitgutter_signs')
|
||||
let old_gitgutter_signs = gitgutter#utility#getbufvar(a:bufnr, 'gitgutter_signs')
|
||||
for line_number in keys(old_gitgutter_signs)
|
||||
if index(a:new_gitgutter_signs_line_numbers, str2nr(line_number)) == -1
|
||||
call add(signs_to_remove, old_gitgutter_signs[line_number].id)
|
||||
@ -126,13 +151,12 @@ function! gitgutter#sign#obsolete_gitgutter_signs_to_remove(new_gitgutter_signs_
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#sign#remove_signs(sign_ids, all_signs) abort
|
||||
let bufnr = gitgutter#utility#bufnr()
|
||||
if a:all_signs && s:supports_star && empty(gitgutter#utility#getbufvar(bufnr, 'other_signs'))
|
||||
let dummy_sign_present = gitgutter#utility#getbufvar(bufnr, 'dummy_sign')
|
||||
execute "sign unplace * buffer=" . bufnr
|
||||
function! s:remove_signs(bufnr, sign_ids, all_signs) abort
|
||||
if a:all_signs && s:supports_star && empty(gitgutter#utility#getbufvar(a:bufnr, 'other_signs'))
|
||||
let dummy_sign_present = gitgutter#utility#getbufvar(a:bufnr, 'dummy_sign')
|
||||
execute "sign unplace * buffer=" . a:bufnr
|
||||
if dummy_sign_present
|
||||
execute "sign place" s:dummy_sign_id "line=" . 9999 "name=GitGutterDummy buffer=" . bufnr
|
||||
execute "sign place" s:dummy_sign_id "line=" . 9999 "name=GitGutterDummy buffer=" . a:bufnr
|
||||
endif
|
||||
else
|
||||
for id in a:sign_ids
|
||||
@ -142,22 +166,21 @@ function! gitgutter#sign#remove_signs(sign_ids, all_signs) abort
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#sign#upsert_new_gitgutter_signs(modified_lines) abort
|
||||
let bufnr = gitgutter#utility#bufnr()
|
||||
let other_signs = gitgutter#utility#getbufvar(bufnr, 'other_signs')
|
||||
let old_gitgutter_signs = gitgutter#utility#getbufvar(bufnr, 'gitgutter_signs')
|
||||
function! s:upsert_new_gitgutter_signs(bufnr, modified_lines) abort
|
||||
let other_signs = gitgutter#utility#getbufvar(a:bufnr, 'other_signs')
|
||||
let old_gitgutter_signs = gitgutter#utility#getbufvar(a:bufnr, 'gitgutter_signs')
|
||||
|
||||
for line in a:modified_lines
|
||||
let line_number = line[0] " <number>
|
||||
if index(other_signs, line_number) == -1 " don't clobber others' signs
|
||||
let name = gitgutter#utility#highlight_name_for_change(line[1])
|
||||
let name = s:highlight_name_for_change(line[1])
|
||||
if !has_key(old_gitgutter_signs, line_number) " insert
|
||||
let id = gitgutter#sign#next_sign_id()
|
||||
execute "sign place" id "line=" . line_number "name=" . name "buffer=" . bufnr
|
||||
let id = s:next_sign_id()
|
||||
execute "sign place" id "line=" . line_number "name=" . name "buffer=" . a:bufnr
|
||||
else " update if sign has changed
|
||||
let old_sign = old_gitgutter_signs[line_number]
|
||||
if old_sign.name !=# name
|
||||
execute "sign place" old_sign.id "name=" . name "buffer=" . bufnr
|
||||
execute "sign place" old_sign.id "name=" . name "buffer=" . a:bufnr
|
||||
end
|
||||
endif
|
||||
endif
|
||||
@ -166,7 +189,7 @@ function! gitgutter#sign#upsert_new_gitgutter_signs(modified_lines) abort
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#sign#next_sign_id() abort
|
||||
function! s:next_sign_id() abort
|
||||
let next_id = s:next_sign_id
|
||||
let s:next_sign_id += 1
|
||||
return next_id
|
||||
@ -177,3 +200,20 @@ endfunction
|
||||
function! gitgutter#sign#reset()
|
||||
let s:next_sign_id = s:first_sign_id
|
||||
endfunction
|
||||
|
||||
|
||||
function! s:highlight_name_for_change(text) abort
|
||||
if a:text ==# 'added'
|
||||
return 'GitGutterLineAdded'
|
||||
elseif a:text ==# 'removed'
|
||||
return 'GitGutterLineRemoved'
|
||||
elseif a:text ==# 'removed_first_line'
|
||||
return 'GitGutterLineRemovedFirstLine'
|
||||
elseif a:text ==# 'modified'
|
||||
return 'GitGutterLineModified'
|
||||
elseif a:text ==# 'modified_removed'
|
||||
return 'GitGutterLineModifiedRemoved'
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
|
@ -1,11 +1,18 @@
|
||||
let s:file = ''
|
||||
let s:using_xolox_shell = -1
|
||||
let s:exit_code = 0
|
||||
function! gitgutter#utility#supports_overscore_sign()
|
||||
if s:windows()
|
||||
return &encoding ==? 'utf-8'
|
||||
else
|
||||
return &termencoding ==? &encoding || &termencoding == ''
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#setbufvar(buffer, varname, val)
|
||||
let dict = get(getbufvar(a:buffer, ''), 'gitgutter', {})
|
||||
let needs_setting = empty(dict)
|
||||
let dict[a:varname] = a:val
|
||||
call setbufvar(a:buffer, 'gitgutter', dict)
|
||||
if needs_setting
|
||||
call setbufvar(a:buffer, 'gitgutter', dict)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#getbufvar(buffer, varname, ...)
|
||||
@ -26,11 +33,11 @@ function! gitgutter#utility#warn(message) abort
|
||||
let v:warningmsg = a:message
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#warn_once(message, key) abort
|
||||
if empty(gitgutter#utility#getbufvar(s:bufnr, a:key))
|
||||
call gitgutter#utility#setbufvar(s:bufnr, a:key, '1')
|
||||
function! gitgutter#utility#warn_once(bufnr, message, key) abort
|
||||
if empty(gitgutter#utility#getbufvar(a:bufnr, a:key))
|
||||
call gitgutter#utility#setbufvar(a:bufnr, a:key, '1')
|
||||
echohl WarningMsg
|
||||
redraw | echo 'vim-gitgutter: ' . a:message
|
||||
redraw | echom 'vim-gitgutter: ' . a:message
|
||||
echohl None
|
||||
let v:warningmsg = a:message
|
||||
endif
|
||||
@ -38,188 +45,166 @@ endfunction
|
||||
|
||||
" 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.
|
||||
function! gitgutter#utility#is_active() abort
|
||||
function! gitgutter#utility#is_active(bufnr) abort
|
||||
return g:gitgutter_enabled &&
|
||||
\ !pumvisible() &&
|
||||
\ gitgutter#utility#is_file_buffer() &&
|
||||
\ gitgutter#utility#exists_file() &&
|
||||
\ gitgutter#utility#not_git_dir()
|
||||
\ s:is_file_buffer(a:bufnr) &&
|
||||
\ s:exists_file(a:bufnr) &&
|
||||
\ s:not_git_dir(a:bufnr)
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#not_git_dir() abort
|
||||
return gitgutter#utility#full_path_to_directory_of_file() !~ '[/\\]\.git\($\|[/\\]\)'
|
||||
function! s:not_git_dir(bufnr) abort
|
||||
return s:dir(a:bufnr) !~ '[/\\]\.git\($\|[/\\]\)'
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#is_file_buffer() abort
|
||||
return empty(getbufvar(s:bufnr, '&buftype'))
|
||||
function! s:is_file_buffer(bufnr) abort
|
||||
return empty(getbufvar(a:bufnr, '&buftype'))
|
||||
endfunction
|
||||
|
||||
" A replacement for the built-in `shellescape(arg)`.
|
||||
"
|
||||
" Recent versions of Vim handle shell escaping pretty well. However older
|
||||
" versions aren't as good. This attempts to do the right thing.
|
||||
"
|
||||
" See:
|
||||
" https://github.com/tpope/vim-fugitive/blob/8f0b8edfbd246c0026b7a2388e1d883d579ac7f6/plugin/fugitive.vim#L29-L37
|
||||
" From tpope/vim-fugitive
|
||||
function! s:winshell()
|
||||
return &shell =~? 'cmd' || exists('+shellslash') && !&shellslash
|
||||
endfunction
|
||||
|
||||
" From tpope/vim-fugitive
|
||||
function! gitgutter#utility#shellescape(arg) abort
|
||||
if a:arg =~ '^[A-Za-z0-9_/.-]\+$'
|
||||
return a:arg
|
||||
elseif &shell =~# 'cmd' || gitgutter#utility#using_xolox_shell()
|
||||
elseif s:winshell()
|
||||
return '"' . substitute(substitute(a:arg, '"', '""', 'g'), '%', '"%"', 'g') . '"'
|
||||
else
|
||||
return shellescape(a:arg)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#set_buffer(bufnr) abort
|
||||
let s:bufnr = a:bufnr
|
||||
let s:file = resolve(bufname(a:bufnr))
|
||||
function! gitgutter#utility#file(bufnr)
|
||||
return s:abs_path(a:bufnr, 1)
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#bufnr()
|
||||
return s:bufnr
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#file()
|
||||
return s:file
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#filename() abort
|
||||
return fnamemodify(s:file, ':t')
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#extension() abort
|
||||
return fnamemodify(s:file, ':e')
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#full_path_to_directory_of_file() abort
|
||||
return fnamemodify(s:file, ':p:h')
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#directory_of_file() abort
|
||||
return fnamemodify(s:file, ':h')
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#exists_file() abort
|
||||
return filereadable(s:file)
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#has_unsaved_changes() abort
|
||||
return getbufvar(s:bufnr, "&mod")
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#has_fresh_changes() abort
|
||||
return getbufvar(s:bufnr, 'changedtick') != gitgutter#utility#getbufvar(s:bufnr, 'last_tick')
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#save_last_seen_change() abort
|
||||
call gitgutter#utility#setbufvar(s:bufnr, 'last_tick', getbufvar(s:bufnr, 'changedtick'))
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#shell_error() abort
|
||||
return gitgutter#utility#using_xolox_shell() ? s:exit_code : v:shell_error
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#using_xolox_shell() abort
|
||||
if s:using_xolox_shell == -1
|
||||
if !g:gitgutter_avoid_cmd_prompt_on_windows
|
||||
let s:using_xolox_shell = 0
|
||||
" Although xolox/vim-shell works on both windows and unix we only want to use
|
||||
" it on windows.
|
||||
elseif has('win32') || has('win64') || has('win32unix')
|
||||
let s:using_xolox_shell = exists('g:xolox#misc#version') && exists('g:xolox#shell#version')
|
||||
else
|
||||
let s:using_xolox_shell = 0
|
||||
endif
|
||||
endif
|
||||
return s:using_xolox_shell
|
||||
" Not shellescaped
|
||||
function! gitgutter#utility#extension(bufnr) abort
|
||||
return fnamemodify(s:abs_path(a:bufnr, 0), ':e')
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#system(cmd, ...) abort
|
||||
call gitgutter#debug#log(a:cmd, a:000)
|
||||
|
||||
if gitgutter#utility#using_xolox_shell()
|
||||
let options = {'command': a:cmd, 'check': 0}
|
||||
if a:0 > 0
|
||||
let options['stdin'] = a:1
|
||||
endif
|
||||
let ret = xolox#misc#os#exec(options)
|
||||
let output = join(ret.stdout, "\n")
|
||||
let s:exit_code = ret.exit_code
|
||||
else
|
||||
silent let output = (a:0 == 0) ? system(a:cmd) : system(a:cmd, a:1)
|
||||
endif
|
||||
call s:use_known_shell()
|
||||
silent let output = (a:0 == 0) ? system(a:cmd) : system(a:cmd, a:1)
|
||||
call s:restore_shell()
|
||||
|
||||
return output
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#file_relative_to_repo_root() abort
|
||||
let file_path_relative_to_repo_root = gitgutter#utility#getbufvar(s:bufnr, 'repo_relative_path')
|
||||
if empty(file_path_relative_to_repo_root)
|
||||
let dir_path_relative_to_repo_root = gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file(g:gitgutter_git_executable.' rev-parse --show-prefix'))
|
||||
let dir_path_relative_to_repo_root = gitgutter#utility#strip_trailing_new_line(dir_path_relative_to_repo_root)
|
||||
let file_path_relative_to_repo_root = dir_path_relative_to_repo_root . gitgutter#utility#filename()
|
||||
call gitgutter#utility#setbufvar(s:bufnr, 'repo_relative_path', file_path_relative_to_repo_root)
|
||||
endif
|
||||
return file_path_relative_to_repo_root
|
||||
" Path of file relative to repo root.
|
||||
"
|
||||
" * empty string - not set
|
||||
" * non-empty string - path
|
||||
" * -1 - pending
|
||||
" * -2 - not tracked by git
|
||||
function! gitgutter#utility#repo_path(bufnr, shellesc) abort
|
||||
let p = gitgutter#utility#getbufvar(a:bufnr, 'path')
|
||||
return a:shellesc ? gitgutter#utility#shellescape(p) : p
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#command_in_directory_of_file(cmd) abort
|
||||
return 'cd '.gitgutter#utility#shellescape(gitgutter#utility#directory_of_file()).' && '.a:cmd
|
||||
endfunction
|
||||
function! gitgutter#utility#set_repo_path(bufnr) abort
|
||||
" Values of path:
|
||||
" * non-empty string - path
|
||||
" * -1 - pending
|
||||
" * -2 - not tracked by git
|
||||
|
||||
function! gitgutter#utility#highlight_name_for_change(text) abort
|
||||
if a:text ==# 'added'
|
||||
return 'GitGutterLineAdded'
|
||||
elseif a:text ==# 'removed'
|
||||
return 'GitGutterLineRemoved'
|
||||
elseif a:text ==# 'removed_first_line'
|
||||
return 'GitGutterLineRemovedFirstLine'
|
||||
elseif a:text ==# 'modified'
|
||||
return 'GitGutterLineModified'
|
||||
elseif a:text ==# 'modified_removed'
|
||||
return 'GitGutterLineModifiedRemoved'
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'path', -1)
|
||||
let cmd = gitgutter#utility#cd_cmd(a:bufnr, g:gitgutter_git_executable.' ls-files --error-unmatch --full-name '.gitgutter#utility#shellescape(s:filename(a:bufnr)))
|
||||
|
||||
if g:gitgutter_async && gitgutter#async#available()
|
||||
if has('lambda')
|
||||
call gitgutter#async#execute(cmd, a:bufnr, {
|
||||
\ 'out': {bufnr, path -> gitgutter#utility#setbufvar(bufnr, 'path', s:strip_trailing_new_line(path))},
|
||||
\ 'err': {bufnr -> gitgutter#utility#setbufvar(bufnr, 'path', -2)},
|
||||
\ })
|
||||
else
|
||||
if has('nvim') && !has('nvim-0.2.0')
|
||||
call gitgutter#async#execute(cmd, a:bufnr, {
|
||||
\ 'out': function('s:set_path'),
|
||||
\ 'err': function('s:not_tracked_by_git')
|
||||
\ })
|
||||
else
|
||||
call gitgutter#async#execute(cmd, a:bufnr, {
|
||||
\ 'out': function('s:set_path'),
|
||||
\ 'err': function('s:set_path', [-2])
|
||||
\ })
|
||||
endif
|
||||
endif
|
||||
else
|
||||
let path = gitgutter#utility#system(cmd)
|
||||
if v:shell_error
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'path', -2)
|
||||
else
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'path', s:strip_trailing_new_line(path))
|
||||
endif
|
||||
endif
|
||||
endfunction
|
||||
|
||||
" Dedups list in-place.
|
||||
" Assumes list has no empty entries.
|
||||
function! gitgutter#utility#dedup(list)
|
||||
return filter(sort(a:list), 'index(a:list, v:val, v:key + 1) == -1')
|
||||
if has('nvim') && !has('nvim-0.2.0')
|
||||
function! s:not_tracked_by_git(bufnr)
|
||||
call s:set_path(a:bufnr, -2)
|
||||
endfunction
|
||||
endif
|
||||
|
||||
function! s:set_path(bufnr, path)
|
||||
if a:bufnr == -2
|
||||
let [bufnr, path] = [a:path, a:bufnr]
|
||||
call gitgutter#utility#setbufvar(bufnr, 'path', path)
|
||||
else
|
||||
call gitgutter#utility#setbufvar(a:bufnr, 'path', s:strip_trailing_new_line(a:path))
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#strip_trailing_new_line(line) abort
|
||||
function! gitgutter#utility#cd_cmd(bufnr, cmd) abort
|
||||
let cd = s:unc_path(a:bufnr) ? 'pushd' : (s:windows() ? 'cd /d' : 'cd')
|
||||
return cd.' '.s:dir(a:bufnr).' && '.a:cmd
|
||||
endfunction
|
||||
|
||||
function! s:unc_path(bufnr)
|
||||
return s:abs_path(a:bufnr, 0) =~ '^\\\\'
|
||||
endfunction
|
||||
|
||||
function! s:use_known_shell() abort
|
||||
if has('unix') && &shell !=# 'sh'
|
||||
let [s:shell, s:shellcmdflag, s:shellredir] = [&shell, &shellcmdflag, &shellredir]
|
||||
let &shell = 'sh'
|
||||
set shellcmdflag=-c shellredir=>%s\ 2>&1
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:restore_shell() abort
|
||||
if has('unix') && exists('s:shell')
|
||||
let [&shell, &shellcmdflag, &shellredir] = [s:shell, s:shellcmdflag, s:shellredir]
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:abs_path(bufnr, shellesc)
|
||||
let p = resolve(expand('#'.a:bufnr.':p'))
|
||||
return a:shellesc ? gitgutter#utility#shellescape(p) : p
|
||||
endfunction
|
||||
|
||||
function! s:dir(bufnr) abort
|
||||
return gitgutter#utility#shellescape(fnamemodify(s:abs_path(a:bufnr, 0), ':h'))
|
||||
endfunction
|
||||
|
||||
" Not shellescaped.
|
||||
function! s:filename(bufnr) abort
|
||||
return fnamemodify(s:abs_path(a:bufnr, 0), ':t')
|
||||
endfunction
|
||||
|
||||
function! s:exists_file(bufnr) abort
|
||||
return filereadable(s:abs_path(a:bufnr, 0))
|
||||
endfunction
|
||||
|
||||
function! s:strip_trailing_new_line(line) abort
|
||||
return substitute(a:line, '\n$', '', '')
|
||||
endfunction
|
||||
|
||||
" True for git v1.7.2+.
|
||||
function! gitgutter#utility#git_supports_command_line_config_override() abort
|
||||
call system(g:gitgutter_git_executable.' -c foo.bar=baz --version')
|
||||
return !v:shell_error
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#stringify(list) abort
|
||||
return join(a:list, "\n")."\n"
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#use_known_shell() abort
|
||||
if has('unix')
|
||||
if &shell !=# 'sh'
|
||||
let s:shell = &shell
|
||||
let s:shellcmdflag = &shellcmdflag
|
||||
let s:shellredir = &shellredir
|
||||
let &shell = 'sh'
|
||||
set shellcmdflag=-c
|
||||
set shellredir=>%s\ 2>&1
|
||||
endif
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#restore_shell() abort
|
||||
if has('unix')
|
||||
if exists('s:shell')
|
||||
let &shell = s:shell
|
||||
let &shellcmdflag = s:shellcmdflag
|
||||
let &shellredir = s:shellredir
|
||||
endif
|
||||
endif
|
||||
function! s:windows()
|
||||
return has('win64') || has('win32') || has('win16')
|
||||
endfunction
|
||||
|
Reference in New Issue
Block a user