robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
ab20681c52
master
1" Vim completion script 2" Language: XML 3" Maintainer: Mikolaj Machowski ( mikmach AT wp DOT pl ) 4" Last Change: 2013 Jun 29 5" Version: 1.9 6" 7" Changelog: 8" 1.9 - 2007 Aug 15 9" - fix closing of namespaced tags (Johannes Weiss) 10" 1.8 - 2006 Jul 18 11" - allow for closing of xml tags even when data file isn't available 12 13" This function will create Dictionary with users namespace strings and values 14" canonical (system) names of data files. Names should be lowercase, 15" descriptive to avoid any future conflicts. For example 'xhtml10s' should be 16" name for data of XHTML 1.0 Strict and 'xhtml10t' for XHTML 1.0 Transitional 17" User interface will be provided by XMLns command defined in ftplugin/xml.vim 18" Currently supported canonicals are: 19" xhtml10s - XHTML 1.0 Strict 20" xsl - XSL 21function ! xmlcomplete#CreateConnection ( canonical , ...) " {{{ 22 23" When only one argument provided treat name as default namespace (without 24" 'prefix:'). 25if exists ( "a:1" ) 26let users = a: 1 27else 28let users = 'DEFAULT' 29endif 30 31" Source data file. Due to suspected errors in autoload do it with 32" :runtime. 33" TODO: make it properly (using autoload, that is) later 34exe "runtime autoload/xml/" . a: canonical . ".vim" 35 36" Remove all traces of unexisting files to return [] when trying 37" omnicomplete something 38" TODO: give warning about non-existing canonicals - should it be? 39if ! exists ( "g:xmldata_" . a: canonical ) 40unlet ! g: xmldata_connection 41return 0 42endif 43 44" We need to initialize Dictionary to add key-value pair 45if ! exists ( "g:xmldata_connection" ) 46let g: xmldata_connection = {} 47endif 48 49let g: xmldata_connection [ users ] = a: canonical 50 51endfunction 52" }}} 53 54function ! xmlcomplete#CreateEntConnection (...) " {{{ 55if a: 0 > 0 56let g: xmldata_entconnect = a: 1 57else 58let g: xmldata_entconnect = 'DEFAULT' 59endif 60endfunction 61" }}} 62 63function ! xmlcomplete#CompleteTags ( findstart , base ) 64if a: findstart 65" locate the start of the word 66let curline = line ( '.' ) 67let line = getline ( '.' ) 68let start = col ( '.' ) - 1 69let compl_begin = col ( '.' ) - 2 70 71while start >= 0 && line [ start - 1 ] =~ '\(\k\|[:.-]\)' 72let start -= 1 73endwhile 74 75if start >= 0 && line [ start - 1 ] =~ '&' 76let b: entitiescompl = 1 77let b: compl_context = '' 78return start 79endif 80 81let b: compl_context = getline ( '.' )[ 0 :( compl_begin )] 82if b: compl_context !~ '<[^>]*$' 83" Look like we may have broken tag. Check previous lines. Up to 84" 10? 85let i = 1 86while 1 87let context_line = getline ( curline - i ) 88if context_line =~ '<[^>]*$' 89" Yep, this is this line 90let context_lines = getline ( curline - i , curline - 1 ) + [ b: compl_context ] 91let b: compl_context = join ( context_lines , ' ' ) 92break 93elseif context_line =~ '>[^<]*$' || i == curline 94" Normal tag line, no need for completion at all 95" OR reached first line without tag at all 96let b: compl_context = '' 97break 98endif 99let i += 1 100endwhile 101" Make sure we don't have counter 102unlet ! i 103endif 104let b: compl_context = matchstr ( b: compl_context , '.*\zs<.*' ) 105 106" Make sure we will have only current namespace 107unlet ! b: xml_namespace 108let b: xml_namespace = matchstr ( b: compl_context , '^<\zs\k*\ze:' ) 109if b: xml_namespace == '' 110let b: xml_namespace = 'DEFAULT' 111endif 112 113return start 114 115else 116" Initialize base return lists 117let res = [] 118let res2 = [] 119" a:base is very short - we need context 120if len ( b: compl_context ) == 0 && ! exists ( "b:entitiescompl" ) 121return [] 122endif 123let context = matchstr ( b: compl_context , '^<\zs.*' ) 124unlet ! b: compl_context 125" There is no connection of namespace and data file. 126if ! exists ( "g:xmldata_connection" ) || g: xmldata_connection == {} 127" There is still possibility we may do something - eg. close tag 128let b: unaryTagsStack = "base meta link hr br param img area input col" 129if context =~ '^\/' 130let opentag = xmlcomplete#GetLastOpenTag ( "b:unaryTagsStack" ) 131return [ opentag . ">" ] 132else 133return [] 134endif 135endif 136 137" Make entities completion 138if exists ( "b:entitiescompl" ) 139unlet ! b: entitiescompl 140 141if ! exists ( "g:xmldata_entconnect" ) || g: xmldata_entconnect == 'DEFAULT' 142let values = g: xmldata { '_' . g: xmldata_connection [ 'DEFAULT' ]}[ 'vimxmlentities' ] 143else 144let values = g: xmldata { '_' . g: xmldata_entconnect }[ 'vimxmlentities' ] 145endif 146 147" Get only lines with entity declarations but throw out 148" parameter-entities - they may be completed in future 149let entdecl = filter ( getline ( 1 , "$" ), 'v:val =~ "<!ENTITY\\s\\+[^%]"' ) 150 151if len ( entdecl ) > 0 152let intent = map ( copy ( entdecl ), 'matchstr(v:val, "<!ENTITY\\s\\+\\zs\\(\\k\\|[.-:]\\)\\+\\ze")' ) 153let values = intent + values 154endif 155 156if len ( a: base ) == 1 157for m in values 158if m =~ '^' . a: base 159call add ( res , m . ';' ) 160endif 161endfor 162return res 163else 164for m in values 165if m =~? '^' . a: base 166call add ( res , m . ';' ) 167elseif m =~? a: base 168call add ( res2 , m . ';' ) 169endif 170endfor 171 172return res + res2 173endif 174 175endif 176if context =~ '>' 177" Generally if context contains > it means we are outside of tag and 178" should abandon action 179return [] 180endif 181 182" find tags matching with "a:base" 183" If a:base contains white space it is attribute. 184" It could be also value of attribute... 185" We have to get first word to offer 186" proper completions 187if context == '' 188let tag = '' 189else 190let tag = split ( context )[ 0 ] 191endif 192" Get rid of namespace 193let tag = substitute ( tag , '^' . b: xml_namespace . ':' , '' , '' ) 194 195 196" Get last word, it should be attr name 197let attr = matchstr ( context , '.*\s\zs.*' ) 198" Possible situations where any prediction would be difficult: 199" 1. Events attributes 200if context =~ '\s' 201 202" If attr contains =\s*[\"'] we catch value of attribute 203if attr =~ "=\s*[\"']" || attr =~ "=\s*$" 204" Let do attribute specific completion 205let attrname = matchstr ( attr , '.*\ze\s*=' ) 206let entered_value = matchstr ( attr , ".*=\\s*[\"']\\?\\zs.*" ) 207 208if tag =~ '^[?!]' 209" Return nothing if we are inside of ! or ? tag 210return [] 211else 212if has_key ( g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}, tag ) && has_key ( g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}[ tag ][ 1 ], attrname ) 213let values = g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}[ tag ][ 1 ][ attrname ] 214else 215return [] 216endif 217endif 218 219if len ( values ) == 0 220return [] 221endif 222 223" We need special version of sbase 224let attrbase = matchstr ( context , ".*[\"']" ) 225let attrquote = matchstr ( attrbase , '.$' ) 226if attrquote !~ "['\"]" 227let attrquoteopen = '"' 228let attrquote = '"' 229else 230let attrquoteopen = '' 231endif 232 233for m in values 234" This if is needed to not offer all completions as-is 235" alphabetically but sort them. Those beginning with entered 236" part will be as first choices 237if m =~ '^' . entered_value 238call add ( res , attrquoteopen . m . attrquote . ' ' ) 239elseif m =~ entered_value 240call add ( res2 , attrquoteopen . m . attrquote . ' ' ) 241endif 242endfor 243 244return res + res2 245 246endif 247 248if tag =~ '?xml' 249" Two possible arguments for <?xml> plus variation 250let attrs = [ 'encoding' , 'version="1.0"' , 'version' ] 251elseif tag =~ '^!' 252" Don't make completion at all 253" 254return [] 255else 256if ! has_key ( g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}, tag ) 257" Abandon when data file isn't complete 258return [] 259endif 260let attrs = keys ( g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}[ tag ][ 1 ]) 261endif 262 263for m in sort ( attrs ) 264if m =~ '^' . attr 265call add ( res , m ) 266elseif m =~ attr 267call add ( res2 , m ) 268endif 269endfor 270let menu = res + res2 271let final_menu = [] 272if has_key ( g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}, 'vimxmlattrinfo' ) 273for i in range ( len ( menu )) 274let item = menu [ i ] 275if has_key ( g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}[ 'vimxmlattrinfo' ], item ) 276let m_menu = g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}[ 'vimxmlattrinfo' ][ item ][ 0 ] 277let m_info = g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}[ 'vimxmlattrinfo' ][ item ][ 1 ] 278else 279let m_menu = '' 280let m_info = '' 281endif 282if tag !~ '^[?!]' && len ( g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}[ tag ][ 1 ][ item ]) > 0 && g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}[ tag ][ 1 ][ item ][ 0 ] =~ '^\(BOOL\|' . item . '\)$' 283let item = item 284else 285let item .= '="' 286endif 287let final_menu += [{ 'word' : item , 'menu' : m_menu , 'info' : m_info }] 288endfor 289else 290for i in range ( len ( menu )) 291let item = menu [ i ] 292if tag !~ '^[?!]' && len ( g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}[ tag ][ 1 ][ item ]) > 0 && g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}[ tag ][ 1 ][ item ][ 0 ] =~ '^\(BOOL\|' . item . '\)$' 293let item = item 294else 295let item .= '="' 296endif 297let final_menu += [ item ] 298endfor 299endif 300return final_menu 301 302endif 303" Close tag 304let b: unaryTagsStack = "base meta link hr br param img area input col" 305if context =~ '^\/' 306let opentag = xmlcomplete#GetLastOpenTag ( "b:unaryTagsStack" ) 307return [ opentag . ">" ] 308endif 309 310" Complete elements of XML structure 311" TODO: #REQUIRED, #IMPLIED, #FIXED, #PCDATA - but these should be detected like 312" entities - in first run 313" keywords: CDATA, ID, IDREF, IDREFS, ENTITY, ENTITIES, NMTOKEN, NMTOKENS 314" are hardly recognizable but keep it in reserve 315" also: EMPTY ANY SYSTEM PUBLIC DATA 316if context =~ '^!' 317let tags = [ '!ELEMENT' , '!DOCTYPE' , '!ATTLIST' , '!ENTITY' , '!NOTATION' , '![CDATA[' , '![INCLUDE[' , '![IGNORE[' ] 318 319for m in tags 320if m =~ '^' . context 321let m = substitute ( m , '^!\[\?' , '' , '' ) 322call add ( res , m ) 323elseif m =~ context 324let m = substitute ( m , '^!\[\?' , '' , '' ) 325call add ( res2 , m ) 326endif 327endfor 328 329return res + res2 330 331endif 332 333" Complete text declaration 334if context =~ '^?' 335let tags = [ '?xml' ] 336 337for m in tags 338if m =~ '^' . context 339call add ( res , substitute ( m , '^?' , '' , '' )) 340elseif m =~ context 341call add ( res , substitute ( m , '^?' , '' , '' )) 342endif 343endfor 344 345return res + res2 346 347endif 348 349" Deal with tag completion. 350let opentag = xmlcomplete#GetLastOpenTag ( "b:unaryTagsStack" ) 351let opentag = substitute ( opentag , '^\k*:' , '' , '' ) 352if opentag == '' 353"return [] 354let tags = keys ( g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}) 355call filter ( tags , 'v:val !~ "^vimxml"' ) 356else 357if ! has_key ( g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}, opentag ) 358" Abandon when data file isn't complete 359return [] 360endif 361let tags = g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}[ opentag ][ 0 ] 362endif 363 364let context = substitute ( context , '^\k*:' , '' , '' ) 365 366for m in tags 367if m =~ '^' . context 368call add ( res , m ) 369elseif m =~ context 370call add ( res2 , m ) 371endif 372endfor 373let menu = res + res2 374if b: xml_namespace == 'DEFAULT' 375let xml_namespace = '' 376else 377let xml_namespace = b: xml_namespace . ':' 378endif 379if has_key ( g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}, 'vimxmltaginfo' ) 380let final_menu = [] 381for i in range ( len ( menu )) 382let item = menu [ i ] 383if has_key ( g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}[ 'vimxmltaginfo' ], item ) 384let m_menu = g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}[ 'vimxmltaginfo' ][ item ][ 0 ] 385let m_info = g: xmldata { '_' . g: xmldata_connection [ b: xml_namespace ]}[ 'vimxmltaginfo' ][ item ][ 1 ] 386else 387let m_menu = '' 388let m_info = '' 389endif 390let final_menu += [{ 'word' : xml_namespace . item , 'menu' : m_menu , 'info' : m_info }] 391endfor 392else 393let final_menu = map ( menu , 'xml_namespace.v:val' ) 394endif 395 396return final_menu 397 398endif 399endfunction 400 401" MM: This is severely reduced closetag.vim used with kind permission of Steven 402" Mueller 403" Changes: strip all comments; delete error messages; add checking for 404" namespace 405" Author: Steven Mueller <diffusor@ugcs.caltech.edu> 406" Last Modified: Tue May 24 13:29:48 PDT 2005 407" Version: 0.9.1 408 409function ! xmlcomplete#GetLastOpenTag ( unaryTagsStack ) 410let linenum = line ( '.' ) 411let lineend = col ( '.' ) - 1 " start: cursor position 412let first = 1 " flag for first line searched 413let b: TagStack = '' " main stack of tags 414let startInComment = s: InComment () 415 416if exists ( "b:xml_namespace" ) 417if b: xml_namespace == 'DEFAULT' 418let tagpat = '</\=\(\k\|[.:-]\)\+\|/>' 419else 420let tagpat = '</\=' . b: xml_namespace . ':\(\k\|[.-]\)\+\|/>' 421endif 422else 423let tagpat = '</\=\(\k\|[.:-]\)\+\|/>' 424endif 425while ( linenum > 0 ) 426let line = getline ( linenum ) 427if first 428let line = strpart ( line , 0 , lineend ) 429else 430let lineend = strlen ( line ) 431endif 432let b: lineTagStack = '' 433let mpos = 0 434let b: TagCol = 0 435while ( mpos > -1 ) 436let mpos = matchend ( line , tagpat ) 437if mpos > -1 438let b: TagCol = b: TagCol + mpos 439let tag = matchstr ( line , tagpat ) 440 441if exists ( 'b:closetag_disable_synID' ) || startInComment == s: InCommentAt ( linenum , b: TagCol ) 442let b: TagLine = linenum 443call s: Push ( matchstr ( tag , '[^<>]\+' ), 'b:lineTagStack' ) 444endif 445let lineend = lineend - mpos 446let line = strpart ( line , mpos , lineend ) 447endif 448endwhile 449while ( ! s: EmptystackP ( 'b:lineTagStack' )) 450let tag = s: Pop ( 'b:lineTagStack' ) 451if match ( tag , '^/' ) == 0 "found end tag 452call s: Push ( tag , 'b:TagStack' ) 453elseif s: EmptystackP ( 'b:TagStack' ) && ! s: Instack ( tag , a: unaryTagsStack ) "found unclosed tag 454return tag 455else 456let endtag = s: Peekstack ( 'b:TagStack' ) 457if endtag == '/' . tag || endtag == '/' 458call s: Pop ( 'b:TagStack' ) "found a open/close tag pair 459elseif ! s: Instack ( tag , a: unaryTagsStack ) "we have a mismatch error 460return '' 461endif 462endif 463endwhile 464let linenum = linenum - 1 |let first = 0 465endwhile 466return '' 467endfunction 468 469function ! s: InComment () 470return synIDattr ( synID ( line ( '.' ), col ( '.' ), 0 ), 'name' ) =~ 'Comment\|String' 471endfunction 472 473function ! s: InCommentAt ( line , col ) 474return synIDattr ( synID ( a: line , a: col , 0 ), 'name' ) =~ 'Comment\|String' 475endfunction 476 477function ! s: SetKeywords () 478let s: IsKeywordBak =& l: iskeyword 479let & l: iskeyword = '33-255' 480endfunction 481 482function ! s: RestoreKeywords () 483let & l: iskeyword = s: IsKeywordBak 484endfunction 485 486function ! s: Push ( el , sname ) 487if ! s: EmptystackP ( a: sname ) 488exe 'let ' . a: sname . "=a:el.' '." . a: sname 489else 490exe 'let ' . a: sname . '=a:el' 491endif 492endfunction 493 494function ! s: EmptystackP ( sname ) 495exe 'let stack=' . a: sname 496if match ( stack , '^ *$' ) == 0 497return 1 498else 499return 0 500endif 501endfunction 502 503function ! s: Instack ( el , sname ) 504exe 'let stack=' . a: sname 505call s: SetKeywords () 506let m = match ( stack , '\<' . a: el . '\>' ) 507call s: RestoreKeywords () 508if m < 0 509return 0 510else 511return 1 512endif 513endfunction 514 515function ! s: Peekstack ( sname ) 516call s: SetKeywords () 517exe 'let stack=' . a: sname 518let top = matchstr ( stack , '\<.\{-1,}\>' ) 519call s: RestoreKeywords () 520return top 521endfunction 522 523function ! s: Pop ( sname ) 524if s: EmptystackP ( a: sname ) 525return '' 526endif 527exe 'let stack=' . a: sname 528call s: SetKeywords () 529let loc = matchend ( stack , '\<.\{-1,}\>' ) 530exe 'let ' . a: sname . '=strpart(stack, loc+1, strlen(stack))' 531let top = strpart ( stack , match ( stack , '\<' ), loc ) 532call s: RestoreKeywords () 533return top 534endfunction 535 536function ! s: Clearstack ( sname ) 537exe 'let ' . a: sname . "=''" 538endfunction 539" vim:set foldmethod=marker: