go.vim: set 100-line limit
[cmccabe-etc] / .vim / indent / go.vim
1 " Vim indent file
2 " Language:     Go
3 " Author:       Alecs King <alecsk@gmail.com>
4 "
5 " inspired by indent/lua.vim
6 "
7 " very simple:
8 " just indent common cases to avoid manually typing tab or backspace
9 "
10 " for better style, please use gofmt after done editing.
11 "
12 " since it just simply uses regex matches,
13 " there might be some mis-indented corner cases.
14
15
16 " Only load this indent file when no other was loaded.
17 if exists("b:did_indent")
18   finish
19 endif
20 let b:did_indent = 1
21
22 " 4-space hard tabs is the preferred style for Go
23 set sw=4
24 set ts=4
25 set noet
26 set tw=100
27
28 setlocal indentexpr=GetGoIndent()
29
30 " To make Vim call GetLuaIndent() when it finds '\s*)', '\s*}', '\s*case', '\s*default'
31 setlocal indentkeys+=0=),0=},0=case,0=default
32
33 setlocal autoindent
34
35 " Only define the function once.
36 if exists("*GetGoIndent")
37   finish
38 endif
39
40 function! GetGoIndent()
41   " Find a non-blank line above the current line.
42   let prevlnum = prevnonblank(v:lnum - 1)
43
44   " Hit the start of the file, use zero indent.
45   if prevlnum == 0
46     return 0
47   endif
48
49   " Add a 'shiftwidth' after lines that start a block:
50   " 'case', 'default', '{', '('
51   let ind = indent(prevlnum)
52   let prevline = getline(prevlnum)
53   let midx = match(prevline, '^\s*\%(case\>\|default\>\)')
54   if midx == -1
55     let midx = match(prevline, '[({]\s*$')
56   endif
57   if midx != -1
58     let ind = ind + &shiftwidth
59   endif
60
61   " Subtract a 'shiftwidth' on 'case', 'default', '}', ')'.
62   " This is the part that requires 'indentkeys'.
63   let midx = match(getline(v:lnum), '^\s*\%(case\>\|default\>\|[)}]\)')
64   if midx != -1
65     let ind = ind - &shiftwidth
66   endif
67
68   return ind
69 endfunction