1" Vim plugin for formatting XML
2" Last Change: 2019 Oct 24
3"     Version: 0.2
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()
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      let line .= join(list[(current + 1):(nextmatch-1)], "\n")
44      call remove(list, current+1, nextmatch-1)
45    endif
46    " split on `>`, but don't split on very first opening <
47    " this means, items can be like ['<tag>', 'tag content</tag>']
48    for item in split(line, '.\@<=[>]\zs')
49      if s:EndTag(item)
50        let s:indent = s:DecreaseIndent()
51        call add(result, s:Indent(item))
52      elseif s:EmptyTag(lastitem)
53        call add(result, s:Indent(item))
54      elseif s:StartTag(lastitem) && s:IsTag(item)
55        let s:indent += 1
56        call add(result, s:Indent(item))
57      else
58        if !s:IsTag(item)
59          " Simply split on '<', if there is one,
60          " but reformat according to &textwidth
61          let t=split(item, '.<\@=\zs')
62          " t should only contain 2 items, but just be safe here
63          if s:IsTag(lastitem)
64            let s:indent+=1
65          endif
66          let result+=s:FormatContent([t[0]])
67          if s:EndTag(t[1])
68            let s:indent = s:DecreaseIndent()
69          endif
70          "for y in t[1:]
71            let result+=s:FormatContent(t[1:])
72          "endfor
73        else
74          call add(result, s:Indent(item))
75        endif
76      endif
77      let lastitem = item
78    endfor
79    let current += 1
80  endfor
81
82  if !empty(result)
83    let lastprevline = getline(v:lnum + count_orig)
84    let delete_lastline = v:lnum + count_orig - 1 == line('$')
85    exe v:lnum. ",". (v:lnum + count_orig - 1). 'd'
86    call append(v:lnum - 1, result)
87    " Might need to remove the last line, if it became empty because of the
88    " append() call
89    let last = v:lnum + len(result)
90    " do not use empty(), it returns true for `empty(0)`
91    if getline(last) is '' && lastprevline is '' && delete_lastline
92      exe last. 'd'
93    endif
94  endif
95
96  " do not run internal formatter!
97  return 0
98endfunc
99" Check if given tag is XML Declaration header {{{1
100func! s:IsXMLDecl(tag)
101  return a:tag =~? '^\s*<?xml\s\?\%(version="[^"]*"\)\?\s\?\%(encoding="[^"]*"\)\? ?>\s*$'
102endfunc
103" Return tag indented by current level {{{1
104func! s:Indent(item)
105  return repeat(' ', shiftwidth()*s:indent). s:Trim(a:item)
106endfu
107" Return item trimmed from leading whitespace {{{1
108func! s:Trim(item)
109  if exists('*trim')
110    return trim(a:item)
111  else
112    return matchstr(a:item, '\S\+.*')
113  endif
114endfunc
115" Check if tag is a new opening tag <tag> {{{1
116func! s:StartTag(tag)
117  let is_comment = s:IsComment(a:tag)
118  return a:tag =~? '^\s*<[^/?]' && !is_comment
119endfunc
120" Check if tag is a Comment start {{{1
121func! s:IsComment(tag)
122  return a:tag =~? '<!--'
123endfunc
124" Remove one level of indentation {{{1
125func! s:DecreaseIndent()
126  return (s:indent > 0 ? s:indent - 1 : 0)
127endfunc
128" Check if tag is a closing tag </tag> {{{1
129func! s:EndTag(tag)
130  return a:tag =~? '^\s*</'
131endfunc
132" Check that the tag is actually a tag and not {{{1
133" something like "foobar</foobar>"
134func! s:IsTag(tag)
135  return s:Trim(a:tag)[0] == '<'
136endfunc
137" Check if tag is empty <tag/> {{{1
138func! s:EmptyTag(tag)
139  return a:tag =~ '/>\s*$'
140endfunc
141" Format input line according to textwidth {{{1
142func! s:FormatContent(list)
143  let result=[]
144  let limit = 80
145  if &textwidth > 0
146    let limit = &textwidth
147  endif
148  let column=0
149  let idx = -1
150  let add_indent = 0
151  let cnt = 0
152  for item in a:list
153    for word in split(item, '\s\+\S\+\zs')
154      let column += strdisplaywidth(word, column)
155      if match(word, "^\\s*\n\\+\\s*$") > -1
156        call add(result, '')
157        let idx += 1
158        let column = 0
159        let add_indent = 1
160      elseif column > limit || cnt == 0
161        let add = s:Indent(s:Trim(word))
162        call add(result, add)
163        let column = strdisplaywidth(add)
164        let idx += 1
165      else
166        if add_indent
167          let result[idx] = s:Indent(s:Trim(word))
168        else
169          let result[idx] .= ' '. s:Trim(word)
170        endif
171        let add_indent = 0
172      endif
173      let cnt += 1
174    endfor
175  endfor
176  return result
177endfunc
178" Restoration And Modelines: {{{1
179let &cpo= s:keepcpo
180unlet s:keepcpo
181" Modeline {{{1
182" vim: fdm=marker fdl=0 ts=2 et sw=0 sts=-1
183