kakoune

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

hbs.kak (4159B)


      1 # http://handlebarsjs.com/
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*[.](hbs) %{
      8     set-option buffer filetype hbs
      9 }
     10 
     11 hook global WinSetOption filetype=hbs %{
     12     require-module hbs
     13 
     14     hook window ModeChange pop:insert:.* -group hbs-trim-indent hbs-trim-indent
     15     hook window InsertChar \n -group hbs-insert hbs-insert-on-new-line
     16     hook window InsertChar \n -group hbs-indent hbs-indent-on-new-line
     17     hook window InsertChar .* -group hbs-indent hbs-indent-on-char
     18     hook window InsertChar '>' -group hbs-indent html-indent-on-greater-than
     19     hook window InsertChar \n -group hbs-indent html-indent-on-new-line
     20 
     21     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window hbs-.+ }
     22 }
     23 
     24 hook -group hbs-highlight global WinSetOption filetype=hbs %{
     25     maybe-add-hbs-to-html
     26     add-highlighter window/hbs-file ref hbs-file
     27     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/hbs-file }
     28 }
     29 
     30 
     31 provide-module hbs %[
     32 
     33 require-module html
     34 
     35 # Highlighters
     36 # ‾‾‾‾‾‾‾‾‾‾‾‾
     37 
     38 add-highlighter shared/hbs regions
     39 add-highlighter shared/hbs/comment          region \{\{!--  --\}\} fill comment
     40 add-highlighter shared/hbs/comment_alt      region \{\{!      \}\} fill comment
     41 add-highlighter shared/hbs/block-expression region \{\{[#/]   \}\} regions
     42 add-highlighter shared/hbs/expression       region \{\{       \}\} regions
     43 
     44 define-command -hidden add-mutual-highlighters -params 1 %~
     45     add-highlighter "shared/hbs/%arg{1}/code" default-region group
     46     add-highlighter "shared/hbs/%arg{1}/single-quote" region '"'    (?<!\\)(\\\\)*" fill string
     47     add-highlighter "shared/hbs/%arg{1}/double-quote" region "'"    (?<!\\)(\\\\)*' fill string
     48     add-highlighter "shared/hbs/%arg{1}/code/variable" regex \b([\w-]+)\b 1:variable
     49     add-highlighter "shared/hbs/%arg{1}/code/attribute" regex \b([\w-]+)= 1:attribute
     50     add-highlighter "shared/hbs/%arg{1}/code/helper" regex (?:(?:\{\{)|\()([#/]?[\w-]+(?:/[\w-]+)*) 1:keyword
     51 ~
     52 
     53 add-mutual-highlighters expression
     54 add-mutual-highlighters block-expression
     55 
     56 add-highlighter shared/hbs/block-expression/code/yield regex \b(as)\s|[\w-]+|\}\} 1:keyword
     57 
     58 
     59 # wrapper around shared/html highlighter.  The shared/hbs highlighter will be
     60 # added into shared/html when a buffer of filetype 'hbs' is actively displayed in the window.
     61 add-highlighter shared/hbs-file regions
     62 add-highlighter shared/hbs-file/html default-region ref html
     63 
     64 # Commands
     65 # ‾‾‾‾‾‾‾‾
     66 
     67 define-command -hidden hbs-trim-indent %{
     68     # remove trailing white spaces
     69     try %{ execute-keys -draft -itersel x s \h+$ <ret> d }
     70 }
     71 
     72 define-command -hidden hbs-indent-on-char %[
     73     evaluate-commands -draft -itersel %[
     74         # de-indent after closing a yielded block tag
     75         try %[ execute-keys -draft , <a-h> s ^\h+\{\{/([\w-.]+(?:/[\w-.]+)*)\}\}$ <ret> {c\{\{#<c-r>1,\{\{/<c-r>1\}\} <ret> s \A|.\z <ret> 1<a-&> ]
     76     ]
     77 ]
     78 
     79 define-command -hidden hbs-insert-on-new-line %{
     80     evaluate-commands -draft -itersel %{
     81         # copy '/' comment prefix and following white spaces
     82         try %{ execute-keys -draft k x s ^\h*\K/\h* <ret> y j p }
     83     }
     84 }
     85 
     86 define-command -hidden hbs-indent-on-new-line %{
     87     evaluate-commands -draft -itersel %{
     88         # preserve previous line indent
     89         try %{ execute-keys -draft <semicolon> K <a-&> }
     90         # filter previous line
     91         try %{ execute-keys -draft k : hbs-trim-indent <ret> }
     92         # indent after lines beginning with : or -
     93         try %{ execute-keys -draft k x <a-k> ^\h*[:-] <ret> j <a-gt> }
     94     }
     95 }
     96 
     97 # Initialization
     98 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     99 
    100 declare-option bool hbs_highlighters_enabled false
    101 
    102 define-command -hidden maybe-add-hbs-to-html %{ evaluate-commands %sh{
    103     if [ "$kak_opt_hbs_highlighters_enabled" = "false" ]; then
    104         printf %s "
    105             add-highlighter shared/html/hbs region '\{\{' '\}\}' ref hbs
    106             add-highlighter shared/html/tag/hbs region '\{\{' '\}\}' ref hbs
    107             set-option global hbs_highlighters_enabled true
    108         "
    109     fi
    110 } }
    111 
    112 ]