mirror of
https://github.com/airblade/vim-gitgutter.git
synced 2025-11-08 11:33:48 -05:00
Move every function into gitgutter namespace.
This makes profiling much easier.
This commit is contained in:
59
autoload/gitgutter/debug.vim
Normal file
59
autoload/gitgutter/debug.vim
Normal file
@@ -0,0 +1,59 @@
|
||||
function! gitgutter#debug#debug()
|
||||
" Open a scratch buffer
|
||||
vsplit __GitGutter_Debug__
|
||||
normal! ggdG
|
||||
setlocal buftype=nofile
|
||||
setlocal bufhidden=delete
|
||||
setlocal noswapfile
|
||||
|
||||
call gitgutter#debug#vim_version()
|
||||
call gitgutter#debug#separator()
|
||||
|
||||
call gitgutter#debug#git_version()
|
||||
call gitgutter#debug#separator()
|
||||
|
||||
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')
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#debug#separator()
|
||||
call gitgutter#debug#output('')
|
||||
endfunction
|
||||
|
||||
function! gitgutter#debug#vim_version()
|
||||
redir => version_info
|
||||
silent execute 'version'
|
||||
redir END
|
||||
call gitgutter#debug#output(split(version_info, '\n')[0:2])
|
||||
endfunction
|
||||
|
||||
function! gitgutter#debug#git_version()
|
||||
let v = system('git --version')
|
||||
call gitgutter#debug#output( substitute(v, '\n$', '', '') )
|
||||
endfunction
|
||||
|
||||
function! gitgutter#debug#option(name)
|
||||
if exists('+' . a:name)
|
||||
let v = eval('&' . a:name)
|
||||
call gitgutter#debug#output(a:name . '=' . v)
|
||||
" redir => output
|
||||
" silent execute "verbose set " . a:name . "?"
|
||||
" redir END
|
||||
" call gitgutter#debug#output(a:name . '=' . output)
|
||||
else
|
||||
call gitgutter#debug#output(a:name . ' [n/a]')
|
||||
end
|
||||
endfunction
|
||||
|
||||
function! gitgutter#debug#output(text)
|
||||
call append(line('$'), a:text)
|
||||
endfunction
|
||||
207
autoload/gitgutter/diff.vim
Normal file
207
autoload/gitgutter/diff.vim
Normal file
@@ -0,0 +1,207 @@
|
||||
let s:grep_available = executable('grep')
|
||||
let s:grep_command = ' | ' . (g:gitgutter_escape_grep ? '\grep' : 'grep') . ' -e ' . gitgutter#utility#shellescape('^@@ ')
|
||||
let s:hunk_re = '^@@ -\(\d\+\),\?\(\d*\) +\(\d\+\),\?\(\d*\) @@'
|
||||
|
||||
|
||||
function! gitgutter#diff#run_diff(realtime, use_external_grep)
|
||||
" Wrap compound command in parentheses to make Windows happy.
|
||||
let cmd = '(git ls-files --error-unmatch ' . gitgutter#utility#shellescape(gitgutter#utility#filename()) . ' && ('
|
||||
|
||||
if a:realtime
|
||||
let blob_name = ':' . gitgutter#utility#shellescape(gitgutter#utility#file_relative_to_repo_root())
|
||||
let blob_file = tempname()
|
||||
let cmd .= 'git show ' . blob_name . ' > ' . blob_file .
|
||||
\ ' && diff -U0 ' . g:gitgutter_diff_args . ' ' . blob_file . ' - '
|
||||
else
|
||||
let cmd .= 'git diff --no-ext-diff --no-color -U0 ' . g:gitgutter_diff_args . ' ' . gitgutter#utility#shellescape(gitgutter#utility#filename())
|
||||
endif
|
||||
|
||||
if a:use_external_grep && s:grep_available
|
||||
let cmd .= s:grep_command
|
||||
endif
|
||||
|
||||
if (a:use_external_grep && 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
|
||||
|
||||
let cmd .= '))'
|
||||
|
||||
if a:realtime
|
||||
let diff = gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file(cmd), gitgutter#utility#buffer_contents())
|
||||
else
|
||||
let diff = gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file(cmd))
|
||||
endif
|
||||
|
||||
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'
|
||||
endif
|
||||
|
||||
return diff
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#parse_diff(diff)
|
||||
let hunks = []
|
||||
for line in split(a:diff, '\n')
|
||||
let hunk_info = gitgutter#diff#parse_hunk(line)
|
||||
if len(hunk_info) == 4
|
||||
call add(hunks, hunk_info)
|
||||
endif
|
||||
endfor
|
||||
return hunks
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#parse_hunk(line)
|
||||
let matches = matchlist(a:line, s:hunk_re)
|
||||
if len(matches) > 0
|
||||
let from_line = str2nr(matches[1])
|
||||
let from_count = (matches[2] == '') ? 1 : str2nr(matches[2])
|
||||
let to_line = str2nr(matches[3])
|
||||
let to_count = (matches[4] == '') ? 1 : str2nr(matches[4])
|
||||
return [from_line, from_count, to_line, to_count]
|
||||
else
|
||||
return []
|
||||
end
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#process_hunks(hunks)
|
||||
call gitgutter#hunk#reset()
|
||||
let modified_lines = []
|
||||
for hunk in a:hunks
|
||||
call extend(modified_lines, gitgutter#diff#process_hunk(hunk))
|
||||
endfor
|
||||
return modified_lines
|
||||
endfunction
|
||||
|
||||
" Returns [ [<line_number (number)>, <name (string)>], ...]
|
||||
function! gitgutter#diff#process_hunk(hunk)
|
||||
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)
|
||||
|
||||
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 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 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 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)
|
||||
|
||||
endif
|
||||
return modifications
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#is_added(from_count, to_count)
|
||||
return a:from_count == 0 && a:to_count > 0
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#is_removed(from_count, to_count)
|
||||
return a:from_count > 0 && a:to_count == 0
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#is_modified(from_count, to_count)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
let offset = 0
|
||||
while offset < a:to_count
|
||||
let line_number = a:to_line + offset
|
||||
call add(a:modifications, [line_number, 'added'])
|
||||
let offset += 1
|
||||
endwhile
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#process_removed(modifications, from_count, to_count, to_line)
|
||||
if a:to_line == 0
|
||||
call add(a:modifications, [1, 'removed_first_line'])
|
||||
else
|
||||
call add(a:modifications, [a:to_line, 'removed'])
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#process_modified(modifications, from_count, to_count, to_line)
|
||||
let offset = 0
|
||||
while offset < a:to_count
|
||||
let line_number = a:to_line + offset
|
||||
call add(a:modifications, [line_number, 'modified'])
|
||||
let offset += 1
|
||||
endwhile
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#process_modified_and_added(modifications, from_count, to_count, to_line)
|
||||
let offset = 0
|
||||
while offset < a:from_count
|
||||
let line_number = a:to_line + offset
|
||||
call add(a:modifications, [line_number, 'modified'])
|
||||
let offset += 1
|
||||
endwhile
|
||||
while offset < a:to_count
|
||||
let line_number = a:to_line + offset
|
||||
call add(a:modifications, [line_number, 'added'])
|
||||
let offset += 1
|
||||
endwhile
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#process_modified_and_removed(modifications, from_count, to_count, to_line)
|
||||
let offset = 0
|
||||
while offset < a:to_count
|
||||
let line_number = a:to_line + offset
|
||||
call add(a:modifications, [line_number, 'modified'])
|
||||
let offset += 1
|
||||
endwhile
|
||||
let a:modifications[-1] = [a:to_line + offset - 1, 'modified_removed']
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#generate_diff_for_hunk(hunk, keep_header)
|
||||
let diff = gitgutter#diff#discard_hunks(gitgutter#diff#run_diff(0, 0), a:hunk, a:keep_header)
|
||||
if !a:keep_header
|
||||
" Discard summary line
|
||||
let diff = join(split(diff, '\n')[1:-1], "\n")
|
||||
endif
|
||||
return diff
|
||||
endfunction
|
||||
|
||||
function! gitgutter#diff#discard_hunks(diff, hunk_to_keep, keep_header)
|
||||
let modified_diff = []
|
||||
let keep_line = a:keep_header
|
||||
for line in split(a:diff, '\n')
|
||||
let hunk_info = gitgutter#diff#parse_hunk(line)
|
||||
if len(hunk_info) == 4 " start of new hunk
|
||||
let keep_line = (hunk_info == a:hunk_to_keep)
|
||||
endif
|
||||
if keep_line
|
||||
call add(modified_diff, line)
|
||||
endif
|
||||
endfor
|
||||
return join(modified_diff, "\n") . "\n"
|
||||
endfunction
|
||||
111
autoload/gitgutter/highlight.vim
Normal file
111
autoload/gitgutter/highlight.vim
Normal file
@@ -0,0 +1,111 @@
|
||||
function! gitgutter#highlight#define_sign_column_highlight()
|
||||
highlight default link SignColumn LineNr
|
||||
endfunction
|
||||
|
||||
function! gitgutter#highlight#define_highlights()
|
||||
let [guibg, ctermbg] = gitgutter#highlight#get_background_colors('SignColumn')
|
||||
|
||||
" Highlights used by the signs.
|
||||
|
||||
execute "highlight GitGutterAddDefault guifg=#009900 guibg=" . guibg . " ctermfg=2 ctermbg=" . ctermbg
|
||||
execute "highlight GitGutterChangeDefault guifg=#bbbb00 guibg=" . guibg . " ctermfg=3 ctermbg=" . ctermbg
|
||||
execute "highlight GitGutterDeleteDefault guifg=#ff2222 guibg=" . guibg . " ctermfg=1 ctermbg=" . ctermbg
|
||||
highlight default link GitGutterChangeDeleteDefault GitGutterChangeDefault
|
||||
|
||||
execute "highlight GitGutterAddInvisible guifg=bg guibg=" . guibg . " ctermfg=" . ctermbg . " ctermbg=" . ctermbg
|
||||
execute "highlight GitGutterChangeInvisible guifg=bg guibg=" . guibg . " ctermfg=" . ctermbg . " ctermbg=" . ctermbg
|
||||
execute "highlight GitGutterDeleteInvisible guifg=bg guibg=" . guibg . " ctermfg=" . ctermbg . " ctermbg=" . ctermbg
|
||||
highlight default link GitGutterChangeDeleteInvisible GitGutterChangeInvisble
|
||||
|
||||
highlight default link GitGutterAdd GitGutterAddDefault
|
||||
highlight default link GitGutterChange GitGutterChangeDefault
|
||||
highlight default link GitGutterDelete GitGutterDeleteDefault
|
||||
highlight default link GitGutterChangeDelete GitGutterChangeDeleteDefault
|
||||
|
||||
" Highlights used for the whole line.
|
||||
|
||||
highlight default link GitGutterAddLine DiffAdd
|
||||
highlight default link GitGutterChangeLine DiffChange
|
||||
highlight default link GitGutterDeleteLine DiffDelete
|
||||
highlight default link GitGutterChangeDeleteLine GitGutterChangeLineDefault
|
||||
endfunction
|
||||
|
||||
function! gitgutter#highlight#define_signs()
|
||||
sign define GitGutterLineAdded
|
||||
sign define GitGutterLineModified
|
||||
sign define GitGutterLineRemoved
|
||||
sign define GitGutterLineRemovedFirstLine
|
||||
sign define GitGutterLineModifiedRemoved
|
||||
sign define GitGutterDummy
|
||||
|
||||
call gitgutter#highlight#define_sign_text()
|
||||
call gitgutter#highlight#define_sign_text_highlights()
|
||||
call gitgutter#highlight#define_sign_line_highlights()
|
||||
endfunction
|
||||
|
||||
function! gitgutter#highlight#define_sign_text()
|
||||
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
|
||||
execute "sign define GitGutterLineRemovedFirstLine text=" . g:gitgutter_sign_removed_first_line
|
||||
execute "sign define GitGutterLineModifiedRemoved text=" . g:gitgutter_sign_modified_removed
|
||||
endfunction
|
||||
|
||||
function! gitgutter#highlight#define_sign_text_highlights()
|
||||
" Once a sign's text attribute has been defined, it cannot be undefined or
|
||||
" set to an empty value. So to make signs' text disappear (when toggling
|
||||
" off or disabling) we make them invisible by setting their foreground colours
|
||||
" to the background's.
|
||||
if g:gitgutter_signs
|
||||
sign define GitGutterLineAdded texthl=GitGutterAdd
|
||||
sign define GitGutterLineModified texthl=GitGutterChange
|
||||
sign define GitGutterLineRemoved texthl=GitGutterDelete
|
||||
sign define GitGutterLineRemovedFirstLine texthl=GitGutterDelete
|
||||
sign define GitGutterLineModifiedRemoved texthl=GitGutterChangeDelete
|
||||
else
|
||||
sign define GitGutterLineAdded texthl=GitGutterAddInvisible
|
||||
sign define GitGutterLineModified texthl=GitGutterChangeInvisible
|
||||
sign define GitGutterLineRemoved texthl=GitGutterDeleteInvisible
|
||||
sign define GitGutterLineRemovedFirstLine texthl=GitGutterDeleteInvisible
|
||||
sign define GitGutterLineModifiedRemoved texthl=GitGutterChangeDeleteInvisible
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#highlight#define_sign_line_highlights()
|
||||
if g:gitgutter_highlight_lines
|
||||
sign define GitGutterLineAdded linehl=GitGutterAddLine
|
||||
sign define GitGutterLineModified linehl=GitGutterChangeLine
|
||||
sign define GitGutterLineRemoved linehl=GitGutterDeleteLine
|
||||
sign define GitGutterLineRemovedFirstLine linehl=GitGutterDeleteLine
|
||||
sign define GitGutterLineModifiedRemoved linehl=GitGutterChangeDeleteLine
|
||||
else
|
||||
sign define GitGutterLineAdded linehl=
|
||||
sign define GitGutterLineModified linehl=
|
||||
sign define GitGutterLineRemoved linehl=
|
||||
sign define GitGutterLineRemovedFirstLine linehl=
|
||||
sign define GitGutterLineModifiedRemoved linehl=
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#highlight#get_background_colors(group)
|
||||
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])
|
||||
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]\+\)')
|
||||
return [guibg, ctermbg]
|
||||
endfunction
|
||||
|
||||
function! gitgutter#highlight#match_highlight(highlight, pattern)
|
||||
let matches = matchlist(a:highlight, a:pattern)
|
||||
if len(matches) == 0
|
||||
return 'NONE'
|
||||
endif
|
||||
return matches[1]
|
||||
endfunction
|
||||
88
autoload/gitgutter/hunk.vim
Normal file
88
autoload/gitgutter/hunk.vim
Normal file
@@ -0,0 +1,88 @@
|
||||
" number of lines [added, modified, removed]
|
||||
let s:summary = [0, 0, 0]
|
||||
let s:hunks = []
|
||||
|
||||
function! gitgutter#hunk#set_hunks(hunks)
|
||||
let s:hunks = a:hunks
|
||||
endfunction
|
||||
|
||||
function! gitgutter#hunk#hunks()
|
||||
return s:hunks
|
||||
endfunction
|
||||
|
||||
function! gitgutter#hunk#summary()
|
||||
return s:summary
|
||||
endfunction
|
||||
|
||||
function! gitgutter#hunk#reset()
|
||||
let s:summary = [0, 0, 0] " TODO: is bling/airline expecting [-1, -1, -1]?
|
||||
endfunction
|
||||
|
||||
function! gitgutter#hunk#increment_lines_added(count)
|
||||
let s:summary[0] += a:count
|
||||
endfunction
|
||||
|
||||
function! gitgutter#hunk#increment_lines_modified(count)
|
||||
let s:summary[1] += a:count
|
||||
endfunction
|
||||
|
||||
function! gitgutter#hunk#increment_lines_removed(count)
|
||||
let s:summary[2] += a:count
|
||||
endfunction
|
||||
|
||||
function! gitgutter#hunk#next_hunk(count)
|
||||
if gitgutter#utility#is_active()
|
||||
let current_line = line('.')
|
||||
let hunk_count = 0
|
||||
for hunk in s:hunks
|
||||
if hunk[2] > current_line
|
||||
let hunk_count += 1
|
||||
if hunk_count == a:count
|
||||
execute 'normal!' hunk[2] . 'G'
|
||||
break
|
||||
endif
|
||||
endif
|
||||
endfor
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#hunk#prev_hunk(count)
|
||||
if gitgutter#utility#is_active()
|
||||
let current_line = line('.')
|
||||
let hunk_count = 0
|
||||
for hunk in reverse(copy(s:hunks))
|
||||
if hunk[2] < current_line
|
||||
let hunk_count += 1
|
||||
if hunk_count == a:count
|
||||
let target = hunk[2] == 0 ? 1 : hunk[2]
|
||||
execute 'normal!' target . 'G'
|
||||
break
|
||||
endif
|
||||
endif
|
||||
endfor
|
||||
endif
|
||||
endfunction
|
||||
|
||||
" Returns the hunk the cursor is currently in or 0 if the cursor isn't in a
|
||||
" hunk.
|
||||
function! gitgutter#hunk#current_hunk()
|
||||
let current_hunk = []
|
||||
let current_line = line('.')
|
||||
|
||||
for hunk in s:hunks
|
||||
if current_line == 1 && hunk[2] == 0
|
||||
let current_hunk = hunk
|
||||
break
|
||||
endif
|
||||
|
||||
if current_line >= hunk[2] && current_line < hunk[2] + (hunk[3] == 0 ? 1 : hunk[3])
|
||||
let current_hunk = hunk
|
||||
break
|
||||
endif
|
||||
endfor
|
||||
|
||||
if len(current_hunk) == 4
|
||||
return current_hunk
|
||||
endif
|
||||
endfunction
|
||||
|
||||
166
autoload/gitgutter/sign.vim
Normal file
166
autoload/gitgutter/sign.vim
Normal file
@@ -0,0 +1,166 @@
|
||||
" Vim doesn't namespace sign ids so every plugin shares the same
|
||||
" namespace. Sign ids are simply integers so to avoid clashes with other
|
||||
" signs we guess at a clear run.
|
||||
"
|
||||
" Note also we currently never reset s:next_sign_id.
|
||||
let s:first_sign_id = 3000
|
||||
let s:next_sign_id = s:first_sign_id
|
||||
let s:dummy_sign_id = s:first_sign_id - 1
|
||||
" Remove-all-signs optimisation requires Vim 7.3.596+.
|
||||
let s:supports_star = v:version > 703 || (v:version == 703 && has("patch596"))
|
||||
|
||||
|
||||
" Removes gitgutter's signs (excluding dummy sign) from the given file.
|
||||
function! gitgutter#sign#clear_signs(file_name)
|
||||
call gitgutter#sign#find_current_signs(a:file_name)
|
||||
|
||||
let sign_ids = map(values(getbufvar(a:file_name, 'gitgutter_gitgutter_signs')), 'v:val.id')
|
||||
call gitgutter#sign#remove_signs(sign_ids, a:file_name, 1)
|
||||
call setbufvar(a:file_name, 'gitgutter_gitgutter_signs', {})
|
||||
endfunction
|
||||
|
||||
|
||||
" Updates gitgutter's signs in the given file.
|
||||
"
|
||||
" modified_lines: list of [<line_number (number)>, <name (string)>]
|
||||
" where name = 'added|removed|modified|modified_removed'
|
||||
function! gitgutter#sign#update_signs(file_name, modified_lines)
|
||||
call gitgutter#sign#find_current_signs(a:file_name)
|
||||
|
||||
let new_gitgutter_signs_line_numbers = map(copy(a:modified_lines), 'v:val[0]')
|
||||
let obsolete_signs = gitgutter#sign#obsolete_gitgutter_signs_to_remove(a:file_name, 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()
|
||||
endif
|
||||
|
||||
call gitgutter#sign#remove_signs(obsolete_signs, a:file_name, s:remove_all_old_signs)
|
||||
call gitgutter#sign#upsert_new_gitgutter_signs(a:file_name, a:modified_lines)
|
||||
|
||||
if flicker_possible
|
||||
call gitgutter#sign#remove_dummy_sign(0)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#sign#add_dummy_sign()
|
||||
if !getbufvar(gitgutter#utility#file(), 'gitgutter_dummy_sign')
|
||||
execute "sign place" s:dummy_sign_id "line=" . 9999 "name=GitGutterDummy file=" . gitgutter#utility#file()
|
||||
call setbufvar(gitgutter#utility#file(), 'gitgutter_dummy_sign', 1)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#sign#remove_dummy_sign(force)
|
||||
if getbufvar(gitgutter#utility#file(), 'gitgutter_dummy_sign') && (a:force || !g:gitgutter_sign_column_always)
|
||||
execute "sign unplace" s:dummy_sign_id "file=" . gitgutter#utility#file()
|
||||
call setbufvar(gitgutter#utility#file(), 'gitgutter_dummy_sign', 0)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
"
|
||||
" Internal functions
|
||||
"
|
||||
|
||||
|
||||
function! gitgutter#sign#find_current_signs(file_name)
|
||||
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 file=" . a:file_name
|
||||
redir END
|
||||
|
||||
for sign_line in filter(split(signs, '\n'), 'v:val =~# "="')
|
||||
" Typical sign line: line=88 id=1234 name=GitGutterLineAdded
|
||||
" We assume splitting is faster than a regexp.
|
||||
let components = split(sign_line)
|
||||
let name = split(components[2], '=')[1]
|
||||
if name =~# 'GitGutterDummy'
|
||||
let dummy_sign_placed = 1
|
||||
else
|
||||
let line_number = str2nr(split(components[0], '=')[1])
|
||||
if name =~# 'GitGutter'
|
||||
let id = str2nr(split(components[1], '=')[1])
|
||||
" Remove orphaned signs (signs placed on lines which have been deleted).
|
||||
" (When a line is deleted its sign lingers. Subsequent lines' signs'
|
||||
" line numbers are decremented appropriately.)
|
||||
if has_key(gitgutter_signs, line_number)
|
||||
execute "sign unplace" gitgutter_signs[line_number].id
|
||||
endif
|
||||
let gitgutter_signs[line_number] = {'id': id, 'name': name}
|
||||
else
|
||||
call add(other_signs, line_number)
|
||||
endif
|
||||
end
|
||||
endfor
|
||||
|
||||
call setbufvar(a:file_name, 'gitgutter_dummy_sign', dummy_sign_placed)
|
||||
call setbufvar(a:file_name, 'gitgutter_gitgutter_signs', gitgutter_signs)
|
||||
call setbufvar(a:file_name, 'gitgutter_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(file_name, new_gitgutter_signs_line_numbers)
|
||||
let signs_to_remove = [] " list of [<id (number)>, ...]
|
||||
let remove_all_signs = 1
|
||||
let old_gitgutter_signs = getbufvar(a:file_name, 'gitgutter_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)
|
||||
else
|
||||
let remove_all_signs = 0
|
||||
endif
|
||||
endfor
|
||||
let s:remove_all_old_signs = remove_all_signs
|
||||
return signs_to_remove
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#sign#remove_signs(sign_ids, file_name, all_signs)
|
||||
if a:all_signs && s:supports_star && empty(getbufvar(a:file_name, 'gitgutter_other_signs'))
|
||||
let dummy_sign_present = getbufvar(a:file_name, 'gitgutter_dummy_sign')
|
||||
execute "sign unplace * file=" . a:file_name
|
||||
if dummy_sign_present
|
||||
execute "sign place" s:dummy_sign_id "line=" . 9999 "name=GitGutterDummy file=" . a:file_name
|
||||
endif
|
||||
else
|
||||
for id in a:sign_ids
|
||||
execute "sign unplace" id
|
||||
endfor
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#sign#upsert_new_gitgutter_signs(file_name, modified_lines)
|
||||
let other_signs = getbufvar(a:file_name, 'gitgutter_other_signs')
|
||||
let old_gitgutter_signs = getbufvar(a:file_name, 'gitgutter_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])
|
||||
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 "file=" . a:file_name
|
||||
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 "file=" . a:file_name
|
||||
end
|
||||
endif
|
||||
endif
|
||||
endfor
|
||||
" At this point b:gitgutter_gitgutter_signs is out of date.
|
||||
endfunction
|
||||
|
||||
|
||||
function! gitgutter#sign#next_sign_id()
|
||||
let next_id = s:next_sign_id
|
||||
let s:next_sign_id += 1
|
||||
return next_id
|
||||
endfunction
|
||||
139
autoload/gitgutter/utility.vim
Normal file
139
autoload/gitgutter/utility.vim
Normal file
@@ -0,0 +1,139 @@
|
||||
let s:file = ''
|
||||
let s:using_xolox_shell = -1
|
||||
let s:exit_code = 0
|
||||
|
||||
|
||||
function! gitgutter#utility#is_active()
|
||||
return g:gitgutter_enabled && gitgutter#utility#exists_file()
|
||||
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
|
||||
function! gitgutter#utility#shellescape(arg)
|
||||
if a:arg =~ '^[A-Za-z0-9_/.-]\+$'
|
||||
return a:arg
|
||||
elseif &shell =~# 'cmd' || gitgutter#utility#using_xolox_shell()
|
||||
return '"' . substitute(substitute(a:arg, '"', '""', 'g'), '%', '"%"', 'g') . '"'
|
||||
else
|
||||
return shellescape(a:arg)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#current_file()
|
||||
return expand('%:p')
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#set_file(file)
|
||||
let s:file = a:file
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#file()
|
||||
return s:file
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#filename()
|
||||
return fnamemodify(s:file, ':t')
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#directory_of_file()
|
||||
return fnamemodify(s:file, ':h')
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#exists_file()
|
||||
return filereadable(gitgutter#utility#file())
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#has_unsaved_changes(file)
|
||||
return getbufvar(a:file, "&mod")
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#has_fresh_changes(file)
|
||||
return getbufvar(a:file, 'changedtick') != getbufvar(a:file, 'gitgutter_last_tick')
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#save_last_seen_change(file)
|
||||
call setbufvar(a:file, 'gitgutter_last_tick', getbufvar(a:file, 'changedtick'))
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#buffer_contents()
|
||||
if &fileformat ==# "dos"
|
||||
let eol = "\r\n"
|
||||
elseif &fileformat ==# "mac"
|
||||
let eol = "\r"
|
||||
else
|
||||
let eol = "\n"
|
||||
endif
|
||||
return join(getbufline(s:file, 1, '$'), eol) . eol
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#shell_error()
|
||||
return gitgutter#utility#using_xolox_shell() ? s:exit_code : v:shell_error
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#using_xolox_shell()
|
||||
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
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#system(cmd, ...)
|
||||
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
|
||||
let output = (a:0 == 0) ? system(a:cmd) : system(a:cmd, a:1)
|
||||
endif
|
||||
return output
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#file_relative_to_repo_root()
|
||||
let file_path_relative_to_repo_root = getbufvar(s:file, 'gitgutter_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('git 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 setbufvar(s:file, 'gitgutter_repo_relative_path', file_path_relative_to_repo_root)
|
||||
endif
|
||||
return file_path_relative_to_repo_root
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#command_in_directory_of_file(cmd)
|
||||
return 'cd ' . gitgutter#utility#shellescape(gitgutter#utility#directory_of_file()) . ' && ' . a:cmd
|
||||
endfunction
|
||||
|
||||
function! gitgutter#utility#highlight_name_for_change(text)
|
||||
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
|
||||
|
||||
function! gitgutter#utility#strip_trailing_new_line(line)
|
||||
return substitute(a:line, '\n$', '', '')
|
||||
endfunction
|
||||
Reference in New Issue
Block a user