From 3098557e25fd5b089c2a4d2fa641a8b1117cf593 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Sun, 22 Mar 2020 19:18:15 +0100 Subject: [PATCH] Add indent rules --- rc/filetype/zig.kak | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) 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 > +>