From 6e1e1dfe094da7e54caa1d5b7825ef8fe264bed4 Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Mon, 19 Aug 2013 15:21:32 +0200 Subject: [PATCH] Update signs in realtime. Thanks to @ashb for the help with this. --- README.mkd | 33 ++++++++++++++++++++++++++++++--- plugin/gitgutter.vim | 33 +++++++++++++++++++++++++++------ 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/README.mkd b/README.mkd index d85da70..e85a638 100644 --- a/README.mkd +++ b/README.mkd @@ -39,7 +39,7 @@ Add `Bundle 'airblade/vim-gitgutter'` to your `~/.vimrc` and then: 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): @@ -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. +### 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 You can customise: @@ -85,6 +99,7 @@ You can customise: * Whether or not vim-gitgutter is on initially (defaults to on) * Whether or not signs are shown (defaults to yes) * 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) 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`. +#### 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 -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. To turn off eager execution, add this to your `~/.vimrc`: -``` +```viml 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`: ```viml +let g:gitgutter_realtime = 0 let g:gitgutter_eager = 0 ``` diff --git a/plugin/gitgutter.vim b/plugin/gitgutter.vim index e19153f..55fdac9 100644 --- a/plugin/gitgutter.vim +++ b/plugin/gitgutter.vim @@ -20,7 +20,8 @@ call s:set('g:gitgutter_signs', 1) call s:set('g:gitgutter_highlight_lines', 0) let s:highlight_lines = g:gitgutter_highlight_lines call s:set('g:gitgutter_sign_column_always', 0) -call s:set('g:gitgutter_eager' , 1) +call s:set('g:gitgutter_realtime', 1) +call s:set('g:gitgutter_eager', 1) call s:set('g:gitgutter_sign_added', '+') call s:set('g:gitgutter_sign_modified', '~') call s:set('g:gitgutter_sign_removed', '_') @@ -204,13 +205,23 @@ endfunction " Diff processing {{{ -function! s:run_diff() - let cmd = 'git diff --no-ext-diff --no-color -U0 ' . g:gitgutter_diff_args . ' ' . shellescape(s:file()) +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()) + endif if s:grep_available let cmd .= s:grep_command endif let cmd = s:escape(cmd) - let diff = system(s:command_in_directory_of_file(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)) + endif return diff endfunction @@ -422,11 +433,16 @@ function! GitGutterAll() endfunction command GitGutterAll call GitGutterAll() -function! GitGutter(file) +" Supply optional argument to use realtime mode. +function! GitGutter(file, ...) call s:set_file(a:file) if s:is_active() 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 modified_lines = s:process_hunks(s:hunks) if g:gitgutter_sign_column_always @@ -550,6 +566,11 @@ endif augroup gitgutter autocmd! + + if g:gitgutter_realtime + autocmd CursorHold,CursorHoldI * call GitGutter(s:current_file(), 1) + endif + if g:gitgutter_eager autocmd BufEnter,BufWritePost,FileWritePost,FileChangedShellPost * call GitGutter(s:current_file()) autocmd TabEnter * call GitGutterAll()