kakoune

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

hjson.kak (4803B)


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