# 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-insert zig-insert-on-new-line 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 # strings and characters add-highlighter shared/zig/string region '"' (?|&|\||\^|~|\?|!)' 0:operator # builtin functions add-highlighter shared/zig/code/ regex "@(?:align|addWithOverflow|as|atomicLoad|atomicStore|bitCast|breakpoint|trap|alignCast|alignOf|cDefine|cImport|cInclude|cUndef|clz|cmpxchgWeak|cmpxchgStrong|compileError|compileLog|constCast|ctz|popCount|divExact|divFloor|divTrunc|embedFile|export|extern|tagName|TagType|errorName|call|errorReturnTrace|fence|fieldParentPtr|field|unionInit|frameAddress|import|inComptime|newStackCall|asyncCall|ptrFromInt|max|min|memcpy|memset|mod|mulAdd|mulWithOverflow|splat|src|bitOffsetOf|byteOffsetOf|offsetOf|OpaqueType|panic|prefetch|ptrCast|intFromPtr|rem|returnAddress|setCold|Type|shuffle|reduce|select|setRuntimeSafety|setEvalBranchQuota|setFloatMode|shlExact|This|hasDecl|hasField|shlWithOverflow|shrExact|sizeOf|bitSizeOf|sqrt|byteSwap|subWithOverflow|intCast|floatCast|floatFromInt|intFromFloat|intFromBool|errSetCast|truncate|typeInfo|typeName|TypeOf|atomicRmw|errorFromInt|intFromError|enumFromInt|intFromEnum|setAlignStack|frame|Frame|frameSize|bitReverse|Vector|volatileCast|sin|cos|tan|exp|exp2|log|log2|log10|fabs|floor|ceil|trunc|wasmMemorySize|wasmMemoryGrow|round)\b" 0:meta # Commands # ‾‾‾‾‾‾‾‾ define-command -hidden zig-trim-indent %{ # delete trailing whitespace try %{ execute-keys -draft -itersel x s \h+$ d } } define-command -hidden zig-insert-on-new-line %< try %[ evaluate-commands -draft -save-regs '/"' %[ # copy // or /// comments prefix or \\ string literal prefix and following whitespace execute-keys -save-regs '' k x1s^\h*((///?|\\\\)+\h*) y try %[ # if the previous comment isn't empty, create a new one execute-keys x^\h*//+\h*$ jxs^\h*P ] catch %[ # if there is no text in the previous comment, remove it completely execute-keys d ] ] # trim trailing whitespace on the previous line try %[ execute-keys -draft k x s\h+$ d ] ] > define-command -hidden zig-indent-on-new-line %< evaluate-commands -draft -itersel %< # preserve indent level try %< execute-keys -draft K > try %< # only if we didn't copy a comment or multiline string execute-keys -draft x ^\h*(//|\\\\) # indent after lines ending in { try %< execute-keys -draft k x \{\h*$ j > # deindent closing } when after cursor try %< execute-keys -draft x ^\h*\} gh / \} m 1 > > # 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 > > §