diff --git a/autoload/debug.vim b/autoload/debug.vim deleted file mode 100644 index 9f6ae1b..0000000 --- a/autoload/debug.vim +++ /dev/null @@ -1,59 +0,0 @@ -function! debug#debug() - " Open a scratch buffer - vsplit __GitGutter_Debug__ - normal! ggdG - setlocal buftype=nofile - setlocal bufhidden=delete - setlocal noswapfile - - call debug#vim_version() - call debug#separator() - - call debug#git_version() - call debug#separator() - - call debug#option('shell') - call debug#option('shellcmdflag') - call debug#option('shellpipe') - call debug#option('shellquote') - call debug#option('shellredir') - call debug#option('shellslash') - call debug#option('shelltemp') - call debug#option('shelltype') - call debug#option('shellxescape') - call debug#option('shellxquote') -endfunction - - -function! debug#separator() - call debug#output('') -endfunction - -function! debug#vim_version() - redir => version_info - silent execute 'version' - redir END - call debug#output(split(version_info, '\n')[0:2]) -endfunction - -function! debug#git_version() - let v = system('git --version') - call debug#output( substitute(v, '\n$', '', '') ) -endfunction - -function! debug#option(name) - if exists('+' . a:name) - let v = eval('&' . a:name) - call debug#output(a:name . '=' . v) - " redir => output - " silent execute "verbose set " . a:name . "?" - " redir END - " call debug#output(a:name . '=' . output) - else - call debug#output(a:name . ' [n/a]') - end -endfunction - -function! debug#output(text) - call append(line('$'), a:text) -endfunction diff --git a/autoload/gitgutter.vim b/autoload/gitgutter.vim index df85166..63a241a 100644 --- a/autoload/gitgutter.vim +++ b/autoload/gitgutter.vim @@ -12,28 +12,28 @@ endfunction " file: (string) the file to process. " realtime: (boolean) when truthy, do a realtime diff; otherwise do a disk-based diff. function! gitgutter#process_buffer(file, realtime) - call utility#set_file(a:file) - if utility#is_active() + call gitgutter#utility#set_file(a:file) + if gitgutter#utility#is_active() if g:gitgutter_sign_column_always - call sign#add_dummy_sign() + call gitgutter#sign#add_dummy_sign() endif try - if !a:realtime || utility#has_fresh_changes(a:file) - let diff = diff#run_diff(a:realtime || utility#has_unsaved_changes(a:file), 1) - call hunk#set_hunks(diff#parse_diff(diff)) - let modified_lines = diff#process_hunks(hunk#hunks()) + if !a:realtime || gitgutter#utility#has_fresh_changes(a:file) + let diff = gitgutter#diff#run_diff(a:realtime || gitgutter#utility#has_unsaved_changes(a:file), 1) + call gitgutter#hunk#set_hunks(gitgutter#diff#parse_diff(diff)) + let modified_lines = gitgutter#diff#process_hunks(gitgutter#hunk#hunks()) if g:gitgutter_signs || g:gitgutter_highlight_lines - call sign#update_signs(a:file, modified_lines) + call gitgutter#sign#update_signs(a:file, modified_lines) endif - call utility#save_last_seen_change(a:file) + call gitgutter#utility#save_last_seen_change(a:file) endif catch /diff failed/ - call hunk#reset() + call gitgutter#hunk#reset() endtry else - call hunk#reset() + call gitgutter#hunk#reset() endif endfunction @@ -47,10 +47,10 @@ function! gitgutter#disable() for buffer_id in buflist let file = expand('#' . buffer_id . ':p') if !empty(file) - call utility#set_file(file) - call sign#clear_signs(utility#file()) - call sign#remove_dummy_sign(1) - call hunk#reset() + call gitgutter#utility#set_file(file) + call gitgutter#sign#clear_signs(gitgutter#utility#file()) + call gitgutter#sign#remove_dummy_sign(1) + call gitgutter#hunk#reset() endif endfor @@ -76,11 +76,11 @@ endfunction function! gitgutter#line_highlights_disable() let g:gitgutter_highlight_lines = 0 - call highlight#define_sign_line_highlights() + call gitgutter#highlight#define_sign_line_highlights() if !g:gitgutter_signs - call sign#clear_signs(utility#file()) - call sign#remove_dummy_sign(0) + call gitgutter#sign#clear_signs(gitgutter#utility#file()) + call gitgutter#sign#remove_dummy_sign(0) endif redraw! @@ -90,7 +90,7 @@ function! gitgutter#line_highlights_enable() let old_highlight_lines = g:gitgutter_highlight_lines let g:gitgutter_highlight_lines = 1 - call highlight#define_sign_line_highlights() + call gitgutter#highlight#define_sign_line_highlights() if !old_highlight_lines && !g:gitgutter_signs call gitgutter#all() @@ -115,7 +115,7 @@ function! gitgutter#signs_enable() let old_signs = g:gitgutter_signs let g:gitgutter_signs = 1 - call highlight#define_sign_text_highlights() + call gitgutter#highlight#define_sign_text_highlights() if !old_signs && !g:gitgutter_highlight_lines call gitgutter#all() @@ -124,11 +124,11 @@ endfunction function! gitgutter#signs_disable() let g:gitgutter_signs = 0 - call highlight#define_sign_text_highlights() + call gitgutter#highlight#define_sign_text_highlights() if !g:gitgutter_highlight_lines - call sign#clear_signs(utility#file()) - call sign#remove_dummy_sign(0) + call gitgutter#sign#clear_signs(gitgutter#utility#file()) + call gitgutter#sign#remove_dummy_sign(0) endif endfunction @@ -145,22 +145,22 @@ endfunction " Hunks {{{ function! gitgutter#stage_hunk() - if utility#is_active() + 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. silent write " find current hunk - let current_hunk = hunk#current_hunk() + let current_hunk = gitgutter#hunk#current_hunk() if empty(current_hunk) return endif " construct a diff - let diff_for_hunk = diff#generate_diff_for_hunk(current_hunk, 1) + let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk(current_hunk, 1) " apply the diff - call utility#system(utility#command_in_directory_of_file('git apply --cached --unidiff-zero - '), diff_for_hunk) + call gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file('git apply --cached --unidiff-zero - '), diff_for_hunk) " refresh gitgutter's view of buffer silent execute "GitGutter" @@ -168,22 +168,22 @@ function! gitgutter#stage_hunk() endfunction function! gitgutter#revert_hunk() - if utility#is_active() + 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. silent write " find current hunk - let current_hunk = hunk#current_hunk() + let current_hunk = gitgutter#hunk#current_hunk() if empty(current_hunk) return endif " construct a diff - let diff_for_hunk = diff#generate_diff_for_hunk(current_hunk, 1) + let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk(current_hunk, 1) " apply the diff - call utility#system(utility#command_in_directory_of_file('git apply --reverse --unidiff-zero - '), diff_for_hunk) + call gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file('git apply --reverse --unidiff-zero - '), diff_for_hunk) " reload file silent edit @@ -191,17 +191,17 @@ function! gitgutter#revert_hunk() endfunction function! gitgutter#preview_hunk() - if utility#is_active() + if gitgutter#utility#is_active() silent write " find current hunk - let current_hunk = hunk#current_hunk() + let current_hunk = gitgutter#hunk#current_hunk() if empty(current_hunk) return endif " construct a diff - let diff_for_hunk = diff#generate_diff_for_hunk(current_hunk, 0) + let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk(current_hunk, 0) " preview the diff silent! wincmd P diff --git a/autoload/gitgutter/debug.vim b/autoload/gitgutter/debug.vim new file mode 100644 index 0000000..c55efca --- /dev/null +++ b/autoload/gitgutter/debug.vim @@ -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 diff --git a/autoload/diff.vim b/autoload/gitgutter/diff.vim similarity index 55% rename from autoload/diff.vim rename to autoload/gitgutter/diff.vim index 7f3e350..c7eb200 100644 --- a/autoload/diff.vim +++ b/autoload/gitgutter/diff.vim @@ -1,19 +1,19 @@ let s:grep_available = executable('grep') -let s:grep_command = ' | ' . (g:gitgutter_escape_grep ? '\grep' : 'grep') . ' -e ' . utility#shellescape('^@@ ') +let s:grep_command = ' | ' . (g:gitgutter_escape_grep ? '\grep' : 'grep') . ' -e ' . gitgutter#utility#shellescape('^@@ ') let s:hunk_re = '^@@ -\(\d\+\),\?\(\d*\) +\(\d\+\),\?\(\d*\) @@' -function! diff#run_diff(realtime, use_external_grep) +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 ' . utility#shellescape(utility#filename()) . ' && (' + let cmd = '(git ls-files --error-unmatch ' . gitgutter#utility#shellescape(gitgutter#utility#filename()) . ' && (' if a:realtime - let blob_name = ':' . utility#shellescape(utility#file_relative_to_repo_root()) + 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 . ' ' . utility#shellescape(utility#filename()) + 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 @@ -31,12 +31,12 @@ function! diff#run_diff(realtime, use_external_grep) let cmd .= '))' if a:realtime - let diff = utility#system(utility#command_in_directory_of_file(cmd), utility#buffer_contents()) + let diff = gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file(cmd), gitgutter#utility#buffer_contents()) else - let diff = utility#system(utility#command_in_directory_of_file(cmd)) + let diff = gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file(cmd)) endif - if utility#shell_error() + 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' @@ -45,10 +45,10 @@ function! diff#run_diff(realtime, use_external_grep) return diff endfunction -function! diff#parse_diff(diff) +function! gitgutter#diff#parse_diff(diff) let hunks = [] for line in split(a:diff, '\n') - let hunk_info = diff#parse_hunk(line) + let hunk_info = gitgutter#diff#parse_hunk(line) if len(hunk_info) == 4 call add(hunks, hunk_info) endif @@ -56,7 +56,7 @@ function! diff#parse_diff(diff) return hunks endfunction -function! diff#parse_hunk(line) +function! gitgutter#diff#parse_hunk(line) let matches = matchlist(a:line, s:hunk_re) if len(matches) > 0 let from_line = str2nr(matches[1]) @@ -69,70 +69,70 @@ function! diff#parse_hunk(line) end endfunction -function! diff#process_hunks(hunks) - call hunk#reset() +function! gitgutter#diff#process_hunks(hunks) + call gitgutter#hunk#reset() let modified_lines = [] for hunk in a:hunks - call extend(modified_lines, diff#process_hunk(hunk)) + call extend(modified_lines, gitgutter#diff#process_hunk(hunk)) endfor return modified_lines endfunction " Returns [ [, ], ...] -function! diff#process_hunk(hunk) +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 diff#is_added(from_count, to_count) - call diff#process_added(modifications, from_count, to_count, to_line) - call hunk#increment_lines_added(to_count) + 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 diff#is_removed(from_count, to_count) - call diff#process_removed(modifications, from_count, to_count, to_line) - call hunk#increment_lines_removed(from_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 diff#is_modified(from_count, to_count) - call diff#process_modified(modifications, from_count, to_count, to_line) - call hunk#increment_lines_modified(to_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 diff#is_modified_and_added(from_count, to_count) - call diff#process_modified_and_added(modifications, from_count, to_count, to_line) - call hunk#increment_lines_added(to_count - from_count) - call hunk#increment_lines_modified(from_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 diff#is_modified_and_removed(from_count, to_count) - call diff#process_modified_and_removed(modifications, from_count, to_count, to_line) - call hunk#increment_lines_modified(to_count) - call hunk#increment_lines_removed(from_count - to_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! diff#is_added(from_count, to_count) +function! gitgutter#diff#is_added(from_count, to_count) return a:from_count == 0 && a:to_count > 0 endfunction -function! diff#is_removed(from_count, to_count) +function! gitgutter#diff#is_removed(from_count, to_count) return a:from_count > 0 && a:to_count == 0 endfunction -function! diff#is_modified(from_count, to_count) +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! diff#is_modified_and_added(from_count, to_count) +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! diff#is_modified_and_removed(from_count, to_count) +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! diff#process_added(modifications, from_count, to_count, to_line) +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 @@ -141,7 +141,7 @@ function! diff#process_added(modifications, from_count, to_count, to_line) endwhile endfunction -function! diff#process_removed(modifications, from_count, to_count, to_line) +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 @@ -149,7 +149,7 @@ function! diff#process_removed(modifications, from_count, to_count, to_line) endif endfunction -function! diff#process_modified(modifications, from_count, to_count, to_line) +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 @@ -158,7 +158,7 @@ function! diff#process_modified(modifications, from_count, to_count, to_line) endwhile endfunction -function! diff#process_modified_and_added(modifications, from_count, to_count, to_line) +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 @@ -172,7 +172,7 @@ function! diff#process_modified_and_added(modifications, from_count, to_count, t endwhile endfunction -function! diff#process_modified_and_removed(modifications, from_count, to_count, to_line) +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 @@ -182,8 +182,8 @@ function! diff#process_modified_and_removed(modifications, from_count, to_count, let a:modifications[-1] = [a:to_line + offset - 1, 'modified_removed'] endfunction -function! diff#generate_diff_for_hunk(hunk, keep_header) - let diff = diff#discard_hunks(diff#run_diff(0, 0), a:hunk, a:keep_header) +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") @@ -191,11 +191,11 @@ function! diff#generate_diff_for_hunk(hunk, keep_header) return diff endfunction -function! diff#discard_hunks(diff, hunk_to_keep, keep_header) +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 = diff#parse_hunk(line) + 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 diff --git a/autoload/highlight.vim b/autoload/gitgutter/highlight.vim similarity index 82% rename from autoload/highlight.vim rename to autoload/gitgutter/highlight.vim index 94e6b51..dd6d56a 100644 --- a/autoload/highlight.vim +++ b/autoload/gitgutter/highlight.vim @@ -1,9 +1,9 @@ -function! highlight#define_sign_column_highlight() +function! gitgutter#highlight#define_sign_column_highlight() highlight default link SignColumn LineNr endfunction -function! highlight#define_highlights() - let [guibg, ctermbg] = highlight#get_background_colors('SignColumn') +function! gitgutter#highlight#define_highlights() + let [guibg, ctermbg] = gitgutter#highlight#get_background_colors('SignColumn') " Highlights used by the signs. @@ -30,7 +30,7 @@ function! highlight#define_highlights() highlight default link GitGutterChangeDeleteLine GitGutterChangeLineDefault endfunction -function! highlight#define_signs() +function! gitgutter#highlight#define_signs() sign define GitGutterLineAdded sign define GitGutterLineModified sign define GitGutterLineRemoved @@ -38,12 +38,12 @@ function! highlight#define_signs() sign define GitGutterLineModifiedRemoved sign define GitGutterDummy - call highlight#define_sign_text() - call highlight#define_sign_text_highlights() - call highlight#define_sign_line_highlights() + call gitgutter#highlight#define_sign_text() + call gitgutter#highlight#define_sign_text_highlights() + call gitgutter#highlight#define_sign_line_highlights() endfunction -function! highlight#define_sign_text() +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 @@ -51,7 +51,7 @@ function! highlight#define_sign_text() execute "sign define GitGutterLineModifiedRemoved text=" . g:gitgutter_sign_modified_removed endfunction -function! highlight#define_sign_text_highlights() +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 @@ -71,7 +71,7 @@ function! highlight#define_sign_text_highlights() endif endfunction -function! highlight#define_sign_line_highlights() +function! gitgutter#highlight#define_sign_line_highlights() if g:gitgutter_highlight_lines sign define GitGutterLineAdded linehl=GitGutterAddLine sign define GitGutterLineModified linehl=GitGutterChangeLine @@ -87,22 +87,22 @@ function! highlight#define_sign_line_highlights() endif endfunction -function! highlight#get_background_colors(group) +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 highlight#get_background_colors(link_matches[1]) + return gitgutter#highlight#get_background_colors(link_matches[1]) endif - let ctermbg = highlight#match_highlight(highlight, 'ctermbg=\([0-9A-Za-z]\+\)') - let guibg = highlight#match_highlight(highlight, 'guibg=\([#0-9A-Za-z]\+\)') + 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! highlight#match_highlight(highlight, pattern) +function! gitgutter#highlight#match_highlight(highlight, pattern) let matches = matchlist(a:highlight, a:pattern) if len(matches) == 0 return 'NONE' diff --git a/autoload/hunk.vim b/autoload/gitgutter/hunk.vim similarity index 75% rename from autoload/hunk.vim rename to autoload/gitgutter/hunk.vim index ee6655d..0f49691 100644 --- a/autoload/hunk.vim +++ b/autoload/gitgutter/hunk.vim @@ -2,36 +2,36 @@ let s:summary = [0, 0, 0] let s:hunks = [] -function! hunk#set_hunks(hunks) +function! gitgutter#hunk#set_hunks(hunks) let s:hunks = a:hunks endfunction -function! hunk#hunks() +function! gitgutter#hunk#hunks() return s:hunks endfunction -function! hunk#summary() +function! gitgutter#hunk#summary() return s:summary endfunction -function! hunk#reset() +function! gitgutter#hunk#reset() let s:summary = [0, 0, 0] " TODO: is bling/airline expecting [-1, -1, -1]? endfunction -function! hunk#increment_lines_added(count) +function! gitgutter#hunk#increment_lines_added(count) let s:summary[0] += a:count endfunction -function! hunk#increment_lines_modified(count) +function! gitgutter#hunk#increment_lines_modified(count) let s:summary[1] += a:count endfunction -function! hunk#increment_lines_removed(count) +function! gitgutter#hunk#increment_lines_removed(count) let s:summary[2] += a:count endfunction -function! hunk#next_hunk(count) - if utility#is_active() +function! gitgutter#hunk#next_hunk(count) + if gitgutter#utility#is_active() let current_line = line('.') let hunk_count = 0 for hunk in s:hunks @@ -46,8 +46,8 @@ function! hunk#next_hunk(count) endif endfunction -function! hunk#prev_hunk(count) - if utility#is_active() +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)) @@ -65,7 +65,7 @@ endfunction " Returns the hunk the cursor is currently in or 0 if the cursor isn't in a " hunk. -function! hunk#current_hunk() +function! gitgutter#hunk#current_hunk() let current_hunk = [] let current_line = line('.') diff --git a/autoload/sign.vim b/autoload/gitgutter/sign.vim similarity index 73% rename from autoload/sign.vim rename to autoload/gitgutter/sign.vim index b555128..60d3978 100644 --- a/autoload/sign.vim +++ b/autoload/gitgutter/sign.vim @@ -11,11 +11,11 @@ let s:supports_star = v:version > 703 || (v:version == 703 && has("patch596")) " Removes gitgutter's signs (excluding dummy sign) from the given file. -function! sign#clear_signs(file_name) - call sign#find_current_signs(a:file_name) +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 sign#remove_signs(sign_ids, a:file_name, 1) + call gitgutter#sign#remove_signs(sign_ids, a:file_name, 1) call setbufvar(a:file_name, 'gitgutter_gitgutter_signs', {}) endfunction @@ -24,37 +24,37 @@ endfunction " " modified_lines: list of [, ] " where name = 'added|removed|modified|modified_removed' -function! sign#update_signs(file_name, modified_lines) - call sign#find_current_signs(a:file_name) +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 = sign#obsolete_gitgutter_signs_to_remove(a:file_name, new_gitgutter_signs_line_numbers) + 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 sign#add_dummy_sign() + call gitgutter#sign#add_dummy_sign() endif - call sign#remove_signs(obsolete_signs, a:file_name, s:remove_all_old_signs) - call sign#upsert_new_gitgutter_signs(a:file_name, a:modified_lines) + 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 sign#remove_dummy_sign(0) + call gitgutter#sign#remove_dummy_sign(0) endif endfunction -function! sign#add_dummy_sign() - if !getbufvar(utility#file(), 'gitgutter_dummy_sign') - execute "sign place" s:dummy_sign_id "line=" . 9999 "name=GitGutterDummy file=" . utility#file() - call setbufvar(utility#file(), 'gitgutter_dummy_sign', 1) +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! sign#remove_dummy_sign(force) - if getbufvar(utility#file(), 'gitgutter_dummy_sign') && (a:force || !g:gitgutter_sign_column_always) - execute "sign unplace" s:dummy_sign_id "file=" . utility#file() - call setbufvar(utility#file(), 'gitgutter_dummy_sign', 0) +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 @@ -64,7 +64,7 @@ endfunction " -function! sign#find_current_signs(file_name) +function! gitgutter#sign#find_current_signs(file_name) let gitgutter_signs = {} " : {'id': , 'name': } let other_signs = [] " [, ...] " Sets `s:remove_all_old_signs` as a side-effect. -function! sign#obsolete_gitgutter_signs_to_remove(file_name, new_gitgutter_signs_line_numbers) +function! gitgutter#sign#obsolete_gitgutter_signs_to_remove(file_name, new_gitgutter_signs_line_numbers) let signs_to_remove = [] " list of [, ...] let remove_all_signs = 1 let old_gitgutter_signs = getbufvar(a:file_name, 'gitgutter_gitgutter_signs') @@ -121,7 +121,7 @@ function! sign#obsolete_gitgutter_signs_to_remove(file_name, new_gitgutter_signs endfunction -function! sign#remove_signs(sign_ids, file_name, all_signs) +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 @@ -136,16 +136,16 @@ function! sign#remove_signs(sign_ids, file_name, all_signs) endfunction -function! sign#upsert_new_gitgutter_signs(file_name, modified_lines) +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] " if index(other_signs, line_number) == -1 " don't clobber others' signs - let name = utility#highlight_name_for_change(line[1]) + let name = gitgutter#utility#highlight_name_for_change(line[1]) if !has_key(old_gitgutter_signs, line_number) " insert - let id = sign#next_sign_id() + 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] @@ -159,7 +159,7 @@ function! sign#upsert_new_gitgutter_signs(file_name, modified_lines) endfunction -function! sign#next_sign_id() +function! gitgutter#sign#next_sign_id() let next_id = s:next_sign_id let s:next_sign_id += 1 return next_id diff --git a/autoload/utility.vim b/autoload/gitgutter/utility.vim similarity index 63% rename from autoload/utility.vim rename to autoload/gitgutter/utility.vim index 10bc385..0137094 100644 --- a/autoload/utility.vim +++ b/autoload/gitgutter/utility.vim @@ -3,8 +3,8 @@ let s:using_xolox_shell = -1 let s:exit_code = 0 -function! utility#is_active() - return g:gitgutter_enabled && utility#exists_file() +function! gitgutter#utility#is_active() + return g:gitgutter_enabled && gitgutter#utility#exists_file() endfunction " A replacement for the built-in `shellescape(arg)`. @@ -14,53 +14,53 @@ endfunction " " See: " https://github.com/tpope/vim-fugitive/blob/8f0b8edfbd246c0026b7a2388e1d883d579ac7f6/plugin/fugitive.vim#L29-L37 -function! utility#shellescape(arg) +function! gitgutter#utility#shellescape(arg) if a:arg =~ '^[A-Za-z0-9_/.-]\+$' return a:arg - elseif &shell =~# 'cmd' || utility#using_xolox_shell() + elseif &shell =~# 'cmd' || gitgutter#utility#using_xolox_shell() return '"' . substitute(substitute(a:arg, '"', '""', 'g'), '%', '"%"', 'g') . '"' else return shellescape(a:arg) endif endfunction -function! utility#current_file() +function! gitgutter#utility#current_file() return expand('%:p') endfunction -function! utility#set_file(file) +function! gitgutter#utility#set_file(file) let s:file = a:file endfunction -function! utility#file() +function! gitgutter#utility#file() return s:file endfunction -function! utility#filename() +function! gitgutter#utility#filename() return fnamemodify(s:file, ':t') endfunction -function! utility#directory_of_file() +function! gitgutter#utility#directory_of_file() return fnamemodify(s:file, ':h') endfunction -function! utility#exists_file() - return filereadable(utility#file()) +function! gitgutter#utility#exists_file() + return filereadable(gitgutter#utility#file()) endfunction -function! utility#has_unsaved_changes(file) +function! gitgutter#utility#has_unsaved_changes(file) return getbufvar(a:file, "&mod") endfunction -function! utility#has_fresh_changes(file) +function! gitgutter#utility#has_fresh_changes(file) return getbufvar(a:file, 'changedtick') != getbufvar(a:file, 'gitgutter_last_tick') endfunction -function! utility#save_last_seen_change(file) +function! gitgutter#utility#save_last_seen_change(file) call setbufvar(a:file, 'gitgutter_last_tick', getbufvar(a:file, 'changedtick')) endfunction -function! utility#buffer_contents() +function! gitgutter#utility#buffer_contents() if &fileformat ==# "dos" let eol = "\r\n" elseif &fileformat ==# "mac" @@ -71,11 +71,11 @@ function! utility#buffer_contents() return join(getbufline(s:file, 1, '$'), eol) . eol endfunction -function! utility#shell_error() - return utility#using_xolox_shell() ? s:exit_code : v:shell_error +function! gitgutter#utility#shell_error() + return gitgutter#utility#using_xolox_shell() ? s:exit_code : v:shell_error endfunction -function! utility#using_xolox_shell() +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 @@ -90,8 +90,8 @@ function! utility#using_xolox_shell() return s:using_xolox_shell endfunction -function! utility#system(cmd, ...) - if utility#using_xolox_shell() +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 @@ -105,22 +105,22 @@ function! utility#system(cmd, ...) return output endfunction -function! utility#file_relative_to_repo_root() +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 = utility#system(utility#command_in_directory_of_file('git rev-parse --show-prefix')) - let dir_path_relative_to_repo_root = utility#strip_trailing_new_line(dir_path_relative_to_repo_root) - let file_path_relative_to_repo_root = dir_path_relative_to_repo_root . utility#filename() + 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! utility#command_in_directory_of_file(cmd) - return 'cd ' . utility#shellescape(utility#directory_of_file()) . ' && ' . a:cmd +function! gitgutter#utility#command_in_directory_of_file(cmd) + return 'cd ' . gitgutter#utility#shellescape(gitgutter#utility#directory_of_file()) . ' && ' . a:cmd endfunction -function! utility#highlight_name_for_change(text) +function! gitgutter#utility#highlight_name_for_change(text) if a:text ==# 'added' return 'GitGutterLineAdded' elseif a:text ==# 'removed' @@ -134,6 +134,6 @@ function! utility#highlight_name_for_change(text) endif endfunction -function! utility#strip_trailing_new_line(line) +function! gitgutter#utility#strip_trailing_new_line(line) return substitute(a:line, '\n$', '', '') endfunction diff --git a/plugin/gitgutter.vim b/plugin/gitgutter.vim index b1baa38..a60d8ba 100644 --- a/plugin/gitgutter.vim +++ b/plugin/gitgutter.vim @@ -43,16 +43,16 @@ call s:set('g:gitgutter_escape_grep', 0) call s:set('g:gitgutter_map_keys', 1) call s:set('g:gitgutter_avoid_cmd_prompt_on_windows', 1) -call highlight#define_sign_column_highlight() -call highlight#define_highlights() -call highlight#define_signs() +call gitgutter#highlight#define_sign_column_highlight() +call gitgutter#highlight#define_highlights() +call gitgutter#highlight#define_signs() " }}} " Primary functions {{{ command GitGutterAll call gitgutter#all() -command GitGutter call gitgutter#process_buffer(utility#current_file(), 0) +command GitGutter call gitgutter#process_buffer(gitgutter#utility#current_file(), 0) command GitGutterDisable call gitgutter#disable() command GitGutterEnable call gitgutter#enable() @@ -78,8 +78,8 @@ command GitGutterSignsToggle call gitgutter#signs_toggle() " Hunks {{{ -command -count=1 GitGutterNextHunk call hunk#next_hunk() -command -count=1 GitGutterPrevHunk call hunk#prev_hunk() +command -count=1 GitGutterNextHunk call gitgutter#hunk#next_hunk() +command -count=1 GitGutterPrevHunk call gitgutter#hunk#prev_hunk() command GitGutterStageHunk call gitgutter#stage_hunk() command GitGutterRevertHunk call gitgutter#revert_hunk() @@ -103,19 +103,19 @@ command GitGutterPreviewHunk call gitgutter#preview_hunk() " `line` - refers to the line number where the change starts " `count` - refers to the number of lines the change covers function! GitGutterGetHunks() - return utility#is_active() ? hunk#hunks() : [] + return gitgutter#utility#is_active() ? gitgutter#hunk#hunks() : [] endfunction " Returns an array that contains a summary of the current hunk status. " The format is [ added, modified, removed ], where each value represents " the number of lines added/modified/removed respectively. function! GitGutterGetHunkSummary() - return hunk#summary() + return gitgutter#hunk#summary() endfunction " }}} -command GitGutterDebug call debug#debug() +command GitGutterDebug call gitgutter#debug#debug() " Maps {{{ @@ -156,7 +156,7 @@ augroup gitgutter autocmd! if g:gitgutter_realtime - autocmd CursorHold,CursorHoldI * call gitgutter#process_buffer(utility#current_file(), 1) + autocmd CursorHold,CursorHoldI * call gitgutter#process_buffer(gitgutter#utility#current_file(), 1) endif if g:gitgutter_eager @@ -164,7 +164,7 @@ augroup gitgutter \ if gettabvar(tabpagenr(), 'gitgutter_didtabenter') \| call settabvar(tabpagenr(), 'gitgutter_didtabenter', 0) \| else - \| call gitgutter#process_buffer(utility#current_file(), 0) + \| call gitgutter#process_buffer(gitgutter#utility#current_file(), 0) \| endif autocmd TabEnter * \ call settabvar(tabpagenr(), 'gitgutter_didtabenter', 1) @@ -173,10 +173,10 @@ augroup gitgutter autocmd FocusGained * call gitgutter#all() endif else - autocmd BufRead,BufWritePost,FileChangedShellPost * call gitgutter#process_buffer(utility#current_file(), 0) + autocmd BufRead,BufWritePost,FileChangedShellPost * call gitgutter#process_buffer(gitgutter#utility#current_file(), 0) endif - autocmd ColorScheme * call highlight#define_sign_column_highlight() | call highlight#define_highlights() + autocmd ColorScheme * call gitgutter#highlight#define_sign_column_highlight() | call gitgutter#highlight#define_highlights() " Disable during :vimgrep autocmd QuickFixCmdPre *vimgrep* let g:gitgutter_enabled = 0