kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

zig.kak (7004B)


      1 # zig syntax highlighting for kakoune (https://ziglang.org)
      2 #
      3 # based off of https://github.com/ziglang/zig.vim/blob/master/syntax/zig.vim
      4 # as well as https://ziglang.org/documentation/master/#Grammar
      5 
      6 # Detection
      7 # ‾‾‾‾‾‾‾‾‾
      8 
      9 hook global BufCreate .*[.](zig|zon) %{
     10   set-option buffer filetype zig
     11 }
     12 
     13 # Initialization
     14 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     15 
     16 hook global WinSetOption filetype=zig %<
     17     require-module zig
     18     hook window ModeChange pop:insert:.* -group zig-trim-indent zig-trim-indent
     19     hook window InsertChar \n -group zig-insert zig-insert-on-new-line
     20     hook window InsertChar \n -group zig-indent zig-indent-on-new-line
     21     hook window InsertChar \} -group zig-indent zig-indent-on-closing
     22 
     23     hook -once -always window WinSetOption filetype=.* %< remove-hooks window zig-.+ >
     24 >
     25 
     26 hook -group zig-highlight global WinSetOption filetype=zig %{
     27     require-module zig
     28     add-highlighter window/zig ref zig
     29     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/zig }
     30 }
     31 
     32 provide-module zig %§
     33 
     34 # Highlighters
     35 # ‾‾‾‾‾‾‾‾‾‾‾‾
     36 
     37 add-highlighter shared/zig regions
     38 add-highlighter shared/zig/code default-region group
     39 
     40 add-highlighter shared/zig/doc_comment region '///(?=[^/])' '$' fill documentation
     41 add-highlighter shared/zig/comment region '//' '$' fill comment
     42 
     43 # strings and characters
     44 add-highlighter shared/zig/string region '"' (?<!\\)(\\\\)*" group
     45 add-highlighter shared/zig/string/ fill string
     46 add-highlighter shared/zig/string/ regex '(?:\\n|\\r|\\t|\\\\|\\''|\\"|\\x[0-9a-fA-F]{2}|\\u\{[0-9a-fA-F]+\})' 0:meta
     47 
     48 add-highlighter shared/zig/character region "'" (?<!\\)(\\\\)*' group
     49 add-highlighter shared/zig/character/ fill string
     50 add-highlighter shared/zig/character/ regex '(?:\\n|\\r|\\t|\\\\|\\''|\\"|\\x[0-9a-fA-F]{2}|\\u\{[0-9a-fA-F]+\})' 0:meta
     51 
     52 add-highlighter shared/zig/multiline_string region '\\\\' '$' fill string
     53 
     54 # attributes
     55 add-highlighter shared/zig/code/ regex '\b(?:const|var|extern|packed|export|pub|noalias|inline|noinline|comptime|callconv|volatile|allowzero|addrspace|align|linksection|threadlocal)\b' 0:attribute
     56 # structures
     57 add-highlighter shared/zig/code/ regex '\b(?:struct|enum|union|error|opaque)\b' 0:attribute
     58 
     59 # statement
     60 add-highlighter shared/zig/code/ regex '\b(?:break|return|continue|asm|defer|errdefer|unreachable|try|catch|suspend|nosuspend|resume)\b' 0:keyword
     61 # conditional
     62 add-highlighter shared/zig/code/ regex '\b(?:if|else|switch|and|or|orelse)\b' 0:keyword
     63 # repeat
     64 add-highlighter shared/zig/code/ regex '\b(?:while|for)\b' 0:keyword
     65 # other keywords
     66 add-highlighter shared/zig/code/ regex '\b(?:fn|test)\b' 0:keyword
     67 
     68 # types
     69 add-highlighter shared/zig/code/ regex '\b(?:bool|f16|f32|f64|f80|f128|void|noreturn|type|anyerror|anyframe|anytype|anyopaque)\b' 0:type
     70 add-highlighter shared/zig/code/ regex '\b(?:i0|u0|isize|usize|comptime_int|comptime_float)\b' 0:type
     71 add-highlighter shared/zig/code/ regex '\b(?:[iu][1-9]\d*)\b' 0:type
     72 add-highlighter shared/zig/code/ regex '\b(?:c_char|c_short|c_ushort|c_int|c_uint|c_long|c_ulong|c_longlong|c_ulonglong|c_longdouble)\b' 0:type
     73 
     74 # primitive values
     75 add-highlighter shared/zig/code/ regex '\b(?:true|false|null|undefined)\b' 0:value
     76 
     77 # integer literals
     78 add-highlighter shared/zig/code/ regex '\b[0-9](_?[0-9])*\b' 0:value
     79 add-highlighter shared/zig/code/ regex '\b0x[0-9a-fA-F](_?[0-9a-fA-F])*\b' 0:value
     80 add-highlighter shared/zig/code/ regex '\b0o[0-7](_?[0-7])*\b' 0:value
     81 add-highlighter shared/zig/code/ regex '\b0b[01](_?[01])*\b' 0:value
     82 
     83 # float literals
     84 add-highlighter shared/zig/code/ regex '\b[0-9]+\.[0-9]+(?:[eE][-+]?[0-9]+)?\b' 0:value
     85 add-highlighter shared/zig/code/ regex '\b0x[0-9a-fA-F]+\.[0-9a-fA-F]+(?:[pP][-+]?[0-9a-fA-F]+)?\b' 0:value
     86 add-highlighter shared/zig/code/ regex '\b[0-9]+\.?[eE][-+]?[0-9]+\b' 0:value
     87 add-highlighter shared/zig/code/ regex '\b0x[0-9a-fA-F]+\.?[eE][-+]?[0-9a-fA-F]+\b' 0:value
     88 
     89 # operators
     90 add-highlighter shared/zig/code/ regex '(?:\+|-|\*|/|%|=|<|>|&|\||\^|~|\?|!)' 0:operator
     91 
     92 # builtin functions
     93 add-highlighter shared/zig/code/ regex "@(?:addrSpaceCast|addWithOverflow|alignCast|alignOf|as|atomicLoad|atomicRmw|atomicStore|bitCast|bitOffsetOf|bitSizeOf|branchHint|breakpoint|mulAdd|byteSwap|bitReverse|offsetOf|call|cDefine|cImport|cInclude|clz|cmpxchgStrong|cmpxchgWeak|compileError|compileLog|constCast|ctz|cUndef|cVaArg|cVaCopy|cVaEnd|cVaStart|disableInstrumentation|disableIntrinsics|divExact|divFloor|divTrunc|embedFile|enumFromInt|errorFromInt|errorName|errorReturnTrace|errorCast|export|extern|field|fieldParentPtr|FieldType|floatCast|floatFromInt|frameAddress|hasDecl|hasField|import|inComptime|intCast|intFromBool|intFromEnum|intFromError|intFromFloat|intFromPtr|max|memcpy|memmove|memset|min|wasmMemorySize|wasmMemoryGrow|mod|mulWithOverflow|panic|popCount|prefetch|ptrCast|ptrFromInt|rem|returnAddress|select|setEvalBranchQuota|setFloatMode|setRuntimeSafety|shlExact|shlWithOverflow|shrExact|shuffle|sizeOf|splat|reduce|src|sqrt|sin|cos|tan|exp|exp2|log|log2|log10|abs|floor|ceil|trunc|round|subWithOverflow|tagName|This|trap|truncate|Type|typeInfo|typeName|TypeOf|unionInit|Vector|volatileCast|workGroupId|workGroupSize|workItemId)\b" 0:meta
     94 
     95 # Commands
     96 # ‾‾‾‾‾‾‾‾
     97 
     98 define-command -hidden zig-trim-indent %{
     99     # delete trailing whitespace
    100     try %{ execute-keys -draft -itersel x s \h+$ <ret> d }
    101 }
    102 
    103 define-command -hidden zig-insert-on-new-line %<
    104     try %[
    105         evaluate-commands -draft -save-regs '/"' %[
    106             # copy // or /// comments prefix or \\ string literal prefix and following whitespace
    107             execute-keys -save-regs '' k x1s^\h*((///?|\\\\)+\h*)<ret> y
    108             try %[
    109                 # if the previous comment isn't empty, create a new one
    110                 execute-keys x<a-K>^\h*//+\h*$<ret> jxs^\h*<ret>P
    111             ] catch %[
    112                 # if there is no text in the previous comment, remove it completely
    113                 execute-keys d
    114             ]
    115         ]
    116 
    117         # trim trailing whitespace on the previous line
    118         try %[ execute-keys -draft k x s\h+$<ret> d ]
    119     ]
    120 >
    121 
    122 define-command -hidden zig-indent-on-new-line %<
    123     evaluate-commands -draft -itersel %<
    124         # preserve indent level
    125         try %< execute-keys -draft <semicolon> K <a-&> >
    126         try %<
    127             # only if we didn't copy a comment or multiline string
    128             execute-keys -draft x <a-K> ^\h*(//|\\\\) <ret>
    129             # indent after lines ending in {
    130             try %< execute-keys -draft k x <a-k> \{\h*$ <ret> j <a-gt> >
    131             # deindent closing } when after cursor
    132             try %< execute-keys -draft x <a-k> ^\h*\} <ret> gh / \} <ret> m <a-S> 1<a-&> >
    133         >
    134         # filter previous line
    135         try %< execute-keys -draft k : zig-trim-indent <ret> >
    136     >
    137 >
    138 
    139 define-command -hidden zig-indent-on-closing %<
    140     # align lone } to indent level of opening line
    141     try %< execute-keys -draft -itersel <a-h> <a-k> ^\h*\}$ <ret> h m <a-S> 1<a-&> >
    142 >
    143 
    144 §