Handle modifed-and-added/removed diffs.

This commit is contained in:
Andy Stewart
2013-03-05 10:00:06 +01:00
parent 518266d8c5
commit 9924c7ca1d
2 changed files with 49 additions and 12 deletions

View File

@@ -62,10 +62,6 @@ The syntax highlighting for your sign column is probably set strangely. Either
highlight clear SignColumn highlight clear SignColumn
``` ```
> Lines removed below a modified line are not shown.
True. This plugin uses Vim's signs which require a sign to be on a line (not between two lines) and only permit one sign per line. Removed lines are signed with an underscore on the line above. If that line has also been modified, the plugin has to choose whether to show the removed-lines sign or the modified-line sign. It prefers the latter.
> What happens if I also use another plugin which uses signs (e.g. Syntastic)? > What happens if I also use another plugin which uses signs (e.g. Syntastic)?
Vim only allows one sign per line. Before adding a sign to a line, vim-gitgutter checks whether a sign has already been added by somebody else. If so it doesn't do anything. In other words vim-gitgutter won't overwrite another plugin's signs. It also won't remove another plugin's signs. Vim only allows one sign per line. Before adding a sign to a line, vim-gitgutter checks whether a sign has already been added by somebody else. If so it doesn't do anything. In other words vim-gitgutter won't overwrite another plugin's signs. It also won't remove another plugin's signs.

View File

@@ -40,10 +40,12 @@ function! s:define_signs()
sign define GitGutterLineAdded text=+ texthl=lineAdded linehl=DiffAdd sign define GitGutterLineAdded text=+ texthl=lineAdded linehl=DiffAdd
sign define GitGutterLineModified text=~ texthl=lineModified linehl=DiffChange sign define GitGutterLineModified text=~ texthl=lineModified linehl=DiffChange
sign define GitGutterLineRemoved text=_ texthl=lineRemoved linehl=DiffDelete sign define GitGutterLineRemoved text=_ texthl=lineRemoved linehl=DiffDelete
sign define GitGutterLineModifiedRemoved text=~_ texthl=lineModified linehl=DiffChange
else else
sign define GitGutterLineAdded text=+ texthl=lineAdded linehl=NONE sign define GitGutterLineAdded text=+ texthl=lineAdded linehl=NONE
sign define GitGutterLineModified text=~ texthl=lineModified linehl=NONE sign define GitGutterLineModified text=~ texthl=lineModified linehl=NONE
sign define GitGutterLineRemoved text=_ texthl=lineRemoved linehl=NONE sign define GitGutterLineRemoved text=_ texthl=lineRemoved linehl=NONE
sign define GitGutterLineModifiedRemoved text=~_ texthl=lineModified linehl=NONE
endif endif
endfunction endfunction
@@ -130,6 +132,7 @@ function! s:process_hunk(hunk)
let from_count = a:hunk[1] let from_count = a:hunk[1]
let to_line = a:hunk[2] let to_line = a:hunk[2]
let to_count = a:hunk[3] let to_count = a:hunk[3]
if s:is_added(from_count, to_count) if s:is_added(from_count, to_count)
let offset = 0 let offset = 0
while offset < to_count while offset < to_count
@@ -137,16 +140,40 @@ function! s:process_hunk(hunk)
call add(modifications, [line_number, 'added']) call add(modifications, [line_number, 'added'])
let offset += 1 let offset += 1
endwhile endwhile
elseif s:is_removed(from_count, to_count) elseif s:is_removed(from_count, to_count)
" removed lines came after `to_line`.
call add(modifications, [to_line, 'removed']) call add(modifications, [to_line, 'removed'])
else " modified
elseif s:is_modified(from_count, to_count)
let offset = 0 let offset = 0
while offset < to_count while offset < to_count
let line_number = to_line + offset let line_number = to_line + offset
call add(modifications, [line_number, 'modified']) call add(modifications, [line_number, 'modified'])
let offset += 1 let offset += 1
endwhile endwhile
elseif s:is_modified_and_added(from_count, to_count)
let offset = 0
while offset < from_count
let line_number = to_line + offset
call add(modifications, [line_number, 'modified'])
let offset += 1
endwhile
while offset < to_count
let line_number = to_line + offset
call add(modifications, [line_number, 'added'])
let offset += 1
endwhile
elseif s:is_modified_and_removed(from_count, to_count)
let offset = 0
while offset < to_count
let line_number = to_line + offset
call add(modifications, [line_number, 'modified'])
let offset += 1
endwhile
call add(modifications, [to_line + offset - 1, 'modified_removed'])
endif endif
return modifications return modifications
endfunction endfunction
@@ -159,6 +186,18 @@ function! s:is_removed(from_count, to_count)
return a:from_count > 0 && a:to_count == 0 return a:from_count > 0 && a:to_count == 0
endfunction endfunction
function! s:is_modified(from_count, to_count)
return a:from_count > 0 && a:to_count > 0 && a:from_count == a:to_count
endfunction
function! s:is_modified_and_added(from_count, to_count)
return a:from_count > 0 && a:to_count > 0 && a:from_count < a:to_count
endfunction
function! s:is_modified_and_removed(from_count, to_count)
return a:from_count > 0 && a:to_count > 0 && a:from_count > a:to_count
endfunction
" }}} " }}}
" Sign processing {{{ " Sign processing {{{
@@ -200,6 +239,8 @@ function! s:show_signs(file_name, modified_lines)
let name = 'GitGutterLineRemoved' let name = 'GitGutterLineRemoved'
elseif type ==? 'modified' elseif type ==? 'modified'
let name = 'GitGutterLineModified' let name = 'GitGutterLineModified'
elseif type ==? 'modified_removed'
let name = 'GitGutterLineModifiedRemoved'
endif endif
call s:add_sign(line_number, name, a:file_name) call s:add_sign(line_number, name, a:file_name)
endfor endfor