File: format.vim

package info (click to toggle)
vim-vimtex 2.17-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,844 kB
  • sloc: makefile: 360; python: 103
file content (187 lines) | stat: -rw-r--r-- 4,762 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
" VimTeX - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve LervÄg
" Email:      karl.yngve@gmail.com
"

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

  setlocal formatexpr=vimtex#format#formatexpr()
endfunction

" }}}1

function! vimtex#format#formatexpr() abort " {{{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

  call vimtex#util#undostore()

  " 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

function! s:format(top, bottom) abort " {{{1
  let l:bottom = a:bottom
  let l:mark = a:bottom
  for l:current in range(a:bottom, a:top, -1)
    let l:line = getline(l:current)

    if vimtex#syntax#in_mathzone(l:current, 1)
          \ && vimtex#syntax#in_mathzone(l:current, col([l:current, '$']))
      let l:mark = l:current - 1
      continue
    endif

    " Skip all lines with comments
    if l:line =~# '\v%(^|[^\\])\%'
      if l:current < l:mark
        let l:bottom += s:format_build_lines(l:current+1, l:mark)
      endif
      let l:mark = l:current - 1
      continue
    endif

    " Handle long lines
    if strdisplaywidth(l:line) > s:textwidth
      let l:bottom += s:format_build_lines(l:current, l:mark)
      let l:mark = l:current-1
    endif

    if l:line =~# g:vimtex_format_border_end
      if l:current < l:mark
        let l:bottom += s:format_build_lines(l:current+1, l:mark)
      endif
      let l:mark = l:current
    endif

    if l:line =~# g:vimtex_format_border_begin
      if l:current < l:mark
        let l:bottom += s:format_build_lines(l:current, l:mark)
      endif
      let l:mark = l:current-1
    endif

    if l:line =~# '^\s*$'
      let l:bottom += s:format_build_lines(l:current+1, l:mark)
      let l:mark = l:current-1
    endif
  endfor

  if a:top <= l:mark
    let l:bottom += s:format_build_lines(a:top, l:mark)
  endif

  return l:bottom
endfunction

" }}}1
function! s:format_build_lines(start, end) abort " {{{1
  "
  " Get the desired text to format as a list of words, but preserve the ending
  " line spaces
  "
  let l:text = join(map(getline(a:start, a:end),
        \ {_, x -> substitute(x, '^\s*', '', '')}))
  let l:spaces = matchstr(l:text, '\s*$')
  let l:words = split(l:text, ' ')
  if empty(l:words) | return 0 | endif

  "
  " Add the words in properly indented and formatted lines
  "
  let l:lnum = a:start-1
  let l:current = s:get_indents(indent(a:start))
  for l:word in l:words
    if strdisplaywidth(l:word) + strdisplaywidth(l:current) > s:textwidth
      call append(l:lnum, substitute(l:current, '\s$', '', ''))
      let l:lnum += 1
      let l:current = s:get_indents(indent(a:start))
    endif
    let l:current .= l:word . ' '
  endfor
  if l:current !~# '^\s*$'
    call append(l:lnum, substitute(l:current, '\s$', '', ''))
    let l:lnum += 1
  endif

  "
  " Append the ending line spaces
  "
  if !empty(l:spaces)
    call setline(l:lnum, getline(l:lnum) . l:spaces)
  endif

  "
  " Remove old text
  "
  silent! execute printf('%s;+%s delete', l:lnum+1, a:end-a:start)

  "
  " Return the difference between number of lines of old and new text
  "
  return l:lnum - a:end
endfunction

" }}}1

function! s:compare_lines(new, old) abort " {{{1
  let l:min_length = min([len(a:new), len(a:old)])
  for l:i in range(l:min_length)
    if a:new[l:i] !=# a:old[l:i]
      return l:i
    endif
  endfor
  return l:min_length
endfunction

" }}}1
function! s:get_indents(number) abort " {{{1
  return !&l:expandtab && &l:shiftwidth == &l:tabstop
        \ ? repeat("\t", a:number/&l:tabstop)
        \ : repeat(' ', a:number)
endfunction

" }}}1