vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

odin.vim (4070B)


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