robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
ab20681c52
master
1" Vim indent file 2" Language: Hare 3" Maintainer: Amelia Clarke <selene@perilune.dev> 4" Last Change: 2024-04-14 5" Upstream: https://git.sr.ht/~sircmpwn/hare.vim 6 7if exists ( 'b:did_indent' ) 8finish 9endif 10let b: did_indent = 1 11 12let s: cpo_save = & cpo 13set cpo & vim 14 15" L0 -> don't deindent labels 16" (s -> use one indent after a trailing ( 17" m1 -> if ) starts a line, indent it the same as its matching ( 18" ks -> add an extra indent to extra lines in an if expression or for expression 19" j1 -> indent code inside {} one level when in parentheses 20" J1 -> see j1 21" *0 -> don't search for unclosed block comments 22" #1 -> don't deindent lines that begin with # 23setlocal cinoptions = L0,(s,m1,ks,j1,J1,*0,#1 24 25" Controls which keys reindent the current line. 26" 0{ -> { at beginning of line 27" 0} -> } at beginning of line 28" 0) -> ) at beginning of line 29" 0] -> ] at beginning of line 30" !^F -> <C-f> (not inserted) 31" o -> <CR> or `o` command 32" O -> `O` command 33" e -> else 34" 0=case -> case 35setlocal indentkeys = 0{,0},0),0],!^F,o,O,e,0=case 36 37setlocal cinwords = if,else,for,switch,match 38 39setlocal indentexpr = GetHareIndent () 40 41let b: undo_indent = 'setl cino< cinw< inde< indk<' 42 43if exists ( '*GetHareIndent()' ) 44finish 45endif 46 47function ! FloorCindent ( lnum ) 48return cindent ( a: lnum ) / shiftwidth () * shiftwidth () 49endfunction 50 51function ! GetHareIndent () 52let line = getline ( v: lnum ) 53let prevlnum = prevnonblank ( v: lnum - 1 ) 54let prevline = getline ( prevlnum ) 55let prevprevline = getline ( prevnonblank ( prevlnum - 1 )) 56 57" This is all very hacky and imperfect, but it's tough to do much better when 58" working with regex-based indenting rules. 59 60" If the previous line ended with =, indent by one shiftwidth. 61if prevline =~# '\v\=\s*(//.*)?$' 62return indent ( prevlnum ) + shiftwidth () 63endif 64 65" If the previous line ended in a semicolon and the line before that ended 66" with =, deindent by one shiftwidth. 67if prevline =~# '\v;\s*(//.*)?$' && prevprevline =~# '\v\=\s*(//.*)?$' 68return indent ( prevlnum ) - shiftwidth () 69endif 70 71" TODO: The following edge-case is still indented incorrectly: 72" case => 73" if (foo) { 74" bar; 75" }; 76" | // cursor is incorrectly deindented by one shiftwidth. 77" 78" This only happens if the {} block is the first statement in the case body. 79" If `case` is typed, the case will also be incorrectly deindented by one 80" shiftwidth. Are you having fun yet? 81 82" Deindent cases. 83if line =~# '\v^\s*case' 84" If the previous line was also a case, don't do any special indenting. 85if prevline =~# '\v^\s*case' 86return indent ( prevlnum ) 87end 88 89" If the previous line was a multiline case, deindent by one shiftwidth. 90if prevline =~# '\v\=\>\s*(//.*)?$' 91return indent ( prevlnum ) - shiftwidth () 92endif 93 94" If the previous line started a block, deindent by one shiftwidth. 95" This handles the first case in a switch/match block. 96if prevline =~# '\v\{\s*(//.*)?$' 97return FloorCindent ( v: lnum ) - shiftwidth () 98end 99 100" If the previous line ended in a semicolon and the line before that wasn't 101" a case, deindent by one shiftwidth. 102if prevline =~# '\v;\s*(//.*)?$' && prevprevline !~# '\v\=\>\s*(//.*)?$' 103return FloorCindent ( v: lnum ) - shiftwidth () 104end 105 106let l: indent = FloorCindent ( v: lnum ) 107 108" If a normal cindent would indent the same amount as the previous line, 109" deindent by one shiftwidth. This fixes some issues with `case let` blocks. 110if l: indent == indent ( prevlnum ) 111return l: indent - shiftwidth () 112endif 113 114" Otherwise, do a normal cindent. 115return l: indent 116endif 117 118" Don't indent an extra shiftwidth for cases which span multiple lines. 119if prevline =~# '\v\=\>\s*(//.*)?$' && prevline !~# '\v^\s*case\W' 120return indent ( prevlnum ) 121endif 122 123" Indent the body of a case. 124" If the previous line ended in a semicolon and the line before that was a 125" case, don't do any special indenting. 126if prevline =~# '\v;\s*(//.*)?$' && prevprevline =~# '\v\=\>\s*(//.*)?$' 127 \&& line !~# '\v^\s*}' 128return indent ( prevlnum ) 129endif 130 131let l: indent = FloorCindent ( v: lnum ) 132 133" If the previous line was a case and a normal cindent wouldn't indent, indent 134" an extra shiftwidth. 135if prevline =~# '\v\=\>\s*(//.*)?$' && l: indent == indent ( prevlnum ) 136return l: indent + shiftwidth () 137endif 138 139" If everything above is false, do a normal cindent. 140return l: indent 141endfunction 142 143let & cpo = s: cpo_save 144unlet s: cpo_save 145 146" vim: et sw=2 sts=2 ts=8