robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
8eba81182a
master
1" Vim plugin for formatting XML 2" Last Change: 2023 March 15th 3" Version: 0.3 4" Author: Christian Brabandt <cb@256bit.org> 5" Repository: https://github.com/chrisbra/vim-xml-ftplugin 6" License: VIM License 7" Documentation: see :h xmlformat.txt (TODO!) 8" --------------------------------------------------------------------- 9" Load Once: {{{1 10if exists ( "g:loaded_xmlformat" ) || & cp 11finish 12endif 13let g: loaded_xmlformat = 1 14let s: keepcpo = & cpo 15set cpo & vim 16 17" Main function: Format the input {{{1 18func ! xmlformat#Format () abort 19" only allow reformatting through the gq command 20" (e.g. Vim is in normal mode) 21if mode () != 'n' 22" do not fall back to internal formatting 23return 0 24endif 25let count_orig = v: count 26let sw = shiftwidth () 27let prev = prevnonblank ( v: lnum - 1 ) 28let s: indent = indent ( prev ) / sw 29let result = [] 30let lastitem = prev ? getline ( prev ) : '' 31let is_xml_decl = 0 32" go through every line, but don't join all content together and join it 33" back. We might lose empty lines 34let list = getline ( v: lnum , ( v: lnum + count_orig - 1 )) 35let current = 0 36for line in list 37" Keep empty input lines? 38if empty ( line ) 39call add ( result , '' ) 40let current += 1 41continue 42elseif line !~# '<[/]\?[^>]*>' 43let nextmatch = match ( list , '^\s*$\|<[/]\?[^>]*>' , current ) 44if nextmatch > -1 45let lineEnd = nextmatch 46else 47let lineEnd = len ( list ) 48endif 49let line .= ' ' . join ( list [( current + 1 ):( lineEnd - 1 )], " " ) 50call remove ( list , current + 1 , lineEnd - 1 ) 51endif 52" split on `>`, but don't split on very first opening < 53" this means, items can be like ['<tag>', 'tag content</tag>'] 54for item in split ( line , '.\@<=[>]\zs' ) 55if s: EndTag ( item ) 56call s: DecreaseIndent () 57call add ( result , s: Indent ( item )) 58elseif s: EmptyTag ( lastitem ) 59call add ( result , s: Indent ( item )) 60elseif s: StartTag ( lastitem ) && s: IsTag ( item ) 61let s: indent += 1 62call add ( result , s: Indent ( item )) 63else 64if ! s: IsTag ( item ) 65" Simply split on '<', if there is one, 66" but reformat according to &textwidth 67let t = split ( item , '.<\@=\zs' ) 68 69" if the content fits well within a single line, add it there 70" so that the output looks like this: 71" 72" <foobar>1</foobar> 73if s: TagContent ( lastitem ) is# s: TagContent ( t [ 1 ]) && strlen ( result [ -1 ]) + strlen ( item ) <= s: Textwidth () 74let result [ -1 ] .= item 75let lastitem = t [ 1 ] 76continue 77endif 78" t should only contain 2 items, but just be safe here 79if s: IsTag ( lastitem ) 80let s: indent += 1 81endif 82let result += s: FormatContent ([ t [ 0 ]]) 83if s: EndTag ( t [ 1 ]) 84call s: DecreaseIndent () 85endif 86let result += s: FormatContent ( t [ 1 :]) 87if s: IsTag ( t [ 1 ]) 88let lastitem = t [ 1 ] 89continue 90endif 91elseif s: IsComment ( item ) 92let result += s: FormatContent ([ item ]) 93else 94call add ( result , s: Indent ( item )) 95endif 96endif 97let lastitem = item 98endfor 99let current += 1 100endfor 101 102if ! empty ( result ) 103let lastprevline = getline ( v: lnum + count_orig ) 104let delete_lastline = v: lnum + count_orig - 1 == line ( '$' ) 105exe 'silent ' .. v: lnum . "," . ( v: lnum + count_orig - 1 ) . 'd' 106call append ( v: lnum - 1 , result ) 107" Might need to remove the last line, if it became empty because of the 108" append() call 109let last = v: lnum + len ( result ) 110" do not use empty(), it returns true for `empty(0)` 111if getline ( last ) is '' && lastprevline is '' && delete_lastline 112exe last . 'd' 113endif 114endif 115 116" do not run internal formatter! 117return 0 118endfunc 119" Check if given tag is XML Declaration header {{{1 120func ! s: IsXMLDecl ( tag ) abort 121return a: tag =~? '^\s*<?xml\s\?\%(version="[^"]*"\)\?\s\?\%(encoding="[^"]*"\)\? ?>\s*$' 122endfunc 123" Return tag indented by current level {{{1 124func ! s: Indent ( item ) abort 125return repeat ( ' ' , shiftwidth () * s: indent ) . s: Trim ( a: item ) 126endfu 127" Return item trimmed from leading whitespace {{{1 128func ! s: Trim ( item ) abort 129if exists ( '*trim' ) 130return trim ( a: item ) 131else 132return matchstr ( a: item , '\S\+.*' ) 133endif 134endfunc 135" Check if tag is a new opening tag <tag> {{{1 136func ! s: StartTag ( tag ) abort 137let is_comment = s: IsComment ( a: tag ) 138return a: tag =~? '^\s*<[^/?]' && ! is_comment 139endfunc 140" Check if tag is a Comment start {{{1 141func ! s: IsComment ( tag ) abort 142return a: tag =~? '<!--' 143endfunc 144" Remove one level of indentation {{{1 145func ! s: DecreaseIndent () abort 146let s: indent = ( s: indent > 0 ? s: indent - 1 : 0 ) 147endfunc 148" Check if tag is a closing tag </tag> {{{1 149func ! s: EndTag ( tag ) abort 150return a: tag =~? '^\s*</' 151endfunc 152" Check that the tag is actually a tag and not {{{1 153" something like "foobar</foobar>" 154func ! s: IsTag ( tag ) abort 155return s: Trim ( a: tag )[ 0 ] == '<' 156endfunc 157" Check if tag is empty <tag/> {{{1 158func ! s: EmptyTag ( tag ) abort 159return a: tag =~ '/>\s*$' 160endfunc 161func ! s: TagContent ( tag ) abort"{{{1 162" Return content of a tag 163return substitute ( a: tag , '^\s*<[/]\?\([^>]*\)>\s*$' , '\1' , '' ) 164endfunc 165func ! s: Textwidth () abort"{{{1 166" return textwidth (or 80 if not set) 167return & textwidth == 0 ? 80 : & textwidth 168endfunc 169" Format input line according to textwidth {{{1 170func ! s: FormatContent ( list ) abort 171let result = [] 172let limit = s: Textwidth () 173let column = 0 174let idx = -1 175let add_indent = 0 176let cnt = 0 177for item in a: list 178for word in split ( item , '\s\+\S\+\zs' ) 179if match ( word , '^\s\+$' ) > -1 180" skip empty words 181continue 182endif 183let column += strdisplaywidth ( word , column ) 184if match ( word , "^\\s*\n\\+\\s*$" ) > -1 185call add ( result , '' ) 186let idx += 1 187let column = 0 188let add_indent = 1 189elseif column > limit || cnt == 0 190let add = s: Indent ( s: Trim ( word )) 191call add ( result , add ) 192let column = strdisplaywidth ( add ) 193let idx += 1 194else 195if add_indent 196let result [ idx ] = s: Indent ( s: Trim ( word )) 197else 198let result [ idx ] .= ' ' . s: Trim ( word ) 199endif 200let add_indent = 0 201endif 202let cnt += 1 203endfor 204endfor 205return result 206endfunc 207" Restoration And Modelines: {{{1 208let & cpo = s: keepcpo 209unlet s: keepcpo 210" Modeline {{{1 211" vim: fdm=marker fdl=0 ts=2 et sw=0 sts=-1