mirror of
https://github.com/airblade/vim-gitgutter.git
synced 2025-11-08 11:33:48 -05:00
Fix handling of file renames
Both `:file` and `:saveas` rename the current file.
`:file` does not write the newly named file to disk, and keeps the same
buffer. It fires a single `BufFilePre` / `BufFilePost` pair of
autocommands:
`BufFilePre` buffer
`BufFilePost` buffer
`:saveas` does write the newly named file to disk, in a new buffer. It
fires two pairs of `BufFilePre` / `BufFilePost` autocommands:
`BufFilePre` original buffer
`BufFilePre` new buffer
`BufFilePost` original buffer
`BufFilePost` new buffer
In both cases the cached path needs to be cleared.
See #860, #551.
Fixes #865.
This commit is contained in:
@@ -265,6 +265,35 @@ function! s:next_tick(cmd)
|
|||||||
call timer_start(1, {-> execute(a:cmd)})
|
call timer_start(1, {-> execute(a:cmd)})
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function! s:on_buffilepre(bufnr)
|
||||||
|
if !exists('s:renaming')
|
||||||
|
let s:renaming = []
|
||||||
|
let s:gitgutter_was_enabled = gitgutter#utility#getbufvar(a:bufnr, 'enabled')
|
||||||
|
endif
|
||||||
|
|
||||||
|
let s:renaming += [a:bufnr]
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:on_buffilepost(bufnr)
|
||||||
|
if len(s:renaming) > 1
|
||||||
|
if s:renaming[0] != a:bufnr
|
||||||
|
throw 'gitgutter rename error' s:renaming[0] a:bufnr
|
||||||
|
endif
|
||||||
|
unlet s:renaming[0]
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
|
||||||
|
" reset cached values
|
||||||
|
GitGutterBufferDisable
|
||||||
|
|
||||||
|
if s:gitgutter_was_enabled
|
||||||
|
GitGutterBufferEnable
|
||||||
|
endif
|
||||||
|
|
||||||
|
unlet s:renaming
|
||||||
|
unlet s:gitgutter_was_enabled
|
||||||
|
endfunction
|
||||||
|
|
||||||
" Autocommands {{{
|
" Autocommands {{{
|
||||||
|
|
||||||
augroup gitgutter
|
augroup gitgutter
|
||||||
@@ -309,8 +338,8 @@ augroup gitgutter
|
|||||||
|
|
||||||
autocmd ColorScheme * call gitgutter#highlight#define_highlights()
|
autocmd ColorScheme * call gitgutter#highlight#define_highlights()
|
||||||
|
|
||||||
autocmd BufFilePre * let b:gitgutter_was_enabled = gitgutter#utility#getbufvar(expand('<abuf>'), 'enabled') | GitGutterBufferDisable
|
autocmd BufFilePre * call s:on_buffilepre(expand('<abuf>'))
|
||||||
autocmd BufFilePost * if b:gitgutter_was_enabled | GitGutterBufferEnable | endif | unlet b:gitgutter_was_enabled
|
autocmd BufFilePost * call s:on_buffilepost(expand('<abuf>'))
|
||||||
|
|
||||||
autocmd QuickFixCmdPre *vimgrep* let b:gitgutter_was_enabled = gitgutter#utility#getbufvar(expand('<abuf>'), 'enabled') | GitGutterBufferDisable
|
autocmd QuickFixCmdPre *vimgrep* let b:gitgutter_was_enabled = gitgutter#utility#getbufvar(expand('<abuf>'), 'enabled') | GitGutterBufferDisable
|
||||||
autocmd QuickFixCmdPost *vimgrep* if b:gitgutter_was_enabled | GitGutterBufferEnable | endif | unlet b:gitgutter_was_enabled
|
autocmd QuickFixCmdPost *vimgrep* if b:gitgutter_was_enabled | GitGutterBufferEnable | endif | unlet b:gitgutter_was_enabled
|
||||||
|
|||||||
@@ -251,6 +251,35 @@ function Test_filename_umlaut()
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_file_cmd()
|
||||||
|
normal ggo*
|
||||||
|
|
||||||
|
file other.txt
|
||||||
|
|
||||||
|
call s:trigger_gitgutter()
|
||||||
|
call assert_equal(1, b:gitgutter.enabled)
|
||||||
|
call assert_equal('', b:gitgutter.path)
|
||||||
|
call s:assert_signs([], 'other.txt')
|
||||||
|
|
||||||
|
write
|
||||||
|
|
||||||
|
call s:trigger_gitgutter()
|
||||||
|
call assert_equal(-2, b:gitgutter.path)
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_saveas()
|
||||||
|
normal ggo*
|
||||||
|
|
||||||
|
saveas other.txt
|
||||||
|
|
||||||
|
call s:trigger_gitgutter()
|
||||||
|
call assert_equal(1, b:gitgutter.enabled)
|
||||||
|
call assert_equal(-2, b:gitgutter.path)
|
||||||
|
call s:assert_signs([], 'other.txt')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" FIXME: this test fails when it is the first (or only) test to be run
|
" FIXME: this test fails when it is the first (or only) test to be run
|
||||||
function Test_follow_symlink()
|
function Test_follow_symlink()
|
||||||
let tmp = 'symlink'
|
let tmp = 'symlink'
|
||||||
|
|||||||
Reference in New Issue
Block a user