2014-07-05 14:32:37 +02:00
|
|
|
# http://json.org
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
# Detection
|
|
|
|
# ‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
hook global BufCreate .*[.](json) %{
|
2017-11-03 08:34:41 +01:00
|
|
|
set-option buffer filetype json
|
2014-07-05 14:32:37 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 05:54:19 +02:00
|
|
|
# Initialization
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
hook global WinSetOption filetype=json %{
|
2019-03-13 06:24:33 +01:00
|
|
|
require-module json
|
2019-04-10 05:54:19 +02:00
|
|
|
|
2022-04-30 11:22:47 +02:00
|
|
|
hook window ModeChange pop:insert:.* -group json-trim-indent json-trim-indent
|
2019-04-10 05:54:19 +02:00
|
|
|
hook window InsertChar .* -group json-indent json-indent-on-char
|
|
|
|
hook window InsertChar \n -group json-indent json-indent-on-new-line
|
|
|
|
|
|
|
|
hook -once -always window WinSetOption filetype=.* %{ remove-hooks window json-.+ }
|
|
|
|
}
|
|
|
|
|
|
|
|
hook -group json-highlight global WinSetOption filetype=json %{
|
|
|
|
add-highlighter window/json ref json
|
|
|
|
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/json }
|
2019-03-13 06:24:33 +01:00
|
|
|
}
|
|
|
|
|
2019-04-10 05:54:19 +02:00
|
|
|
|
2019-03-13 06:24:33 +01:00
|
|
|
provide-module json %(
|
|
|
|
|
2014-07-05 14:32:37 +02:00
|
|
|
# Highlighters
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
2018-07-01 11:53:35 +02:00
|
|
|
add-highlighter shared/json regions
|
|
|
|
add-highlighter shared/json/code default-region group
|
2018-07-02 12:59:12 +02:00
|
|
|
add-highlighter shared/json/string region '"' (?<!\\)(\\\\)*" fill string
|
2014-07-05 14:32:37 +02:00
|
|
|
|
2018-07-01 11:53:35 +02:00
|
|
|
add-highlighter shared/json/code/ regex \b(true|false|null|\d+(?:\.\d+)?(?:[eE][+-]?\d*)?)\b 0:value
|
2014-07-05 14:32:37 +02:00
|
|
|
|
|
|
|
# Commands
|
|
|
|
# ‾‾‾‾‾‾‾‾
|
|
|
|
|
2018-12-19 10:10:26 +01:00
|
|
|
define-command -hidden json-trim-indent %{
|
2015-11-04 10:48:47 +01:00
|
|
|
# remove trailing white spaces
|
2022-03-16 23:20:07 +01:00
|
|
|
try %{ execute-keys -draft -itersel x s \h+$ <ret> d }
|
2014-07-05 14:32:37 +02:00
|
|
|
}
|
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command -hidden json-indent-on-char %<
|
2017-11-03 09:09:45 +01:00
|
|
|
evaluate-commands -draft -itersel %<
|
2014-07-16 13:56:40 +02:00
|
|
|
# align closer token to its opener when alone on a line
|
2020-07-28 04:48:10 +02:00
|
|
|
try %< execute-keys -draft <a-h> <a-k> ^\h+[\]}]$ <ret> m <a-S> 1<a-&> >
|
2015-11-10 14:36:16 +01:00
|
|
|
>
|
|
|
|
>
|
2014-07-05 14:32:37 +02:00
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command -hidden json-indent-on-new-line %<
|
2017-11-03 09:09:45 +01:00
|
|
|
evaluate-commands -draft -itersel %<
|
2014-07-05 14:32:37 +02:00
|
|
|
# preserve previous line indent
|
2019-10-22 11:02:06 +02:00
|
|
|
try %{ execute-keys -draft <semicolon> K <a-&> }
|
2014-07-05 14:32:37 +02:00
|
|
|
# filter previous line
|
2018-12-19 10:10:26 +01:00
|
|
|
try %{ execute-keys -draft k : json-trim-indent <ret> }
|
2020-07-28 04:55:34 +02:00
|
|
|
# indent after lines ending with opener token
|
2022-03-16 23:20:07 +01:00
|
|
|
try %< execute-keys -draft k x <a-k> [[{]\h*$ <ret> j <a-gt> >
|
2020-07-28 04:58:25 +02:00
|
|
|
# deindent closer token(s) 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-&> >
|
2015-11-10 14:36:16 +01:00
|
|
|
>
|
|
|
|
>
|
2014-07-05 14:32:37 +02:00
|
|
|
|
2019-03-13 06:24:33 +01:00
|
|
|
)
|