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
|
|
|
|
2019-10-16 11:19:43 +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
|
2017-11-03 09:09:45 +01:00
|
|
|
try %{ execute-keys -draft -itersel <a-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:46:47 +02:00
|
|
|
try %< execute-keys -draft <a-h> <a-k> ^\h+[\]}]$ <ret> m s \A|.\z <ret> 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> }
|
2014-07-05 14:32:37 +02:00
|
|
|
# indent after lines beginning with opener token
|
2017-11-03 09:09:45 +01:00
|
|
|
try %< execute-keys -draft k <a-x> <a-k> ^\h*[[{] <ret> j <a-gt> >
|
2015-11-10 14:36:16 +01:00
|
|
|
>
|
|
|
|
>
|
2014-07-05 14:32:37 +02:00
|
|
|
|
2019-03-13 06:24:33 +01:00
|
|
|
)
|