kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

json5.kak (4396B)


      1 # http://json5.org
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*[.](json5) %{
      8     set-option buffer filetype json5
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=json5 %{
     15     require-module json5
     16 
     17     hook window ModeChange pop:insert:.* -group json5-trim-indent json5-trim-indent
     18     hook window InsertChar \n -group json5-indent json5-indent-on-new-line
     19     hook window InsertChar .* -group json5-indent json5-indent-on-char
     20 
     21     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window json5-.+ }
     22 }
     23 
     24 hook -group json5-highlight global WinSetOption filetype=json5 %{
     25     add-highlighter window/json5 ref json5
     26     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/json5 }
     27 }
     28 
     29 
     30 provide-module json5 %(
     31 
     32 # Highlighters
     33 # ‾‾‾‾‾‾‾‾‾‾‾‾
     34 
     35 add-highlighter shared/json5 regions
     36 add-highlighter shared/json5/code default-region group
     37 add-highlighter shared/json5/string region '"' (?<!\\)(\\\\)*" fill string
     38 add-highlighter shared/json5/string2 region "'" (?<!\\)(\\\\)*' fill string
     39 add-highlighter shared/json5/comment region '//' '$' fill comment
     40 add-highlighter shared/json5/multiline_comment region /\* \*/ fill comment
     41 
     42 add-highlighter shared/json5/code/ regex ^(\s*[^,:\[\]\{\}\s]+): 1:variable
     43 add-highlighter shared/json5/code/ regex ,(\s*[^,:\[\]\{\}\s]+): 1:variable
     44 add-highlighter shared/json5/code/ regex \{(\s*[^,:\[\]\{\}\s]+): 1:variable
     45 
     46 add-highlighter shared/json5/code/ regex \b(true|false|null|\d+(?:\.\d+)?(?:[eE][+-]?\d*)?|0x[\da-fA-F]+)\b 0:value
     47 
     48 # Commands
     49 # ‾‾‾‾‾‾‾‾
     50 
     51 define-command -hidden json5-trim-indent %{
     52     # remove trailing white spaces
     53     try %{ execute-keys -draft -itersel x s \h+$ <ret> d }
     54 }
     55 
     56 define-command -hidden json5-indent-on-char %<
     57     evaluate-commands -draft -itersel %<
     58         # align closer token to its opener when alone on a line
     59         try %< execute-keys -draft <a-h> <a-k> ^\h+[\]}]$ <ret> m <a-S> 1<a-&> >
     60     >
     61 >
     62 
     63 define-command -hidden json5-indent-on-new-line %{
     64     evaluate-commands -itersel -draft %{
     65         execute-keys <semicolon>
     66         try %{
     67             evaluate-commands -draft -save-regs '/"' %{
     68                 # copy the commenting prefix
     69                 execute-keys -save-regs '' k x1s^(\h*//+\h*)<ret> y
     70                 try %{
     71                     # if the previous comment isn't empty, create a new one
     72                     execute-keys x<a-K>^\h*//+\h*$<ret> jxs^\h*<ret>P
     73                 } catch %{
     74                     # if there is no text in the previous comment, remove it completely
     75                     execute-keys d
     76                 }
     77             }
     78 
     79             # trim trailing whitespace on the previous line
     80             try %{ execute-keys -draft k x s\h+$<ret> d }
     81         }
     82 
     83         try %{
     84             # if the previous line isn't within a comment scope, break
     85             execute-keys -draft kx <a-k>^(\h*/\*|\h+\*(?!/))<ret>
     86 
     87             # find comment opening, validate it was not closed, and check its using star prefixes
     88             execute-keys -draft <a-?>/\*<ret><a-H> <a-K>\*/<ret> <a-k>\A\h*/\*(\N*\n\h*\*)*\N*\n\h*.\z<ret>
     89 
     90             try %{
     91                 # if the previous line is opening the comment, insert star preceeded by space
     92                 execute-keys -draft kx<a-k>^\h*/\*<ret>
     93                 execute-keys -draft i*<space><esc>
     94             } catch %{
     95                try %{
     96                     # if the next line is a comment line insert a star
     97                     execute-keys -draft jx<a-k>^\h+\*<ret>
     98                     execute-keys -draft i*<space><esc>
     99                 } catch %{
    100                     try %{
    101                         # if the previous line is an empty comment line, close the comment scope
    102                         execute-keys -draft kx<a-k>^\h+\*\h+$<ret> x1s\*(\h*)<ret>c/<esc>
    103                     } catch %{
    104                         # if the previous line is a non-empty comment line, add a star
    105                         execute-keys -draft i*<space><esc>
    106                     }
    107                 }
    108             }
    109 
    110             # trim trailing whitespace on the previous line
    111             try %{ execute-keys -draft k x s\h+$<ret> d }
    112             # align the new star with the previous one
    113             execute-keys Kx1s^[^*]*(\*)<ret>&
    114         }
    115     }
    116 }
    117 
    118 )