Fixed #592: Better handling of delimiter indentation

This commit is contained in:
Karl Yngve Lervåg 2016-10-11 22:56:50 +02:00
parent 1d126b98b9
commit 4249328a06
2 changed files with 76 additions and 33 deletions

View File

@ -213,19 +213,9 @@ endfunction
" }}}1
function! vimtex#delim#get_valid_regexps(...) " {{{1
"
" Arguments: (Optional)
" line number
" column number
"
" Returns:
" [regexp_open_delims, regexp_close_delims]
"
return call('vimtex#util#in_mathzone', a:000)
\ ? [s:re.delim_math.open, s:re.delim_math.close]
\ : [s:re.delim_tex.open, s:re.delim_tex.close]
function! vimtex#delim#get_delim_regexes(...) " {{{1
return [s:re.delim_math.open, s:re.delim_math.close,
\ s:re.delim_tex.open, s:re.delim_tex.close]
endfunction
" }}}1

View File

@ -46,11 +46,11 @@ function! VimtexIndent(lnum) " {{{1
let l:nprev = s:get_prev_line(l:nprev, 'ignore-ampersands')
if l:nprev == 0 | return 0 | endif
let l:prev = getline(l:nprev)
let l:prev = substitute(getline(l:nprev), '\\\@<!%.*', '', '')
let l:ind = indent(l:nprev)
let l:ind += s:indent_envs(l:cur, l:prev)
let l:ind += s:indent_delims(l:cur, l:prev, a:lnum)
let l:ind += s:indent_delims(l:cur, a:lnum, l:prev, l:nprev)
let l:ind += s:indent_tikz(l:nprev, l:prev)
return l:ind
endfunction
@ -104,12 +104,79 @@ let s:envs_begitem = s:envs_item . '\|' . s:envs_beglist
let s:envs_enditem = s:envs_item . '\|' . s:envs_endlist
" }}}1
function! s:indent_delims(cur, prev, lnum) " {{{1
let [l:open, l:close] = vimtex#delim#get_valid_regexps(a:lnum, col('.'))
return &sw*( max([s:count(a:prev, l:open) - s:count(a:prev, l:close), 0])
\ - max([s:count(a:cur, l:close) - s:count(a:cur, l:open), 0]))
function! s:indent_delims(cur, ncur, prev, nprev) " {{{1
let [l:n1, l:dummy, l:m1] = s:split(a:prev, a:nprev)
let [l:n2, l:m2, l:dummy] = s:split(a:cur, a:ncur)
return &sw*( max([s:count(l:n1, s:to) + s:count(l:m1, s:mo)
\ - s:count(l:n1, s:tc) - s:count(l:m1, s:mc), 0])
\ - max([s:count(l:n2, s:tc) + s:count(l:m2, s:mc)
\ - s:count(l:n2, s:to) - s:count(l:m2, s:mo), 0]))
endfunction
let [s:mo, s:mc, s:to, s:tc] = vimtex#delim#get_delim_regexes()
function! s:count(line, pattern) " {{{2
let sum = 0
let indx = match(a:line, a:pattern)
while indx >= 0
let sum += 1
let match = matchstr(a:line, a:pattern, indx)
let indx += len(match)
let indx = match(a:line, a:pattern, indx)
endwhile
return sum
endfunction
" }}}2
function! s:split(line, lnum) " {{{2
let l:map = map(range(1,col([a:lnum, strlen(a:line)])),
\ '[v:val, vimtex#util#in_mathzone(a:lnum, v:val)]')
" Adjust math mode limits (currently handle only $'s)
let l:prev = 1
for l:i in range(len(l:map))
if l:map[l:i][1] == 1 && l:prev == 0
let l:prev = l:map[l:i][1]
let l:map[l:i][1] = 0
else
let l:prev = l:map[l:i][1]
endif
endfor
if l:map[0][1] == 1 && a:line[0] ==# '$'
let l:map[0][1] = 0
endif
" Extract normal text
let l:normal = ''
for [l:i, l:val] in l:map
if l:val == 0
let l:normal .= a:line[l:i - 1]
endif
endfor
let l:normal = substitute(l:normal, '\\verb\(.\).\{}\1', '', 'g')
" Extract math text from beginning of line
let l:math_pre = ''
let l:indx = 0
while l:map[l:indx][1] == 1
let l:math_pre .= a:line[l:map[l:indx][0] - 1]
let l:indx += 1
endwhile
" Extract math text from end of line
let l:math_end = ''
let l:indx = -1
while l:map[l:indx][1] == 1
let l:math_end = a:line[l:map[l:indx][0] - 1] . l:math_end
let l:indx -= 1
endwhile
return [l:normal, l:math_pre, l:math_end]
endfunction
" }}}2
" }}}1
function! s:indent_tikz(lnum, prev) " {{{1
if vimtex#env#is_inside('tikzpicture')
@ -142,20 +209,6 @@ let s:tikz_commands = '\v\\%(' . join([
" }}}1
function! s:count(line, pattern) " {{{1
let sum = 0
let indx = match(a:line, a:pattern)
while indx >= 0
let sum += 1
let match = matchstr(a:line, a:pattern, indx)
let indx += len(match)
let indx = match(a:line, a:pattern, indx)
endwhile
return sum
endfunction
" }}}1
let &cpo = s:cpo_save
unlet s:cpo_save