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