1" Vim plugin for formatting XML
2" Last Change: Thu, 07 Dec 2018
3"     Version: 0.1
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 sw  = shiftwidth()
26  let prev = prevnonblank(v:lnum-1)
27  let s:indent = indent(prev)/sw
28  let result = []
29  let lastitem = prev ? getline(prev) : ''
30  let is_xml_decl = 0
31  " split on `<`, but don't split on very first opening <
32  for item in split(join(getline(v:lnum, (v:lnum + v:count - 1))), '.\@<=[>]\zs')
33    if s:EndTag(item)
34      let s:indent = s:DecreaseIndent()
35      call add(result, s:Indent(item))
36    elseif s:EmptyTag(lastitem)
37      call add(result, s:Indent(item))
38    elseif s:StartTag(lastitem) && s:IsTag(item)
39      let s:indent += 1
40      call add(result, s:Indent(item))
41     else
42       if !s:IsTag(item)
43         " Simply split on '<'
44         let t=split(item, '.<\@=\zs')
45         let s:indent+=1
46         call add(result, s:Indent(t[0]))
47         let s:indent = s:DecreaseIndent()
48         call add(result, s:Indent(t[1]))
49       else
50        call add(result, s:Indent(item))
51      endif
52     endif
53     let lastitem = item
54   endfor
55
56   if !empty(result)
57    exe v:lnum. ",". (v:lnum + v:count - 1). 'd'
58    call append(v:lnum - 1, result)
59    " Might need to remove the last line, if it became empty because of the
60    " append() call
61    let last = v:lnum + len(result)
62    if getline(last) is ''
63      exe last. 'd'
64    endif
65  endif
66
67  " do not run internal formatter!
68  return 0
69endfunc
70" Check if given tag is XML Declaration header {{{1
71func! s:IsXMLDecl(tag)
72  return a:tag =~? '^\s*<?xml\s\?\%(version="[^"]*"\)\?\s\?\%(encoding="[^"]*"\)\? ?>\s*$'
73endfunc
74" Return tag indented by current level {{{1
75func! s:Indent(item)
76  return repeat(' ', shiftwidth()*s:indent). s:Trim(a:item)
77endfu
78" Return item trimmed from leading whitespace {{{1
79func! s:Trim(item)
80  if exists('*trim')
81    return trim(a:item)
82  else
83    return matchstr(a:item, '\S\+.*')
84  endif
85endfunc
86" Check if tag is a new opening tag <tag> {{{1
87func! s:StartTag(tag)
88  let is_comment = s:IsComment(a:tag)
89  return a:tag =~? '^\s*<[^/?]' && !is_comment
90endfunc
91func! s:IsComment(tag)
92  return a:tag =~? '<!--'
93endfunc
94" Remove one level of indentation {{{1
95func! s:DecreaseIndent()
96  return (s:indent > 0 ? s:indent - 1 : 0)
97endfunc
98" Check if tag is a closing tag </tag> {{{1
99func! s:EndTag(tag)
100  return a:tag =~? '^\s*</'
101endfunc
102" Check that the tag is actually a tag and not {{{1
103" something like "foobar</foobar>"
104func! s:IsTag(tag)
105  return s:Trim(a:tag)[0] == '<'
106endfunc
107" Check if tag is empty <tag/> {{{1
108func! s:EmptyTag(tag)
109  return a:tag =~ '/>\s*$'
110endfunc
111" Restoration And Modelines: {{{1
112let &cpo= s:keepcpo
113unlet s:keepcpo
114" Modeline {{{1
115" vim: fdm=marker fdl=0 ts=2 et sw=0 sts=-1
116