go.vim 1.6 KB

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