# HG changeset patch # User Bram Moolenaar # Date 1583086504 -3600 # Node ID d4deb2e50667f09d1847bc0ae0abf4e6dbb3e3db # Parent 9d0db06b6ad239c9e8604fb1805ae56f920a03c7 Update runtime files Commit: https://github.com/vim/vim/commit/eab6dff19f387469a200011bc6cf3508f5e43a4a Author: Bram Moolenaar Date: Sun Mar 1 19:06:45 2020 +0100 Update runtime files diff --git a/runtime/autoload/xmlformat.vim b/runtime/autoload/xmlformat.vim --- a/runtime/autoload/xmlformat.vim +++ b/runtime/autoload/xmlformat.vim @@ -1,6 +1,6 @@ " Vim plugin for formatting XML -" Last Change: 2019 Oct 24 -" Version: 0.2 +" Last Change: 2020 Jan 06 +" Version: 0.3 " Author: Christian Brabandt " Repository: https://github.com/chrisbra/vim-xml-ftplugin " License: VIM License @@ -15,7 +15,7 @@ let s:keepcpo = &cpo set cpo&vim " Main function: Format the input {{{1 -func! xmlformat#Format() +func! xmlformat#Format() abort " only allow reformatting through the gq command " (e.g. Vim is in normal mode) if mode() != 'n' @@ -40,14 +40,16 @@ func! xmlformat#Format() continue elseif line !~# '<[/]\?[^>]*>' let nextmatch = match(list, '<[/]\?[^>]*>', current) - let line .= join(list[(current + 1):(nextmatch-1)], "\n") - call remove(list, current+1, nextmatch-1) + if nextmatch > -1 + let line .= ' '. join(list[(current + 1):(nextmatch-1)], " ") + call remove(list, current+1, nextmatch-1) + endif endif " split on `>`, but don't split on very first opening < " this means, items can be like ['', 'tag content'] for item in split(line, '.\@<=[>]\zs') if s:EndTag(item) - let s:indent = s:DecreaseIndent() + call s:DecreaseIndent() call add(result, s:Indent(item)) elseif s:EmptyTag(lastitem) call add(result, s:Indent(item)) @@ -59,13 +61,23 @@ func! xmlformat#Format() " Simply split on '<', if there is one, " but reformat according to &textwidth let t=split(item, '.<\@=\zs') + + " if the content fits well within a single line, add it there + " so that the output looks like this: + " + " 1 + if s:TagContent(lastitem) is# s:TagContent(t[1]) && strlen(result[-1]) + strlen(item) <= s:Textwidth() + let result[-1] .= item + let lastitem = t[1] + continue + endif " t should only contain 2 items, but just be safe here if s:IsTag(lastitem) let s:indent+=1 endif let result+=s:FormatContent([t[0]]) if s:EndTag(t[1]) - let s:indent = s:DecreaseIndent() + call s:DecreaseIndent() endif "for y in t[1:] let result+=s:FormatContent(t[1:]) @@ -97,15 +109,15 @@ func! xmlformat#Format() return 0 endfunc " Check if given tag is XML Declaration header {{{1 -func! s:IsXMLDecl(tag) +func! s:IsXMLDecl(tag) abort return a:tag =~? '^\s*\s*$' endfunc " Return tag indented by current level {{{1 -func! s:Indent(item) +func! s:Indent(item) abort return repeat(' ', shiftwidth()*s:indent). s:Trim(a:item) endfu " Return item trimmed from leading whitespace {{{1 -func! s:Trim(item) +func! s:Trim(item) abort if exists('*trim') return trim(a:item) else @@ -113,44 +125,53 @@ func! s:Trim(item) endif endfunc " Check if tag is a new opening tag {{{1 -func! s:StartTag(tag) +func! s:StartTag(tag) abort let is_comment = s:IsComment(a:tag) return a:tag =~? '^\s*<[^/?]' && !is_comment endfunc " Check if tag is a Comment start {{{1 -func! s:IsComment(tag) +func! s:IsComment(tag) abort return a:tag =~? '