robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
a2c3591720
master
1" HTML folding script, :h ft-html-plugin 2" Latest Change: 2025 May 10 3" Original Author: Aliaksei Budavei <0x000c70@gmail.com> 4 5function ! htmlfold#MapBalancedTags () abort 6" Describe only _a capturable-name prefix_ for start and end patterns of 7" a tag so that start tags with attributes spanning across lines can also be 8" matched with a single call of "getline()". 9let tag = '\m\c</\=\([0-9A-Za-z-]\+\)' 10let names = [] 11let pairs = [] 12let ends = [] 13let pos = getpos ( '.' ) 14 15try 16call cursor ( 1 , 1 ) 17let [ lnum , cnum ] = searchpos ( tag , 'cnW' ) 18 19" Pair up nearest non-inlined tags in scope. 20while lnum > 0 21let name_attr = synIDattr ( synID ( lnum , cnum , 0 ), 'name' ) 22 23if name_attr ==# 'htmlTag' || name_attr ==# 'htmlScriptTag' 24let name = get ( matchlist ( getline ( lnum ), tag , ( cnum - 1 )), 1 , '' ) 25 26if ! empty ( name ) 27call insert ( names , tolower ( name ), 0 ) 28call insert ( pairs , [ lnum , -1 ], 0 ) 29endif 30elseif name_attr ==# 'htmlEndTag' 31let name = get ( matchlist ( getline ( lnum ), tag , ( cnum - 1 )), 1 , '' ) 32 33if ! empty ( name ) 34let idx = index ( names , tolower ( name )) 35 36if idx >= 0 37" Dismiss inlined balanced tags and opened-only tags. 38if pairs [ idx ][ 0 ] != lnum 39let pairs [ idx ][ 1 ] = lnum 40call add ( ends , lnum ) 41endif 42 43" Claim a pair. 44let names [: idx ] = repeat ([ '' ], ( idx + 1 )) 45endif 46endif 47endif 48 49" Advance the cursor, at "<", past "</a", "<a>", etc. 50call cursor ( lnum , ( cnum + 3 )) 51let [ lnum , cnum ] = searchpos ( tag , 'cnW' ) 52endwhile 53finally 54call setpos ( '.' , pos ) 55endtry 56 57if empty ( ends ) 58return {} 59endif 60 61let folds = {} 62let pending_end = ends [ 0 ] 63let level = 0 64 65while ! empty ( pairs ) 66let [ start , end ] = remove ( pairs , -1 ) 67 68if end < 0 69continue 70endif 71 72if start >= pending_end 73" Mark a sibling tag. 74call remove ( ends , 0 ) 75 76while start >= ends [ 0 ] 77" Mark a parent tag. 78call remove ( ends , 0 ) 79let level -= 1 80endwhile 81 82let pending_end = ends [ 0 ] 83else 84" Mark a child tag. 85let level += 1 86endif 87 88" Flatten the innermost inlined folds. 89let folds [ start ] = get ( folds , start , ( '>' . level )) 90let folds [ end ] = get ( folds , end , ( '<' . level )) 91endwhile 92 93return folds 94endfunction 95 96" See ":help vim9-mix". 97if ! has ( "vim9script" ) 98finish 99endif 100 101def ! g:htmlfold#MapBalancedTags(): dict<string> 102 #Describe only _a capturable-name prefix_ for start and end patterns of 103 # a tag so that start tags with attributes spanning across lines can also be 104 # matched with a single call of "getline()". 105const tag : string = '\m\c</\=\([0-9A-Za-z-]\+\)' 106var names: list<string> = [] 107var pairs: list<list<number>> = [] 108var ends: list<number> = [] 109const pos : list<number> = getpos('.') 110 111try 112cursor (1, 1) 113var [lnum: number, cnum: number] = searchpos(tag,'cnW' ) 114 115 #Pair up nearest non-inlined tags in scope. 116while lnum > 0 117const name_attr : string = synIDattr(synID(lnum, cnum,0), 'name') 118 119if name_attr ==# 'htmlTag' || name_attr ==# 'htmlScriptTag' 120const name : string = get(matchlist(getline(lnum), tag, (cnum -1)), 1, '') 121 122if ! empty ( name ) 123insert (names, tolower(name), 0) 124insert (pairs, [lnum, -1], 0) 125endif 126elseif name_attr ==# 'htmlEndTag' 127const name : string = get(matchlist(getline(lnum), tag, (cnum -1)), 1, '') 128 129if ! empty ( name ) 130const idx : number = index(names, tolower(name)) 131 132if idx >= 0 133 #Dismiss inlined balanced tags and opened-only tags. 134if pairs [ idx ][ 0 ] != lnum 135pairs [idx][1] = lnum 136add (ends, lnum) 137endif 138 139 #Claim a pair. 140names [: idx] = repeat([''], (idx + 1)) 141endif 142endif 143endif 144 145 #Advance the cursor, at "<", past "</a", "<a>", etc. 146cursor (lnum, (cnum + 3)) 147 [lnum, cnum] = searchpos(tag, 'cnW') 148endwhile 149finally 150setpos ('.', pos) 151endtry 152 153if empty ( ends ) 154return {} 155endif 156 157var folds: dict<string> = {} 158var pending_end: number = ends[0] 159var level: number = 0 160 161while ! empty ( pairs ) 162const [ start : number , end: number] = remove(pairs, -1) 163 164if end < 0 165continue 166endif 167 168if start >= pending_end 169 #Mark a sibling tag. 170remove (ends, 0) 171 172while start >= ends [ 0 ] 173 #Mark a parent tag. 174remove (ends, 0) 175level -= 1 176endwhile 177 178pending _end = ends[0] 179else 180 #Mark a child tag. 181level += 1 182endif 183 184 #Flatten the innermost inlined folds. 185folds [start] = get(folds, start, ('>' .. level)) 186folds [end] = get(folds, end, ('<' .. level)) 187endwhile 188 189return folds 190enddef 191 192" vim: fdm=syntax sw=2 ts=8 noet