kakoune

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

pony.kak (7048B)


      1 # http://ponylang.org
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*[.](pony) %{
      8     set-option buffer filetype pony
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=pony %{
     15     require-module pony
     16 
     17     set-option window static_words %opt{pony_static_words}
     18 
     19     # cleanup trailing whitespaces on current line insert end
     20     hook window ModeChange pop:insert:.* -group pony-trim-indent %{ try %{ execute-keys -draft <semicolon> x s ^\h+$ <ret> d } }
     21     hook window InsertChar \n -group pony-indent pony-indent-on-new-line
     22     hook window InsertChar \n -group pony-insert pony-insert-on-new-line
     23 
     24     set-option buffer extra_word_chars '_' "'"
     25 
     26     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window pony-.+ }
     27 }
     28 
     29 hook -group pony-highlight global WinSetOption filetype=pony %{
     30     add-highlighter window/pony ref pony
     31     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter pony }
     32 }
     33 
     34 provide-module pony %§
     35 
     36 # Highlighters & Completion
     37 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     38 
     39 add-highlighter shared/pony regions
     40 add-highlighter shared/pony/code default-region group
     41 
     42 add-highlighter shared/pony/comment region '/\*' '\*/' fill comment
     43 add-highlighter shared/pony/line_comment region '//' '$' fill comment
     44 
     45 # String literals
     46 add-highlighter shared/pony/string region -match-capture '("""|")' '(?<!\\)(?:\\\\)*("""|")' group
     47 add-highlighter shared/pony/string/ fill string
     48 add-highlighter shared/pony/string/ regex '(?:\\a|\\b|\\e|\\f|\\n|\\r|\\t||\\v|\\\\|\\''|\\0|\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\u[0-9a-fA-F]{6})' 0:meta
     49 
     50 # Character literals
     51 # negative lookbehind to account for variables with prime such as myvar' or myvar''
     52 add-highlighter shared/pony/character region "(?<![a-z0-9'])'" (?<!\\)(\\\\)*' group
     53 add-highlighter shared/pony/character/ fill string
     54 add-highlighter shared/pony/character/ regex '(?:\\a|\\b|\\e|\\f|\\n|\\r|\\t||\\v|\\\\|\\''|\\0|\\x[0-9a-fA-F]{2})' 0:meta
     55 
     56 # Operators
     57 add-highlighter shared/pony/code/ regex '=|\+|-|\*|/|%|%%|<<|>>|==|!=|<|<=|>=|>|=>|;' 0:operator
     58 add-highlighter shared/pony/code/ regex '(\+|-|\*|/|%|%%|<<|>>|==|!=|<|<=|>=|>)~' 0:operator
     59 add-highlighter shared/pony/code/ regex '(\+|-|\*|/|%|%%)\?' 0:operator
     60 add-highlighter shared/pony/code/ regex '^\h*|' 0:operator
     61 add-highlighter shared/pony/code/ regex '\b_\b' 0:operator
     62 
     63 # Integer literals
     64 add-highlighter shared/pony/code/ regex '\b[0-9](_?[0-9])*\b' 0:value
     65 add-highlighter shared/pony/code/ regex '\b0x[0-9a-fA-F](_?[0-9a-fA-F])*\b' 0:value
     66 add-highlighter shared/pony/code/ regex '\b0b[01](_?[01])*\b' 0:value
     67 
     68 # Float literals
     69 add-highlighter shared/pony/code/ regex '\b[0-9]+\.[0-9]+(?:[eE][-+]?[0-9]+)?\b' 0:value
     70 add-highlighter shared/pony/code/ regex '\b[0-9]+[eE][-+]?[0-9]+\b' 0:value
     71 
     72 # Type literals
     73 add-highlighter shared/pony/code/ regex '\b_?[A-Z][A-Za-z0-9]*\b' 0:type
     74 
     75 # Literal words are highlighted below to also allow for autocompletion by appending
     76 # them to static_words.
     77 evaluate-commands %sh{
     78     # Grammar
     79     values="true|false|this"
     80     meta='use'
     81     # Keyword list is collected using `keyword.kwlist` from `keyword`
     82     keywords="as|break|match|continue|else|elseif|try|in|return|end|for|is"
     83     keywords="${keywords}|recover|consume|error|do|then|if|while|where|with"
     84     keywords="${keywords}|class|struct|object|actor|interface|trait|primitive"
     85     keywords="${keywords}|type|var|let|embed|repeat|until|addressof"
     86     func_decl="new|fun|be"
     87     capabilities="iso|ref|box|tag|val|trn"
     88     operators="and|or|xor|not"
     89     builtin_types="String|None|Any|Array"
     90 
     91     # Add the language's grammar to the static completion list
     92     static_words="${values} ${meta} ${keywords} ${types_decl} ${capabilities}"
     93     static_words="${static_words} ${operators} ${builtin_types}"
     94     printf %s\\n "declare-option str-list pony_static_words ${static_words}" | tr '|' ' '
     95 
     96     # Highlight keywords
     97     # The (?!') ensures that keywords with prime are highlighted as usual, for example try'
     98     printf %s "
     99         add-highlighter shared/pony/code/ regex \b(${values})\b(?!') 0:value
    100         add-highlighter shared/pony/code/ regex \b(${operators})\b(?!') 0:operator
    101         add-highlighter shared/pony/code/ regex \b(${meta})\b(?!') 0:meta
    102         add-highlighter shared/pony/code/ regex \b(${func_decl})(\s+(${capabilities}))?(\s+\w+)[\[(] 1:keyword 3:attribute 4:function
    103         add-highlighter shared/pony/code/ regex \b(${func_decl})\b(?!') 0:keyword
    104         add-highlighter shared/pony/code/ regex \b(${keywords})\b(?!') 0:keyword
    105         add-highlighter shared/pony/code/ regex \b(${capabilities})\b(?!')(!|\\^)? 1:attribute 2:attribute
    106     "
    107 
    108     # Highlight types and attributes
    109     printf %s "
    110         add-highlighter shared/pony/code/ regex '@[\w_]+\b' 0:attribute
    111     "
    112 }
    113 
    114 # Commands
    115 # ‾‾‾‾‾‾‾‾
    116 
    117 define-command -hidden pony-insert-on-new-line %<
    118     evaluate-commands -no-hooks -draft -itersel %<
    119         # copy // comments prefix and following white spaces
    120         try %{ execute-keys -draft k x s ^\h*\K//\h* <ret> y jgi P }
    121 
    122         # wisely add `end` keyword
    123         evaluate-commands -save-regs x %<
    124             try %{ execute-keys -draft k x s ^ \h + <ret> \" x y } catch %{ reg x '' }
    125             try %<
    126                 evaluate-commands -draft %<
    127                     # Check if previous line opens a block
    128                     execute-keys -draft kx <a-k>^<c-r>x(try|match|recover|repeat|object|.+\bdo$|.+\bthen$)[^0-9A-Za-z_']<ret>
    129                     # Check that we didn't put an end on the previous line that closes the block
    130                     execute-keys -draft kx <a-K> \bend$<ret>
    131                     # Check that we do not already have an end for this indent level which is first set via `pony-indent-on-new-line` hook
    132                     execute-keys -draft }i J x <a-K> ^<c-r>x(end|else|elseif|until)[^0-9A-Za-z_']<ret>
    133                 >
    134                 execute-keys -draft o<c-r>xend<esc> # insert a new line with containing end
    135             >
    136         >
    137     >
    138 >
    139 
    140 define-command -hidden pony-indent-on-new-line %{
    141     evaluate-commands -no-hooks -draft -itersel %{
    142         # preserve previous line indent
    143         try %{ execute-keys -draft , K <a-&> }
    144         # cleanup trailing whitespaces from previous line
    145         try %{ execute-keys -draft k x s \h+$ <ret> d }
    146         # indent after line ending with: =>, do, try, then, else, =; or
    147         # starting with: actor, class, struct, trait, interface, if, elseif, recover, repeat
    148         # excluding: primitive, type -- because they are often written in a single line
    149         try %{ execute-keys -draft , k x <a-k> (\b(?:do|try|then|else|recover|repeat)|=>|=)$ | ^\h*(actor|class|struct|trait|interface|object)\b <ret> j <a-gt> }
    150         # else, end are always de-indented
    151         try %{ execute-keys -draft , k x <a-k> \b(else|end):$ <ret> k x s ^\h* <ret> y j x <a-k> ^<c-r>" <ret> J <a-lt> }
    152     }
    153 }
    154 
    155 §