diff options
Diffstat (limited to 'indent')
-rw-r--r-- | indent/vlang.vim | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/indent/vlang.vim b/indent/vlang.vim new file mode 100644 index 00000000..58b441f9 --- /dev/null +++ b/indent/vlang.vim @@ -0,0 +1,69 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'v') == -1 + +" Based on the Go identation file. +" +" 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. + +if exists("b:did_indent") + finish +endif +let b:did_indent = 1 + +setlocal nolisp +setlocal autoindent +setlocal indentexpr=VlangIndent(v:lnum) +setlocal indentkeys+=<:>,0=},0=) + +if exists("*VlangIndent") + finish +endif + +let s:cpo_save = &cpo +set cpo&vim + +function! VlangIndent(lnum) abort + let prevlnum = prevnonblank(a:lnum-1) + if prevlnum == 0 + return 0 + endif + + let prevl = substitute(getline(prevlnum), '//.*$', '', '') + let thisl = substitute(getline(a:lnum), '//.*$', '', '') + let previ = indent(prevlnum) + + let ind = previ + + if prevl =~ '[({]\s*$' + " previous line opened a block + let ind += shiftwidth() + endif + if prevl =~# '^\s*\(case .*\|default\):$' + " previous line is part of a switch statement + let ind += shiftwidth() + endif + + if thisl =~ '^\s*[)}]' + " this line closed a block + let ind -= shiftwidth() + endif + + " 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 -= shiftwidth() + endif + + return ind +endfunction + +" restore Vi compatibility settings +let &cpo = s:cpo_save +unlet s:cpo_save + +" vim: sw=2 ts=2 et + +endif |