From 6190f8ce70bf863638362a39478e5a41f8c37e53 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Sat, 21 Mar 2020 22:45:40 +0100 Subject: [PATCH 2/6] Add syntax highlighting for zig --- rc/filetype/zig.kak | 80 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 rc/filetype/zig.kak diff --git a/rc/filetype/zig.kak b/rc/filetype/zig.kak new file mode 100644 index 00000000..998c0f94 --- /dev/null +++ b/rc/filetype/zig.kak @@ -0,0 +1,80 @@ +# 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 + +hook global BufCreate .*[.]zig %{ + set-option buffer filetype 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 } +} + +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 From 23b28df1b8be0ecbf14fdaa8c8f6bbb1fc167f64 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Sun, 22 Mar 2020 10:50:29 +0100 Subject: [PATCH 3/6] Use attribute face where appropriate --- rc/filetype/zig.kak | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/rc/filetype/zig.kak b/rc/filetype/zig.kak index 998c0f94..8770e848 100644 --- a/rc/filetype/zig.kak +++ b/rc/filetype/zig.kak @@ -23,10 +23,11 @@ add-highlighter shared/zig/string_double region '"' (? Date: Sun, 22 Mar 2020 19:18:15 +0100 Subject: [PATCH 4/6] 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 > +> From 6198cc5e474c19a00cd4b2da52146051c13fdeaa Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Sun, 22 Mar 2020 19:24:39 +0100 Subject: [PATCH 5/6] Encapsulate zig highlighting and commands in a module --- rc/filetype/zig.kak | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/rc/filetype/zig.kak b/rc/filetype/zig.kak index 209f1449..ac14447e 100644 --- a/rc/filetype/zig.kak +++ b/rc/filetype/zig.kak @@ -3,11 +3,18 @@ # 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 @@ -16,10 +23,16 @@ hook global WinSetOption filetype=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 @@ -88,6 +101,9 @@ add-highlighter shared/zig/code/ regex "@(?:truncate|typeId|typeInfo|typeName|Ty 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 } @@ -113,3 +129,5 @@ define-command -hidden zig-indent-on-closing %< # align lone } to indent level of opening line try %< execute-keys -draft -itersel ^\h*\}$ h m 1 > > + +§ From 9396f508c2be7aa275b9610f426db03169b23ca3 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Sun, 22 Mar 2020 20:19:43 +0100 Subject: [PATCH 6/6] Fix indentation of auto-extended comments --- rc/filetype/zig.kak | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rc/filetype/zig.kak b/rc/filetype/zig.kak index ac14447e..602c7dab 100644 --- a/rc/filetype/zig.kak +++ b/rc/filetype/zig.kak @@ -111,9 +111,11 @@ define-command -hidden zig-trim-indent %{ define-command -hidden zig-indent-on-new-line %< evaluate-commands -draft -itersel %< - # copy // or /// comments prefix and following white spaces 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 >