json.kak (2167B)
1 # http://json.org 2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 3 4 # Detection 5 # ‾‾‾‾‾‾‾‾‾ 6 7 hook global BufCreate .*[.](json)(l)? %{ 8 set-option buffer filetype json 9 } 10 11 # Initialization 12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 13 14 hook global WinSetOption filetype=json %{ 15 require-module json 16 17 hook window ModeChange pop:insert:.* -group json-trim-indent json-trim-indent 18 hook window InsertChar .* -group json-indent json-indent-on-char 19 hook window InsertChar \n -group json-indent json-indent-on-new-line 20 21 hook -once -always window WinSetOption filetype=.* %{ remove-hooks window json-.+ } 22 } 23 24 hook -group json-highlight global WinSetOption filetype=json %{ 25 add-highlighter window/json ref json 26 hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/json } 27 } 28 29 30 provide-module json %( 31 32 # Highlighters 33 # ‾‾‾‾‾‾‾‾‾‾‾‾ 34 35 add-highlighter shared/json regions 36 add-highlighter shared/json/code default-region group 37 add-highlighter shared/json/string region '"' (?<!\\)(\\\\)*" fill string 38 39 add-highlighter shared/json/code/ regex \b(true|false|null|\d+(?:\.\d+)?(?:[eE][+-]?\d*)?)\b 0:value 40 41 # Commands 42 # ‾‾‾‾‾‾‾‾ 43 44 define-command -hidden json-trim-indent %{ 45 # remove trailing white spaces 46 try %{ execute-keys -draft -itersel x s \h+$ <ret> d } 47 } 48 49 define-command -hidden json-indent-on-char %< 50 evaluate-commands -draft -itersel %< 51 # align closer token to its opener when alone on a line 52 try %< execute-keys -draft <a-h> <a-k> ^\h+[\]}]$ <ret> m <a-S> 1<a-&> > 53 > 54 > 55 56 define-command -hidden json-indent-on-new-line %< 57 evaluate-commands -draft -itersel %< 58 # preserve previous line indent 59 try %{ execute-keys -draft <semicolon> K <a-&> } 60 # filter previous line 61 try %{ execute-keys -draft k : json-trim-indent <ret> } 62 # indent after lines ending with opener token 63 try %< execute-keys -draft k x <a-k> [[{]\h*$ <ret> j <a-gt> > 64 # deindent closer token(s) when after cursor 65 try %< execute-keys -draft x <a-k> ^\h*[}\]] <ret> gh / [}\]] <ret> m <a-S> 1<a-&> > 66 > 67 > 68 69 )