blob: c89c8784b90bea62465812c52cf277e4b46457dc [file] [log] [blame]
Bram Moolenaar7db25fe2018-05-13 00:02:36 +02001" Vim plugin for formatting XML
Bram Moolenaareab6dff2020-03-01 19:06:45 +01002" Last Change: 2020 Jan 06
3" Version: 0.3
Bram Moolenaard47d5222018-12-09 20:43:55 +01004" Author: Christian Brabandt <cb@256bit.org>
5" Repository: https://github.com/chrisbra/vim-xml-ftplugin
6" License: VIM License
Bram Moolenaar7db25fe2018-05-13 00:02:36 +02007" Documentation: see :h xmlformat.txt (TODO!)
8" ---------------------------------------------------------------------
9" Load Once: {{{1
10if exists("g:loaded_xmlformat") || &cp
11 finish
12endif
13let g:loaded_xmlformat = 1
14let s:keepcpo = &cpo
15set cpo&vim
16
17" Main function: Format the input {{{1
Bram Moolenaareab6dff2020-03-01 19:06:45 +010018func! xmlformat#Format() abort
Bram Moolenaar7db25fe2018-05-13 00:02:36 +020019 " 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 Moolenaar96f45c02019-10-26 19:53:45 +020025 let count_orig = v:count
Bram Moolenaar7db25fe2018-05-13 00:02:36 +020026 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 Moolenaar96f45c02019-10-26 19:53:45 +020032 " 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 Moolenaareab6dff2020-03-01 19:06:45 +010043 if nextmatch > -1
44 let line .= ' '. join(list[(current + 1):(nextmatch-1)], " ")
45 call remove(list, current+1, nextmatch-1)
46 endif
Bram Moolenaar96f45c02019-10-26 19:53:45 +020047 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 Moolenaareab6dff2020-03-01 19:06:45 +010052 call s:DecreaseIndent()
Bram Moolenaar7db25fe2018-05-13 00:02:36 +020053 call add(result, s:Indent(item))
Bram Moolenaar96f45c02019-10-26 19:53:45 +020054 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 Moolenaareab6dff2020-03-01 19:06:45 +010064
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 Moolenaar96f45c02019-10-26 19:53:45 +020074 " 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 Moolenaareab6dff2020-03-01 19:06:45 +010080 call s:DecreaseIndent()
Bram Moolenaar96f45c02019-10-26 19:53:45 +020081 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 Moolenaar7db25fe2018-05-13 00:02:36 +020088 endif
Bram Moolenaar96f45c02019-10-26 19:53:45 +020089 let lastitem = item
90 endfor
91 let current += 1
92 endfor
Bram Moolenaar7db25fe2018-05-13 00:02:36 +020093
Bram Moolenaar96f45c02019-10-26 19:53:45 +020094 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 Moolenaar7db25fe2018-05-13 00:02:36 +020098 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 Moolenaar96f45c02019-10-26 19:53:45 +0200102 " do not use empty(), it returns true for `empty(0)`
103 if getline(last) is '' && lastprevline is '' && delete_lastline
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200104 exe last. 'd'
105 endif
106 endif
107
108 " do not run internal formatter!
109 return 0
110endfunc
111" Check if given tag is XML Declaration header {{{1
Bram Moolenaareab6dff2020-03-01 19:06:45 +0100112func! s:IsXMLDecl(tag) abort
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200113 return a:tag =~? '^\s*<?xml\s\?\%(version="[^"]*"\)\?\s\?\%(encoding="[^"]*"\)\? ?>\s*$'
114endfunc
115" Return tag indented by current level {{{1
Bram Moolenaareab6dff2020-03-01 19:06:45 +0100116func! s:Indent(item) abort
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200117 return repeat(' ', shiftwidth()*s:indent). s:Trim(a:item)
118endfu
119" Return item trimmed from leading whitespace {{{1
Bram Moolenaareab6dff2020-03-01 19:06:45 +0100120func! s:Trim(item) abort
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200121 if exists('*trim')
122 return trim(a:item)
123 else
124 return matchstr(a:item, '\S\+.*')
125 endif
126endfunc
127" Check if tag is a new opening tag <tag> {{{1
Bram Moolenaareab6dff2020-03-01 19:06:45 +0100128func! s:StartTag(tag) abort
Bram Moolenaard47d5222018-12-09 20:43:55 +0100129 let is_comment = s:IsComment(a:tag)
130 return a:tag =~? '^\s*<[^/?]' && !is_comment
131endfunc
Bram Moolenaar96f45c02019-10-26 19:53:45 +0200132" Check if tag is a Comment start {{{1
Bram Moolenaareab6dff2020-03-01 19:06:45 +0100133func! s:IsComment(tag) abort
Bram Moolenaard47d5222018-12-09 20:43:55 +0100134 return a:tag =~? '<!--'
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200135endfunc
136" Remove one level of indentation {{{1
Bram Moolenaareab6dff2020-03-01 19:06:45 +0100137func! s:DecreaseIndent() abort
138 let s:indent = (s:indent > 0 ? s:indent - 1 : 0)
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200139endfunc
140" Check if tag is a closing tag </tag> {{{1
Bram Moolenaareab6dff2020-03-01 19:06:45 +0100141func! s:EndTag(tag) abort
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200142 return a:tag =~? '^\s*</'
143endfunc
144" Check that the tag is actually a tag and not {{{1
145" something like "foobar</foobar>"
Bram Moolenaareab6dff2020-03-01 19:06:45 +0100146func! s:IsTag(tag) abort
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200147 return s:Trim(a:tag)[0] == '<'
148endfunc
149" Check if tag is empty <tag/> {{{1
Bram Moolenaareab6dff2020-03-01 19:06:45 +0100150func! s:EmptyTag(tag) abort
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200151 return a:tag =~ '/>\s*$'
152endfunc
Bram Moolenaareab6dff2020-03-01 19:06:45 +0100153func! s:TagContent(tag) abort "{{{1
154 " Return content of a tag
155 return substitute(a:tag, '^\s*<[/]\?\([^>]*\)>\s*$', '\1', '')
156endfunc
157func! s:Textwidth() abort "{{{1
158 " return textwidth (or 80 if not set)
159 return &textwidth == 0 ? 80 : &textwidth
160endfunc
Bram Moolenaar96f45c02019-10-26 19:53:45 +0200161" Format input line according to textwidth {{{1
Bram Moolenaareab6dff2020-03-01 19:06:45 +0100162func! s:FormatContent(list) abort
Bram Moolenaar96f45c02019-10-26 19:53:45 +0200163 let result=[]
Bram Moolenaareab6dff2020-03-01 19:06:45 +0100164 let limit = s:Textwidth()
Bram Moolenaar96f45c02019-10-26 19:53:45 +0200165 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 Moolenaareab6dff2020-03-01 19:06:45 +0100171 if match(word, '^\s\+$') > -1
172 " skip empty words
173 continue
174 endif
Bram Moolenaar96f45c02019-10-26 19:53:45 +0200175 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
198endfunc
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200199" Restoration And Modelines: {{{1
200let &cpo= s:keepcpo
201unlet s:keepcpo
202" Modeline {{{1
203" vim: fdm=marker fdl=0 ts=2 et sw=0 sts=-1