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