1" Vim plugin for formatting XML
2" Last Change: 2020 Jan 06
3"     Version: 0.3
4"      Author: Christian Brabandt <[email protected]>
5"  Repository: https://github.com/chrisbra/vim-xml-ftplugin
6"     License: VIM License
7" Documentation: see :h xmlformat.txt (TODO!)
8" ---------------------------------------------------------------------
9" Load Once: {{{1
10if exists("g:loaded_xmlformat") || &cp
11  finish
12endif
13let g:loaded_xmlformat = 1
14let s:keepcpo       = &cpo
15set cpo&vim
16
17" Main function: Format the input {{{1
18func! xmlformat#Format() abort
19  " only allow reformatting through the gq command
20  " (e.g. Vim is in normal mode)
21  if mode() != 'n'
22    " do not fall back to internal formatting
23    return 0
24  endif
25  let count_orig = v:count
26  let sw  = shiftwidth()
27  let prev = prevnonblank(v:lnum-1)
28  let s:indent = indent(prev)/sw
29  let result = []
30  let lastitem = prev ? getline(prev) : ''
31  let is_xml_decl = 0
32  " go through every line, but don't join all content together and join it
33  " back. We might lose empty lines
34  let list = getline(v:lnum, (v:lnum + count_orig - 1))
35  let current = 0
36  for line in list
37    " Keep empty input lines?
38    if empty(line)
39      call add(result, '')
40      continue
41    elseif line !~# '<[/]\?[^>]*>'
42      let nextmatch = match(list, '<[/]\?[^>]*>', current)
43      if nextmatch > -1
44        let line .= ' '. join(list[(current + 1):(nextmatch-1)], " ")
45        call remove(list, current+1, nextmatch-1)
46      endif
47    endif
48    " split on `>`, but don't split on very first opening <
49    " this means, items can be like ['<tag>', 'tag content</tag>']
50    for item in split(line, '.\@<=[>]\zs')
51      if s:EndTag(item)
52        call s:DecreaseIndent()
53        call add(result, s:Indent(item))
54      elseif s:EmptyTag(lastitem)
55        call add(result, s:Indent(item))
56      elseif s:StartTag(lastitem) && s:IsTag(item)
57        let s:indent += 1
58        call add(result, s:Indent(item))
59      else
60        if !s:IsTag(item)
61          " Simply split on '<', if there is one,
62          " but reformat according to &textwidth
63          let t=split(item, '.<\@=\zs')
64
65          " if the content fits well within a single line, add it there
66          " so that the output looks like this:
67          "
68          " <foobar>1</foobar>
69          if s:TagContent(lastitem) is# s:TagContent(t[1]) && strlen(result[-1]) + strlen(item) <= s:Textwidth()
70            let result[-1] .= item
71            let lastitem = t[1]
72            continue
73          endif
74          " t should only contain 2 items, but just be safe here
75          if s:IsTag(lastitem)
76            let s:indent+=1
77          endif
78          let result+=s:FormatContent([t[0]])
79          if s:EndTag(t[1])
80            call s:DecreaseIndent()
81          endif
82          "for y in t[1:]
83            let result+=s:FormatContent(t[1:])
84          "endfor
85        else
86          call add(result, s:Indent(item))
87        endif
88      endif
89      let lastitem = item
90    endfor
91    let current += 1
92  endfor
93
94  if !empty(result)
95    let lastprevline = getline(v:lnum + count_orig)
96    let delete_lastline = v:lnum + count_orig - 1 == line('$')
97    exe v:lnum. ",". (v:lnum + count_orig - 1). 'd'
98    call append(v:lnum - 1, result)
99    " Might need to remove the last line, if it became empty because of the
100    " append() call
101    let last = v:lnum + len(result)
102    " do not use empty(), it returns true for `empty(0)`
103    if getline(last) is '' && lastprevline is '' && delete_lastline
104      exe last. 'd'
105    endif
106  endif
107
108  " do not run internal formatter!
109  return 0
110endfunc
111" Check if given tag is XML Declaration header {{{1
112func! s:IsXMLDecl(tag) abort
113  return a:tag =~? '^\s*<?xml\s\?\%(version="[^"]*"\)\?\s\?\%(encoding="[^"]*"\)\? ?>\s*$'
114endfunc
115" Return tag indented by current level {{{1
116func! s:Indent(item) abort
117  return repeat(' ', shiftwidth()*s:indent). s:Trim(a:item)
118endfu
119" Return item trimmed from leading whitespace {{{1
120func! s:Trim(item) abort
121  if exists('*trim')
122    return trim(a:item)
123  else
124    return matchstr(a:item, '\S\+.*')
125  endif
126endfunc
127" Check if tag is a new opening tag <tag> {{{1
128func! s:StartTag(tag) abort
129  let is_comment = s:IsComment(a:tag)
130  return a:tag =~? '^\s*<[^/?]' && !is_comment
131endfunc
132" Check if tag is a Comment start {{{1
133func! s:IsComment(tag) abort
134  return a:tag =~? '<!--'
135endfunc
136" Remove one level of indentation {{{1
137func! s:DecreaseIndent() abort
138  let s:indent = (s:indent > 0 ? s:indent - 1 : 0)
139endfunc
140" Check if tag is a closing tag </tag> {{{1
141func! s:EndTag(tag) abort
142  return a:tag =~? '^\s*</'
143endfunc
144" Check that the tag is actually a tag and not {{{1
145" something like "foobar</foobar>"
146func! s:IsTag(tag) abort
147  return s:Trim(a:tag)[0] == '<'
148endfunc
149" Check if tag is empty <tag/> {{{1
150func! s:EmptyTag(tag) abort
151  return a:tag =~ '/>\s*$'
152endfunc
153func! s:TagContent(tag) abort "{{{1
154  " Return content of a tag
155  return substitute(a:tag, '^\s*<[/]\?\([^>]*\)>\s*$', '\1', '')
156endfunc
157func! s:Textwidth() abort "{{{1
158  " return textwidth (or 80 if not set)
159  return &textwidth == 0 ? 80 : &textwidth
160endfunc
161" Format input line according to textwidth {{{1
162func! s:FormatContent(list) abort
163  let result=[]
164  let limit = s:Textwidth()
165  let column=0
166  let idx = -1
167  let add_indent = 0
168  let cnt = 0
169  for item in a:list
170    for word in split(item, '\s\+\S\+\zs')
171      if match(word, '^\s\+$') > -1
172        " skip empty words
173        continue
174      endif
175      let column += strdisplaywidth(word, column)
176      if match(word, "^\\s*\n\\+\\s*$") > -1
177        call add(result, '')
178        let idx += 1
179        let column = 0
180        let add_indent = 1
181      elseif column > limit || cnt == 0
182        let add = s:Indent(s:Trim(word))
183        call add(result, add)
184        let column = strdisplaywidth(add)
185        let idx += 1
186      else
187        if add_indent
188          let result[idx] = s:Indent(s:Trim(word))
189        else
190          let result[idx] .= ' '. s:Trim(word)
191        endif
192        let add_indent = 0
193      endif
194      let cnt += 1
195    endfor
196  endfor
197  return result
198endfunc
199" Restoration And Modelines: {{{1
200let &cpo= s:keepcpo
201unlet s:keepcpo
202" Modeline {{{1
203" vim: fdm=marker fdl=0 ts=2 et sw=0 sts=-1
204