go.vim 1.7 KB

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