robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa5819f5a5
master
1" Vim indent plugin file 2" Language: Odin 3" Maintainer: Maxim Kim <habamax@gmail.com> 4" Website: https://github.com/habamax/vim-odin 5" Last Change: 2024-01-15 6" 7" This file has been manually translated from Vim9 script. 8 9if exists ( "b:did_indent" ) 10finish 11endif 12let b: did_indent = 1 13 14let s: cpo_save = & cpo 15set cpo & vim 16 17let b: undo_indent = 'setlocal cindent< cinoptions< cinkeys< indentexpr<' 18 19setlocal cindent 20setlocal cinoptions = L0,m1,(s,j1,J1,l1,+0,:0,#1 21setlocal cinkeys = 0{,0},0),0],!^F,:,o,O 22 23setlocal indentexpr = s :GetOdinIndent(v:lnum) 24 25function s: PrevLine ( lnum ) abort 26let plnum = a: lnum - 1 27while plnum > 1 28let plnum = prevnonblank ( plnum ) 29let pline = getline ( plnum ) 30" XXX: take into account nested multiline /* /* */ */ comments 31if pline =~# '\*/\s*$' 32while getline ( plnum ) !~# '/\*' && plnum > 1 33let plnum -= 1 34endwhile 35if getline ( plnum ) =~# '^\s*/\*' 36let plnum -= 1 37else 38break 39endif 40elseif pline =~# '^\s*//' 41let plnum -= 1 42else 43break 44endif 45endwhile 46return plnum 47endfunction 48 49function s: GetOdinIndent ( lnum ) abort 50let plnum = s: PrevLine ( a: lnum ) 51let pline = getline ( plnum ) 52let pindent = indent ( plnum ) 53" workaround of cindent "hang" 54" if the previous line looks like: 55" : #{} 56" : #whatever{whateverelse} 57" and variations where : # { } are in the string 58" cindent(lnum) hangs 59if pline =~# ':\s\+#.*{.*}' 60return pindent 61endif 62 63let indent = cindent ( a: lnum ) 64let line = getline ( a: lnum ) 65 66if line =~# '^\s*#\k\+' 67if pline =~# '[{:]\s*$' 68let indent = pindent + shiftwidth () 69else 70let indent = pindent 71endif 72elseif pline =~# 'switch\s.*{\s*$' 73let indent = pindent 74elseif pline =~# 'case\s*.*,\s*\(//.*\)\?$' " https://github.com/habamax/vim-odin/issues/8 75let indent = pindent + matchstr ( pline , 'case\s*' ) ->strcharlen () 76elseif line =~# '^\s*case\s\+.*,\s*$' 77let indent = pindent - shiftwidth () 78elseif pline =~# 'case\s*.*:\s*\(//.*\)\?$' 79if line !~# '^\s*}\s*$' && line !~# '^\s*case[[:space:]:]' 80let indent = pindent + shiftwidth () 81endif 82elseif pline =~# '^\s*@.*' && line !~# '^\s*}' 83let indent = pindent 84elseif pline =~# ':[:=].*}\s*$' 85let indent = pindent 86elseif pline =~# '^\s*}\s*$' 87if line !~# '^\s*}' && line !~# 'case\s*.*:\s*$' 88let indent = pindent 89else 90let indent = pindent - shiftwidth () 91endif 92elseif pline =~# '\S:\s*$' 93" looking up for a case something, 94" whatever, 95" anything: 96" ... 20 lines before 97for idx in range ( plnum - 1 , plnum - 21 , -1 ) 98if plnum < 1 99break 100endif 101if getline ( idx ) =~# '^\s*case\s.*,\s*$' 102let indent = indent ( idx ) + shiftwidth () 103break 104endif 105endfor 106elseif pline =~# '{[^{]*}\s*$' && line !~# '^\s*[})]\s*$' " https://github.com/habamax/vim-odin/issues/2 107let indent = pindent 108elseif pline =~# '^\s*}\s*$' " https://github.com/habamax/vim-odin/issues/3 109" Find line with opening { and check if there is a label: 110" If there is, return indent of the closing } 111call cursor ( plnum , 1 ) 112silent normal ! % 113let brlnum = line ( '.' ) 114let brline = getline ( '.' ) 115if plnum != brlnum && ( brline =~# '^\s*\k\+:\s\+for' || brline =~# '^\s*\k\+\s*:=' ) 116let indent = pindent 117endif 118endif 119 120return indent 121endfunction 122 123let & cpo = s: cpo_save 124unlet s: cpo_save