kakoune

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

protobuf.kak (4193B)


      1 # https://developers.google.com/protocol-buffers/
      2 
      3 # Detection
      4 # ‾‾‾‾‾‾‾‾‾
      5 
      6 hook global BufCreate .*\.proto$ %{
      7     set-option buffer filetype protobuf
      8 }
      9 
     10 # Initialization
     11 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     12 
     13 hook global WinSetOption filetype=protobuf %[
     14     require-module protobuf
     15 
     16     set-option window static_words %opt{protobuf_static_words}
     17 
     18     hook window ModeChange pop:insert:.* -group protobuf-trim-indent protobuf-trim-indent
     19     hook -group protobuf-insert window InsertChar \n protobuf-insert-on-newline
     20     hook -group protobuf-indent window InsertChar \n protobuf-indent-on-newline
     21     hook -group protobuf-indent window InsertChar \{ protobuf-indent-on-opening-curly-brace
     22     hook -group protobuf-indent window InsertChar \} protobuf-indent-on-closing-curly-brace
     23 
     24     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window protobuf-.+ }
     25 ]
     26 
     27 hook -group protobuf-highlight global WinSetOption filetype=protobuf %{
     28     add-highlighter window/protobuf ref protobuf
     29     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/protobuf }
     30 }
     31 
     32 provide-module protobuf %[
     33 
     34 # Highlighters
     35 # ‾‾‾‾‾‾‾‾‾‾‾‾
     36 
     37 add-highlighter shared/protobuf regions
     38 add-highlighter shared/protobuf/code default-region group
     39 add-highlighter shared/protobuf/double_string region '"' (?<!\\)(\\\\)*" fill string
     40 add-highlighter shared/protobuf/comment region /\* \*/ fill comment
     41 add-highlighter shared/protobuf/comment_line region '//' $ fill comment
     42 
     43 add-highlighter shared/protobuf/code/ regex %{(0x)?[0-9]+\b} 0:value
     44 
     45 evaluate-commands %sh{
     46     # Grammer
     47     keywords='default deprecated enum extend import message oneof option
     48               package service syntax'
     49     attributes='optional repeated required'
     50     types='double float int32 int64 uint32 uint64 sint32 sint64 fixed32 fixed64
     51            sfixed32 sfixed64 bool string bytes rpc'
     52     values='false true'
     53 
     54     join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; }
     55 
     56     # Add the language's grammer to the static completion list
     57     printf %s\\n "declare-option str-list protobuf_static_words $(join "${keywords} ${attributes} ${types} ${values}" ' ')"
     58 
     59     # Highlight keywords
     60     printf %s "
     61         add-highlighter shared/protobuf/code/keywords regex \b($(join "${keywords}" '|'))\b 0:keyword
     62         add-highlighter shared/protobuf/code/attributes regex \b($(join "${attributes}" '|'))\b 0:attribute
     63         add-highlighter shared/protobuf/code/types regex \b($(join "${types}" '|'))\b 0:type
     64         add-highlighter shared/protobuf/code/values regex \b($(join "${values}" '|'))\b 0:value
     65     "
     66 }
     67 
     68 # Commands
     69 # ‾‾‾‾‾‾‾‾
     70 
     71 define-command -hidden protobuf-trim-indent %{
     72     evaluate-commands -no-hooks -draft -itersel %{
     73         execute-keys x
     74         # remove trailing white spaces
     75         try %{ execute-keys -draft s \h + $ <ret> d }
     76     }
     77 }
     78 
     79 define-command -hidden protobuf-insert-on-newline %{
     80     evaluate-commands -draft -itersel %[
     81         # copy // comments prefix
     82         try %{ execute-keys -draft <semicolon><c-s>kx s ^\h*\K/{2,}(\h*(?=\S))? <ret> y<c-o>P<esc> }
     83     ]
     84 }
     85 
     86 define-command -hidden protobuf-indent-on-newline %~
     87     evaluate-commands -draft -itersel %[
     88         # preserve previous line indent
     89         try %{ execute-keys -draft <semicolon>K<a-&> }
     90         # indent after lines ending with {
     91         try %[ execute-keys -draft kx <a-k> \{\h*$ <ret> j<a-gt> ]
     92         # cleanup trailing white spaces on the previous line
     93         try %{ execute-keys -draft kx s \h+$ <ret>d }
     94         # deindent closing brace(s) when after cursor
     95         try %[ execute-keys -draft x <a-k> ^\h*\} <ret> gh / \} <ret> m <a-S> 1<a-&> ]
     96     ]
     97 ~
     98 
     99 define-command -hidden protobuf-indent-on-opening-curly-brace %[
    100     # align indent with opening paren when { is entered on a new line after the closing paren
    101     try %[ execute-keys -draft -itersel h<a-F>)M <a-k> \A\(.*\)\h*\n\h*\{\z <ret> s \A|.\z <ret> 1<a-&> ]
    102 ]
    103 
    104 define-command -hidden protobuf-indent-on-closing-curly-brace %[
    105     # align to opening curly brace when alone on a line
    106     try %[ execute-keys -itersel -draft <a-h><a-k>^\h+\}$<ret>hms\A|.\z<ret>1<a-&> ]
    107 ]
    108 
    109 ]