Skip to content

Commit

Permalink
fix: split but respect tex groups
Browse files Browse the repository at this point in the history
refer: #2145
  • Loading branch information
lervag committed Aug 25, 2021
1 parent e615738 commit 72a4623
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
4 changes: 3 additions & 1 deletion autoload/vimtex/parser/toc/bibliography.vim
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ endfunction
" }}}1
function! s:matcher.parse_options(context, entry) abort dict " {{{1
" Parse the options
let l:opt_pairs = map(split(self.options, ','), 'split(v:val, ''='')')
let l:opt_pairs = map(
\ vimtex#util#texsplit(self.options),
\ 'split(v:val, ''='')')
let l:opts = {}
for [l:key, l:val] in l:opt_pairs
let l:key = substitute(l:key, '^\s*\|\s*$', '', 'g')
Expand Down
2 changes: 1 addition & 1 deletion autoload/vimtex/profile.vim
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function! vimtex#profile#stop() abort " {{{1
endfunction

" }}}1
"

function! vimtex#profile#open() abort " {{{1
source ~/.vim/vimrc
silent edit prof.log
Expand Down
31 changes: 31 additions & 0 deletions autoload/vimtex/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,37 @@ function! vimtex#util#tex2tree(str) abort " {{{1
return tree
endfunction

" }}}1
function! vimtex#util#texsplit(str) abort " {{{1
" Splits "str", but respect TeX groups ({...})
if empty(a:str) | return [] | endif

let parts = []
let i1 = 0
let i2 = -1
let depth = 0

while v:true
let i2 = match(a:str, '[,{}]', i2 + 1)

if i2 < 0
call add(parts, strpart(a:str, i1))
break
endif

if a:str[i2] ==# '{'
let depth += 1
elseif a:str[i2] ==# '}'
let depth -= 1
elseif depth == 0
call add(parts, strpart(a:str, i1, i2 - i1))
let i1 = i2 + 1
endif
endwhile

return parts
endfunction

" }}}1
function! vimtex#util#trim(str) abort " {{{1
if exists('*trim') | return trim(a:str) | endif
Expand Down
9 changes: 6 additions & 3 deletions test/test-utils/test1.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ set nomore

let s:tree = vimtex#util#tex2tree(
\ '\newlabel{test}{{\textsc {D\''ej\`a vu}\relax }{caption.1}{}}')
call assert_equal(len(s:tree), 3)
call assert_equal(s:tree[2][0][0], '\textsc')
call assert_equal(3, len(s:tree))
call assert_equal('\textsc', s:tree[2][0][0])

" Test for #1599: Fail label completion due to tex2unicode
let s:line = vimtex#util#tex2unicode('{\textsc {D\''ej\`a}\relax }')
call assert_equal(s:line, '{\textsc {Déjà}\relax }')
call assert_equal('{\textsc {Déjà}\relax }', s:line)

let s:str = 'title={Temporary, complete list of references}'
call assert_equal([s:str], vimtex#util#texsplit(s:str))

call vimtex#test#finished()

0 comments on commit 72a4623

Please sign in to comment.