You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.6 KiB

  1. " Copyright 2011 The Go Authors. All rights reserved.
  2. " Use of this source code is governed by a BSD-style
  3. " license that can be found in the LICENSE file.
  4. "
  5. " indent/go.vim: Vim indent file for Go.
  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. if exists("*GoIndent")
  21. finish
  22. endif
  23. function! GoIndent(lnum)
  24. let prevlnum = prevnonblank(a:lnum-1)
  25. if prevlnum == 0
  26. " top of file
  27. return 0
  28. endif
  29. " grab the previous and current line, stripping comments.
  30. let prevl = substitute(getline(prevlnum), '//.*$', '', '')
  31. let thisl = substitute(getline(a:lnum), '//.*$', '', '')
  32. let previ = indent(prevlnum)
  33. let ind = previ
  34. if prevl =~ '[({]\s*$'
  35. " previous line opened a block
  36. let ind += &sw
  37. endif
  38. if prevl =~# '^\s*\(case .*\|default\):$'
  39. " previous line is part of a switch statement
  40. let ind += &sw
  41. endif
  42. " TODO: handle if the previous line is a label.
  43. if thisl =~ '^\s*[)}]'
  44. " this line closed a block
  45. let ind -= &sw
  46. endif
  47. " Colons are tricky.
  48. " We want to outdent if it's part of a switch ("case foo:" or "default:").
  49. " We ignore trying to deal with jump labels because (a) they're rare, and
  50. " (b) they're hard to disambiguate from a composite literal key.
  51. if thisl =~# '^\s*\(case .*\|default\):$'
  52. let ind -= &sw
  53. endif
  54. return ind
  55. endfunction