Skip to content

Commit 5eb10c5

Browse files
committed
runtime(xml): update XML runtime files
closes: #19112 Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent c45e16a commit 5eb10c5

3 files changed

Lines changed: 53 additions & 22 deletions

File tree

runtime/autoload/xmlformat.vim

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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

runtime/ftplugin/xml.vim

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
" Vim filetype plugin file
22
" Language: xml
33
" Maintainer: Christian Brabandt <cb@256bit.org>
4-
" Last Changed: Dec 07th, 2018
5-
" 2024 Jan 14 by Vim Project (browsefilter)
6-
" 2024 May 23 by Riley Bruins <ribru17@gmail.com> ('commentstring')
7-
" Repository: https://github.com/chrisbra/vim-xml-ftplugin
8-
" Previous Maintainer: Dan Sharp
9-
" URL: http://dwsharp.users.sourceforge.net/vim/ftplugin
4+
" Last Changed: 2024 May 24
5+
" Repository: https://github.com/chrisbra/vim-xml-ftplugin
6+
" Previously
7+
" Maintainer: Dan Sharp <dwsharp at users dot sourceforge dot net>
8+
" URL: http://dwsharp.users.sourceforge.net/vim/ftplugin
109

1110
if exists("b:did_ftplugin") | finish | endif
1211
let b:did_ftplugin = 1
@@ -54,12 +53,8 @@ command! -nargs=? XMLent call xmlcomplete#CreateEntConnection(<f-args>)
5453
if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
5554
let b:browsefilter="XML Files (*.xml)\t*.xml\n" .
5655
\ "DTD Files (*.dtd)\t*.dtd\n" .
57-
\ "XSD Files (*.xsd)\t*.xsd\n"
58-
if has("win32")
59-
let b:browsefilter .= "All Files (*.*)\t*\n"
60-
else
61-
let b:browsefilter .= "All Files (*)\t*\n"
62-
endif
56+
\ "XSD Files (*.xsd)\t*.xsd\n" .
57+
\ "All Files (*.*)\t*.*\n"
6358
endif
6459

6560
" Undo the stuff we changed.

runtime/indent/xml.vim

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ endfun
8888

8989
" [-- return the sum of indents of a:lnum --]
9090
fun! <SID>XmlIndentSum(line, style, add)
91-
if <SID>IsXMLContinuation(a:line) && a:style == 0 && !<SID>IsXMLEmptyClosingTag(a:line)
91+
if <SID>IsXMLContinuation(a:line) &&
92+
\ a:style == 0 &&
93+
\ !<SID>IsXMLEmptyClosingTag(a:line)
9294
" no complete tag, add one additional indent level
9395
" but only for the current line
9496
return a:add + shiftwidth()
@@ -157,6 +159,17 @@ fun! XmlIndentGet(lnum, use_syntax_check)
157159
" no extra indent, looks like a text continuation line
158160
return pind
159161
endif
162+
elseif empty(syn_name_start) && syn_name_end =~? 'xmlTag'
163+
" Special case: such a line, shouldn't be indented, just because it
164+
" ends with a tag
165+
" 'foobar <i>inline tags</i>'
166+
if (match(curline, '<\([:a-zA-Z_]\+\)[^>]*>.*</\1>') > -1)
167+
return pind
168+
endif
169+
endif
170+
171+
if curline =~ '^\s*</[a-zA-Z_]>'
172+
return <SID>ReturnIndentForMatchingTag(curline)
160173
endif
161174

162175
" Get indent from previous tag line
@@ -181,6 +194,21 @@ func! <SID>IsXMLEmptyClosingTag(line)
181194
return a:line =~? '<[^>]*/>\s*$'
182195
endfunc
183196

197+
func! <SID>ReturnIndentForMatchingTag(line)
198+
" For a line with just a simple closing tag
199+
" get the indent from a matching opening tag
200+
if a:line =~? '^\s*</[a-z_]*>'
201+
let _c = getcursorpos()
202+
let pat = matchstr(a:line, '^\s*</\zs[a-z_]\+\ze>')
203+
" position cursor before the opening tag
204+
norm! 0
205+
" get the indent from the matching opening tag
206+
let match_line = searchpair('<' .. pat .. '>', '', '</' .. pat .. '>', 'bn')
207+
call setpos('.', _c)
208+
return indent(match_line)
209+
endif
210+
endfunc
211+
184212
" return indent for a commented line,
185213
" the middle part might be indented one additional level
186214
func! <SID>XmlIndentComment(lnum)

0 commit comments

Comments
 (0)