Reduce duplicate work due to overlapping autocommands.

This commit is contained in:
Andy Stewart
2014-01-07 13:34:21 +01:00
parent 021a922083
commit d420a44536
2 changed files with 58 additions and 35 deletions

View File

@@ -74,12 +74,12 @@ See the customisation section below for how to change the defaults.
By default the signs are updated when you: By default the signs are updated when you:
* Stop typing * Stop typing (realtime)
* Change buffer * Change buffer (eager)
* Change tab * Change tab (eager)
* Save a buffer * Save a buffer (always)
* Change a file outside Vim * Change a file outside Vim (always)
* Focus the GUI. * Focus the GUI (eager but not gVim on Windows).
This can cause a noticeable lag on some systems so you can configure the plugin to update less often. See the customisation section below. This can cause a noticeable lag on some systems so you can configure the plugin to update less often. See the customisation section below.
@@ -200,7 +200,7 @@ let g:gitgutter_realtime = 0
#### To stop vim-gitgutter running eagerly #### To stop vim-gitgutter running eagerly
By default the plugin also runs every time you read a file, on `BufEnter`, `TabEnter` and `FocusGained`. By default the plugin also runs on `BufEnter` (to notice `git add` outside vim), `TabEnter` and `FocusGained`.
This can cause a noticeable lag for some people so you can set the plugin to run instead only when you read or write a file. This can cause a noticeable lag for some people so you can set the plugin to run instead only when you read or write a file.
@@ -210,7 +210,9 @@ To turn off eager execution, add this to your `~/.vimrc`:
let g:gitgutter_eager = 0 let g:gitgutter_eager = 0
``` ```
Note that `FocusGained` cannot be used with gVim on Windows due to a Vim/shell bug causing an infinite loop. Note that `FocusGained` is not activated in gVim on Windows due to a Vim/shell bug causing an infinite loop.
The plugin always runs on `FileChangedShellPost` to notice `git stash` outside vim.
### FAQ ### FAQ

View File

@@ -38,38 +38,51 @@ call highlight#define_signs()
" Public interface {{{ " Public interface {{{
function! GitGutterAll() function! GitGutterAll()
for buffer_id in tabpagebuflist() for buffer_id in tabpagebuflist()
call GitGutter(expand('#' . buffer_id . ':p')) let file = expand('#' . buffer_id . ':p')
if !empty(file)
call GitGutter(file, 0, 0)
endif
endfor endfor
endfunction endfunction
command GitGutterAll call GitGutterAll() command GitGutterAll call GitGutterAll()
" Supply optional argument to use realtime mode. " Does the actual work.
function! GitGutter(file, ...) "
call utility#set_file(a:file) " file: (string) the file to process.
if utility#is_active() " realtime: (boolean) when truthy, do a realtime diff; otherwise do a disk-based diff.
if (a:0 == 1) || utility#has_unsaved_changes(a:file) " fresh_changes: (boolean) when truthy, only process if there are buffer changes
let diff = diff#run_diff(1) " since the last gitgutter process; otherwise always process.
else function! GitGutter(file, realtime, fresh_changes)
let diff = diff#run_diff(0) if !a:fresh_changes || getbufvar(a:file, 'changedtick') != getbufvar(a:file, 'gitgutter_last_tick', -1)
endif
let s:hunks = diff#parse_diff(diff) call utility#set_file(a:file)
let modified_lines = diff#process_hunks(s:hunks) if utility#is_active()
if g:gitgutter_sign_column_always if a:realtime || utility#has_unsaved_changes(a:file)
call sign#add_dummy_sign() let diff = diff#run_diff(1)
else
if utility#differences(s:hunks)
call sign#add_dummy_sign() " prevent flicker
else else
call sign#remove_dummy_sign() let diff = diff#run_diff(0)
endif endif
let s:hunks = diff#parse_diff(diff)
let modified_lines = diff#process_hunks(s:hunks)
if g:gitgutter_sign_column_always
call sign#add_dummy_sign()
else
if utility#differences(s:hunks)
call sign#add_dummy_sign() " prevent flicker
else
call sign#remove_dummy_sign()
endif
endif
call sign#update_signs(a:file, modified_lines)
else
call hunk#reset()
endif endif
call sign#update_signs(a:file, modified_lines)
else call setbufvar(a:file, 'gitgutter_last_tick', getbufvar(a:file, 'changedtick'))
call hunk#reset()
endif endif
endfunction endfunction
command GitGutter call GitGutter(utility#current_file()) command GitGutter call GitGutter(utility#current_file(), 0, 0)
function! GitGutterDisable() function! GitGutterDisable()
let g:gitgutter_enabled = 0 let g:gitgutter_enabled = 0
@@ -189,18 +202,26 @@ 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(utility#current_file(), 1, 1)
endif endif
if g:gitgutter_eager if g:gitgutter_eager
autocmd BufEnter,BufWritePost,FileWritePost,FileChangedShellPost * call GitGutter(utility#current_file()) autocmd BufEnter,BufWritePost,FileChangedShellPost *
autocmd TabEnter * call GitGutterAll() \ if gettabvar(tabpagenr(), 'gitgutter_didtabenter')
\| call settabvar(tabpagenr(), 'gitgutter_didtabenter', 0)
\| else
\| call GitGutter(utility#current_file(), 0, 0)
\| endif
autocmd TabEnter *
\ call settabvar(tabpagenr(), 'gitgutter_didtabenter', 1)
\| call GitGutterAll()
if !has('gui_win32') if !has('gui_win32')
autocmd FocusGained * call GitGutterAll() autocmd FocusGained * call GitGutterAll()
endif endif
else else
autocmd BufReadPost,BufWritePost,FileReadPost,FileWritePost,FileChangedShellPost * call GitGutter(utility#current_file()) autocmd BufRead,BufWritePost,FileChangedShellPost * call GitGutter(utility#current_file())
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()
augroup END augroup END