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