13912
|
1 " Vim plugin for formatting XML
|
14006
|
2 " Last Change: Thu, 22 May 2018 21:26:55 +0100
|
13912
|
3 " Version: 0.1
|
|
4 " Author: Christian Brabandt <cb@256bit.org>
|
|
5 " Script: http://www.vim.org/scripts/script.php?script_id=
|
|
6 " License: VIM License
|
|
7 " GetLatestVimScripts: ???? 18 :AutoInstall: xmlformat.vim
|
|
8 " Documentation: see :h xmlformat.txt (TODO!)
|
|
9 " ---------------------------------------------------------------------
|
|
10 " Load Once: {{{1
|
|
11 if exists("g:loaded_xmlformat") || &cp
|
|
12 finish
|
|
13 endif
|
|
14 let g:loaded_xmlformat = 1
|
|
15 let s:keepcpo = &cpo
|
|
16 set cpo&vim
|
|
17
|
|
18 " Main function: Format the input {{{1
|
|
19 func! xmlformat#Format()
|
|
20 " only allow reformatting through the gq command
|
|
21 " (e.g. Vim is in normal mode)
|
|
22 if mode() != 'n'
|
|
23 " do not fall back to internal formatting
|
|
24 return 0
|
|
25 endif
|
|
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 " split on `<`, but don't split on very first opening <
|
14006
|
33 for item in split(join(getline(v:lnum, (v:lnum + v:count - 1))), '.\@<=[>]\zs')
|
13912
|
34 if s:EndTag(item)
|
|
35 let s:indent = s:DecreaseIndent()
|
|
36 call add(result, s:Indent(item))
|
|
37 elseif s:EmptyTag(lastitem)
|
|
38 call add(result, s:Indent(item))
|
|
39 elseif s:StartTag(lastitem) && s:IsTag(item)
|
|
40 let s:indent += 1
|
|
41 call add(result, s:Indent(item))
|
|
42 else
|
|
43 if !s:IsTag(item)
|
|
44 " Simply split on '<'
|
|
45 let t=split(item, '.<\@=\zs')
|
|
46 let s:indent+=1
|
|
47 call add(result, s:Indent(t[0]))
|
|
48 let s:indent = s:DecreaseIndent()
|
|
49 call add(result, s:Indent(t[1]))
|
|
50 else
|
|
51 call add(result, s:Indent(item))
|
|
52 endif
|
|
53 endif
|
|
54 let lastitem = item
|
|
55 endfor
|
|
56
|
|
57 if !empty(result)
|
|
58 exe v:lnum. ",". (v:lnum + v:count - 1). 'd'
|
|
59 call append(v:lnum - 1, result)
|
|
60 " Might need to remove the last line, if it became empty because of the
|
|
61 " append() call
|
|
62 let last = v:lnum + len(result)
|
|
63 if getline(last) is ''
|
|
64 exe last. 'd'
|
|
65 endif
|
|
66 endif
|
|
67
|
|
68 " do not run internal formatter!
|
|
69 return 0
|
|
70 endfunc
|
|
71 " Check if given tag is XML Declaration header {{{1
|
|
72 func! s:IsXMLDecl(tag)
|
|
73 return a:tag =~? '^\s*<?xml\s\?\%(version="[^"]*"\)\?\s\?\%(encoding="[^"]*"\)\? ?>\s*$'
|
|
74 endfunc
|
|
75 " Return tag indented by current level {{{1
|
|
76 func! s:Indent(item)
|
|
77 return repeat(' ', shiftwidth()*s:indent). s:Trim(a:item)
|
|
78 endfu
|
|
79 " Return item trimmed from leading whitespace {{{1
|
|
80 func! s:Trim(item)
|
|
81 if exists('*trim')
|
|
82 return trim(a:item)
|
|
83 else
|
|
84 return matchstr(a:item, '\S\+.*')
|
|
85 endif
|
|
86 endfunc
|
|
87 " Check if tag is a new opening tag <tag> {{{1
|
|
88 func! s:StartTag(tag)
|
|
89 return a:tag =~? '^\s*<[^/?]'
|
|
90 endfunc
|
|
91 " Remove one level of indentation {{{1
|
|
92 func! s:DecreaseIndent()
|
|
93 return (s:indent > 0 ? s:indent - 1 : 0)
|
|
94 endfunc
|
|
95 " Check if tag is a closing tag </tag> {{{1
|
|
96 func! s:EndTag(tag)
|
|
97 return a:tag =~? '^\s*</'
|
|
98 endfunc
|
|
99 " Check that the tag is actually a tag and not {{{1
|
|
100 " something like "foobar</foobar>"
|
|
101 func! s:IsTag(tag)
|
|
102 return s:Trim(a:tag)[0] == '<'
|
|
103 endfunc
|
|
104 " Check if tag is empty <tag/> {{{1
|
|
105 func! s:EmptyTag(tag)
|
|
106 return a:tag =~ '/>\s*$'
|
|
107 endfunc
|
|
108 " Restoration And Modelines: {{{1
|
|
109 let &cpo= s:keepcpo
|
|
110 unlet s:keepcpo
|
|
111 " Modeline {{{1
|
|
112 " vim: fdm=marker fdl=0 ts=2 et sw=0 sts=-1
|