|
| 1 | +if polyglot#init#is_disabled(expand('<sfile>:p'), 'svelte', 'indent/svelte-xml.vim') |
| 2 | + finish |
| 3 | +endif |
| 4 | + |
| 5 | +" Language: xml |
| 6 | +" Maintainer: Johannes Zellner <[email protected]> |
| 7 | +" Last Change: 2017 Jun 13 |
| 8 | +" Notes: 1) does not indent pure non-xml code (e.g. embedded scripts) |
| 9 | +" 2) will be confused by unbalanced tags in comments |
| 10 | +" or CDATA sections. |
| 11 | +" 2009-05-26 patch by Nikolai Weibull |
| 12 | +" TODO: implement pre-like tags, see xml_indent_open / xml_indent_close |
| 13 | + |
| 14 | +" Only load this indent file when no other was loaded. |
| 15 | +if exists("b:did_indent") |
| 16 | + finish |
| 17 | +endif |
| 18 | +let b:did_indent = 1 |
| 19 | +let s:keepcpo= &cpo |
| 20 | +set cpo&vim |
| 21 | + |
| 22 | +" [-- local settings (must come before aborting the script) --] |
| 23 | +setlocal indentexpr=XmlIndentGet(v:lnum,1) |
| 24 | +setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,} |
| 25 | + |
| 26 | +if !exists('b:xml_indent_open') |
| 27 | + let b:xml_indent_open = '.\{-}<\a' |
| 28 | + " pre tag, e.g. <address> |
| 29 | + " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!' |
| 30 | +endif |
| 31 | + |
| 32 | +if !exists('b:xml_indent_close') |
| 33 | + let b:xml_indent_close = '.\{-}</' |
| 34 | + " end pre tag, e.g. </address> |
| 35 | + " let b:xml_indent_close = '.\{-}</\(address\)\@!' |
| 36 | +endif |
| 37 | + |
| 38 | +let &cpo = s:keepcpo |
| 39 | +unlet s:keepcpo |
| 40 | + |
| 41 | +" [-- finish, if the function already exists --] |
| 42 | +if exists('*XmlIndentGet') |
| 43 | + finish |
| 44 | +endif |
| 45 | + |
| 46 | +let s:keepcpo= &cpo |
| 47 | +set cpo&vim |
| 48 | + |
| 49 | +fun! <SID>XmlIndentWithPattern(line, pat) |
| 50 | + let s = substitute('x'.a:line, a:pat, "\1", 'g') |
| 51 | + return strlen(substitute(s, "[^\1].*$", '', '')) |
| 52 | +endfun |
| 53 | + |
| 54 | +" [-- check if it's xml --] |
| 55 | +fun! <SID>XmlIndentSynCheck(lnum) |
| 56 | + if '' != &syntax |
| 57 | + let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name') |
| 58 | + let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name') |
| 59 | + if '' != syn1 && syn1 !~ 'xml' && '' != syn2 && syn2 !~ 'xml' |
| 60 | + " don't indent pure non-xml code |
| 61 | + return 0 |
| 62 | + elseif syn1 =~ '^xmlComment' && syn2 =~ '^xmlComment' |
| 63 | + " indent comments specially |
| 64 | + return -1 |
| 65 | + endif |
| 66 | + endif |
| 67 | + return 1 |
| 68 | +endfun |
| 69 | + |
| 70 | +" [-- return the sum of indents of a:lnum --] |
| 71 | +fun! <SID>XmlIndentSum(lnum, style, add) |
| 72 | + let line = getline(a:lnum) |
| 73 | + if a:style == match(line, '^\s*</') |
| 74 | + return (shiftwidth() * |
| 75 | + \ (<SID>XmlIndentWithPattern(line, b:xml_indent_open) |
| 76 | + \ - <SID>XmlIndentWithPattern(line, b:xml_indent_close) |
| 77 | + \ - <SID>XmlIndentWithPattern(line, '.\{-}/>'))) + a:add |
| 78 | + else |
| 79 | + return a:add |
| 80 | + endif |
| 81 | +endfun |
| 82 | + |
| 83 | +fun! XmlIndentGet(lnum, use_syntax_check) |
| 84 | + " Find a non-empty line above the current line. |
| 85 | + let lnum = prevnonblank(a:lnum - 1) |
| 86 | + |
| 87 | + " Hit the start of the file, use zero indent. |
| 88 | + if lnum == 0 |
| 89 | + return 0 |
| 90 | + endif |
| 91 | + |
| 92 | + if a:use_syntax_check |
| 93 | + let check_lnum = <SID>XmlIndentSynCheck(lnum) |
| 94 | + let check_alnum = <SID>XmlIndentSynCheck(a:lnum) |
| 95 | + if 0 == check_lnum || 0 == check_alnum |
| 96 | + return indent(a:lnum) |
| 97 | + elseif -1 == check_lnum || -1 == check_alnum |
| 98 | + return -1 |
| 99 | + endif |
| 100 | + endif |
| 101 | + |
| 102 | + let ind = <SID>XmlIndentSum(lnum, -1, indent(lnum)) |
| 103 | + let ind = <SID>XmlIndentSum(a:lnum, 0, ind) |
| 104 | + |
| 105 | + return ind |
| 106 | +endfun |
| 107 | + |
| 108 | +let &cpo = s:keepcpo |
| 109 | +unlet s:keepcpo |
| 110 | + |
| 111 | +" vim:ts=8 |
0 commit comments