<commit_before>" VimTeX - LaTeX plugin for Vim
"
" Maintainer: <NAME>
" Email:      <EMAIL>
"

function! vimtex#parser#auxiliary#parse(file) abort " {{{1
  return s:parse_recurse(a:file, [])
endfunction

" }}}1
function! vimtex#parser#auxiliary#labels() abort " {{{1
  "
  " Searches aux files recursively for commands of the form
  "
  "   \newlabel{name}{{number}{page}.*}.*
  "   \newlabel{name}{{text {number}}{page}.*}.*
  "   \newlabel{name}{{number}{page}{...}{type}.*}.*
  "
  " Returns a list of candidates like {'word': name, 'menu': type number page}.
  "
  let l:files = [[b:vimtex.aux(), '']]

  " Handle local file editing (e.g. subfiles package)
  if exists('b:vimtex_local') && b:vimtex_local.active
    let l:files += [[vimtex#state#get(b:vimtex_local.main_id).aux(), '']]
  endif

  " Add externaldocuments (from \externaldocument in preamble)
  let l:files += map(
        \ vimtex#parser#get_externalfiles(),
        \ '[v:val.aux, v:val.opt]')

  let l:cache = vimtex#cache#open('refcomplete', {
        \ 'local': 1,
        \ 'default': {'labels': [], 'ftime': -1}
        \})

  let l:labels = []
  for [l:file, l:prefix] in filter(l:files, 'filereadable(v:val[0])')
    let l:current = l:cache.get(l:file)
    let l:ftime = getftime(l:file)
    if l:ftime > l:current.ftime
      let l:current.ftime = l:ftime
      let l:current.labels = s:parse_labels(l:file, l:prefix)
      let l:cache.modified = 1
    endif

    let l:labels += l:current.labels
  endfor

  " Write cache to file
  call l:cache.write()

  return l:labels
endfunction

" }}}1

function! s:parse_recurse(file, parsed) abort " {{{1
  if !filereadable(a:file) || index(a:parsed, a:file) >= 0
    return []
  endif
  call add(a:parsed, a:file)

  let l:lines = []
  for l:line in readfile(a:file)
    call add(l:lines, l:line)

    if l:line =~# '\\@input{'
      let l:file = s:input_line_parser(l:line, a:file)
      call extend(l:lines, s:parse_recurse(l:file, a:parsed))
    endif
  endfor

  return l:lines
endfunction

" }}}1
function! s:input_line_parser(line, file) abort " {{{1
  let l:file = matchstr(a:line, '\\@input{\zs[^}]\+\ze}')

  " Remove extension to simplify the parsing (e.g. for "my file name".aux)
  let l:file = substitute(l:file, '\.aux', '', '')

  " Trim whitespaces and quotes from beginning/end of string, append extension
  let l:file = substitute(l:file, '^\(\s\|"\)*', '', '')
  let l:file = substitute(l:file, '\(\s\|"\)*$', '', '')
  let l:file .= '.aux'

  " Use absolute paths
  if l:file !~# '\v^(\/|[A-Z]:)'
    let l:file = fnamemodify(a:file, ':p:h') . '/' . l:file
  endif

  " Only return filename if it is readable
  return filereadable(l:file) ? l:file : ''
endfunction

" }}}1

function! s:parse_labels(file, prefix) abort " {{{1
  " Get lines from aux files
  let l:lines = vimtex#parser#auxiliary(a:file)
  let l:lines = filter(l:lines, 'v:val =~# ''\\newlabel{''')
  let l:lines = filter(l:lines, 'v:val !~# ''@cref''')
  let l:lines = filter(l:lines, 'v:val !~# ''sub@''')
  let l:lines = filter(l:lines, 'v:val !~# ''tocindent-\?[0-9]''')

  " Parse labels from lines
  let l:labels = []
  for l:line in l:lines
    let l:line = vimtex#util#tex2unicode(l:line)
    let l:tree = vimtex#util#tex2tree(l:line)[1:]
    let l:name = get(remove(l:tree, 0), 0, '')
    if empty(l:name) | continue | endif

    let l:name = a:prefix . l:name
    let l:context = remove(l:tree, 0)
    if type(l:context) == v:t_list && len(l:context) > 1
      let l:menu = ''
      try
        let l:type = substitute(l:context[3][0], '\..*$', ' ', '')
        let l:type = substitute(l:type, 'AMS', 'Equation', '')
        let l:menu .= toupper(l:type[0]) . l:type[1:]
      catch
      endtry

      let l:number = s:parse_number(l:context[0])
      if l:menu =~# 'Equation'
        let l:number = '(' . l:number . ')'
      endif
      let l:menu .= l:number

      try
        let l:menu .= ' [p. ' . l:context[1][0] . ']'
      catch
      endtry
      call add(l:labels, {'word': l:name, 'menu': l:menu})
    endif
  endfor<commit_msg>fix: parsing issue for aux label string<commit_after>" VimTeX - LaTeX plugin for Vim
"
" Maintainer: <NAME>
" Email:      <EMAIL>
"

