go.vim 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. " Vim indent file
  2. " Language: Go
  3. " Author: Alecs King <alecsk@gmail.com>
  4. "
  5. " inspired by indent/lua.vim
  6. "
  7. " very simple:
  8. " just indent common cases to avoid manually typing tab or backspace
  9. "
  10. " for better style, please use gofmt after done editing.
  11. "
  12. " since it just simply uses regex matches,
  13. " there might be some mis-indented corner cases.
  14. "
  15. " Only load this indent file when no other was loaded.
  16. if exists("b:did_indent")
  17. finish
  18. endif
  19. let b:did_indent = 1
  20. setlocal indentexpr=GetGoIndent()
  21. " To make Vim call GetLuaIndent() when it finds '\s*)', '\s*}', '\s*case', '\s*default'
  22. setlocal indentkeys+=0=),0=},0=case,0=default
  23. setlocal autoindent
  24. " Only define the function once.
  25. if exists("*GetGoIndent")
  26. finish
  27. endif
  28. function! GetGoIndent()
  29. " Find a non-blank line above the current line.
  30. let prevlnum = prevnonblank(v:lnum - 1)
  31. " Hit the start of the file, use zero indent.
  32. if prevlnum == 0
  33. return 0
  34. endif
  35. " Add a 'shiftwidth' after lines that start a block:
  36. " 'case', 'default', '{', '('
  37. let ind = indent(prevlnum)
  38. let prevline = getline(prevlnum)
  39. let midx = match(prevline, '^\s*\%(case\>\|default\>\)')
  40. if midx == -1
  41. let midx = match(prevline, '[({]\s*$')
  42. endif
  43. if midx != -1
  44. let ind = ind + &shiftwidth
  45. endif
  46. " Subtract a 'shiftwidth' on 'case', 'default', '}', ')'.
  47. " This is the part that requires 'indentkeys'.
  48. let midx = match(getline(v:lnum), '^\s*\%(case\>\|default\>\|[)}]\)')
  49. if midx != -1
  50. let ind = ind - &shiftwidth
  51. endif
  52. return ind
  53. endfunction