robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
87140f234a
master
1" Author: Antony Lee <anntzer.lee@gmail.com> 2" Description: Helper functions for reStructuredText syntax folding 3" Last Modified: 2018-12-29 4 5function s: CacheRstFold () 6if ! g: rst_fold_enabled 7return 8endif 9 10let closure = { 'header_types' : {}, 'max_level' : 0 , 'levels' : {}} 11function closure . Process ( match ) dict 12let curline = getcurpos ()[ 1 ] 13if has_key ( self . levels , curline - 1 ) 14" For over+under-lined headers, the regex will match both at the 15" overline and at the title itself; in that case, skip the second match. 16return 17endif 18let lines = split ( a: match , '\n' ) 19let key = repeat ( lines [ -1 ][ 0 ], len ( lines )) 20if ! has_key ( self . header_types , key ) 21let self . max_level += 1 22let self . header_types [ key ] = self . max_level 23endif 24let self . levels [ curline ] = self . header_types [ key ] 25endfunction 26let save_cursor = getcurpos () 27let save_mark = getpos ( "'[" ) 28silent keeppatterns %s/\v^%(%(([=`:.'"~^_*+#-])\1+\n)?.{1,2}\n([=`:.'"~^_*+#-])\2+)|%(%(([=`:.''"~^_*+#-])\3{2,}\n)?.{3,}\n([=`:.''"~^_*+#-])\4{2,})$/\=closure.Process(submatch(0))/gn 29call setpos ( '.' , save_cursor ) 30call setpos ( "'[" , save_mark ) 31let b: RstFoldCache = closure . levels 32endfunction 33 34function RstFold#GetRstFold () 35if ! g: rst_fold_enabled 36return 37endif 38 39if ! has_key ( b:, 'RstFoldCache' ) 40call s: CacheRstFold () 41endif 42if has_key ( b: RstFoldCache , v: lnum ) 43return '>' . b: RstFoldCache [ v: lnum ] 44else 45return '=' 46endif 47endfunction 48 49function RstFold#GetRstFoldText () 50if ! g: rst_fold_enabled 51return 52endif 53 54if ! has_key ( b:, 'RstFoldCache' ) 55call s: CacheRstFold () 56endif 57let indent = repeat ( ' ' , b: RstFoldCache [ v: foldstart ] - 1 ) 58let thisline = getline ( v: foldstart ) 59" For over+under-lined headers, skip the overline. 60let text = thisline =~ '^\([=`:.''"~^_*+#-]\)\1\+$' ? getline ( v: foldstart + 1 ) : thisline 61return indent . text 62endfunction