diff --git a/rc/filetype/zig.kak b/rc/filetype/zig.kak new file mode 100644 index 00000000..602c7dab --- /dev/null +++ b/rc/filetype/zig.kak @@ -0,0 +1,135 @@ +# zig syntax highlighting for kakoune (https://ziglang.org) +# +# based off of https://github.com/ziglang/zig.vim/blob/master/syntax/zig.vim +# as well as https://ziglang.org/documentation/master/#Grammar + +# Detection +# ‾‾‾‾‾‾‾‾‾ + +hook global BufCreate .*[.]zig %{ + set-option buffer filetype zig +} + +# Initialization +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ + +hook global WinSetOption filetype=zig %< + require-module 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 %{ + require-module zig + add-highlighter window/zig ref zig + hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/zig } +} + +provide-module zig %§ + +# Highlighters +# ‾‾‾‾‾‾‾‾‾‾‾‾ + +add-highlighter shared/zig regions +add-highlighter shared/zig/code default-region group + +add-highlighter shared/zig/doc_comment region '///[^/]' '$' fill documentation +add-highlighter shared/zig/comment region '//' '$' fill comment + +# TODO: highlight escape sequences within strings +add-highlighter shared/zig/string_double region '"' (?|&|\||\^|~|\?|!)' 0:operator + +# builtin functions +add-highlighter shared/zig/code/ regex "@(?:addWithOverflow|ArgType|atomicLoad|atomicStore|bitCast|breakpoint)\b" 0:function +add-highlighter shared/zig/code/ regex "@(?:alignCast|alignOf|cDefine|cImport|cInclude)\b" 0:function +add-highlighter shared/zig/code/ regex "@(?:cUndef|canImplicitCast|clz|cmpxchgWeak|cmpxchgStrong|compileError)\b" 0:function +add-highlighter shared/zig/code/ regex "@(?:compileLog|ctz|popCount|divExact|divFloor|divTrunc)\b" 0:function +add-highlighter shared/zig/code/ regex "@(?:embedFile|export|tagName|TagType|errorName|call)\b" 0:function +add-highlighter shared/zig/code/ regex "@(?:errorReturnTrace|fence|fieldParentPtr|field|unionInit)\b" 0:function +add-highlighter shared/zig/code/ regex "@(?:frameAddress|import|newStackCall|asyncCall|intToPtr|IntType)\b" 0:function +add-highlighter shared/zig/code/ regex "@(?:memberCount|memberName|memberType|as)\b" 0:function +add-highlighter shared/zig/code/ regex "@(?:memcpy|memset|mod|mulWithOverflow|splat)\b" 0:function +add-highlighter shared/zig/code/ regex "@(?:bitOffsetOf|byteOffsetOf|OpaqueType|panic|ptrCast)\b" 0:function +add-highlighter shared/zig/code/ regex "@(?:ptrToInt|rem|returnAddress|setCold|Type|shuffle)\b" 0:function +add-highlighter shared/zig/code/ regex "@(?:setRuntimeSafety|setEvalBranchQuota|setFloatMode)\b" 0:function +add-highlighter shared/zig/code/ regex "@(?:setGlobalLinkage|setGlobalSection|shlExact|This|hasDecl|hasField)\b" 0:function +add-highlighter shared/zig/code/ regex "@(?:shlWithOverflow|shrExact|sizeOf|bitSizeOf|sqrt|byteSwap|subWithOverflow|intCast|floatCast|intToFloat|floatToInt|boolToInt|errSetCast)\b" 0:function +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 + +# Commands +# ‾‾‾‾‾‾‾‾ + +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 %< + try %< + # copy // or /// comments prefix and following whitespace + execute-keys -draft k s ^\h*\K///?\h* y gh j P + # preserve indent level + try %< execute-keys -draft K > + > 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 > +> + +§