Skip to content

Commit

Permalink
feat: improve cmd parser
Browse files Browse the repository at this point in the history
  • Loading branch information
lervag committed Oct 7, 2021
1 parent 0cf298d commit 93fac56
Showing 1 changed file with 22 additions and 31 deletions.
53 changes: 22 additions & 31 deletions autoload/vimtex/cmd.vim
Original file line number Diff line number Diff line change
Expand Up @@ -610,41 +610,41 @@ 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)
let res.pos_end.lnum = arg.close.lnum
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
let res.pos_end.cnum = opt.close.cnum
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
let res.pos_end.cnum = arg.close.cnum
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
Expand All @@ -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')
Expand All @@ -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 {}
Expand All @@ -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
Expand Down

0 comments on commit 93fac56

Please sign in to comment.