Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 1 | " Vim plugin for formatting XML |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 2 | " Last Change: 2020 Jan 06 |
| 3 | " Version: 0.3 |
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 |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 18 | func! xmlformat#Format() abort |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 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) |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 43 | if nextmatch > -1 |
| 44 | let line .= ' '. join(list[(current + 1):(nextmatch-1)], " ") |
| 45 | call remove(list, current+1, nextmatch-1) |
| 46 | endif |
Bram Moolenaar | 96f45c0 | 2019-10-26 19:53:45 +0200 | [diff] [blame] | 47 | endif |
| 48 | " split on `>`, but don't split on very first opening < |
| 49 | " this means, items can be like ['<tag>', 'tag content</tag>'] |
| 50 | for item in split(line, '.\@<=[>]\zs') |
| 51 | if s:EndTag(item) |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 52 | call s:DecreaseIndent() |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 53 | call add(result, s:Indent(item)) |
Bram Moolenaar | 96f45c0 | 2019-10-26 19:53:45 +0200 | [diff] [blame] | 54 | elseif s:EmptyTag(lastitem) |
| 55 | call add(result, s:Indent(item)) |
| 56 | elseif s:StartTag(lastitem) && s:IsTag(item) |
| 57 | let s:indent += 1 |
| 58 | call add(result, s:Indent(item)) |
| 59 | else |
| 60 | if !s:IsTag(item) |
| 61 | " Simply split on '<', if there is one, |
| 62 | " but reformat according to &textwidth |
| 63 | let t=split(item, '.<\@=\zs') |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 64 | |
| 65 | " if the content fits well within a single line, add it there |
| 66 | " so that the output looks like this: |
| 67 | " |
| 68 | " <foobar>1</foobar> |
| 69 | if s:TagContent(lastitem) is# s:TagContent(t[1]) && strlen(result[-1]) + strlen(item) <= s:Textwidth() |
| 70 | let result[-1] .= item |
| 71 | let lastitem = t[1] |
| 72 | continue |
| 73 | endif |
Bram Moolenaar | 96f45c0 | 2019-10-26 19:53:45 +0200 | [diff] [blame] | 74 | " t should only contain 2 items, but just be safe here |
| 75 | if s:IsTag(lastitem) |
| 76 | let s:indent+=1 |
| 77 | endif |
| 78 | let result+=s:FormatContent([t[0]]) |
| 79 | if s:EndTag(t[1]) |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 80 | call s:DecreaseIndent() |
Bram Moolenaar | 96f45c0 | 2019-10-26 19:53:45 +0200 | [diff] [blame] | 81 | endif |
| 82 | "for y in t[1:] |
| 83 | let result+=s:FormatContent(t[1:]) |
| 84 | "endfor |
| 85 | else |
| 86 | call add(result, s:Indent(item)) |
| 87 | endif |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 88 | endif |
Bram Moolenaar | 96f45c0 | 2019-10-26 19:53:45 +0200 | [diff] [blame] | 89 | let lastitem = item |
| 90 | endfor |
| 91 | let current += 1 |
| 92 | endfor |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 93 | |
Bram Moolenaar | 96f45c0 | 2019-10-26 19:53:45 +0200 | [diff] [blame] | 94 | if !empty(result) |
| 95 | let lastprevline = getline(v:lnum + count_orig) |
| 96 | let delete_lastline = v:lnum + count_orig - 1 == line('$') |
| 97 | exe v:lnum. ",". (v:lnum + count_orig - 1). 'd' |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 98 | call append(v:lnum - 1, result) |
| 99 | " Might need to remove the last line, if it became empty because of the |
| 100 | " append() call |
| 101 | let last = v:lnum + len(result) |
Bram Moolenaar | 96f45c0 | 2019-10-26 19:53:45 +0200 | [diff] [blame] | 102 | " do not use empty(), it returns true for `empty(0)` |
| 103 | if getline(last) is '' && lastprevline is '' && delete_lastline |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 104 | exe last. 'd' |
| 105 | endif |
| 106 | endif |
| 107 | |
| 108 | " do not run internal formatter! |
| 109 | return 0 |
| 110 | endfunc |
| 111 | " Check if given tag is XML Declaration header {{{1 |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 112 | func! s:IsXMLDecl(tag) abort |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 113 | return a:tag =~? '^\s*<?xml\s\?\%(version="[^"]*"\)\?\s\?\%(encoding="[^"]*"\)\? ?>\s*$' |
| 114 | endfunc |
| 115 | " Return tag indented by current level {{{1 |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 116 | func! s:Indent(item) abort |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 117 | return repeat(' ', shiftwidth()*s:indent). s:Trim(a:item) |
| 118 | endfu |
| 119 | " Return item trimmed from leading whitespace {{{1 |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 120 | func! s:Trim(item) abort |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 121 | if exists('*trim') |
| 122 | return trim(a:item) |
| 123 | else |
| 124 | return matchstr(a:item, '\S\+.*') |
| 125 | endif |
| 126 | endfunc |
| 127 | " Check if tag is a new opening tag <tag> {{{1 |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 128 | func! s:StartTag(tag) abort |
Bram Moolenaar | d47d522 | 2018-12-09 20:43:55 +0100 | [diff] [blame] | 129 | let is_comment = s:IsComment(a:tag) |
| 130 | return a:tag =~? '^\s*<[^/?]' && !is_comment |
| 131 | endfunc |
Bram Moolenaar | 96f45c0 | 2019-10-26 19:53:45 +0200 | [diff] [blame] | 132 | " Check if tag is a Comment start {{{1 |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 133 | func! s:IsComment(tag) abort |
Bram Moolenaar | d47d522 | 2018-12-09 20:43:55 +0100 | [diff] [blame] | 134 | return a:tag =~? '<!--' |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 135 | endfunc |
| 136 | " Remove one level of indentation {{{1 |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 137 | func! s:DecreaseIndent() abort |
| 138 | let s:indent = (s:indent > 0 ? s:indent - 1 : 0) |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 139 | endfunc |
| 140 | " Check if tag is a closing tag </tag> {{{1 |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 141 | func! s:EndTag(tag) abort |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 142 | return a:tag =~? '^\s*</' |
| 143 | endfunc |
| 144 | " Check that the tag is actually a tag and not {{{1 |
| 145 | " something like "foobar</foobar>" |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 146 | func! s:IsTag(tag) abort |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 147 | return s:Trim(a:tag)[0] == '<' |
| 148 | endfunc |
| 149 | " Check if tag is empty <tag/> {{{1 |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 150 | func! s:EmptyTag(tag) abort |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 151 | return a:tag =~ '/>\s*$' |
| 152 | endfunc |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 153 | func! s:TagContent(tag) abort "{{{1 |
| 154 | " Return content of a tag |
| 155 | return substitute(a:tag, '^\s*<[/]\?\([^>]*\)>\s*$', '\1', '') |
| 156 | endfunc |
| 157 | func! s:Textwidth() abort "{{{1 |
| 158 | " return textwidth (or 80 if not set) |
| 159 | return &textwidth == 0 ? 80 : &textwidth |
| 160 | endfunc |
Bram Moolenaar | 96f45c0 | 2019-10-26 19:53:45 +0200 | [diff] [blame] | 161 | " Format input line according to textwidth {{{1 |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 162 | func! s:FormatContent(list) abort |
Bram Moolenaar | 96f45c0 | 2019-10-26 19:53:45 +0200 | [diff] [blame] | 163 | let result=[] |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 164 | let limit = s:Textwidth() |
Bram Moolenaar | 96f45c0 | 2019-10-26 19:53:45 +0200 | [diff] [blame] | 165 | let column=0 |
| 166 | let idx = -1 |
| 167 | let add_indent = 0 |
| 168 | let cnt = 0 |
| 169 | for item in a:list |
| 170 | for word in split(item, '\s\+\S\+\zs') |
Bram Moolenaar | eab6dff | 2020-03-01 19:06:45 +0100 | [diff] [blame] | 171 | if match(word, '^\s\+$') > -1 |
| 172 | " skip empty words |
| 173 | continue |
| 174 | endif |
Bram Moolenaar | 96f45c0 | 2019-10-26 19:53:45 +0200 | [diff] [blame] | 175 | let column += strdisplaywidth(word, column) |
| 176 | if match(word, "^\\s*\n\\+\\s*$") > -1 |
| 177 | call add(result, '') |
| 178 | let idx += 1 |
| 179 | let column = 0 |
| 180 | let add_indent = 1 |
| 181 | elseif column > limit || cnt == 0 |
| 182 | let add = s:Indent(s:Trim(word)) |
| 183 | call add(result, add) |
| 184 | let column = strdisplaywidth(add) |
| 185 | let idx += 1 |
| 186 | else |
| 187 | if add_indent |
| 188 | let result[idx] = s:Indent(s:Trim(word)) |
| 189 | else |
| 190 | let result[idx] .= ' '. s:Trim(word) |
| 191 | endif |
| 192 | let add_indent = 0 |
| 193 | endif |
| 194 | let cnt += 1 |
| 195 | endfor |
| 196 | endfor |
| 197 | return result |
| 198 | endfunc |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 199 | " Restoration And Modelines: {{{1 |
| 200 | let &cpo= s:keepcpo |
| 201 | unlet s:keepcpo |
| 202 | " Modeline {{{1 |
| 203 | " vim: fdm=marker fdl=0 ts=2 et sw=0 sts=-1 |