13912
|
1 " Vim plugin for formatting XML
|
19574
|
2 " Last Change: 2020 Jan 06
|
|
3 " Version: 0.3
|
15131
|
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
|
19574
|
18 func! xmlformat#Format() abort
|
13912
|
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
|
18456
|
25 let count_orig = v:count
|
13912
|
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
|
18456
|
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)
|
19574
|
43 if nextmatch > -1
|
|
44 let line .= ' '. join(list[(current + 1):(nextmatch-1)], " ")
|
|
45 call remove(list, current+1, nextmatch-1)
|
|
46 endif
|
18456
|
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)
|
19574
|
52 call s:DecreaseIndent()
|
18456
|
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
|
13912
|
58 call add(result, s:Indent(item))
|
18456
|
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')
|
19574
|
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
|
18456
|
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])
|
19574
|
80 call s:DecreaseIndent()
|
18456
|
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
|
13912
|
88 endif
|
18456
|
89 let lastitem = item
|
|
90 endfor
|
|
91 let current += 1
|
|
92 endfor
|
13912
|
93
|
18456
|
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'
|
13912
|
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)
|
18456
|
102 " do not use empty(), it returns true for `empty(0)`
|
|
103 if getline(last) is '' && lastprevline is '' && delete_lastline
|
13912
|
104 exe last. 'd'
|
|
105 endif
|
|
106 endif
|
|
107
|
|
108 " do not run internal formatter!
|
|
109 return 0
|
|
110 endfunc
|
|
111 " Check if given tag is XML Declaration header {{{1
|
19574
|
112 func! s:IsXMLDecl(tag) abort
|
13912
|
113 return a:tag =~? '^\s*<?xml\s\?\%(version="[^"]*"\)\?\s\?\%(encoding="[^"]*"\)\? ?>\s*$'
|
|
114 endfunc
|
|
115 " Return tag indented by current level {{{1
|
19574
|
116 func! s:Indent(item) abort
|
13912
|
117 return repeat(' ', shiftwidth()*s:indent). s:Trim(a:item)
|
|
118 endfu
|
|
119 " Return item trimmed from leading whitespace {{{1
|
19574
|
120 func! s:Trim(item) abort
|
13912
|
121 if exists('*trim')
|
|
122 return trim(a:item)
|
|
123 else
|
|
124 return matchstr(a:item, '\S\+.*')
|
|
125 endif
|
|
126 endfunc
|
|
127 " Check if tag is a new opening tag <tag> {{{1
|
19574
|
128 func! s:StartTag(tag) abort
|
15131
|
129 let is_comment = s:IsComment(a:tag)
|
|
130 return a:tag =~? '^\s*<[^/?]' && !is_comment
|
|
131 endfunc
|
18456
|
132 " Check if tag is a Comment start {{{1
|
19574
|
133 func! s:IsComment(tag) abort
|
15131
|
134 return a:tag =~? '<!--'
|
13912
|
135 endfunc
|
|
136 " Remove one level of indentation {{{1
|
19574
|
137 func! s:DecreaseIndent() abort
|
|
138 let s:indent = (s:indent > 0 ? s:indent - 1 : 0)
|
13912
|
139 endfunc
|
|
140 " Check if tag is a closing tag </tag> {{{1
|
19574
|
141 func! s:EndTag(tag) abort
|
13912
|
142 return a:tag =~? '^\s*</'
|
|
143 endfunc
|
|
144 " Check that the tag is actually a tag and not {{{1
|
|
145 " something like "foobar</foobar>"
|
19574
|
146 func! s:IsTag(tag) abort
|
13912
|
147 return s:Trim(a:tag)[0] == '<'
|
|
148 endfunc
|
|
149 " Check if tag is empty <tag/> {{{1
|
19574
|
150 func! s:EmptyTag(tag) abort
|
13912
|
151 return a:tag =~ '/>\s*$'
|
|
152 endfunc
|
19574
|
153 func! s:TagContent(tag) abort "{{{1
|
|
154 " Return content of a tag
|
|
155 return substitute(a:tag, '^\s*<[/]\?\([^>]*\)>\s*$', '\1', '')
|
|
156 endfunc
|
|
157 func! s:Textwidth() abort "{{{1
|
|
158 " return textwidth (or 80 if not set)
|
|
159 return &textwidth == 0 ? 80 : &textwidth
|
|
160 endfunc
|
18456
|
161 " Format input line according to textwidth {{{1
|
19574
|
162 func! s:FormatContent(list) abort
|
18456
|
163 let result=[]
|
19574
|
164 let limit = s:Textwidth()
|
18456
|
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')
|
19574
|
171 if match(word, '^\s\+$') > -1
|
|
172 " skip empty words
|
|
173 continue
|
|
174 endif
|
18456
|
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
|
|
198 endfunc
|
13912
|
199 " Restoration And Modelines: {{{1
|
|
200 let &cpo= s:keepcpo
|
|
201 unlet s:keepcpo
|
|
202 " Modeline {{{1
|
|
203 " vim: fdm=marker fdl=0 ts=2 et sw=0 sts=-1
|