Bram Moolenaar | fb53927 | 2014-08-22 19:21:47 +0200 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: Go |
| 3 | " Maintainer: David Barnett (https://github.com/google/vim-ft-go) |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 4 | " Last Change: 2017 Jun 13 |
Bram Moolenaar | fb53927 | 2014-08-22 19:21:47 +0200 | [diff] [blame] | 5 | " |
| 6 | " TODO: |
| 7 | " - function invocations split across lines |
| 8 | " - general line splits (line ends in an operator) |
| 9 | |
| 10 | if exists('b:did_indent') |
| 11 | finish |
| 12 | endif |
| 13 | let b:did_indent = 1 |
| 14 | |
| 15 | " C indentation is too far off useful, mainly due to Go's := operator. |
| 16 | " Let's just define our own. |
| 17 | setlocal nolisp |
| 18 | setlocal autoindent |
| 19 | setlocal indentexpr=GoIndent(v:lnum) |
| 20 | setlocal indentkeys+=<:>,0=},0=) |
| 21 | |
| 22 | if exists('*GoIndent') |
| 23 | finish |
| 24 | endif |
| 25 | |
Bram Moolenaar | fb53927 | 2014-08-22 19:21:47 +0200 | [diff] [blame] | 26 | function! GoIndent(lnum) |
| 27 | let l:prevlnum = prevnonblank(a:lnum-1) |
| 28 | if l:prevlnum == 0 |
| 29 | " top of file |
| 30 | return 0 |
| 31 | endif |
| 32 | |
| 33 | " grab the previous and current line, stripping comments. |
| 34 | let l:prevl = substitute(getline(l:prevlnum), '//.*$', '', '') |
| 35 | let l:thisl = substitute(getline(a:lnum), '//.*$', '', '') |
| 36 | let l:previ = indent(l:prevlnum) |
| 37 | |
| 38 | let l:ind = l:previ |
| 39 | |
| 40 | if l:prevl =~ '[({]\s*$' |
| 41 | " previous line opened a block |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 42 | let l:ind += shiftwidth() |
Bram Moolenaar | fb53927 | 2014-08-22 19:21:47 +0200 | [diff] [blame] | 43 | endif |
| 44 | if l:prevl =~# '^\s*\(case .*\|default\):$' |
| 45 | " previous line is part of a switch statement |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 46 | let l:ind += shiftwidth() |
Bram Moolenaar | fb53927 | 2014-08-22 19:21:47 +0200 | [diff] [blame] | 47 | endif |
| 48 | " TODO: handle if the previous line is a label. |
| 49 | |
| 50 | if l:thisl =~ '^\s*[)}]' |
| 51 | " this line closed a block |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 52 | let l:ind -= shiftwidth() |
Bram Moolenaar | fb53927 | 2014-08-22 19:21:47 +0200 | [diff] [blame] | 53 | endif |
| 54 | |
| 55 | " Colons are tricky. |
| 56 | " We want to outdent if it's part of a switch ("case foo:" or "default:"). |
| 57 | " We ignore trying to deal with jump labels because (a) they're rare, and |
| 58 | " (b) they're hard to disambiguate from a composite literal key. |
| 59 | if l:thisl =~# '^\s*\(case .*\|default\):$' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 60 | let l:ind -= shiftwidth() |
Bram Moolenaar | fb53927 | 2014-08-22 19:21:47 +0200 | [diff] [blame] | 61 | endif |
| 62 | |
| 63 | return l:ind |
| 64 | endfunction |
| 65 | |
| 66 | " vim: sw=2 sts=2 et |