Update signs in realtime.

Thanks to @ashb for the help with this.
This commit is contained in:
Andy Stewart
2013-08-19 15:21:32 +02:00
parent c896c9c7f6
commit 6e1e1dfe09
2 changed files with 57 additions and 9 deletions

View File

@@ -39,7 +39,7 @@ Add `Bundle 'airblade/vim-gitgutter'` to your `~/.vimrc` and then:
You don't have to do anything: it just works. You don't have to do anything: it just works.
Please note the plugin diffs your saved buffers, i.e. the files on disk. It produces the same result as running `git diff` on the command line. It won't see any unsaved changes to a buffer. With one exception the plugin diffs your saved buffers, i.e. the files on disk. It produces the same result as running `git diff` on the command line. The exception is realtime updating: in this case the plugin diffs the (unsaved) buffer contents against the version in git.
You can explicitly turn vim-gitgutter off and on (defaults to on): You can explicitly turn vim-gitgutter off and on (defaults to on):
@@ -72,6 +72,20 @@ Finally, you can force vim-gitgutter to update its signs across all visible buff
See the customisation section below for how to change the defaults. See the customisation section below for how to change the defaults.
### When are the signs updated?
By default the signs are updated when you:
* Stop typing
* Change buffer
* Change tab
* Save a buffer
* Change a file outside Vim
* Focus the GUI.
This can cause a noticeable lag on some systems so you can configure the plugin to update less often. See the customisation section below.
### Customisation ### Customisation
You can customise: You can customise:
@@ -85,6 +99,7 @@ You can customise:
* Whether or not vim-gitgutter is on initially (defaults to on) * Whether or not vim-gitgutter is on initially (defaults to on)
* Whether or not signs are shown (defaults to yes) * Whether or not signs are shown (defaults to yes)
* Whether or not line highlighting is on initially (defaults to off) * Whether or not line highlighting is on initially (defaults to off)
* Whether or not vim-gitgutter runs in "realtime" (defaults to yes)
* Whether or not vim-gitgutter runs eagerly (defaults to yes) * Whether or not vim-gitgutter runs eagerly (defaults to yes)
Please note that vim-gitgutter won't override any colours or highlights you've set in your colorscheme. Please note that vim-gitgutter won't override any colours or highlights you've set in your colorscheme.
@@ -174,15 +189,26 @@ Add `let g:gitgutter_enabled = 0` to your `~/.vimrc`.
Add `let g:gitgutter_highlight_lines = 1` to your `~/.vimrc`. Add `let g:gitgutter_highlight_lines = 1` to your `~/.vimrc`.
#### To stop vim-gitgutter running in realtime
By default the plugin runs when you stop typing. The delay is governed by `updatetime` (Vim's default is `4000`ms, i.e. 4 seconds; I prefer `750`.)
To turn this off, add the following to your `~/.vimrc`:
```viml
let g:gitgutter_realtime = 0
```
#### To stop vim-gitgutter running eagerly #### To stop vim-gitgutter running eagerly
By default the plugin runs every time you read a file, on `BufEnter`, `TabEnter` and `FocusGained`. By default the plugin also runs every time you read a file, on `BufEnter`, `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.
To turn off eager execution, add this to your `~/.vimrc`: To turn off eager execution, add this to your `~/.vimrc`:
``` ```viml
let g:gitgutter_eager = 0 let g:gitgutter_eager = 0
``` ```
@@ -200,6 +226,7 @@ Your colorscheme is configuring the `SignColumn` highlight group weirdly. Pleas
By default vim-gitgutter runs often so the signs are as accurate as possible. However on some systems this causes a noticeable lag. If you would like to trade a little accuracy for speed, add this to your `~/.vimrc`: By default vim-gitgutter runs often so the signs are as accurate as possible. However on some systems this causes a noticeable lag. If you would like to trade a little accuracy for speed, add this to your `~/.vimrc`:
```viml ```viml
let g:gitgutter_realtime = 0
let g:gitgutter_eager = 0 let g:gitgutter_eager = 0
``` ```

View File

@@ -20,6 +20,7 @@ call s:set('g:gitgutter_signs', 1)
call s:set('g:gitgutter_highlight_lines', 0) call s:set('g:gitgutter_highlight_lines', 0)
let s:highlight_lines = g:gitgutter_highlight_lines let s:highlight_lines = g:gitgutter_highlight_lines
call s:set('g:gitgutter_sign_column_always', 0) call s:set('g:gitgutter_sign_column_always', 0)
call s:set('g:gitgutter_realtime', 1)
call s:set('g:gitgutter_eager', 1) call s:set('g:gitgutter_eager', 1)
call s:set('g:gitgutter_sign_added', '+') call s:set('g:gitgutter_sign_added', '+')
call s:set('g:gitgutter_sign_modified', '~') call s:set('g:gitgutter_sign_modified', '~')
@@ -204,13 +205,23 @@ endfunction
" Diff processing {{{ " Diff processing {{{
function! s:run_diff() function! s:run_diff(realtime)
if a:realtime
let blob_name = ':./' . fnamemodify(s:file(),':t')
let cmd = 'diff -U0 ' . g:gitgutter_diff_args . ' <(git show '. blob_name .') - '
else
let cmd = 'git diff --no-ext-diff --no-color -U0 ' . g:gitgutter_diff_args . ' ' . shellescape(s:file()) let cmd = 'git diff --no-ext-diff --no-color -U0 ' . g:gitgutter_diff_args . ' ' . shellescape(s:file())
endif
if s:grep_available if s:grep_available
let cmd .= s:grep_command let cmd .= s:grep_command
endif endif
let cmd = s:escape(cmd) let cmd = s:escape(cmd)
if a:realtime
let buffer_contents = join(getline(1, '$'), "\n") . "\n"
let diff = system(s:command_in_directory_of_file(cmd), buffer_contents)
else
let diff = system(s:command_in_directory_of_file(cmd)) let diff = system(s:command_in_directory_of_file(cmd))
endif
return diff return diff
endfunction endfunction
@@ -422,11 +433,16 @@ function! GitGutterAll()
endfunction endfunction
command GitGutterAll call GitGutterAll() command GitGutterAll call GitGutterAll()
function! GitGutter(file) " Supply optional argument to use realtime mode.
function! GitGutter(file, ...)
call s:set_file(a:file) call s:set_file(a:file)
if s:is_active() if s:is_active()
call s:init() call s:init()
let diff = s:run_diff() if a:0 == 1
let diff = s:run_diff(1)
else
let diff = s:run_diff(0)
endif
let s:hunks = s:parse_diff(diff) let s:hunks = s:parse_diff(diff)
let modified_lines = s:process_hunks(s:hunks) let modified_lines = s:process_hunks(s:hunks)
if g:gitgutter_sign_column_always if g:gitgutter_sign_column_always
@@ -550,6 +566,11 @@ endif
augroup gitgutter augroup gitgutter
autocmd! autocmd!
if g:gitgutter_realtime
autocmd CursorHold,CursorHoldI * call GitGutter(s:current_file(), 1)
endif
if g:gitgutter_eager if g:gitgutter_eager
autocmd BufEnter,BufWritePost,FileWritePost,FileChangedShellPost * call GitGutter(s:current_file()) autocmd BufEnter,BufWritePost,FileWritePost,FileChangedShellPost * call GitGutter(s:current_file())
autocmd TabEnter * call GitGutterAll() autocmd TabEnter * call GitGutterAll()