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

function! vimtex#fold#bib#init() abort " {{{1
  let b:vimtex_fold_bib_maxwidth = s:get_max_key_width()

  augroup vimtex_buffers
    autocmd BufWrite <buffer>
          \ let b:vimtex_fold_bib_maxwidth = s:get_max_key_width()
  augroup END

  setlocal foldmethod=expr
  setlocal foldexpr=vimtex#fold#bib#level(v:lnum)
  setlocal foldtext=vimtex#fold#bib#text()
endfunction

" }}}1

function! vimtex#fold#bib#level(lnum) abort " {{{1
  " Handle blank lines
  if empty(trim(getline(a:lnum)))
    let l:prev_level = vimtex#fold#bib#level(a:lnum - 1)

    if l:prev_level == '<1' || l:prev_level == 0
      return 0
    endif

    " Check if previous line is a standalone entry
    if l:prev_level == '>1'
      let l:prev_line = trim(getline(a:lnum - 1))
      if l:prev_line[0] == '@'
            \ && s:count(l:prev_line, '{') == s:count(l:prev_line, '}')
        return 0
      endif
    endif

    return 1
  endif

  " Search for the beginning of the entry
  let l:curpos = getcurpos()
  call cursor(a:lnum, 0)
  let l:firstline = search('\v^\s*\@', 'bcnW')
  call setpos('.', l:curpos)

  " Check if we're at fold start
  if l:firstline == a:lnum | return '>1' | endif

  " Beginning of entry wasn't found
  if l:firstline == 0 | return 0 | endif

  " Check if braces are closed by the current line
  let l:text = join(getline(l:firstline, a:lnum))
  return s:count(l:text, '{') == s:count(l:text, '}') ? '<1' : 1
endfunction

" }}}1
function! vimtex#fold#bib#text() abort " {{{1
  let l:bib_entries = vimtex#parser#bib#parse_cheap(v:foldstart, v:foldend, {})
  if len(l:bib_entries) != 1 | return foldtext() | endif

  let l:entry = l:bib_entries[0]
  if empty(l:entry.type) || empty(l:entry.key)
    return foldtext()
  endif

  let l:foldtext = '@' . l:entry.type . '{' . l:entry.key . '}'

  let l:width = strdisplaywidth(l:foldtext)
  if l:width > b:vimtex_fold_bib_maxwidth
    let l:foldtext = printf('%.' . b:vimtex_fold_bib_maxwidth . 'S', l:foldtext)
    let l:width = strdisplaywidth(l:foldtext)<commit_msg>chore: simplify and improve vimtex#fold#bib#level<commit_after>" VimTeX - LaTeX plugin for Vim
"
" Maintainer: <NAME>
" Email:      <EMAIL>
"

function! vimtex#fold#bib#init() abort " {{{1
  let b:vimtex_fold_bib_maxwidth = s:get_max_key_width()

  augroup vimtex_buffers
    autocmd BufWrite <buffer>
          \ let b:vimtex_fold_bib_maxwidth = s:get_max_key_width()
  augroup END

  setlocal foldmethod=expr
  setlocal foldexpr=vimtex#fold#bib#level(v:lnum)
  setlocal foldtext=vimtex#fold#bib#text()
endfunction

" }}}1

function! vimtex#fold#bib#level(lnum) abort " {{{1
  " Handle blank lines
  if empty(trim(getline(a:lnum)))
    if a:lnum == 1 | return 0 | endif

    let l:prev_level = vimtex#fold#bib#level(a:lnum - 1)
    return l:prev_level == '<1' ? 0 : l:prev_level
  endif

  " Search for the beginning of the entry
  let l:curpos = getcurpos()
  call cursor(a:lnum, 0)
  let l:firstline = search('\v^\s*\@', 'bcnW')
  call setpos('.', l:curpos)

  " Check if we're at fold start
  let l:text = join(getline(l:firstline, a:lnum))
  let l:count_open = s:count(l:text, '{')
  let l:braces_balanced = l:count_open == s:count(l:text, '}')
  if l:firstline == a:lnum
    return l:count_open > 0 && l:braces_balanced ? 0 : '>1'
  endif

  " Beginning of entry wasn't found
  if l:firstline == 0 | return 0 | endif

  " Check if braces are closed by the current line
  return l:braces_balanced ? '<1' : 1
endfunction

" }}}1
function! vimtex#fold#bib#text() abort " {{{1
  let l:bib_entries = vimtex#parser#bib#parse_cheap(v:foldstart, v:foldend, {})
  if len(l:bib_entries) != 1 | return foldtext() | endif

  let l:entry = l:bib_entries[0]
  if empty(l:entry.type) || empty(l:entry.key)
    return foldtext()
  endif

  let l:foldtext = '@' . l:entry.type . '{' . l:entry.key . '}'

  let l:width = strdisplaywidth(l:foldtext)
  if l:width > b:vimtex_fold_bib_maxwidth
    let l:foldtext = printf('%.' . b:vimtex_fold_bib_maxwidth . 'S', l:foldtext)
    let l:width = strdisplaywidth(l:foldtext)
{'fraction_non_alphanumeric': 0.14899296287308905, 'fraction_numerical': 0.01019170104343606, 'mean_word_length': 3.9129916567342073, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, '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': '788065', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1596, 'n_words': 365}