robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
ae7ab90159
master
1" Vim indent file 2" Language: Go 3" Maintainer: David Barnett (https://github.com/google/vim-ft-go) 4" Last Change: 2017 Jun 13 5" 2023 Aug 28 by Vim Project (undo_indent) 6" 7" TODO: 8" - function invocations split across lines 9" - general line splits (line ends in an operator) 10 11if exists ( 'b:did_indent' ) 12finish 13endif 14let 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. 18setlocal nolisp 19setlocal autoindent 20setlocal indentexpr = GoIndent (v:lnum) 21setlocal indentkeys += <:>,0=},0=) 22 23let b: undo_indent = "setl ai< inde< indk< lisp<" 24 25if exists ( '*GoIndent' ) 26finish 27endif 28 29function ! GoIndent ( lnum ) 30let l: prevlnum = prevnonblank ( a: lnum - 1 ) 31if l: prevlnum == 0 32" top of file 33return 0 34endif 35 36" grab the previous and current line, stripping comments. 37let l: prevl = substitute ( getline ( l: prevlnum ), '//.*$' , '' , '' ) 38let l: thisl = substitute ( getline ( a: lnum ), '//.*$' , '' , '' ) 39let l: previ = indent ( l: prevlnum ) 40 41let l: ind = l: previ 42 43if l: prevl =~ '[({]\s*$' 44" previous line opened a block 45let l: ind += shiftwidth () 46endif 47if l: prevl =~# '^\s*\(case .*\|default\):$' 48" previous line is part of a switch statement 49let l: ind += shiftwidth () 50endif 51" TODO: handle if the previous line is a label. 52 53if l: thisl =~ '^\s*[)}]' 54" this line closed a block 55let l: ind -= shiftwidth () 56endif 57 58" Colons are tricky. 59" We want to outdent if it's part of a switch ("case foo:" or "default:"). 60" We ignore trying to deal with jump labels because (a) they're rare, and 61" (b) they're hard to disambiguate from a composite literal key. 62if l: thisl =~# '^\s*\(case .*\|default\):$' 63let l: ind -= shiftwidth () 64endif 65 66return l: ind 67endfunction 68 69" vim: sw=2 sts=2 et