2020-03-21 22:45:40 +01:00
|
|
|
# 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
|
|
|
|
|
2020-03-22 19:24:39 +01:00
|
|
|
# Detection
|
|
|
|
# ‾‾‾‾‾‾‾‾‾
|
|
|
|
|
2020-03-21 22:45:40 +01:00
|
|
|
hook global BufCreate .*[.]zig %{
|
|
|
|
set-option buffer filetype zig
|
|
|
|
}
|
|
|
|
|
2020-03-22 19:24:39 +01:00
|
|
|
# Initialization
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
2020-03-22 19:18:15 +01:00
|
|
|
hook global WinSetOption filetype=zig %<
|
2020-03-22 19:24:39 +01:00
|
|
|
require-module zig
|
2020-03-22 19:18:15 +01:00
|
|
|
hook window ModeChange pop:insert:.* -group zig-trim-indent zig-trim-indent
|
2021-04-17 10:17:01 +02:00
|
|
|
hook window InsertChar \n -group zig-insert zig-insert-on-new-line
|
2020-03-22 19:18:15 +01:00
|
|
|
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-.+ >
|
|
|
|
>
|
|
|
|
|
2020-03-21 22:45:40 +01:00
|
|
|
hook -group zig-highlight global WinSetOption filetype=zig %{
|
2020-03-22 19:24:39 +01:00
|
|
|
require-module zig
|
2020-03-21 22:45:40 +01:00
|
|
|
add-highlighter window/zig ref zig
|
|
|
|
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/zig }
|
|
|
|
}
|
|
|
|
|
2020-03-22 19:24:39 +01:00
|
|
|
provide-module zig %§
|
|
|
|
|
|
|
|
# Highlighters
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
2020-03-21 22:45:40 +01:00
|
|
|
add-highlighter shared/zig regions
|
|
|
|
add-highlighter shared/zig/code default-region group
|
|
|
|
|
2021-01-18 00:44:32 +01:00
|
|
|
add-highlighter shared/zig/doc_comment region '///(?=[^/])' '$' fill documentation
|
2020-11-23 17:51:21 +01:00
|
|
|
add-highlighter shared/zig/comment region '//' '$' fill comment
|
2020-03-21 22:45:40 +01:00
|
|
|
|
2020-11-23 17:51:21 +01:00
|
|
|
# strings and characters
|
|
|
|
add-highlighter shared/zig/string region '"' (?<!\\)(\\\\)*" group
|
|
|
|
add-highlighter shared/zig/string/ fill string
|
|
|
|
add-highlighter shared/zig/string/ regex '(?:\\n|\\r|\\t|\\\\|\\''|\\"|\\x[0-9a-fA-F]{2}|\\u\{[0-9a-fA-F]+\})' 0:meta
|
|
|
|
|
|
|
|
add-highlighter shared/zig/character region "'" (?<!\\)(\\\\)*' group
|
|
|
|
add-highlighter shared/zig/character/ fill string
|
|
|
|
add-highlighter shared/zig/character/ regex '(?:\\n|\\r|\\t|\\\\|\\''|\\"|\\x[0-9a-fA-F]{2}|\\u\{[0-9a-fA-F]+\})' 0:meta
|
|
|
|
|
|
|
|
add-highlighter shared/zig/multiline_string region '\\\\' '$' fill string
|
2020-03-21 22:45:40 +01:00
|
|
|
|
2020-03-22 10:50:29 +01:00
|
|
|
# attributes
|
|
|
|
add-highlighter shared/zig/code/ regex '\b(?:const|var|extern|packed|export|pub|noalias|inline|noinline|comptime|callconv|volatile|allowzero|align|linksection|threadlocal)\b' 0:attribute
|
|
|
|
# structures
|
2020-11-23 17:51:21 +01:00
|
|
|
add-highlighter shared/zig/code/ regex '\b(?:struct|enum|union|error|opaque)\b' 0:attribute
|
2020-03-22 10:50:29 +01:00
|
|
|
|
2020-03-21 22:45:40 +01:00
|
|
|
# statement
|
2021-06-12 12:26:32 +02:00
|
|
|
add-highlighter shared/zig/code/ regex '\b(?:break|return|continue|asm|defer|errdefer|unreachable|try|catch|async|noasync|await|suspend|nosuspend|resume)\b' 0:keyword
|
2020-03-21 22:45:40 +01:00
|
|
|
# conditional
|
|
|
|
add-highlighter shared/zig/code/ regex '\b(?:if|else|switch|and|or|orelse)\b' 0:keyword
|
|
|
|
# repeat
|
|
|
|
add-highlighter shared/zig/code/ regex '\b(?:while|for)\b' 0:keyword
|
|
|
|
# other keywords
|
|
|
|
add-highlighter shared/zig/code/ regex '\b(?:fn|usingnamespace|test)\b' 0:keyword
|
|
|
|
|
|
|
|
# types
|
2021-12-31 06:00:35 +01:00
|
|
|
add-highlighter shared/zig/code/ regex '\b(?:bool|f16|f32|f64|f128|void|noreturn|type|anyerror|anyframe|anytype|anyopaque)\b' 0:type
|
2020-03-21 22:45:40 +01:00
|
|
|
add-highlighter shared/zig/code/ regex '\b(?:i0|u0|isize||usize|comptime_int|comptime_float)\b' 0:type
|
|
|
|
add-highlighter shared/zig/code/ regex '\b(?:[iu][1-9]\d*)\b' 0:type
|
2021-12-31 06:00:35 +01:00
|
|
|
add-highlighter shared/zig/code/ regex '\b(?:c_short|c_ushort|c_int|c_uint|c_long|c_ulong|c_longlong|c_ulonglong|c_longdouble)\b' 0:type
|
2020-03-21 22:45:40 +01:00
|
|
|
|
|
|
|
# primitive values
|
|
|
|
add-highlighter shared/zig/code/ regex '\b(?:true|false|null|undefined)\b' 0:value
|
|
|
|
|
|
|
|
# integer literals
|
2020-05-22 15:47:05 +02:00
|
|
|
add-highlighter shared/zig/code/ regex '\b[0-9](_?[0-9])*\b' 0:value
|
|
|
|
add-highlighter shared/zig/code/ regex '\b0x[0-9a-fA-F](_?[0-9a-fA-F])*\b' 0:value
|
|
|
|
add-highlighter shared/zig/code/ regex '\b0o[0-7](_?[0-7])*\b' 0:value
|
|
|
|
add-highlighter shared/zig/code/ regex '\b0b[01](_?[01])*\b' 0:value
|
2020-03-21 22:45:40 +01:00
|
|
|
|
|
|
|
# float literals
|
|
|
|
add-highlighter shared/zig/code/ regex '\b[0-9]+\.[0-9]+(?:[eE][-+]?[0-9]+)?\b' 0:value
|
|
|
|
add-highlighter shared/zig/code/ regex '\b0x[0-9a-fA-F]+\.[0-9a-fA-F]+(?:[pP][-+]?[0-9a-fA-F]+)?\b' 0:value
|
|
|
|
add-highlighter shared/zig/code/ regex '\b[0-9]+\.?[eE][-+]?[0-9]+\b' 0:value
|
|
|
|
add-highlighter shared/zig/code/ regex '\b0x[0-9a-fA-F]+\.?[eE][-+]?[0-9a-fA-F]+\b' 0:value
|
|
|
|
|
|
|
|
# operators
|
|
|
|
add-highlighter shared/zig/code/ regex '(?:\+|-|\*|/|%|=|<|>|&|\||\^|~|\?|!)' 0:operator
|
|
|
|
|
|
|
|
# builtin functions
|
2021-12-31 06:00:35 +01:00
|
|
|
add-highlighter shared/zig/code/ regex "@(?:addWithOverflow|alignCast|alignOf|as|asyncCall|atomicLoad|atomicRmw|atomicStore|bitCast|bitOffsetOf|boolToInt|bitSizeOf|breakpoint|mulAdd|byteSwap|bitReverse|offsetOf|call|cDefine|cImport|cInclude|clz|cmpxchgStrong|cmpxchgWeak|compileError|compileLog|ctz|cUndef|divExact|divFloor|divTrunc|embedFile|enumToInt|errorName|errorReturnTrace|errorToInt|errSetCast|export|extern|fence|field|fieldParentPtr|floatCast|floatToInt|frame|Frame|frameAddress|frameSize|hasDecl|hasField|import|intCast|intToEnum|intToError|intToFloat|intToPtr|maximum|memcpy|memset|minimum|wasmMemorySize|wasmMemoryGrow|mod|mulWithOverflow|panic|popCount|prefetch|ptrCast|ptrToInt|rem|returnAddress|select|setAlignStack|setCold|setEvalBranchQuota|setFloatMode|setRuntimeSafety|shlExact|shlWithOverflow|shrExact|shuffle|sizeOf|splat|reduce|src|sqrt|sin|cos|exp|exp2|log|log2|log10|fabs|floor|ceil|trunc|round|subWithOverflow|tagName|This|truncate|Type|typeInfo|typeName|TypeOf|unionInit)\b" 0:function
|
2020-03-22 19:18:15 +01:00
|
|
|
|
2020-03-22 19:24:39 +01:00
|
|
|
# Commands
|
|
|
|
# ‾‾‾‾‾‾‾‾
|
|
|
|
|
2020-03-22 19:18:15 +01:00
|
|
|
define-command -hidden zig-trim-indent %{
|
|
|
|
# delete trailing whitespace
|
2022-03-16 23:20:07 +01:00
|
|
|
try %{ execute-keys -draft -itersel x s \h+$ <ret> d }
|
2020-03-22 19:18:15 +01:00
|
|
|
}
|
|
|
|
|
2021-04-17 10:17:01 +02:00
|
|
|
define-command -hidden zig-insert-on-new-line %<
|
|
|
|
evaluate-commands -draft -itersel %<
|
2021-06-12 12:38:27 +02:00
|
|
|
# copy // or /// comments prefix or \\ string literal prefix and following whitespace
|
2022-03-16 23:20:07 +01:00
|
|
|
try %< execute-keys -draft k x s ^\h*\K(///?|\\\\)\h* <ret> y gh j P >
|
2021-04-17 10:17:01 +02:00
|
|
|
>
|
|
|
|
>
|
|
|
|
|
2020-03-22 19:18:15 +01:00
|
|
|
define-command -hidden zig-indent-on-new-line %<
|
|
|
|
evaluate-commands -draft -itersel %<
|
2021-04-17 10:17:01 +02:00
|
|
|
# preserve indent level
|
|
|
|
try %< execute-keys -draft <semicolon> K <a-&> >
|
2020-03-22 19:18:15 +01:00
|
|
|
try %<
|
2021-06-12 12:38:27 +02:00
|
|
|
# only if we didn't copy a comment or multiline string
|
2022-03-16 23:20:07 +01:00
|
|
|
execute-keys -draft x <a-K> ^\h*(//|\\\\) <ret>
|
2020-03-22 19:18:15 +01:00
|
|
|
# indent after lines ending in {
|
2022-03-16 23:20:07 +01:00
|
|
|
try %< execute-keys -draft k x <a-k> \{\h*$ <ret> j <a-gt> >
|
2020-07-30 05:46:51 +02:00
|
|
|
# deindent closing } when after cursor
|
2022-03-16 23:20:07 +01:00
|
|
|
try %< execute-keys -draft x <a-k> ^\h*\} <ret> gh / \} <ret> m <a-S> 1<a-&> >
|
2020-03-22 19:18:15 +01:00
|
|
|
>
|
|
|
|
# filter previous line
|
|
|
|
try %< execute-keys -draft k : zig-trim-indent <ret> >
|
|
|
|
>
|
|
|
|
>
|
|
|
|
|
|
|
|
define-command -hidden zig-indent-on-closing %<
|
|
|
|
# align lone } to indent level of opening line
|
|
|
|
try %< execute-keys -draft -itersel <a-h> <a-k> ^\h*\}$ <ret> h m <a-S> 1<a-&> >
|
|
|
|
>
|
2020-03-22 19:24:39 +01:00
|
|
|
|
|
|
|
§
|