design change of main data structure

s:active_buffers was renamed to s:sy. It holds all active buffers
(buffers that are associated with signify) and their metadata like jump
IDs.

It's a big change but it probably makes future improvements much easier.

Closes issue #2.
This commit is contained in:
Marco Hinz 2013-03-08 09:11:18 +01:00
parent 5cd00ffd33
commit eddd0cab80

View File

@ -31,18 +31,18 @@ endif
let g:loaded_signify = 1 let g:loaded_signify = 1
" Default values {{{1 " Default values {{{1
" Overwrite non-signify signs by default let s:line_highlight = 0 " disable line highlighting
let s:line_highlight = 0 let s:colors_set = 0 " do colors have to be reset?
let s:colors_set = 0 let s:last_jump_was_next = -1 " last movement was next or prev?
let s:last_jump_was_next = -1
let s:active_buffers = {}
let s:other_signs_line_numbers = {}
let s:other_signs_line_numbers = {} " holds IDs of other signs
let s:sy = {} " the main data structure
" overwrite non-signify signs by default
let s:sign_overwrite = exists('g:signify_sign_overwrite') ? g:signify_sign_overwrite : 1 let s:sign_overwrite = exists('g:signify_sign_overwrite') ? g:signify_sign_overwrite : 1
let s:id_start = 0x100 let s:id_start = 0x100
let s:id_top = s:id_start let s:id_top = s:id_start
let s:id_jump = s:id_start
" Default mappings {{{1 " Default mappings {{{1
if exists('g:signify_mapping_next_hunk') if exists('g:signify_mapping_next_hunk')
@ -92,9 +92,9 @@ endif
aug signify aug signify
au! au!
au ColorScheme * call s:set_colors() au ColorScheme * call s:set_colors()
au BufWritePost,FocusGained * call s:start() au BufWritePost,FocusGained * call s:start(expand('<afile>:p'))
au BufEnter * let s:colors_set = 0 | call s:start() au BufEnter * let s:colors_set = 0 | call s:start(expand('<afile>:p'))
au BufDelete * call s:stop() | call s:remove_from_buffer_list(expand('%:p')) au BufDelete * call s:stop(expand('<afile>:p')) | call s:remove_from_buffer_list(expand('<afile>:p'))
aug END aug END
com! -nargs=0 -bar SignifyToggle call s:toggle_signify() com! -nargs=0 -bar SignifyToggle call s:toggle_signify()
@ -104,10 +104,8 @@ com! -nargs=0 -bar SignifyJumpToPrevHunk call s:jump_to_prev_hunk()
" Internal functions {{{1 " Internal functions {{{1
" Functions -> s:start() {{{2 " Functions -> s:start() {{{2
function! s:start() abort function! s:start(path) abort
let l:path = expand('%:p') if empty(a:path) || &ft == 'help'
if empty(l:path) || &ft == 'help'
return return
endif endif
@ -128,17 +126,21 @@ function! s:start() abort
endif endif
" New buffer.. add to list. " New buffer.. add to list.
if !has_key(s:active_buffers, l:path) if !has_key(s:sy, a:path)
let s:active_buffers[l:path] = { 'active': 1 } let s:sy[a:path] = { 'active': 1, 'ids': [], 'id_jump': -1, 'id_top': s:id_top, 'last_jump_was_next': -1 }
" Inactive buffer.. bail out. " Inactive buffer.. bail out.
elseif s:active_buffers[l:path].active == 0 elseif s:sy[a:path].active == 0
return return
else
let s:sy[a:path].id_top = s:id_top
let s:sy[a:path].id_jump = s:id_top
let s:sy[a:path].last_jump_was_next = -1
endif endif
" Is a diff available? " Is a diff available?
let diff = s:get_diff(l:path) let diff = s:get_diff(a:path)
if empty(diff) if empty(diff)
call s:remove_signs() call s:remove_signs(a:path)
return return
endif endif
@ -148,21 +150,22 @@ function! s:start() abort
let s:colors_set = 1 let s:colors_set = 1
endif endif
call s:remove_signs() call s:remove_signs(a:path)
let s:id_top = s:id_start
if s:sign_overwrite == 0 if s:sign_overwrite == 0
call s:get_other_signs(l:path) call s:get_other_signs(a:path)
endif endif
" Use git's diff cmd to set our signs. " Use git's diff cmd to set our signs.
call s:process_diff(diff) call s:process_diff(diff)
let s:sy[a:path].id_top = (s:id_top - 1)
endfunction endfunction
" Functions -> s:stop() {{{2 " Functions -> s:stop() {{{2
function! s:stop() abort function! s:stop(path) abort
call s:remove_signs() echom expand('%:p')
call s:remove_signs(a:path)
aug signify aug signify
au! * <buffer> au! * <buffer>
aug END aug END
@ -171,12 +174,12 @@ endfunction
" Functions -> s:toggle_signify() {{{2 " Functions -> s:toggle_signify() {{{2
function! s:toggle_signify() abort function! s:toggle_signify() abort
let l:path = expand('%:p') let l:path = expand('%:p')
if has_key(s:active_buffers, l:path) && (s:active_buffers[l:path].active == 1) if has_key(s:sy, l:path) && (s:sy[l:path].active == 1)
call s:stop() call s:stop(l:path)
let s:active_buffers[l:path].active = 0 let s:sy[l:path].active = 0
else else
let s:active_buffers[l:path].active = 1 let s:sy[l:path].active = 1
call s:start() call s:start(l:path)
endif endif
endfunction endfunction
@ -200,21 +203,23 @@ function! s:set_sign(lnum, type, path)
return return
endif endif
call add(s:sy[a:path].ids, s:id_top)
exe 'sign place '. s:id_top .' line='. a:lnum .' name='. a:type .' file='. a:path exe 'sign place '. s:id_top .' line='. a:lnum .' name='. a:type .' file='. a:path
let s:id_top += 1 let s:id_top += 1
endfunction endfunction
" Functions -> s:remove_signs() {{{2 " Functions -> s:remove_signs() {{{2
function! s:remove_signs() abort function! s:remove_signs(path) abort
if s:sign_overwrite == 1 if s:sign_overwrite == 1
sign unplace * for id in s:sy[a:path].ids
else
for id in range(s:id_start, s:id_top - 1)
exe 'sign unplace '. id exe 'sign unplace '. id
endfor endfor
endif endif
let s:sy[a:path].ids = []
endfunction endfunction
" Functions -> s:get_diff() {{{2 " Functions -> s:get_diff() {{{2
function! s:get_diff(path) abort function! s:get_diff(path) abort
if !executable('grep') if !executable('grep')
@ -384,44 +389,60 @@ function! s:toggle_line_highlighting() abort
exe 'sign define SignifyChange text=! texthl=SignifyChange linehl='. change exe 'sign define SignifyChange text=! texthl=SignifyChange linehl='. change
let s:line_highlight = 1 let s:line_highlight = 1
endif endif
call s:start() call s:start(expand('%'))
endfunction endfunction
" Functions -> s:jump_to_next_hunk() {{{2 " Functions -> s:jump_to_next_hunk() {{{2
function! s:jump_to_next_hunk() function! s:jump_to_next_hunk()
if s:last_jump_was_next == 0 let path = expand('%:p')
let s:id_jump += 2
if s:sy[path].last_jump_was_next == 0
let s:sy[path].id_jump += 2
endif endif
exe 'sign jump '. s:id_jump .' file='. expand('%:p')
let s:id_jump = (s:id_jump == (s:id_top - 1)) ? (s:id_start) : (s:id_jump + 1) if s:sy[path].id_jump > s:sy[path].id_top
let s:last_jump_was_next = 1 let s:sy[path].id_jump = s:sy[path].ids[0]
endif
exe 'sign jump '. s:sy[path].id_jump .' file='. path
let s:sy[path].id_jump += 1
let s:sy[path].last_jump_was_next = 1
endfunction endfunction
" Functions -> s:jump_to_prev_hunk() {{{2 " Functions -> s:jump_to_prev_hunk() {{{2
function! s:jump_to_prev_hunk() function! s:jump_to_prev_hunk()
if s:last_jump_was_next == 1 let path = expand('%:p')
let s:id_jump -= 2
if s:sy[path].last_jump_was_next == 1
let s:sy[path].id_jump -= 2
endif endif
exe 'sign jump '. s:id_jump .' file='. expand('%:p')
let s:id_jump = (s:id_jump == s:id_start) ? (s:id_top - 1) : (s:id_jump - 1) if s:sy[path].id_jump < s:sy[path].ids[0]
let s:last_jump_was_next = 0 let s:sy[path].id_jump = s:sy[path].id_top
endif
exe 'sign jump '. s:sy[path].id_jump .' file='. path
let s:sy[path].id_jump -= 1
let s:sy[path].last_jump_was_next = 0
endfunction endfunction
" Functions -> s:remove_from_buffer_list() {{{2 " Functions -> s:remove_from_buffer_list() {{{2
function! s:remove_from_buffer_list(path) abort function! s:remove_from_buffer_list(path) abort
if has_key(s:active_buffers, a:path) if has_key(s:sy, a:path)
call remove(s:active_buffers, a:path) call remove(s:sy, a:path)
endif endif
endfunction endfunction
" Functions -> SignifyDebugListActiveBuffers() {{{2 " Functions -> SignifyDebugListActiveBuffers() {{{2
function! SignifyDebugListActiveBuffers() abort function! SignifyDebugListActiveBuffers() abort
if len(s:active_buffers) == 0 if len(s:sy) == 0
echo 'No active buffers!' echo 'No active buffers!'
return return
endif endif
for i in items(s:active_buffers) for i in items(s:sy)
echo i echo i
endfor endfor
endfunction endfunction