mirror of
https://github.com/airblade/vim-gitgutter.git
synced 2025-11-09 03:53:47 -05:00
Move logic out of main plugin file.
This commit is contained in:
155
autoload/gitgutter.vim
Normal file
155
autoload/gitgutter.vim
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
" Primary functions {{{
|
||||||
|
|
||||||
|
function! gitgutter#all()
|
||||||
|
for buffer_id in tabpagebuflist()
|
||||||
|
let file = expand('#' . buffer_id . ':p')
|
||||||
|
if !empty(file)
|
||||||
|
call gitgutter#process_buffer(file, 0)
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
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()
|
||||||
|
if g:gitgutter_sign_column_always
|
||||||
|
call 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 g:gitgutter_signs
|
||||||
|
call sign#update_signs(a:file, modified_lines)
|
||||||
|
endif
|
||||||
|
|
||||||
|
call utility#save_last_seen_change(a:file)
|
||||||
|
endif
|
||||||
|
catch /diff failed/
|
||||||
|
call hunk#reset()
|
||||||
|
endtry
|
||||||
|
else
|
||||||
|
call hunk#reset()
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! gitgutter#disable()
|
||||||
|
let g:gitgutter_enabled = 0
|
||||||
|
call sign#clear_signs(utility#file())
|
||||||
|
call sign#remove_dummy_sign(1)
|
||||||
|
call hunk#reset()
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! gitgutter#enable()
|
||||||
|
let g:gitgutter_enabled = 1
|
||||||
|
call gitgutter#process_buffer(utility#current_file(), 0)
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! gitgutter#toggle()
|
||||||
|
if g:gitgutter_enabled
|
||||||
|
call gitgutter#disable()
|
||||||
|
else
|
||||||
|
call gitgutter#enable()
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
" Line highlights {{{
|
||||||
|
|
||||||
|
function! gitgutter#line_highlights_disable()
|
||||||
|
let g:gitgutter_highlight_lines = 0
|
||||||
|
call highlight#define_sign_line_highlights()
|
||||||
|
redraw!
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! gitgutter#line_highlights_enable()
|
||||||
|
let g:gitgutter_highlight_lines = 1
|
||||||
|
call highlight#define_sign_line_highlights()
|
||||||
|
redraw!
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! gitgutter#line_highlights_toggle()
|
||||||
|
let g:gitgutter_highlight_lines = !g:gitgutter_highlight_lines
|
||||||
|
call highlight#define_sign_line_highlights()
|
||||||
|
redraw!
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
" Signs {{{
|
||||||
|
|
||||||
|
function! gitgutter#signs_enable()
|
||||||
|
let g:gitgutter_signs = 1
|
||||||
|
call gitgutter#all()
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! gitgutter#signs_disable()
|
||||||
|
let g:gitgutter_signs = 0
|
||||||
|
call sign#clear_signs(utility#file())
|
||||||
|
call sign#remove_dummy_sign(0)
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! gitgutter#signs_toggle()
|
||||||
|
if g:gitgutter_signs
|
||||||
|
call gitgutter#signs_disable()
|
||||||
|
else
|
||||||
|
call gitgutter#signs_enable()
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
" Hunks {{{
|
||||||
|
|
||||||
|
function! gitgutter#stage_hunk()
|
||||||
|
if 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()
|
||||||
|
if empty(current_hunk)
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
|
||||||
|
" construct a diff
|
||||||
|
let diff_for_hunk = diff#generate_diff_for_hunk(current_hunk)
|
||||||
|
|
||||||
|
" apply the diff
|
||||||
|
call system(utility#command_in_directory_of_file('git apply --cached --unidiff-zero - '), diff_for_hunk)
|
||||||
|
|
||||||
|
" refresh gitgutter's view of buffer
|
||||||
|
silent execute "GitGutter"
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! gitgutter#revert_hunk()
|
||||||
|
if 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()
|
||||||
|
if empty(current_hunk)
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
|
||||||
|
" construct a diff
|
||||||
|
let diff_for_hunk = diff#generate_diff_for_hunk(current_hunk)
|
||||||
|
|
||||||
|
" apply the diff
|
||||||
|
call system(utility#command_in_directory_of_file('git apply --reverse --unidiff-zero - '), diff_for_hunk)
|
||||||
|
|
||||||
|
" reload file
|
||||||
|
silent edit
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
" }}}
|
||||||
@@ -45,196 +45,40 @@ call highlight#define_signs()
|
|||||||
|
|
||||||
" }}}
|
" }}}
|
||||||
|
|
||||||
|
|
||||||
" Primary functions {{{
|
" Primary functions {{{
|
||||||
|
|
||||||
function! GitGutterAll()
|
command GitGutterAll call gitgutter#all()
|
||||||
for buffer_id in tabpagebuflist()
|
command GitGutter call gitgutter#process_buffer(utility#current_file(), 0)
|
||||||
let file = expand('#' . buffer_id . ':p')
|
|
||||||
if !empty(file)
|
|
||||||
call GitGutter(file, 0)
|
|
||||||
endif
|
|
||||||
endfor
|
|
||||||
endfunction
|
|
||||||
command GitGutterAll call GitGutterAll()
|
|
||||||
|
|
||||||
" Does the actual work.
|
command GitGutterDisable call gitgutter#disable()
|
||||||
"
|
command GitGutterEnable call gitgutter#enable()
|
||||||
" file: (string) the file to process.
|
command GitGutterToggle call gitgutter#toggle()
|
||||||
" realtime: (boolean) when truthy, do a realtime diff; otherwise do a disk-based diff.
|
|
||||||
function! GitGutter(file, realtime)
|
|
||||||
call utility#set_file(a:file)
|
|
||||||
if utility#is_active()
|
|
||||||
if g:gitgutter_sign_column_always
|
|
||||||
call 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 g:gitgutter_signs
|
|
||||||
call sign#update_signs(a:file, modified_lines)
|
|
||||||
endif
|
|
||||||
|
|
||||||
call utility#save_last_seen_change(a:file)
|
|
||||||
endif
|
|
||||||
catch /diff failed/
|
|
||||||
call hunk#reset()
|
|
||||||
endtry
|
|
||||||
else
|
|
||||||
call hunk#reset()
|
|
||||||
endif
|
|
||||||
endfunction
|
|
||||||
command GitGutter call GitGutter(utility#current_file(), 0)
|
|
||||||
|
|
||||||
" }}}
|
" }}}
|
||||||
|
|
||||||
|
" Line highlights {{{
|
||||||
|
|
||||||
" The plugin: enable / disable / toggle {{{
|
command GitGutterLineHighlightsDisable call gitgutter#line_highlights_disable()
|
||||||
|
command GitGutterLineHighlightsEnable call gitgutter#line_highlights_enable()
|
||||||
function! GitGutterDisable()
|
command GitGutterLineHighlightsToggle call gitgutter#line_highlights_toggle()
|
||||||
let g:gitgutter_enabled = 0
|
|
||||||
call sign#clear_signs(utility#file())
|
|
||||||
call sign#remove_dummy_sign(1)
|
|
||||||
call hunk#reset()
|
|
||||||
endfunction
|
|
||||||
command GitGutterDisable call GitGutterDisable()
|
|
||||||
|
|
||||||
function! GitGutterEnable()
|
|
||||||
let g:gitgutter_enabled = 1
|
|
||||||
call GitGutter(utility#current_file(), 0)
|
|
||||||
endfunction
|
|
||||||
command GitGutterEnable call GitGutterEnable()
|
|
||||||
|
|
||||||
function! GitGutterToggle()
|
|
||||||
if g:gitgutter_enabled
|
|
||||||
call GitGutterDisable()
|
|
||||||
else
|
|
||||||
call GitGutterEnable()
|
|
||||||
endif
|
|
||||||
endfunction
|
|
||||||
command GitGutterToggle call GitGutterToggle()
|
|
||||||
|
|
||||||
" }}}
|
" }}}
|
||||||
|
|
||||||
|
" Signs {{{
|
||||||
|
|
||||||
" Line highlights: enable / disable / toggle {{{
|
command GitGutterSignsEnable call gitgutter#signs_enable()
|
||||||
|
command GitGutterSignsDisable call gitgutter#signs_disable()
|
||||||
function! GitGutterLineHighlightsDisable()
|
command GitGutterSignsToggle call gitgutter#signs_toggle()
|
||||||
let g:gitgutter_highlight_lines = 0
|
|
||||||
call highlight#define_sign_line_highlights()
|
|
||||||
redraw!
|
|
||||||
endfunction
|
|
||||||
command GitGutterLineHighlightsDisable call GitGutterLineHighlightsDisable()
|
|
||||||
|
|
||||||
function! GitGutterLineHighlightsEnable()
|
|
||||||
let g:gitgutter_highlight_lines = 1
|
|
||||||
call highlight#define_sign_line_highlights()
|
|
||||||
redraw!
|
|
||||||
endfunction
|
|
||||||
command GitGutterLineHighlightsEnable call GitGutterLineHighlightsEnable()
|
|
||||||
|
|
||||||
function! GitGutterLineHighlightsToggle()
|
|
||||||
let g:gitgutter_highlight_lines = !g:gitgutter_highlight_lines
|
|
||||||
call highlight#define_sign_line_highlights()
|
|
||||||
redraw!
|
|
||||||
endfunction
|
|
||||||
command GitGutterLineHighlightsToggle call GitGutterLineHighlightsToggle()
|
|
||||||
|
|
||||||
" }}}
|
" }}}
|
||||||
|
|
||||||
|
" Hunks {{{
|
||||||
" Signs: enable / disable / toggle {{{
|
|
||||||
|
|
||||||
function! GitGutterSignsEnable()
|
|
||||||
let g:gitgutter_signs = 1
|
|
||||||
call GitGutterAll()
|
|
||||||
endfunction
|
|
||||||
command GitGutterSignsEnable call GitGutterSignsEnable()
|
|
||||||
|
|
||||||
function! GitGutterSignsDisable()
|
|
||||||
let g:gitgutter_signs = 0
|
|
||||||
call sign#clear_signs(utility#file())
|
|
||||||
call sign#remove_dummy_sign(0)
|
|
||||||
endfunction
|
|
||||||
command GitGutterSignsDisable call GitGutterSignsDisable()
|
|
||||||
|
|
||||||
function! GitGutterSignsToggle()
|
|
||||||
if g:gitgutter_signs
|
|
||||||
call GitGutterSignsDisable()
|
|
||||||
else
|
|
||||||
call GitGutterSignsEnable()
|
|
||||||
endif
|
|
||||||
endfunction
|
|
||||||
command GitGutterSignsToggle call GitGutterSignsToggle()
|
|
||||||
|
|
||||||
" }}}
|
|
||||||
|
|
||||||
|
|
||||||
" Hunks: jump to next/previous {{{
|
|
||||||
|
|
||||||
command -count=1 GitGutterNextHunk call hunk#next_hunk(<count>)
|
command -count=1 GitGutterNextHunk call hunk#next_hunk(<count>)
|
||||||
command -count=1 GitGutterPrevHunk call hunk#prev_hunk(<count>)
|
command -count=1 GitGutterPrevHunk call hunk#prev_hunk(<count>)
|
||||||
|
|
||||||
" }}}
|
command GitGutterStageHunk call gitgutter#stage_hunk()
|
||||||
|
command GitGutterRevertHunk call gitgutter#revert_hunk()
|
||||||
|
|
||||||
" Hunks: stage/revert {{{
|
|
||||||
|
|
||||||
function! GitGutterStageHunk()
|
|
||||||
if 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()
|
|
||||||
if empty(current_hunk)
|
|
||||||
return
|
|
||||||
endif
|
|
||||||
|
|
||||||
" construct a diff
|
|
||||||
let diff_for_hunk = diff#generate_diff_for_hunk(current_hunk)
|
|
||||||
|
|
||||||
" apply the diff
|
|
||||||
call system(utility#command_in_directory_of_file('git apply --cached --unidiff-zero - '), diff_for_hunk)
|
|
||||||
|
|
||||||
" refresh gitgutter's view of buffer
|
|
||||||
silent execute "GitGutter"
|
|
||||||
endif
|
|
||||||
endfunction
|
|
||||||
command GitGutterStageHunk call GitGutterStageHunk()
|
|
||||||
|
|
||||||
function! GitGutterRevertHunk()
|
|
||||||
if 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()
|
|
||||||
if empty(current_hunk)
|
|
||||||
return
|
|
||||||
endif
|
|
||||||
|
|
||||||
" construct a diff
|
|
||||||
let diff_for_hunk = diff#generate_diff_for_hunk(current_hunk)
|
|
||||||
|
|
||||||
" apply the diff
|
|
||||||
call system(utility#command_in_directory_of_file('git apply --reverse --unidiff-zero - '), diff_for_hunk)
|
|
||||||
|
|
||||||
" reload file
|
|
||||||
silent edit
|
|
||||||
endif
|
|
||||||
endfunction
|
|
||||||
command GitGutterRevertHunk call GitGutterRevertHunk()
|
|
||||||
|
|
||||||
" }}}
|
|
||||||
|
|
||||||
|
|
||||||
" Hunk stats {{{
|
|
||||||
|
|
||||||
" Returns the git-diff hunks for the file or an empty list if there
|
" Returns the git-diff hunks for the file or an empty list if there
|
||||||
" aren't any hunks.
|
" aren't any hunks.
|
||||||
@@ -297,14 +141,13 @@ endif
|
|||||||
|
|
||||||
" }}}
|
" }}}
|
||||||
|
|
||||||
|
|
||||||
" Autocommands {{{
|
" Autocommands {{{
|
||||||
|
|
||||||
augroup gitgutter
|
augroup gitgutter
|
||||||
autocmd!
|
autocmd!
|
||||||
|
|
||||||
if g:gitgutter_realtime
|
if g:gitgutter_realtime
|
||||||
autocmd CursorHold,CursorHoldI * call GitGutter(utility#current_file(), 1)
|
autocmd CursorHold,CursorHoldI * call gitgutter#process_buffer(utility#current_file(), 1)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if g:gitgutter_eager
|
if g:gitgutter_eager
|
||||||
@@ -312,16 +155,16 @@ augroup gitgutter
|
|||||||
\ if gettabvar(tabpagenr(), 'gitgutter_didtabenter')
|
\ if gettabvar(tabpagenr(), 'gitgutter_didtabenter')
|
||||||
\| call settabvar(tabpagenr(), 'gitgutter_didtabenter', 0)
|
\| call settabvar(tabpagenr(), 'gitgutter_didtabenter', 0)
|
||||||
\| else
|
\| else
|
||||||
\| call GitGutter(utility#current_file(), 0)
|
\| call gitgutter#process_buffer(utility#current_file(), 0)
|
||||||
\| endif
|
\| endif
|
||||||
autocmd TabEnter *
|
autocmd TabEnter *
|
||||||
\ call settabvar(tabpagenr(), 'gitgutter_didtabenter', 1)
|
\ call settabvar(tabpagenr(), 'gitgutter_didtabenter', 1)
|
||||||
\| call GitGutterAll()
|
\| call gitgutter#all()
|
||||||
if !has('gui_win32')
|
if !has('gui_win32')
|
||||||
autocmd FocusGained * call GitGutterAll()
|
autocmd FocusGained * call gitgutter#all()
|
||||||
endif
|
endif
|
||||||
else
|
else
|
||||||
autocmd BufRead,BufWritePost,FileChangedShellPost * call GitGutter(utility#current_file(), 0)
|
autocmd BufRead,BufWritePost,FileChangedShellPost * call gitgutter#process_buffer(utility#current_file(), 0)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
autocmd ColorScheme * call highlight#define_sign_column_highlight() | call highlight#define_highlights()
|
autocmd ColorScheme * call highlight#define_sign_column_highlight() | call highlight#define_highlights()
|
||||||
@@ -333,5 +176,4 @@ augroup END
|
|||||||
|
|
||||||
" }}}
|
" }}}
|
||||||
|
|
||||||
|
|
||||||
" vim:set et sw=2 fdm=marker:
|
" vim:set et sw=2 fdm=marker:
|
||||||
|
|||||||
Reference in New Issue
Block a user