diff --git a/rc/filetype/wren.kak b/rc/filetype/wren.kak new file mode 100644 index 0000000000..413cc556a4 --- /dev/null +++ b/rc/filetype/wren.kak @@ -0,0 +1,154 @@ +provide-module -override wren %§ + add-highlighter shared/wren regions + + add-highlighter shared/wren/line_comment region '//' '$' fill comment + add-highlighter shared/wren/block_comment region -recurse '/\*' '/\*' '\*/' fill comment + + add-highlighter shared/wren/raw_string region '"""' '(?>|&{1,2}|\^|\|{1,2}|[<>]=?)|\bis\b|[!=]?=|\?|:)' 0:operator + + add-highlighter shared/wren/code/ regex 'class\h+([A-Za-z][\w_]*)\h+(is\h+[A-Za-z][\w_]*)\h*(?=\{)' 1:type 2:attribute + + add-highlighter shared/wren/code/ regex '\b(?i)-?\d+\b' 0:value + add-highlighter shared/wren/code/ regex '\b-?0x(?i)[\da-f]+\b' 0:value + add-highlighter shared/wren/code/ regex '\b(?i)-?\d+\.\d+\b' 0:value + add-highlighter shared/wren/code/ regex '\b(?i)-?\d+\.\d+e[+-]?\d+\b' 0:value + + add-highlighter shared/wren/code/ regex '^\h*import\h*"(.*?)"' 1:module + + add-highlighter shared/wren/code/ regex '\bFn\.new\h*(?=[\{\(])' 0:+b@value + add-highlighter shared/wren/code/ regex '\bFiber\.new\h*(?=[\{\(])' 0:+b@value + add-highlighter shared/wren/code/ regex '\bFiber\.current\b' 0:+b@value + add-highlighter shared/wren/code/ regex '\bSystem\.clock\b' 0:+b@value + + add-highlighter shared/wren/code/ regex '\bFiber\.(yield|abort|suspend)\h*(?=[\{\(])' 0:+b@function + add-highlighter shared/wren/code/ regex '\bSystem\.((print|write)(All)?)\h*(?=[\{\(])' 0:+b@function + add-highlighter shared/wren/code/ regex '\bSystem\.gc\h*(?=\()' 0:+b@function + + add-highlighter shared/wren/code/ regex '\bList\.filled\h*(?=\()' 0:+b@function + add-highlighter shared/wren/code/ regex '\b(List|Map)\.new\h*(?=\()' 0:+b@value + + add-highlighter shared/wren/code/ regex '\bNum\.fromString\h*(?=\()' 0:+b@function + add-highlighter shared/wren/code/ regex \ + '\bNum\.(infinity|nan|pi|tau|largest|smallest|(min|max)SafeInteger)\b' 0:+b@value + + add-highlighter shared/wren/code/ regex '\bObject\.same\h*(?=\()' 0:+b@function + add-highlighter shared/wren/code/ regex '\bString\.from(Byte|CodePoint)\h*(?=\()' 0:+b@function + + declare-option str-list wren_static_words \ + 'import' 'true' 'false' 'null' 'as' 'break' 'class' 'construct' 'continue' 'else' 'for' 'foreign' 'if' 'in' 'return' 'static' 'super' 'this' \ + 'var' 'while' 'Bool' 'Class' 'Fiber' 'Fn' 'List' 'Map' 'Null' 'Num' 'Object' 'Range' 'Sequence' 'String' 'System' +§ + +hook global BufCreate (.*/)?.*\.wren %{ set-option buffer filetype wren } + +hook -group wren-highlight global WinSetOption filetype=wren %{ + require-module wren + add-highlighter window/wren ref wren + hook -once -always window WinSetOption filetype=.* %{ + remove-highlighter window/wren + } +} + +hook global WinSetOption filetype=wren %{ + require-module wren + + set-option window static_words %opt{wren_static_words} + + hook window ModeChange pop:insert:.* -group wren-trim-indent %{ try %{ execute-keys -draft xs^\h+$d } } + hook window InsertChar \n -group wren-indent wren-indent-on-new-line + hook window InsertChar \{ -group wren-indent wren-indent-on-opening-curly-brace + hook window InsertChar \} -group wren-indent wren-indent-on-closing-curly-brace + hook window InsertChar \n -group wren-comment-insert wren-insert-comment-on-new-line + hook window InsertChar \n -group wren-closing-delimiter-insert wren-insert-closing-delimiter-on-new-line +} + +define-command -hidden wren-indent-on-new-line %~ + evaluate-commands -draft -itersel %= + # preserve previous line indent + try %{ execute-keys -draft K } + # cleanup trailing white spaces on the previous line + try %{ execute-keys -draft kx s \h+$ d } + try %< + try %{ # line comment + execute-keys -draft kx s ^\h*// + } catch %{ # block comment + execute-keys -draft /\* \*/ + } + > catch %< + # indent after lines with an unclosed { or ( + try %< execute-keys -draft [c[({],[)}] \A[({][^\n]*\n[^\n]*\n?\z j > + # deindent closing brace(s) when after cursor + try %[ execute-keys -draft x ^\h*[})] gh / [})] m 1 ] + > + = +~ + +define-command -hidden wren-indent-on-opening-curly-brace %[ + # align indent with opening paren when { is entered on a new line after the closing paren + try %[ execute-keys -draft -itersel h)M \A\(.*\)\h*\n\h*\{\z s \A|.\z 1 ] +] + +define-command -hidden wren-indent-on-closing-curly-brace %[ + # align to opening curly brace when alone on a line + try %[ execute-keys -itersel -draft ^\h+\}$hms\A|.\z1 ] +] + +define-command -hidden wren-insert-comment-on-new-line %[ + evaluate-commands -no-hooks -draft -itersel %[ + # copy // comments prefix and following white spaces + try %{ execute-keys -draft kx s ^\h*\K/{2,}\h* yP } + ] +] + +define-command -hidden wren-insert-closing-delimiter-on-new-line %[ + evaluate-commands -no-hooks -draft -itersel %[ + # Wisely add '}'. + evaluate-commands -save-regs x %[ + # Save previous line indent in register x. + try %[ execute-keys -draft kxs^\h+"xy ] catch %[ reg x '' ] + try %[ + # Validate previous line and that it is not closed yet. + execute-keys -draft kx ^x.*\{\h*\(?\h*$ j}iJx ^x\)?\h*\} + # Insert closing '}'. + execute-keys -draft ox} + # Delete trailing '}' on the line below the '{'. + execute-keys -draft xs\}$d + ] + ] + + # Wisely add ')'. + evaluate-commands -save-regs x %[ + # Save previous line indent in register x. + try %[ execute-keys -draft kxs^\h+"xy ] catch %[ reg x '' ] + try %[ + # Validate previous line and that it is not closed yet. + execute-keys -draft kx ^x.*\(\h*$ J}iJx ^x\) + # Insert closing ')'. + execute-keys -draft ox) + # Delete trailing ')' on the line below the '('. + execute-keys -draft xs\)\h*\}?\h*$d + ] + ] + ] +]