Matthias Braun | 2718fb1 | 2015-04-21 01:35:42 +0000 | [diff] [blame] | 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 |
| 14 | if exists("b:did_indent") |
| 15 | finish |
| 16 | endif |
| 17 | let b:did_indent = 1 |
| 18 | |
| 19 | setlocal shiftwidth=2 expandtab |
| 20 | |
| 21 | setlocal indentkeys=0{,0},<:>,!^F,o,O,e |
| 22 | setlocal indentexpr=GetLLVMIndent() |
| 23 | |
| 24 | if exists("*GetLLVMIndent") |
| 25 | finish |
| 26 | endif |
| 27 | |
Matthias Braun | 19ffc26c | 2015-05-04 21:41:25 +0000 | [diff] [blame] | 28 | function! FindOpenBrace(lnum) |
Matthias Braun | 2718fb1 | 2015-04-21 01:35:42 +0000 | [diff] [blame] | 29 | call cursor(a:lnum, 1) |
| 30 | return searchpair('{', '', '}', 'bW') |
| 31 | endfun |
| 32 | |
| 33 | function! 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*$' |
Matthias Braun | 19ffc26c | 2015-05-04 21:41:25 +0000 | [diff] [blame] | 47 | let blockbegin = FindOpenBrace(v:lnum) |
Matthias Braun | 2718fb1 | 2015-04-21 01:35:42 +0000 | [diff] [blame] | 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) |
Matthias Braun | 19ffc26c | 2015-05-04 21:41:25 +0000 | [diff] [blame] | 55 | while prev_lnum > 0 && synIDattr(synID(prev_lnum, indent(prev_lnum)+1, 0), "name") =? "string\|comment" |
Matthias Braun | 2718fb1 | 2015-04-21 01:35:42 +0000 | [diff] [blame] | 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 |
| 72 | endfunction |