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

function! vimtex#format#init_buffer() " {{{1
  if !g:vimtex_format_enabled | return | endif

  setlocal formatexpr=vimtex#format#formatexpr()
endfunction

" }}}1

function! vimtex#format#formatexpr() " {{{1
  if mode() =~# '[iR]' | return -1 | endif

  let l:foldenable = &l:foldenable
  setlocal nofoldenable

  let l:top = v:lnum
  let l:bottom = v:lnum + v:count - 1
  let l:lines_old = getline(l:top, l:bottom)
  let l:tries = 5
  let s:textwidth = &l:textwidth == 0 ? 79 : &l:textwidth

  " This is a hack to make undo restore the correct position
  if mode() !=# 'i'
    normal! ix
    normal! x
  endif

  " Main formatting algorithm
  while l:tries > 0
    " Format the range of lines
    let l:bottom = s:format(l:top, l:bottom)

    " Ensure proper indentation
    if l:top < l:bottom
      silent! execute printf('normal! %sG=%sG', l:top+1, l:bottom)
    endif

    " Check if any lines have changed
    let l:lines_new = getline(l:top, l:bottom)
    let l:index = s:compare_lines(l:lines_new, l:lines_old)
    let l:top += l:index
    if l:top > l:bottom | break | endif
    let l:lines_old = l:lines_new[l:index : -1]
    let l:tries -= 1
  endwhile

  " Set cursor at appropriate position
  execute 'normal!' l:bottom . 'G^'

  " Don't change the text if the formatting algorithm failed
  if l:tries == 0
    silent! undo
    call vimtex#log#warning('Formatting of selected text failed!')
  endif

  let &l:foldenable = l:foldenable
endfunction

" }}}1<commit_msg>Fixed annoying window skip during formatting<commit_after>" vimtex - LaTeX plugin for Vim
"
" Maintainer: <NAME>
" Email:      <EMAIL>
"

function! vimtex#format#init_buffer() " {{{1
  if !g:vimtex_format_enabled | return | endif

  setlocal formatexpr=vimtex#format#formatexpr()
endfunction

" }}}1

function! vimtex#format#formatexpr() " {{{1
  if mode() =~# '[iR]' | return -1 | endif

  " Temporary disable folds and save view
  let l:save_view = winsaveview()
  let l:foldenable = &l:foldenable
  setlocal nofoldenable

  let l:top = v:lnum
  let l:bottom = v:lnum + v:count - 1
  let l:lines_old = getline(l:top, l:bottom)
  let l:tries = 5
  let s:textwidth = &l:textwidth == 0 ? 79 : &l:textwidth

  " This is a hack to make undo restore the correct position
  if mode() !=# 'i'
    normal! ix
    normal! x
  endif

  " Main formatting algorithm
  while l:tries > 0
    " Format the range of lines
    let l:bottom = s:format(l:top, l:bottom)

    " Ensure proper indentation
    if l:top < l:bottom
      silent! execute printf('normal! %sG=%sG', l:top+1, l:bottom)
    endif

    " Check if any lines have changed
    let l:lines_new = getline(l:top, l:bottom)
    let l:index = s:compare_lines(l:lines_new, l:lines_old)
    let l:top += l:index
    if l:top > l:bottom | break | endif
    let l:lines_old = l:lines_new[l:index : -1]
    let l:tries -= 1
  endwhile

  " Restore fold and view
  let &l:foldenable = l:foldenable
  call winrestview(l:save_view)

  " Set cursor at appropriate position
  execute 'normal!' l:bottom . 'G^'

  " Don't change the text if the formatting algorithm failed
  if l:tries == 0
    silent! undo
    call vimtex#log#warning('Formatting of selected text failed!')
  endif
endfunction

" }}}1
{'fraction_non_alphanumeric': 0.12111144669284205, 'fraction_numerical': 0.009060706735125339, 'mean_word_length': 3.4756756756756757, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '3277304', 'n_tokens_mistral': 1254, 'n_tokens_neox': 1189, 'n_words': 355}