1" Vim indent file
2" Language:   llvm
3" Maintainer: The LLVM team, http://llvm.org/
4" What this indent plugin currently does:
5"  - If no other rule matches copy indent from previous non-empty,
6"    non-commented line
7"  - On '}' align the same as the line containing the matching '{'
8"  - If previous line ends with ':' increase indentation
9"  - If the current line ends with ':' indent at the same level as the
10"    enclosing '{'/'}' block
11" Stuff that would be nice to add:
12"  - Continue comments on next line
13"  - If there is an opening+unclosed parenthesis on previous line indent to that
14if exists("b:did_indent")
15  finish
16endif
17let b:did_indent = 1
18
19setlocal shiftwidth=2 expandtab
20
21setlocal indentkeys=0{,0},<:>,!^F,o,O,e
22setlocal indentexpr=GetLLVMIndent()
23
24if exists("*GetLLVMIndent")
25  finish
26endif
27
28function! FindOpenBrace(lnum)
29  call cursor(a:lnum, 1)
30  return searchpair('{', '', '}', 'bW')
31endfun
32
33function! GetLLVMIndent()
34  " On '}' align the same as the line containing the matching '{'
35  let thisline = getline(v:lnum)
36  if thisline =~ '^\s*}'
37    call cursor(v:lnum, 1)
38    silent normal %
39    let opening_lnum = line('.')
40    if opening_lnum != v:lnum
41      return indent(opening_lnum)
42    endif
43  endif
44
45  " Indent labels the same as the current opening block
46  if thisline =~ ':\s*$'
47    let blockbegin = FindOpenBrace(v:lnum)
48    if blockbegin > 0
49      return indent(blockbegin)
50    endif
51  endif
52
53  " Find a non-blank not-completely commented line above the current line.
54  let prev_lnum = prevnonblank(v:lnum - 1)
55  while prev_lnum > 0 && synIDattr(synID(prev_lnum, indent(prev_lnum)+1, 0), "name") =? "string\|comment"
56    let prev_lnum = prevnonblank(prev_lnum-1)
57  endwhile
58  " Hit the start of the file, use zero indent.
59  if prev_lnum == 0
60    return 0
61  endif
62
63  let ind = indent(prev_lnum)
64  let prevline = getline(prev_lnum)
65
66  " Add a 'shiftwidth' after lines that start a block or labels
67  if prevline =~ '{\s*$' || prevline =~ ':\s*$'
68    let ind = ind + &shiftwidth
69  endif
70
71  return ind
72endfunction
73