vim

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

cabal.vim (2530B)


      1 " Vim indent file
      2 " Language: Haskell Cabal Build file
      3 " Maintainer: Mateo Gjika <@mateoxh>
      4 
      5 if exists('b:did_indent')
      6   finish
      7 endif
      8 
      9 let b:did_indent = 1
     10 
     11 setlocal indentexpr=GetCabalIndent()
     12 setlocal indentkeys=!^F,o,O,e,0=elif,<:>
     13 
     14 let b:undo_indent = 'setlocal inde< indk<'
     15 
     16 let s:save_cpo = &cpo
     17 set cpo&vim
     18 
     19 function! GetCabalIndent() abort
     20   let categories = '\v\c^<(executable|(foreign-)?library|flag|source-repository|test-suite|benchmark|common|custom-setup)>'
     21   let line = getline(v:lnum)
     22   let prevline = getline(v:lnum - 1)
     23 
     24   if line =~# categories
     25     return 0
     26   endif
     27 
     28   if line =~# '^\s*--'
     29     return -1
     30   endif
     31 
     32   if line =~# '^\s*}'
     33     let [lnum, col] = s:searchpairpos('{','','}','bnW')
     34     if [lnum, col] == [0, 0]
     35       return -1
     36     else
     37       return indent(lnum)
     38     endif
     39   endif
     40 
     41   if line =~# '^\s*||'
     42     if prevline =~# '^\s*||'
     43       return indent(v:lnum - 1)
     44     else
     45       return indent(v:lnum - 1) + 1
     46     endif
     47   endif
     48 
     49   if line =~# '\v^\s*<elif>'
     50     let [lnum, col] = s:searchpairpos('\v<if>', '', '\v<elif>\zs', 'bnW')
     51     return col - 1
     52   elseif line =~# '\v^\s*<else>'
     53     let [lnum, col] = s:searchpairpos('\v<if>', '\v<elif>', '\v<else>\zs', 'bnW')
     54     return col - 1
     55   endif
     56 
     57   if prevline =~# '\v^\s*<(if|elif|else)>'
     58     return indent(v:lnum - 1) + shiftwidth()
     59   endif
     60 
     61   if prevline =~# categories
     62     return indent(v:lnum - 1) + shiftwidth()
     63   endif
     64 
     65   if empty(prevline) || line =~# '\v^\s*\S+:'
     66     call cursor(v:lnum, 1)
     67     let prevCond = search('\v^\s*<(if|elif|else)>', 'bnW', 0, 0,
     68           \ "synIDattr(synID(line('.'),col('.'),1),'name') =~? 'comment'")
     69     let prevCat = search(categories, 'bnW', 0, 0,
     70           \ "synIDattr(synID(line('.'),col('.'),1),'name') =~? 'comment'")
     71     if prevCond > prevCat
     72       return indent(v:lnum) > indent(prevCond)
     73             \ ? indent(prevCond) + shiftwidth()
     74             \ : indent(prevCond)
     75     elseif prevCat > 0
     76       return indent(prevCat) + shiftwidth()
     77     else
     78       return 0
     79     endif
     80   endif
     81 
     82   if line !~# '\v^\s*(<if>|--)'
     83     if prevline =~# '\v^\s*\S+:$'
     84       return indent(v:lnum - 1) + shiftwidth()
     85     endif
     86     if prevline =~# '\v^\s*\S+:\s*\S'
     87       return match(prevline, '\v^\s*\S+:\s*\zs')
     88     endif
     89   endif
     90 
     91   return indent(prevnonblank(v:lnum - 1))
     92 endfunction
     93 
     94 function! s:searchpairpos(start, middle, end, flags) abort
     95   return searchpairpos(a:start, a:middle, a:end, a:flags,
     96         \ "synIDattr(synID(line('.'),col('.'),1),'name') =~? 'comment'")
     97 endfunction
     98 
     99 let &cpo = s:save_cpo
    100 unlet s:save_cpo