Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
xml.vim247 linesDownload Raw Back to indent
1" Language: XML2" Maintainer: Christian Brabandt <cb@256bit.org>3" Repository: https://github.com/chrisbra/vim-xml-ftplugin4" Previous Maintainer: Johannes Zellner <johannes@zellner.org>5" Last Changed: 2020 Nov 4th6" Last Change:7" 20200529 - Handle empty closing tags correctly8" 20191202 - Handle docbk filetype9" 20190726 - Correctly handle non-tagged data10" 20190204 - correctly handle wrap tags11"            https://github.com/chrisbra/vim-xml-ftplugin/issues/512" 20190128 - Make sure to find previous tag13"            https://github.com/chrisbra/vim-xml-ftplugin/issues/414" 20181116 - Fix indentation when tags start with a colon or an underscore15"            https://github.com/vim/vim/pull/92616" 20181022 - Do not overwrite indentkeys setting17"            https://github.com/chrisbra/vim-xml-ftplugin/issues/118" 20180724 - Correctly indent xml comments https://github.com/vim/vim/issues/320019"20" Notes:21"   1) does not indent pure non-xml code (e.g. embedded scripts)22"       2) will be confused by unbalanced tags in comments23"       or CDATA sections.24"       2009-05-26 patch by Nikolai Weibull25" TODO:     implement pre-like tags, see xml_indent_open / xml_indent_close26 27" Only load this indent file when no other was loaded.28if exists("b:did_indent")29    finish30endif31let b:did_indent = 132let s:keepcpo= &cpo33set cpo&vim34 35" [-- local settings (must come before aborting the script) --]36" Attention: Parameter use_syntax_check is used by the docbk.vim indent script37setlocal indentexpr=XmlIndentGet(v:lnum,1)38setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,},!^F39" autoindent: used when the indentexpr returns -140setlocal autoindent41 42let b:undo_indent = "setl ai< inde< indk<"43 44if !exists('b:xml_indent_open')45    let b:xml_indent_open = '.\{-}<[:A-Z_a-z]'46    " pre tag, e.g. <address>47    " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!'48endif49 50if !exists('b:xml_indent_close')51    let b:xml_indent_close = '.\{-}</\|/>.\{-}'52    " end pre tag, e.g. </address>53    " let b:xml_indent_close = '.\{-}</\(address\)\@!'54endif55 56if !exists('b:xml_indent_continuation_filetype')57    let b:xml_indent_continuation_filetype = 'xml'58endif59 60let &cpo = s:keepcpo61unlet s:keepcpo62 63" [-- finish, if the function already exists --]64if exists('*XmlIndentGet')65    finish66endif67 68let s:keepcpo= &cpo69set cpo&vim70 71fun! <SID>XmlIndentWithPattern(line, pat)72    let s = substitute('x'.a:line, a:pat, "\1", 'g')73    return strlen(substitute(s, "[^\1].*$", '', ''))74endfun75 76" [-- check if it's xml --]77fun! <SID>XmlIndentSynCheck(lnum)78    if &syntax != ''79        let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name')80        let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name')81        if syn1 != '' && syn1 !~ 'xml' && syn2 != '' && syn2 !~ 'xml'82            " don't indent pure non-xml code83            return 084        endif85    endif86    return 187endfun88 89" [-- return the sum of indents of a:lnum --]90fun! <SID>XmlIndentSum(line, style, add)91    if <SID>IsXMLContinuation(a:line) &&92        \ a:style == 0 &&93        \ !<SID>IsXMLEmptyClosingTag(a:line)94        " no complete tag, add one additional indent level95        " but only for the current line96        return a:add + shiftwidth()97    elseif <SID>HasNoTagEnd(a:line)98        " no complete tag, return initial indent99        return a:add100    endif101    if a:style == match(a:line, '^\s*</')102        return (shiftwidth() *103        \  (<SID>XmlIndentWithPattern(a:line, b:xml_indent_open)104        \ - <SID>XmlIndentWithPattern(a:line, b:xml_indent_close)105        \ - <SID>XmlIndentWithPattern(a:line, '.\{-}/>'))) + a:add106    else107        return a:add108    endif109endfun110 111" Main indent function112fun! XmlIndentGet(lnum, use_syntax_check)113    " Find a non-empty line above the current line.114    if prevnonblank(a:lnum - 1) == 0115        " Hit the start of the file, use zero indent.116        return 0117    endif118    " Find previous line with a tag (regardless whether open or closed,119    " but always restrict the match to a line before the current one120    " Note: xml declaration: <?xml version="1.0"?>121    "       won't be found, as it is not a legal tag name122    let ptag_pattern = '\%(.\{-}<[/:A-Z_a-z]\)'. '\%(\&\%<'. a:lnum .'l\)'123    let ptag = search(ptag_pattern, 'bnW')124    " no previous tag125    if ptag == 0126        return 0127    endif128 129    let pline = getline(ptag)130    let pind  = indent(ptag)131 132    let syn_name_start = '' " Syntax element at start of line (excluding whitespace)133    let syn_name_end = ''   " Syntax element at end of line134    let curline = getline(a:lnum)135    if a:use_syntax_check136        let check_lnum = <SID>XmlIndentSynCheck(ptag)137        let check_alnum = <SID>XmlIndentSynCheck(a:lnum)138        if check_lnum == 0 || check_alnum == 0139            return indent(a:lnum)140        endif141        let syn_name_end   = synIDattr(synID(a:lnum, strlen(curline) - 1, 1), 'name')142        let syn_name_start = synIDattr(synID(a:lnum, match(curline, '\S') + 1, 1), 'name')143        let prev_syn_name_end   = synIDattr(synID(ptag, strlen(pline) - 1, 1), 'name')144        " not needed (yet?)145        " let prev_syn_name_start = synIDattr(synID(ptag, match(pline, '\S') + 1, 1), 'name')146    endif147 148    if syn_name_end =~ 'Comment' && syn_name_start =~ 'Comment'149        return <SID>XmlIndentComment(a:lnum)150    elseif empty(syn_name_start) && empty(syn_name_end) && a:use_syntax_check151        " non-xml tag content: use indent from 'autoindent'152        if pline =~ b:xml_indent_close153            return pind154        elseif !empty(prev_syn_name_end)155            " only indent by an extra shiftwidth, if the previous line ends156            " with an XML like tag157           return pind + shiftwidth()158        else159            " no extra indent, looks like a text continuation line160           return pind161        endif162    elseif empty(syn_name_start) && syn_name_end =~? 'xmlTag'163        " Special case: such a line, shouldn't be indented, just because it164        " ends with a tag165        " 'foobar <i>inline tags</i>'166        if (match(curline, '<\([:a-zA-Z_]\+\)[^>]*>.*</\1>') > -1)167            return pind168        endif169    endif170 171    if curline =~ '^\s*</[a-zA-Z_]>'172        return <SID>ReturnIndentForMatchingTag(curline)173    endif174 175    " Get indent from previous tag line176    let ind = <SID>XmlIndentSum(pline, -1, pind)177    " Determine indent from current line178    let ind = <SID>XmlIndentSum(curline, 0, ind)179    return ind180endfun181 182func! <SID>IsXMLContinuation(line)183    " Checks, whether or not the line matches a start-of-tag184    return a:line !~ '^\s*<' && &ft =~# b:xml_indent_continuation_filetype185endfunc186 187func! <SID>HasNoTagEnd(line)188    " Checks whether or not the line matches '>' (so finishes a tag)189    return a:line !~ '>\s*$'190endfunc191 192func! <SID>IsXMLEmptyClosingTag(line)193    " Checks whether the line ends with an empty closing tag such as <lb/>194    return a:line =~? '<[^>]*/>\s*$'195endfunc196 197func! <SID>ReturnIndentForMatchingTag(line)198    " For a line with just a simple closing tag199    " get the indent from a matching opening tag200    if a:line =~? '^\s*</[a-z_]*>'201        let _c = getcursorpos()202        let pat = matchstr(a:line, '^\s*</\zs[a-z_]\+\ze>')203        " position cursor before the opening tag204        norm! 0205        " get the indent from the matching opening tag206        let match_line = searchpair('<' .. pat .. '>', '', '</' .. pat .. '>', 'bn')207        call setpos('.', _c)208        return indent(match_line)209    endif210endfunc211 212" return indent for a commented line,213" the middle part might be indented one additional level214func! <SID>XmlIndentComment(lnum)215    let ptagopen = search('.\{-}<[:A-Z_a-z]\_[^/]\{-}>.\{-}', 'bnW')216    let ptagclose = search(b:xml_indent_close, 'bnW')217    if getline(a:lnum) =~ '<!--'218        " if previous tag was a closing tag, do not add219        " one additional level of indent220        if ptagclose > ptagopen && a:lnum > ptagclose221            " If the previous tag was closed on the same line as it was222            " declared, we should indent with its indent level.223            if !<SID>IsXMLContinuation(getline(ptagclose))224                return indent(ptagclose)225            else226                return indent(ptagclose) - shiftwidth()227            endif228        elseif ptagclose == ptagopen229            return indent(ptagclose)230        else231            " start of comment, add one indentation level232            return indent(ptagopen) + shiftwidth()233        endif234    elseif getline(a:lnum) =~ '-->'235        " end of comment, same as start of comment236        return indent(search('<!--', 'bnW'))237    else238        " middle part of comment, add one additional level239        return indent(search('<!--', 'bnW')) + shiftwidth()240    endif241endfunc242 243let &cpo = s:keepcpo244unlet s:keepcpo245 246" vim:ts=4 et sts=-1 sw=0247 
codekingpro/portable-devtools · Team Ai