function! vimtex#parser#auxiliary#parse(file) abort " {{{1
  return s:parse_recurse(a:file, [])
endfunction

" }}}1
function! vimtex#parser#auxiliary#labels() abort " {{{1
  "
  " Searches aux files recursively for commands of the form
  "
  "   \newlabel{name}{{number}{page}.*}.*
  "   \newlabel{name}{{text {number}}{page}.*}.*
  "   \newlabel{name}{{number}{page}{...}{type}.*}.*
  "
  " Returns a list of candidates like {'word': name, 'menu': type number page}.
  "
  let l:files = [[b:vimtex.aux(), '']]

  " Handle local file editing (e.g. subfiles package)
  if exists('b:vimtex_local') && b:vimtex_local.active
    let l:files += [[vimtex#state#get(b:vimtex_local.main_id).aux(), '']]
  endif

  " Add externaldocuments (from \externaldocument in preamble)
  let l:files += map(
        \ vimtex#parser#get_externalfiles(),
        \ '[v:val.aux, v:val.opt]')

  let l:cache = vimtex#cache#open('refcomplete', {
        \ 'local': 1,
        \ 'default': {'labels': [], 'ftime': -1}
        \})

  let l:labels = []
  for [l:file, l:prefix] in filter(l:files, 'filereadable(v:val[0])')
    let l:current = l:cache.get(l:file)
    let l:ftime = getftime(l:file)
    if l:ftime > l:current.ftime
      let l:current.ftime = l:ftime
      let l:current.labels = s:parse_labels(l:file, l:prefix)
      let l:cache.modified = 1
    endif

    let l:labels += l:current.labels
  endfor

  " Write cache to file
  call l:cache.write()

  return l:labels
endfunction

" }}}1

function! s:parse_recurse(file, parsed) abort " {{{1
  if !filereadable(a:file) || index(a:parsed, a:file) >= 0
    return []
  endif
  call add(a:parsed, a:file)

  let l:lines = []
  for l:line in readfile(a:file)
    call add(l:lines, l:line)

    if l:line =~# '\\@input{'
      let l:file = s:input_line_parser(l:line, a:file)
      call extend(l:lines, s:parse_recurse(l:file, a:parsed))
    endif
  endfor

  return l:lines
endfunction

" }}}1
function! s:input_line_parser(line, file) abort " {{{1
  let l:file = matchstr(a:line, '\\@input{\zs[^}]\+\ze}')

  " Remove extension to simplify the parsing (e.g. for "my file name".aux)
  let l:file = substitute(l:file, '\.aux', '', '')

  " Trim whitespaces and quotes from beginning/end of string, append extension
  let l:file = substitute(l:file, '^\(\s\|"\)*', '', '')
  let l:file = substitute(l:file, '\(\s\|"\)*$', '', '')
  let l:file .= '.aux'

  " Use absolute paths
  if l:file !~# '\v^(\/|[A-Z]:)'
    let l:file = fnamemodify(a:file, ':p:h') . '/' . l:file
  endif

  " Only return filename if it is readable
  return filereadable(l:file) ? l:file : ''
endfunction

" }}}1

function! s:parse_labels(file, prefix) abort " {{{1
  " Get lines from aux files
  let l:lines = vimtex#parser#auxiliary(a:file)
  let l:lines = filter(l:lines, 'v:val =~# ''\\newlabel{''')
  let l:lines = filter(l:lines, 'v:val !~# ''@cref''')
  let l:lines = filter(l:lines, 'v:val !~# ''sub@''')
  let l:lines = filter(l:lines, 'v:val !~# ''tocindent-\?[0-9]''')

  " Parse labels from lines
  let l:labels = []
  for l:line in l:lines
    let l:line = vimtex#util#tex2unicode(l:line)
    let l:tree = vimtex#util#tex2tree(l:line)[1:]
    if len(l:tree) < 2 | continue | endif

    let l:name = get(remove(l:tree, 0), 0, '')
    if empty(l:name) | continue | endif

    let l:name = a:prefix . l:name
    let l:context = remove(l:tree, 0)
    if type(l:context) == v:t_list && len(l:context) > 1
      let l:menu = ''
      try
        let l:type = substitute(l:context[3][0], '\..*$', ' ', '')
        let l:type = substitute(l:type, 'AMS', 'Equation', '')
        let l:menu .= toupper(l:type[0]) . l:type[1:]
      catch
      endtry

      let l:number = s:parse_number(l:context[0])
      if l:menu =~# 'Equation'
        let l:number = '(' . l:number . ')'
      endif
      let l:menu .= l:number

      try
        let l:menu .= ' [p. ' . l:context[1][0] . ']'
      catch
      endtry
      call add(l:labels, {'word': l:name, 'menu': l:menu})
    endif
  endfor
{'fraction_non_alphanumeric': 0.19340256566890654, 'fraction_numerical': 0.007452657299938913, 'mean_word_length': 3.202258726899384, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5882860', 'n_tokens_mistral': 3251, 'n_tokens_neox': 3118, 'n_words': 716}