mirror of
https://github.com/golang/go
synced 2024-11-25 03:27:58 -07:00
misc/vim: new Vim indentation script.
This uses a fully custom function for indenting Go code in Vim. It provides a lot more flexibility than a cindent-based approach, so this version gets the := operator correct, as well as switch labels and jump labels. One outstanding TODO is to handle lines immediately after jump labels. R=adg, n13m3y3r, jnwhiteh, dchest, rsc, rlight2 CC=golang-dev, rivercheng https://golang.org/cl/4534047
This commit is contained in:
parent
092f34feca
commit
1a7d09c847
@ -4,27 +4,68 @@
|
|||||||
"
|
"
|
||||||
" indent/go.vim: Vim indent file for Go.
|
" 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")
|
if exists("b:did_indent")
|
||||||
finish
|
finish
|
||||||
endif
|
endif
|
||||||
let b:did_indent = 1
|
let b:did_indent = 1
|
||||||
|
|
||||||
" C indentation is mostly correct
|
" C indentation is too far off useful, mainly due to Go's := operator.
|
||||||
setlocal cindent
|
" Let's just define our own.
|
||||||
|
setlocal nolisp
|
||||||
|
setlocal autoindent
|
||||||
|
setlocal indentexpr=GoIndent(v:lnum)
|
||||||
|
setlocal indentkeys+=<:>,0=},0=)
|
||||||
|
|
||||||
" Options set:
|
if exists("*GoIndent")
|
||||||
" +0 -- Don't indent continuation lines (because Go doesn't use semicolons
|
finish
|
||||||
" much)
|
endif
|
||||||
" L0 -- Don't move jump labels (NOTE: this isn't correct when working with
|
|
||||||
" gofmt, but it does keep struct literals properly indented.)
|
function! GoIndent(lnum)
|
||||||
" :0 -- Align case labels with switch statement
|
let prevlnum = prevnonblank(a:lnum-1)
|
||||||
" l1 -- Always align case body relative to case labels
|
if prevlnum == 0
|
||||||
" J1 -- Indent JSON-style objects (properly indents struct-literals)
|
" top of file
|
||||||
" (0, Ws -- Indent lines inside of unclosed parentheses by one shiftwidth
|
return 0
|
||||||
" m1 -- Align closing parenthesis line with first non-blank of matching
|
endif
|
||||||
" parenthesis line
|
|
||||||
"
|
" grab the previous and current line, stripping comments.
|
||||||
" Known issue: Trying to do a multi-line struct literal in a short variable
|
let prevl = substitute(getline(prevlnum), '//.*$', '', '')
|
||||||
" declaration will not indent properly.
|
let thisl = substitute(getline(a:lnum), '//.*$', '', '')
|
||||||
setlocal cinoptions+=+0,L0,:0,l1,J1,(0,Ws,m1
|
let previ = indent(prevlnum)
|
||||||
|
|
||||||
|
let ind = previ
|
||||||
|
|
||||||
|
if prevl =~ '[({]\s*$'
|
||||||
|
" previous line opened a block
|
||||||
|
let ind += &sw
|
||||||
|
endif
|
||||||
|
if prevl =~# '^\s*\(case .*\|default\):$'
|
||||||
|
" previous line is part of a switch statement
|
||||||
|
let ind += &sw
|
||||||
|
endif
|
||||||
|
" TODO: handle if the previous line is a label.
|
||||||
|
|
||||||
|
if thisl =~ '^\s*[)}]'
|
||||||
|
" this line closed a block
|
||||||
|
let ind -= &sw
|
||||||
|
endif
|
||||||
|
|
||||||
|
" Colons are tricky.
|
||||||
|
" We want to outdent if it's part of a switch ("case foo:" or "default:"),
|
||||||
|
if thisl =~# '^\s*\(case .*\|default\):$'
|
||||||
|
let ind -= &sw
|
||||||
|
endif
|
||||||
|
" ... and put jump labels in the first column (ignore "default:").
|
||||||
|
if thisl =~ '^\s*\S\+:\s*$'
|
||||||
|
" ignore "default:" and if there's a string on the line;
|
||||||
|
" the latter will more likely be something like "blah: %v".
|
||||||
|
if thisl !~# '^\s*default:\s*$' && thisl !~# '".*:'
|
||||||
|
return 0
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
return ind
|
||||||
|
endfunction
|
||||||
|
Loading…
Reference in New Issue
Block a user