1
0
Fork 0
mirror of synced 2024-11-29 20:25:34 -05:00
ultimate-vim/sources_non_forked/vim-go/indent/go.vim

79 lines
1.8 KiB
VimL
Raw Normal View History

2014-10-31 17:30:24 -04:00
" Copyright 2011 The Go Authors. All rights reserved.
" Use of this source code is governed by a BSD-style
" license that can be found in the LICENSE file.
"
" indent/go.vim: Vim indent file for Go.
"
" TODO:
" - function invocations split across lines
" - general line splits (line ends in an operator)
if exists("b:did_indent")
2016-06-26 07:12:36 -04:00
finish
2014-10-31 17:30:24 -04:00
endif
let b:did_indent = 1
" C indentation is too far off useful, mainly due to Go's := operator.
" Let's just define our own.
setlocal nolisp
setlocal autoindent
setlocal indentexpr=GoIndent(v:lnum)
setlocal indentkeys+=<:>,0=},0=)
if exists("*GoIndent")
2016-06-26 07:12:36 -04:00
finish
2015-02-24 05:45:22 -05:00
endif
" use shiftwidth function only if it's available
if exists('*shiftwidth')
2016-06-26 07:12:36 -04:00
func s:sw()
return shiftwidth()
endfunc
2015-02-24 05:45:22 -05:00
else
2016-06-26 07:12:36 -04:00
func s:sw()
return &sw
endfunc
2014-10-31 17:30:24 -04:00
endif
function! GoIndent(lnum)
2016-06-26 07:12:36 -04:00
let prevlnum = prevnonblank(a:lnum-1)
if prevlnum == 0
" top of file
return 0
endif
2014-10-31 17:30:24 -04:00
2016-06-26 07:12:36 -04:00
" grab the previous and current line, stripping comments.
let prevl = substitute(getline(prevlnum), '//.*$', '', '')
let thisl = substitute(getline(a:lnum), '//.*$', '', '')
let previ = indent(prevlnum)
2014-10-31 17:30:24 -04:00
2016-06-26 07:12:36 -04:00
let ind = previ
2014-10-31 17:30:24 -04:00
2016-06-26 07:12:36 -04:00
if prevl =~ '[({]\s*$'
" previous line opened a block
let ind += s:sw()
endif
if prevl =~# '^\s*\(case .*\|default\):$'
" previous line is part of a switch statement
let ind += s:sw()
endif
" TODO: handle if the previous line is a label.
2014-10-31 17:30:24 -04:00
2016-06-26 07:12:36 -04:00
if thisl =~ '^\s*[)}]'
" this line closed a block
let ind -= s:sw()
endif
2014-10-31 17:30:24 -04:00
2016-06-26 07:12:36 -04:00
" Colons are tricky.
" We want to outdent if it's part of a switch ("case foo:" or "default:").
" We ignore trying to deal with jump labels because (a) they're rare, and
" (b) they're hard to disambiguate from a composite literal key.
if thisl =~# '^\s*\(case .*\|default\):$'
let ind -= s:sw()
endif
2014-10-31 17:30:24 -04:00
2016-06-26 07:12:36 -04:00
return ind
2014-10-31 17:30:24 -04:00
endfunction
2015-02-24 05:45:22 -05:00
2016-06-26 07:12:36 -04:00
" vim: sw=2 ts=2 et