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