Mercurial > vim
annotate runtime/indent/xml.vim @ 20596:4ec94bcaef86
Added tag v8.2.0851 for changeset 3609e842f8229ebc52cbb5e172eabcf75be81e2b
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 30 May 2020 20:00:04 +0200 |
parents | 8dde7ced3344 |
children | 29c5f168c6fd |
rev | line source |
---|---|
18456 | 1 " Language: XML |
2 " Maintainer: Christian Brabandt <cb@256bit.org> | |
3 " Repository: https://github.com/chrisbra/vim-xml-ftplugin | |
4 " Previous Maintainer: Johannes Zellner <johannes@zellner.org> | |
18790 | 5 " Last Changed: 2019 Dec 02 |
15194 | 6 " Last Change: |
18790 | 7 " 20191202 - Handle docbk filetype |
17571 | 8 " 20190726 - Correctly handle non-tagged data |
16086 | 9 " 20190204 - correctly handle wrap tags |
10 " https://github.com/chrisbra/vim-xml-ftplugin/issues/5 | |
15729 | 11 " 20190128 - Make sure to find previous tag |
12 " https://github.com/chrisbra/vim-xml-ftplugin/issues/4 | |
15194 | 13 " 20181116 - Fix indentation when tags start with a colon or an underscore |
14 " https://github.com/vim/vim/pull/926 | |
15 " 20181022 - Do not overwrite indentkeys setting | |
16 " https://github.com/chrisbra/vim-xml-ftplugin/issues/1 | |
17 " 20180724 - Correctly indent xml comments https://github.com/vim/vim/issues/3200 | |
18 " | |
19 " Notes: | |
20 " 1) does not indent pure non-xml code (e.g. embedded scripts) | |
21 " 2) will be confused by unbalanced tags in comments | |
22 " or CDATA sections. | |
23 " 2009-05-26 patch by Nikolai Weibull | |
24 " TODO: implement pre-like tags, see xml_indent_open / xml_indent_close | |
7 | 25 |
26 " Only load this indent file when no other was loaded. | |
27 if exists("b:did_indent") | |
28 finish | |
29 endif | |
30 let b:did_indent = 1 | |
3526
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
31 let s:keepcpo= &cpo |
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
32 set cpo&vim |
7 | 33 |
34 " [-- local settings (must come before aborting the script) --] | |
15194 | 35 " Attention: Parameter use_syntax_check is used by the docbk.vim indent script |
7 | 36 setlocal indentexpr=XmlIndentGet(v:lnum,1) |
14999 | 37 setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,},!^F |
17571 | 38 " autoindent: used when the indentexpr returns -1 |
39 setlocal autoindent | |
7 | 40 |
41 if !exists('b:xml_indent_open') | |
15194 | 42 let b:xml_indent_open = '.\{-}<[:A-Z_a-z]' |
7 | 43 " pre tag, e.g. <address> |
44 " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!' | |
45 endif | |
46 | |
47 if !exists('b:xml_indent_close') | |
48 let b:xml_indent_close = '.\{-}</' | |
49 " end pre tag, e.g. </address> | |
50 " let b:xml_indent_close = '.\{-}</\(address\)\@!' | |
51 endif | |
52 | |
3713 | 53 let &cpo = s:keepcpo |
54 unlet s:keepcpo | |
55 | |
7 | 56 " [-- finish, if the function already exists --] |
3713 | 57 if exists('*XmlIndentGet') |
15194 | 58 finish |
3713 | 59 endif |
60 | |
61 let s:keepcpo= &cpo | |
62 set cpo&vim | |
7 | 63 |
64 fun! <SID>XmlIndentWithPattern(line, pat) | |
65 let s = substitute('x'.a:line, a:pat, "\1", 'g') | |
66 return strlen(substitute(s, "[^\1].*$", '', '')) | |
67 endfun | |
68 | |
69 " [-- check if it's xml --] | |
70 fun! <SID>XmlIndentSynCheck(lnum) | |
15194 | 71 if &syntax != '' |
72 let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name') | |
73 let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name') | |
74 if syn1 != '' && syn1 !~ 'xml' && syn2 != '' && syn2 !~ 'xml' | |
75 " don't indent pure non-xml code | |
76 return 0 | |
77 endif | |
7 | 78 endif |
79 return 1 | |
80 endfun | |
81 | |
82 " [-- return the sum of indents of a:lnum --] | |
16086 | 83 fun! <SID>XmlIndentSum(line, style, add) |
84 if <SID>IsXMLContinuation(a:line) && a:style == 0 | |
85 " no complete tag, add one additional indent level | |
86 " but only for the current line | |
87 return a:add + shiftwidth() | |
88 elseif <SID>HasNoTagEnd(a:line) | |
89 " no complete tag, return initial indent | |
90 return a:add | |
91 endif | |
92 if a:style == match(a:line, '^\s*</') | |
15194 | 93 return (shiftwidth() * |
16086 | 94 \ (<SID>XmlIndentWithPattern(a:line, b:xml_indent_open) |
95 \ - <SID>XmlIndentWithPattern(a:line, b:xml_indent_close) | |
96 \ - <SID>XmlIndentWithPattern(a:line, '.\{-}/>'))) + a:add | |
7 | 97 else |
15194 | 98 return a:add |
7 | 99 endif |
100 endfun | |
101 | |
15194 | 102 " Main indent function |
7 | 103 fun! XmlIndentGet(lnum, use_syntax_check) |
104 " Find a non-empty line above the current line. | |
16086 | 105 if prevnonblank(a:lnum - 1) == 0 |
106 " Hit the start of the file, use zero indent. | |
15194 | 107 return 0 |
7 | 108 endif |
15729 | 109 " Find previous line with a tag (regardless whether open or closed, |
17571 | 110 " but always restrict the match to a line before the current one |
16086 | 111 " Note: xml declaration: <?xml version="1.0"?> |
112 " won't be found, as it is not a legal tag name | |
17571 | 113 let ptag_pattern = '\%(.\{-}<[/:A-Z_a-z]\)'. '\%(\&\%<'. a:lnum .'l\)' |
16086 | 114 let ptag = search(ptag_pattern, 'bnW') |
115 " no previous tag | |
116 if ptag == 0 | |
117 return 0 | |
118 endif | |
15729 | 119 |
17571 | 120 let pline = getline(ptag) |
121 let pind = indent(ptag) | |
122 | |
123 let syn_name_start = '' " Syntax element at start of line (excluding whitespace) | |
124 let syn_name_end = '' " Syntax element at end of line | |
125 let curline = getline(a:lnum) | |
7 | 126 if a:use_syntax_check |
16086 | 127 let check_lnum = <SID>XmlIndentSynCheck(ptag) |
15194 | 128 let check_alnum = <SID>XmlIndentSynCheck(a:lnum) |
129 if check_lnum == 0 || check_alnum == 0 | |
130 return indent(a:lnum) | |
131 endif | |
17571 | 132 let syn_name_end = synIDattr(synID(a:lnum, strlen(curline) - 1, 1), 'name') |
133 let syn_name_start = synIDattr(synID(a:lnum, match(curline, '\S') + 1, 1), 'name') | |
7 | 134 endif |
135 | |
17571 | 136 if syn_name_end =~ 'Comment' && syn_name_start =~ 'Comment' |
137 return <SID>XmlIndentComment(a:lnum) | |
18790 | 138 elseif empty(syn_name_start) && empty(syn_name_end) && a:use_syntax_check |
17571 | 139 " non-xml tag content: use indent from 'autoindent' |
140 return pind + shiftwidth() | |
141 endif | |
142 | |
15194 | 143 " Get indent from previous tag line |
16086 | 144 let ind = <SID>XmlIndentSum(pline, -1, pind) |
15194 | 145 " Determine indent from current line |
17571 | 146 let ind = <SID>XmlIndentSum(curline, 0, ind) |
7 | 147 return ind |
148 endfun | |
149 | |
16086 | 150 func! <SID>IsXMLContinuation(line) |
151 " Checks, whether or not the line matches a start-of-tag | |
18790 | 152 return a:line !~ '^\s*<' && &ft is# 'xml' |
16086 | 153 endfunc |
154 | |
155 func! <SID>HasNoTagEnd(line) | |
156 " Checks whether or not the line matches '>' (so finishes a tag) | |
157 return a:line !~ '>\s*$' | |
158 endfunc | |
159 | |
15194 | 160 " return indent for a commented line, |
17571 | 161 " the middle part might be indented one additional level |
15194 | 162 func! <SID>XmlIndentComment(lnum) |
16086 | 163 let ptagopen = search(b:xml_indent_open, 'bnW') |
164 let ptagclose = search(b:xml_indent_close, 'bnW') | |
15194 | 165 if getline(a:lnum) =~ '<!--' |
166 " if previous tag was a closing tag, do not add | |
167 " one additional level of indent | |
168 if ptagclose > ptagopen && a:lnum > ptagclose | |
169 return indent(ptagclose) | |
170 else | |
171 " start of comment, add one indentation level | |
172 return indent(ptagopen) + shiftwidth() | |
173 endif | |
174 elseif getline(a:lnum) =~ '-->' | |
175 " end of comment, same as start of comment | |
16086 | 176 return indent(search('<!--', 'bnW')) |
15194 | 177 else |
178 " middle part of comment, add one additional level | |
16086 | 179 return indent(search('<!--', 'bnW')) + shiftwidth() |
15194 | 180 endif |
181 endfunc | |
182 | |
3526
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
183 let &cpo = s:keepcpo |
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
184 unlet s:keepcpo |
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
185 |
15194 | 186 " vim:ts=4 et sts=-1 sw=0 |