Yinzuo Jiang | 011f222 | 2024-07-29 20:51:05 +0200 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: Apache Thrift |
| 3 | " Maintainer: Yinzuo Jiang <jiangyinzuo@foxmail.com> |
| 4 | " Last Change: 2024/07/29 |
| 5 | |
| 6 | " Only load this indent file when no other was loaded. |
| 7 | if exists("b:did_indent") |
| 8 | finish |
| 9 | endif |
| 10 | let b:did_indent = 1 |
| 11 | |
| 12 | setlocal cindent |
| 13 | setlocal indentexpr=GetThriftIndent() |
| 14 | |
| 15 | let b:undo_indent = "set cindent< indentexpr<" |
| 16 | |
| 17 | " Only define the function once. |
| 18 | if exists("*GetThriftIndent") |
| 19 | finish |
| 20 | endif |
| 21 | |
| 22 | let s:keepcpo= &cpo |
| 23 | set cpo&vim |
| 24 | |
| 25 | function! SkipThriftBlanksAndComments(startline) |
| 26 | let lnum = a:startline |
| 27 | while lnum > 1 |
| 28 | let lnum = prevnonblank(lnum) |
| 29 | if getline(lnum) =~ '\*/\s*$' |
| 30 | while getline(lnum) !~ '/\*' && lnum > 1 |
| 31 | let lnum = lnum - 1 |
| 32 | endwhile |
| 33 | if getline(lnum) =~ '^\s*/\*' |
| 34 | let lnum = lnum - 1 |
| 35 | else |
| 36 | break |
| 37 | endif |
| 38 | elseif getline(lnum) =~ '^\s*\(//\|#\)' |
| 39 | let lnum = lnum - 1 |
| 40 | else |
| 41 | break |
| 42 | endif |
| 43 | endwhile |
| 44 | return lnum |
| 45 | endfunction |
| 46 | |
| 47 | function GetThriftIndent() |
| 48 | " Thrift is just like C; use the built-in C indenting and then correct a few |
| 49 | " specific cases. |
| 50 | let theIndent = cindent(v:lnum) |
| 51 | |
| 52 | " If we're in the middle of a comment then just trust cindent |
| 53 | if getline(v:lnum) =~ '^\s*\*' |
| 54 | return theIndent |
| 55 | endif |
| 56 | |
| 57 | let line = substitute(getline(v:lnum), '\(//\|#\).*$', '', '') |
| 58 | let previousNum = SkipThriftBlanksAndComments(v:lnum - 1) |
| 59 | let previous = substitute(getline(previousNum), '\(//\|#\).*$', '', '') |
| 60 | |
| 61 | let l:indent = indent(previousNum) |
| 62 | if previous =~ "{" && previous !~ "}" |
| 63 | let l:indent += shiftwidth() |
| 64 | endif |
| 65 | if line =~ "}" && line !~ "{" |
| 66 | let l:indent -= shiftwidth() |
| 67 | endif |
| 68 | return l:indent |
| 69 | endfunction |
| 70 | |
| 71 | let &cpo = s:keepcpo |
| 72 | unlet s:keepcpo |
| 73 | |
| 74 | " vim: sw=2 sts=2 et |