diff --git a/rc/filetype/zig.kak b/rc/filetype/zig.kak index 8770e848..209f1449 100644 --- a/rc/filetype/zig.kak +++ b/rc/filetype/zig.kak @@ -7,6 +7,14 @@ hook global BufCreate .*[.]zig %{ set-option buffer filetype zig } +hook global WinSetOption filetype=zig %< + hook window ModeChange pop:insert:.* -group zig-trim-indent zig-trim-indent + hook window InsertChar \n -group zig-indent zig-indent-on-new-line + hook window InsertChar \} -group zig-indent zig-indent-on-closing + + hook -once -always window WinSetOption filetype=.* %< remove-hooks window zig-.+ > +> + hook -group zig-highlight global WinSetOption filetype=zig %{ add-highlighter window/zig ref zig hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/zig } @@ -79,3 +87,29 @@ add-highlighter shared/zig/code/ regex "@(?:shlWithOverflow|shrExact|sizeOf|bitS add-highlighter shared/zig/code/ regex "@(?:truncate|typeId|typeInfo|typeName|TypeOf|atomicRmw|bytesToSlice|sliceToBytes)\b" 0:function add-highlighter shared/zig/code/ regex "@(?:intToError|errorToInt|intToEnum|enumToInt|setAlignStack|frame|Frame|frameSize|bitReverse|Vector)\b" 0:function add-highlighter shared/zig/code/ regex "@(?:sin|cos|exp|exp2|log|log2|log10|fabs|floor|ceil|trunc|round)\b" 0:function + +define-command -hidden zig-trim-indent %{ + # delete trailing whitespace + try %{ execute-keys -draft -itersel s \h+$ d } +} + +define-command -hidden zig-indent-on-new-line %< + evaluate-commands -draft -itersel %< + # copy // or /// comments prefix and following white spaces + try %< + execute-keys -draft k s ^\h*\K///?\h* y gh j P + > catch %< + # preserve indent level + try %< execute-keys -draft K > + # indent after lines ending in { + try %< execute-keys -draft k \{\h*$ j > + > + # filter previous line + try %< execute-keys -draft k : zig-trim-indent > + > +> + +define-command -hidden zig-indent-on-closing %< + # align lone } to indent level of opening line + try %< execute-keys -draft -itersel ^\h*\}$ h m 1 > +>