Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 1 | " Vim plugin for formatting XML |
Bram Moolenaar | 0b0f099 | 2018-05-22 21:41:30 +0200 | [diff] [blame] | 2 | " Last Change: Thu, 22 May 2018 21:26:55 +0100 |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 3 | " Version: 0.1 |
| 4 | " Author: Christian Brabandt <cb@256bit.org> |
Bram Moolenaar | 91f84f6 | 2018-07-29 15:07:52 +0200 | [diff] [blame] | 5 | " Repository: https://github.com/chrisbra/vim-xml-ftplugin |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 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 |
| 25 | let sw = shiftwidth() |
| 26 | let prev = prevnonblank(v:lnum-1) |
| 27 | let s:indent = indent(prev)/sw |
| 28 | let result = [] |
| 29 | let lastitem = prev ? getline(prev) : '' |
| 30 | let is_xml_decl = 0 |
| 31 | " split on `<`, but don't split on very first opening < |
Bram Moolenaar | 0b0f099 | 2018-05-22 21:41:30 +0200 | [diff] [blame] | 32 | for item in split(join(getline(v:lnum, (v:lnum + v:count - 1))), '.\@<=[>]\zs') |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 33 | if s:EndTag(item) |
| 34 | let s:indent = s:DecreaseIndent() |
| 35 | call add(result, s:Indent(item)) |
| 36 | elseif s:EmptyTag(lastitem) |
| 37 | call add(result, s:Indent(item)) |
| 38 | elseif s:StartTag(lastitem) && s:IsTag(item) |
| 39 | let s:indent += 1 |
| 40 | call add(result, s:Indent(item)) |
| 41 | else |
| 42 | if !s:IsTag(item) |
| 43 | " Simply split on '<' |
| 44 | let t=split(item, '.<\@=\zs') |
| 45 | let s:indent+=1 |
| 46 | call add(result, s:Indent(t[0])) |
| 47 | let s:indent = s:DecreaseIndent() |
| 48 | call add(result, s:Indent(t[1])) |
| 49 | else |
| 50 | call add(result, s:Indent(item)) |
| 51 | endif |
| 52 | endif |
| 53 | let lastitem = item |
| 54 | endfor |
| 55 | |
| 56 | if !empty(result) |
| 57 | exe v:lnum. ",". (v:lnum + v:count - 1). 'd' |
| 58 | call append(v:lnum - 1, result) |
| 59 | " Might need to remove the last line, if it became empty because of the |
| 60 | " append() call |
| 61 | let last = v:lnum + len(result) |
| 62 | if getline(last) is '' |
| 63 | exe last. 'd' |
| 64 | endif |
| 65 | endif |
| 66 | |
| 67 | " do not run internal formatter! |
| 68 | return 0 |
| 69 | endfunc |
| 70 | " Check if given tag is XML Declaration header {{{1 |
| 71 | func! s:IsXMLDecl(tag) |
| 72 | return a:tag =~? '^\s*<?xml\s\?\%(version="[^"]*"\)\?\s\?\%(encoding="[^"]*"\)\? ?>\s*$' |
| 73 | endfunc |
| 74 | " Return tag indented by current level {{{1 |
| 75 | func! s:Indent(item) |
| 76 | return repeat(' ', shiftwidth()*s:indent). s:Trim(a:item) |
| 77 | endfu |
| 78 | " Return item trimmed from leading whitespace {{{1 |
| 79 | func! s:Trim(item) |
| 80 | if exists('*trim') |
| 81 | return trim(a:item) |
| 82 | else |
| 83 | return matchstr(a:item, '\S\+.*') |
| 84 | endif |
| 85 | endfunc |
| 86 | " Check if tag is a new opening tag <tag> {{{1 |
| 87 | func! s:StartTag(tag) |
| 88 | return a:tag =~? '^\s*<[^/?]' |
| 89 | endfunc |
| 90 | " Remove one level of indentation {{{1 |
| 91 | func! s:DecreaseIndent() |
| 92 | return (s:indent > 0 ? s:indent - 1 : 0) |
| 93 | endfunc |
| 94 | " Check if tag is a closing tag </tag> {{{1 |
| 95 | func! s:EndTag(tag) |
| 96 | return a:tag =~? '^\s*</' |
| 97 | endfunc |
| 98 | " Check that the tag is actually a tag and not {{{1 |
| 99 | " something like "foobar</foobar>" |
| 100 | func! s:IsTag(tag) |
| 101 | return s:Trim(a:tag)[0] == '<' |
| 102 | endfunc |
| 103 | " Check if tag is empty <tag/> {{{1 |
| 104 | func! s:EmptyTag(tag) |
| 105 | return a:tag =~ '/>\s*$' |
| 106 | endfunc |
| 107 | " Restoration And Modelines: {{{1 |
| 108 | let &cpo= s:keepcpo |
| 109 | unlet s:keepcpo |
| 110 | " Modeline {{{1 |
| 111 | " vim: fdm=marker fdl=0 ts=2 et sw=0 sts=-1 |