11" Vim plugin for formatting XML
2- " Last Change: 2020 Jan 06
2+ " Last Change: 2023 March 15th
33" Version: 0.3
44" Author: Christian Brabandt <cb@256bit.org>
55" Repository: https://github.com/chrisbra/vim-xml-ftplugin
@@ -37,13 +37,17 @@ func! xmlformat#Format() abort
3737 " Keep empty input lines?
3838 if empty (line )
3939 call add (result, ' ' )
40+ let current += 1
4041 continue
4142 elseif line !~# ' <[/]\?[^>]*>'
42- let nextmatch = match (list , ' <[/]\?[^>]*>' , current)
43- if nextmatch > -1
44- let line .= ' ' . join (list [(current + 1 ):(nextmatch- 1 )], " " )
45- call remove (list , current+ 1 , nextmatch- 1 )
43+ let nextmatch = match (list , ' ^\s*$\|<[/]\?[^>]*>' , current)
44+ if nextmatch > -1
45+ let lineEnd = nextmatch
46+ else
47+ let lineEnd = len (list )
4648 endif
49+ let line .= ' ' . join (list [(current + 1 ):(lineEnd- 1 )], " " )
50+ call remove (list , current+ 1 , lineEnd- 1 )
4751 endif
4852 " split on `>`, but don't split on very first opening <
4953 " this means, items can be like ['<tag>', 'tag content</tag>']
@@ -79,9 +83,13 @@ func! xmlformat#Format() abort
7983 if s: EndTag (t [1 ])
8084 call s: DecreaseIndent ()
8185 endif
82- " for y in t[1:]
83- let result+= s: FormatContent (t [1 :])
84- " endfor
86+ let result+= s: FormatContent (t [1 :])
87+ if s: IsTag (t [1 ])
88+ let lastitem = t [1 ]
89+ continue
90+ endif
91+ elseif s: IsComment (item)
92+ let result+= s: FormatContent ([item])
8593 else
8694 call add (result, s: Indent (item))
8795 endif
@@ -94,7 +102,7 @@ func! xmlformat#Format() abort
94102 if ! empty (result)
95103 let lastprevline = getline (v: lnum + count_orig)
96104 let delete_lastline = v: lnum + count_orig - 1 == line (' $' )
97- exe v: lnum . " ," . (v: lnum + count_orig - 1 ). ' d'
105+ exe ' silent ' .. v: lnum . " ," . (v: lnum + count_orig - 1 ). ' d'
98106 call append (v: lnum - 1 , result)
99107 " Might need to remove the last line, if it became empty because of the
100108 " append() call
0 commit comments