diff --git a/autoload/vimtex/cmd.vim b/autoload/vimtex/cmd.vim index 39ae7e5d9f..ef27fbe26d 100644 --- a/autoload/vimtex/cmd.vim +++ b/autoload/vimtex/cmd.vim @@ -610,7 +610,7 @@ function! s:get_cmd(direction) abort " {{{1 " Environments always start with environment name and allows option " afterwards if res.name ==# '\begin' - let arg = s:get_cmd_part('{', res.pos_end) + let arg = s:get_cmd_part_delim('{', res.pos_end) if empty(arg) | return res | endif call add(res.args, arg) @@ -618,17 +618,9 @@ function! s:get_cmd(direction) abort " {{{1 let res.pos_end.cnum = arg.close.cnum endif - " Get arguments + " Parse the arguments while v:true - let arg = s:get_cmd_overlay(res.pos_end.lnum, res.pos_end.cnum) - if !empty(arg) - call add(res.args_chevrons, arg) - let res.pos_end.lnum = arg.close.lnum - let res.pos_end.cnum = arg.close.cnum - continue - endif - - let opt = s:get_cmd_part('[', res.pos_end) + let opt = s:get_cmd_part_delim('[', res.pos_end) if !empty(opt) call add(res.opts, opt) let res.pos_end.lnum = opt.close.lnum @@ -636,7 +628,7 @@ function! s:get_cmd(direction) abort " {{{1 continue endif - let arg = s:get_cmd_part('{', res.pos_end) + let arg = s:get_cmd_part_delim('{', res.pos_end) if !empty(arg) call add(res.args, arg) let res.pos_end.lnum = arg.close.lnum @@ -644,7 +636,15 @@ function! s:get_cmd(direction) abort " {{{1 continue endif - let arg = s:get_cmd_parens(res.pos_end.lnum, res.pos_end.cnum) + let arg = s:get_cmd_part_simple(['(', ')'], res.pos_end) + if !empty(arg) + call add(res.args_chevrons, arg) + let res.pos_end.lnum = arg.close.lnum + let res.pos_end.cnum = arg.close.cnum + continue + endif + + let arg = s:get_cmd_part_simple(['<', '>'], res.pos_end) if !empty(arg) call add(res.args_parens, arg) let res.pos_end.lnum = arg.close.lnum @@ -671,7 +671,7 @@ function! s:get_cmd_name(next) abort " {{{1 endfunction " }}}1 -function! s:get_cmd_part(part, start_pos) abort " {{{1 +function! s:get_cmd_part_delim(open_delim, start_pos) abort " {{{1 let l:save_pos = vimtex#pos#get_cursor() call vimtex#pos#set_cursor(a:start_pos) let l:open = vimtex#delim#get_next('delim_tex', 'open') @@ -685,7 +685,7 @@ function! s:get_cmd_part(part, start_pos) abort " {{{1 " let l:separate = s:text_between(a:start_pos, l:open) let l:newlines = count(l:separate, "\n") - if l:open.match !=# a:part + if l:open.match !=# a:open_delim \ || strlen(substitute(l:separate, '\_s\+', '', 'g')) != 0 \ || l:newlines > 1 return {} @@ -704,27 +704,18 @@ function! s:get_cmd_part(part, start_pos) abort " {{{1 endfunction " }}}1 -function! s:get_cmd_parens(lnum, cnum) abort " {{{1 - let l:match = matchstr(getline(a:lnum), '^\s*[^)]*)', a:cnum) +function! s:get_cmd_part_simple(delims, start_pos) abort " {{{1 + let l:lnum = a:start_pos.lnum + let l:cnum = a:start_pos.cnum + let l:regex = '^\s*' . a:delims[0] . '[^' . a:delims[1] . ']*' . a:delims[1] - return empty(l:match) - \ ? {} - \ : { - \ 'open' : {'lnum' : a:lnum, 'cnum' : a:cnum + 1}, - \ 'close' : {'lnum' : a:lnum, 'cnum' : a:cnum + strlen(l:match)}, - \ 'text' : l:match - \ } -endfunction - -" }}}1 -function! s:get_cmd_overlay(lnum, cnum) abort " {{{1 - let l:match = matchstr(getline(a:lnum), '^\s*[^>]*>', a:cnum) + let l:match = matchstr(getline(l:lnum), l:regex, l:cnum) return empty(l:match) \ ? {} \ : { - \ 'open' : {'lnum' : a:lnum, 'cnum' : a:cnum + 1}, - \ 'close' : {'lnum' : a:lnum, 'cnum' : a:cnum + strlen(l:match)}, + \ 'open' : {'lnum' : l:lnum, 'cnum' : l:cnum + 1}, + \ 'close' : {'lnum' : l:lnum, 'cnum' : l:cnum + strlen(l:match)}, \ 'text' : l:match \ } endfunction