xref: /llvm-project/llvm/utils/vim/indent/llvm.vim (revision 19ffc26c5e375ae6682a3877a959d195dbcf7524)
12718fb1dSMatthias Braun" Vim indent file
22718fb1dSMatthias Braun" Language:   llvm
32718fb1dSMatthias Braun" Maintainer: The LLVM team, http://llvm.org/
42718fb1dSMatthias Braun" What this indent plugin currently does:
52718fb1dSMatthias Braun"  - If no other rule matches copy indent from previous non-empty,
62718fb1dSMatthias Braun"    non-commented line
72718fb1dSMatthias Braun"  - On '}' align the same as the line containing the matching '{'
82718fb1dSMatthias Braun"  - If previous line ends with ':' increase indentation
92718fb1dSMatthias Braun"  - If the current line ends with ':' indent at the same level as the
102718fb1dSMatthias Braun"    enclosing '{'/'}' block
112718fb1dSMatthias Braun" Stuff that would be nice to add:
122718fb1dSMatthias Braun"  - Continue comments on next line
132718fb1dSMatthias Braun"  - If there is an opening+unclosed parenthesis on previous line indent to that
142718fb1dSMatthias Braunif exists("b:did_indent")
152718fb1dSMatthias Braun  finish
162718fb1dSMatthias Braunendif
172718fb1dSMatthias Braunlet b:did_indent = 1
182718fb1dSMatthias Braun
192718fb1dSMatthias Braunsetlocal shiftwidth=2 expandtab
202718fb1dSMatthias Braun
212718fb1dSMatthias Braunsetlocal indentkeys=0{,0},<:>,!^F,o,O,e
222718fb1dSMatthias Braunsetlocal indentexpr=GetLLVMIndent()
232718fb1dSMatthias Braun
242718fb1dSMatthias Braunif exists("*GetLLVMIndent")
252718fb1dSMatthias Braun  finish
262718fb1dSMatthias Braunendif
272718fb1dSMatthias Braun
28*19ffc26cSMatthias Braunfunction! FindOpenBrace(lnum)
292718fb1dSMatthias Braun  call cursor(a:lnum, 1)
302718fb1dSMatthias Braun  return searchpair('{', '', '}', 'bW')
312718fb1dSMatthias Braunendfun
322718fb1dSMatthias Braun
332718fb1dSMatthias Braunfunction! GetLLVMIndent()
342718fb1dSMatthias Braun  " On '}' align the same as the line containing the matching '{'
352718fb1dSMatthias Braun  let thisline = getline(v:lnum)
362718fb1dSMatthias Braun  if thisline =~ '^\s*}'
372718fb1dSMatthias Braun    call cursor(v:lnum, 1)
382718fb1dSMatthias Braun    silent normal %
392718fb1dSMatthias Braun    let opening_lnum = line('.')
402718fb1dSMatthias Braun    if opening_lnum != v:lnum
412718fb1dSMatthias Braun      return indent(opening_lnum)
422718fb1dSMatthias Braun    endif
432718fb1dSMatthias Braun  endif
442718fb1dSMatthias Braun
452718fb1dSMatthias Braun  " Indent labels the same as the current opening block
462718fb1dSMatthias Braun  if thisline =~ ':\s*$'
47*19ffc26cSMatthias Braun    let blockbegin = FindOpenBrace(v:lnum)
482718fb1dSMatthias Braun    if blockbegin > 0
492718fb1dSMatthias Braun      return indent(blockbegin)
502718fb1dSMatthias Braun    endif
512718fb1dSMatthias Braun  endif
522718fb1dSMatthias Braun
532718fb1dSMatthias Braun  " Find a non-blank not-completely commented line above the current line.
542718fb1dSMatthias Braun  let prev_lnum = prevnonblank(v:lnum - 1)
55*19ffc26cSMatthias Braun  while prev_lnum > 0 && synIDattr(synID(prev_lnum, indent(prev_lnum)+1, 0), "name") =? "string\|comment"
562718fb1dSMatthias Braun    let prev_lnum = prevnonblank(prev_lnum-1)
572718fb1dSMatthias Braun  endwhile
582718fb1dSMatthias Braun  " Hit the start of the file, use zero indent.
592718fb1dSMatthias Braun  if prev_lnum == 0
602718fb1dSMatthias Braun    return 0
612718fb1dSMatthias Braun  endif
622718fb1dSMatthias Braun
632718fb1dSMatthias Braun  let ind = indent(prev_lnum)
642718fb1dSMatthias Braun  let prevline = getline(prev_lnum)
652718fb1dSMatthias Braun
662718fb1dSMatthias Braun  " Add a 'shiftwidth' after lines that start a block or labels
672718fb1dSMatthias Braun  if prevline =~ '{\s*$' || prevline =~ ':\s*$'
682718fb1dSMatthias Braun    let ind = ind + &shiftwidth
692718fb1dSMatthias Braun  endif
702718fb1dSMatthias Braun
712718fb1dSMatthias Braun  return ind
722718fb1dSMatthias Braunendfunction
73