github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/misc/vim/indent/go.vim (about) 1 " Copyright 2011 The Go Authors. All rights reserved. 2 " Use of this source code is governed by a BSD-style 3 " license that can be found in the LICENSE file. 4 " 5 " indent/go.vim: Vim indent file for Go. 6 " 7 " TODO: 8 " - function invocations split across lines 9 " - general line splits (line ends in an operator) 10 11 if exists("b:did_indent") 12 finish 13 endif 14 let b:did_indent = 1 15 16 " C indentation is too far off useful, mainly due to Go's := operator. 17 " Let's just define our own. 18 setlocal nolisp 19 setlocal autoindent 20 setlocal indentexpr=GoIndent(v:lnum) 21 setlocal indentkeys+=<:>,0=},0=) 22 23 if exists("*GoIndent") 24 finish 25 endif 26 27 function! GoIndent(lnum) 28 let prevlnum = prevnonblank(a:lnum-1) 29 if prevlnum == 0 30 " top of file 31 return 0 32 endif 33 34 " grab the previous and current line, stripping comments. 35 let prevl = substitute(getline(prevlnum), '//.*$', '', '') 36 let thisl = substitute(getline(a:lnum), '//.*$', '', '') 37 let previ = indent(prevlnum) 38 39 let ind = previ 40 41 if prevl =~ '[({]\s*$' 42 " previous line opened a block 43 let ind += &sw 44 endif 45 if prevl =~# '^\s*\(case .*\|default\):$' 46 " previous line is part of a switch statement 47 let ind += &sw 48 endif 49 " TODO: handle if the previous line is a label. 50 51 if thisl =~ '^\s*[)}]' 52 " this line closed a block 53 let ind -= &sw 54 endif 55 56 " Colons are tricky. 57 " We want to outdent if it's part of a switch ("case foo:" or "default:"). 58 " We ignore trying to deal with jump labels because (a) they're rare, and 59 " (b) they're hard to disambiguate from a composite literal key. 60 if thisl =~# '^\s*\(case .*\|default\):$' 61 let ind -= &sw 62 endif 63 64 return ind 65 endfunction