dippin-dotfiles/.config/kak/syntaxes/kdl.kak
2025-04-26 14:26:50 -06:00

98 lines
4.5 KiB
Text

# https://kdl.dev
# Currently only supports kdl v1
hook global BufCreate .*[.](kdl) %{
set-option buffer filetype kdl
}
# Initialization
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
hook global WinSetOption filetype=kdl %{
require-module kdl
hook window ModeChange pop:insert:.* -group kdl-trim-indent kdl-trim-indent
hook window InsertChar .* -group kdl-indent kdl-indent-on-char
hook window InsertChar \n -group kdl-indent kdl-indent-on-new-line
hook -once -always window WinSetOption filetype=.* %{ remove-hooks window kdl-.+ }
}
hook -group kdl-highlight global WinSetOption filetype=kdl %{
add-highlighter window/kdl ref kdl
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/kdl }
}
provide-module kdl %@
# Highlighters
# ‾‾‾‾‾‾‾‾‾‾‾‾
add-highlighter shared/kdl regions
add-highlighter shared/kdl/code default-region group
add-highlighter shared/kdl/raw_string region -match-capture 'r(#*)"' '"(#*)' fill string
add-highlighter shared/kdl/string region '"' (?<!\\)(\\\\)*" fill string
add-highlighter shared/kdl/comment region -recurse /\* /\* \*/ fill comment
add-highlighter shared/kdl/line_comment region // $ fill comment
# Slashdash (/-) comments are annoying to highlight properly without a proper parser due to the fact that they can comment
# out any kdl construct..
# The below is, at best, an approximation, and there are almost certainly edge cases missed.
add-highlighter shared/kdl/slashdash_child region -recurse \{ /-[^\n]+\{ \} fill comment
add-highlighter shared/kdl/slashdash_string region '/-\s+"' (?<!\\)(\\\\)*" fill comment
add-highlighter shared/kdl/slashdash_raw_string region -match-capture '/-\s+r"(#*)' '"(#*)' fill comment
add-highlighter shared/kdl/slashdash_builtin_value region /-\s+(true|false|null) \s fill comment
add-highlighter shared/kdl/slashdash_binary region /-\s+0b[01_]+ \s fill comment
add-highlighter shared/kdl/slashdash_octal region /-\s+0o[0-7_]+ \s fill comment
add-highlighter shared/kdl/slashdash_hex region /-\s+0x[a-fA-F0-9_]+ \s fill comment
add-highlighter shared/kdl/slashdash_decimal region /-\s+[0-9-+][0-9_]* \s fill comment
add-highlighter shared/kdl/slashdash_float region /-\s+[0-9-+][0-9_]*\.[0-9_]+ \s fill comment
add-highlighter shared/kdl/slashdash_float_exp region /-\s+[0-9-+][0-9_]*(\.[0-9_]+)?[eE][-+]?[0-9_]+ \s fill comment
add-highlighter shared/kdl/slashdash_prop_string region '/-\s+[\u000021-\u00FFFF]+="' (?<!\\)(\\\\)*" fill comment
add-highlighter shared/kdl/slashdash_prop_raw_string region -match-capture '/-\s+[\u000021-\u00FFFF]+=r"(#*)' '"(#*)' fill comment
add-highlighter shared/kdl/slashdash_prop_other region /-\s+[\u000021-\u00FFFF]+= \s fill comment
add-highlighter shared/kdl/slashdash_node region /-\s+[\u000021-\u00FFFF]+ $ fill comment
add-highlighter shared/kdl/code/node regex \b([\u000021-\u00FFFF]*)\b 0:variable # Everything not covered below is a node
add-highlighter shared/kdl/code/property regex \b([\u000021-\u00FFFF]+)(=) 0:operator 1:attribute
add-highlighter shared/kdl/code/builtin_value regex \b(true|false|null)\b 0:value
add-highlighter shared/kdl/code/binary regex \b(0b[01_]+)\b 0:value
add-highlighter shared/kdl/code/octal regex \b(0o[0-7_]+)\b 0:value
add-highlighter shared/kdl/code/hex regex \b(0x[a-fA-F0-9_]+)\b 0:value
add-highlighter shared/kdl/code/decimal regex \b([0-9-+][0-9_]*)\b 0:value
add-highlighter shared/kdl/code/float regex \b([0-9-+][0-9_]*\.[0-9_]+)\b 0:value
add-highlighter shared/kdl/code/float_exp regex \b([0-9-+][0-9_]*(\.[0-9_]+)?[eE][-+]?[0-9_]+)\b 0:value
# Commands
# ‾‾‾‾‾‾‾‾
define-command -hidden kdl-trim-indent %{
# remove trailing white spaces
try %{ execute-keys -draft -itersel x s \h+$ <ret> d }
}
define-command -hidden kdl-indent-on-char %<
evaluate-commands -draft -itersel %<
# align closer token to its opener when alone on a line
try %< execute-keys -draft <a-h> <a-k> ^\h+[\]}]$ <ret> m <a-S> 1<a-&> >
>
>
define-command -hidden kdl-indent-on-new-line %<
evaluate-commands -draft -itersel %<
# preserve previous line indent
try %{ execute-keys -draft <semicolon> K <a-&> }
# filter previous line
try %{ execute-keys -draft k : kdl-trim-indent <ret> }
# indent after lines ending with opener token
try %< execute-keys -draft k x <a-k> [[{]\h*$ <ret> j <a-gt> >
# deindent closer token(s) when after cursor
try %< execute-keys -draft x <a-k> ^\h*[}\]] <ret> gh / [}\]] <ret> m <a-S> 1<a-&> >
>
>
@