kak-custom/filetype/jsoncc.kak
2024-09-12 14:40:34 -04:00

73 lines
2.4 KiB
Plaintext

# Detection
# ‾‾‾‾‾‾‾‾‾
hook global BufCreate .*[.](jsoncc?) %{
set-option buffer filetype jsoncc
set-option buffer comment_line '//'
set-option buffer comment_block_begin '/*'
set-option buffer comment_block_end '*/'
}
# Initialization
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
hook global WinSetOption filetype=jsoncc %{
require-module jsoncc
hook window ModeChange pop:insert:.* -group jsoncc-trim-indent jsoncc-trim-indent
hook window InsertChar .* -group jsoncc-indent jsoncc-indent-on-char
hook window InsertChar \n -group jsoncc-indent jsoncc-indent-on-new-line
hook -once -always window WinSetOption filetype=.* %{ remove-hooks window jsoncc-.+ }
}
hook -group jsoncc-highlight global WinSetOption filetype=jsoncc %{
add-highlighter window/jsoncc ref jsoncc
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/jsoncc }
}
provide-module jsoncc %(
# Highlighters
# ‾‾‾‾‾‾‾‾‾‾‾‾
add-highlighter shared/jsoncc regions
add-highlighter shared/jsoncc/code default-region group
add-highlighter shared/jsoncc/string region '"' (?<!\\)(\\\\)*" fill string
add-highlighter shared/jsoncc/comment region /\* \*/ fill comment
add-highlighter shared/jsoncc/comment_line region // $ fill comment
add-highlighter shared/jsoncc/code/ regex \b(true|false|null|\d+(?:\.\d+)?(?:[eE][+-]?\d*)?)\b 0:value
# Commands
# ‾‾‾‾‾‾‾‾
define-command -hidden jsoncc-trim-indent %{
# remove trailing white spaces
try %{ execute-keys -draft -itersel x s \h+$ <ret> d }
}
define-command -hidden jsoncc-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 jsoncc-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 : jsoncc-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-&> >
>
>
)