vim-signify/autoload/sy/fold.vim

129 lines
2.8 KiB
VimL
Raw Normal View History

2015-06-02 09:12:01 -04:00
" vim: et sw=2 sts=2
" Function: SignifyFoldExpr {{{1
function! SignifyFoldExpr(lnum)
return s:levels[a:lnum]
endfunction
" Function: SignifyFoldText {{{1
function! SignifyFoldText()
let linelen = &textwidth ? &textwidth : 80
let marker = &foldmarker[:stridx(&foldmarker, ',')-1]
let range = foldclosedend(v:foldstart) - foldclosed(v:foldstart) + 1
let left = substitute(getline(v:foldstart), marker, '', '')
let leftlen = len(left)
let right = printf('%d [%d]', range, v:foldlevel)
let rightlen = len(right)
let tmp = strpart(left, 0, linelen - rightlen)
let tmplen = len(tmp)
if leftlen > tmplen
let left = strpart(tmp, 0, tmplen - 4) . '... '
let leftlen = tmplen
endif
let fill = repeat(' ', linelen - (leftlen + rightlen))
" return left . fill . right . repeat(' ', 100)
return left . fill . right
endfunction
" Function: #do {{{1
function! sy#fold#do(bang) abort
2015-06-02 09:12:01 -04:00
if !exists('b:sy')
echomsg 'signify: I cannot detect any changes!'
return
endif
if empty(a:bang)
tabedit %
endif
2015-06-02 09:12:01 -04:00
let [s:context0, s:context1] = get(g:, 'signify_fold_context', [3, 8])
let s:levels = s:get_levels(s:get_lines())
set foldexpr=SignifyFoldExpr(v:lnum)
set foldtext=SignifyFoldText()
if empty(a:bang)
if ! empty(get(g:, 'old_fdm', ''))
unlet g:old_fdm
endif
else
let g:old_fdm=&foldmethod
endif
2015-06-02 09:12:01 -04:00
set foldmethod=expr
set foldlevel=0
let g:signify_fold = 1
endfunction
function! sy#fold#disable() "abort
if empty(get(g:, 'old_fdm', '')) && get(g:, 'signify_fold')
tabclose
else
let &foldmethod=g:old_fdm
try
normal zE
catch
endtry
endif
let g:signify_fold = 0
2015-06-02 09:12:01 -04:00
endfunction
" Function: s:get_lines {{{1
function! s:get_lines() abort
let lang = v:lang
language message C
redir => signlist
silent! execute 'sign place buffer='. b:sy.buffer
redir END
silent! execute 'language message' lang
let lines = []
for line in split(signlist, '\n')[2:]
call insert(lines, matchlist(line, '\v^\s+line\=(\d+)')[1], 0)
endfor
return reverse(lines)
endfunction
" Function: #fold_toggle {{{1
function! sy#fold#toggle(bang) abort
if get(g:, 'signify_fold')
call sy#fold#disable()
else
call sy#fold#do(a:bang)
endif
redraw!
call sy#start()
endfunction
" }}}
2015-06-02 09:12:01 -04:00
" Function: s:get_levels {{{1
function! s:get_levels(lines) abort
let levels = {}
for line in range(1, line('$'))
let levels[line] = 2
endfor
for line in a:lines
for l in range(line - s:context1, line + s:context1)
if (l < 1) || (l > line('$'))
continue
endif
if levels[l] == 2
let levels[l] = 1
endif
for ll in range(line - s:context0, line + s:context0)
let levels[ll] = 0
endfor
endfor
endfor
return levels
endfunction