13912
|
1 " Vim plugin for formatting XML
|
15131
|
2 " Last Change: Thu, 07 Dec 2018
|
|
3 " Version: 0.1
|
|
4 " Author: Christian Brabandt <cb@256bit.org>
|
|
5 " Repository: https://github.com/chrisbra/vim-xml-ftplugin
|
|
6 " License: VIM License
|
13912
|
7 " Documentation: see :h xmlformat.txt (TODO!)
|
|
8 " ---------------------------------------------------------------------
|
|
9 " Load Once: {{{1
|
|
10 if exists("g:loaded_xmlformat") || &cp
|
|
11 finish
|
|
12 endif
|
|
13 let g:loaded_xmlformat = 1
|
|
14 let s:keepcpo = &cpo
|
|
15 set cpo&vim
|
|
16
|
|
17 " Main function: Format the input {{{1
|
|
18 func! 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 <
|
14006
|
32 for item in split(join(getline(v:lnum, (v:lnum + v:count - 1))), '.\@<=[>]\zs')
|
13912
|
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
|
|
69 endfunc
|
|
70 " Check if given tag is XML Declaration header {{{1
|
|
71 func! s:IsXMLDecl(tag)
|
|
72 return a:tag =~? '^\s*<?xml\s\?\%(version="[^"]*"\)\?\s\?\%(encoding="[^"]*"\)\? ?>\s*$'
|
|
73 endfunc
|
|
74 " Return tag indented by current level {{{1
|
|
75 func! s:Indent(item)
|
|
76 return repeat(' ', shiftwidth()*s:indent). s:Trim(a:item)
|
|
77 endfu
|
|
78 " Return item trimmed from leading whitespace {{{1
|
|
79 func! s:Trim(item)
|
|
80 if exists('*trim')
|
|
81 return trim(a:item)
|
|
82 else
|
|
83 return matchstr(a:item, '\S\+.*')
|
|
84 endif
|
|
85 endfunc
|
|
86 " Check if tag is a new opening tag <tag> {{{1
|
|
87 func! s:StartTag(tag)
|
15131
|
88 let is_comment = s:IsComment(a:tag)
|
|
89 return a:tag =~? '^\s*<[^/?]' && !is_comment
|
|
90 endfunc
|
|
91 func! s:IsComment(tag)
|
|
92 return a:tag =~? '<!--'
|
13912
|
93 endfunc
|
|
94 " Remove one level of indentation {{{1
|
|
95 func! s:DecreaseIndent()
|
|
96 return (s:indent > 0 ? s:indent - 1 : 0)
|
|
97 endfunc
|
|
98 " Check if tag is a closing tag </tag> {{{1
|
|
99 func! s:EndTag(tag)
|
|
100 return a:tag =~? '^\s*</'
|
|
101 endfunc
|
|
102 " Check that the tag is actually a tag and not {{{1
|
|
103 " something like "foobar</foobar>"
|
|
104 func! s:IsTag(tag)
|
|
105 return s:Trim(a:tag)[0] == '<'
|
|
106 endfunc
|
|
107 " Check if tag is empty <tag/> {{{1
|
|
108 func! s:EmptyTag(tag)
|
|
109 return a:tag =~ '/>\s*$'
|
|
110 endfunc
|
|
111 " Restoration And Modelines: {{{1
|
|
112 let &cpo= s:keepcpo
|
|
113 unlet s:keepcpo
|
|
114 " Modeline {{{1
|
|
115 " vim: fdm=marker fdl=0 ts=2 et sw=0 sts=-1
|