zig.vim 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. " Vim filetype indent file
  2. " Language: Zig
  3. " Upstream: https://github.com/ziglang/zig.vim
  4. " Only load this indent file when no other was loaded.
  5. if exists("b:did_indent")
  6. finish
  7. endif
  8. let b:did_indent = 1
  9. if (!has("cindent") || !has("eval"))
  10. finish
  11. endif
  12. setlocal cindent
  13. " L0 -> 0 indent for jump labels (i.e. case statement in c).
  14. " j1 -> indenting for "javascript object declarations"
  15. " J1 -> see j1
  16. " w1 -> starting a new line with `(` at the same indent as `(`
  17. " m1 -> if `)` starts a line, match its indent with the first char of its
  18. " matching `(` line
  19. " (s -> use one indent, when starting a new line after a trailing `(`
  20. setlocal cinoptions=L0,m1,(s,j1,J1,l1
  21. " cinkeys: controls what keys trigger indent formatting
  22. " 0{ -> {
  23. " 0} -> }
  24. " 0) -> )
  25. " 0] -> ]
  26. " !^F -> make CTRL-F (^F) reindent the current line when typed
  27. " o -> when <CR> or `o` is used
  28. " O -> when the `O` command is used
  29. setlocal cinkeys=0{,0},0),0],!^F,o,O
  30. setlocal indentexpr=GetZigIndent(v:lnum)
  31. let b:undo_indent = "setlocal cindent< cinkeys< cinoptions< indentexpr<"
  32. function! GetZigIndent(lnum)
  33. let curretLineNum = a:lnum
  34. let currentLine = getline(a:lnum)
  35. " cindent doesn't handle multi-line strings properly, so force no indent
  36. if currentLine =~ '^\s*\\\\.*'
  37. return -1
  38. endif
  39. let prevLineNum = prevnonblank(a:lnum-1)
  40. let prevLine = getline(prevLineNum)
  41. " for lines that look like
  42. " },
  43. " };
  44. " try treating them the same as a }
  45. if prevLine =~ '\v^\s*},$'
  46. if currentLine =~ '\v^\s*};$' || currentLine =~ '\v^\s*}$'
  47. return indent(prevLineNum) - 4
  48. endif
  49. return indent(prevLineNum-1) - 4
  50. endif
  51. if currentLine =~ '\v^\s*},$'
  52. return indent(prevLineNum) - 4
  53. endif
  54. if currentLine =~ '\v^\s*};$'
  55. return indent(prevLineNum) - 4
  56. endif
  57. " cindent doesn't handle this case correctly:
  58. " switch (1): {
  59. " 1 => true,
  60. " ~
  61. " ^---- indents to here
  62. if prevLine =~ '.*=>.*,$' && currentLine !~ '.*}$'
  63. return indent(prevLineNum)
  64. endif
  65. return cindent(a:lnum)
  66. endfunction