Bram Moolenaar | 86b4816 | 2022-12-06 18:20:10 +0000 | [diff] [blame] | 1 | " 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. |
| 6 | if exists("b:did_indent") |
| 7 | finish |
| 8 | endif |
| 9 | let b:did_indent = 1 |
| 10 | |
| 11 | if (!has("cindent") || !has("eval")) |
| 12 | finish |
| 13 | endif |
| 14 | |
| 15 | setlocal 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 `(` |
| 24 | setlocal 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 |
| 34 | setlocal cinkeys=0{,0},0),0],!^F,o,O |
| 35 | |
| 36 | setlocal indentexpr=GetZigIndent(v:lnum) |
| 37 | |
| 38 | let b:undo_indent = "setlocal cindent< cinkeys< cinoptions< indentexpr<" |
| 39 | |
| 40 | function! 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) |
| 80 | endfunction |