blob: e0860e12a2bdb6865faf6f844dd0173b2fcb2d6b [file] [log] [blame]
Yinzuo Jiang011f2222024-07-29 20:51:05 +02001" 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.
7if exists("b:did_indent")
8 finish
9endif
10let b:did_indent = 1
11
12setlocal cindent
13setlocal indentexpr=GetThriftIndent()
14
15let b:undo_indent = "set cindent< indentexpr<"
16
17" Only define the function once.
18if exists("*GetThriftIndent")
19 finish
20endif
21
22let s:keepcpo= &cpo
23set cpo&vim
24
25function! 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
45endfunction
46
47function 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
69endfunction
70
71let &cpo = s:keepcpo
72unlet s:keepcpo
73
74" vim: sw=2 sts=2 et