kakoune

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

vala.kak (5226B)


      1 # https://vala.dev
      2 #
      3 # Vala Reference Manual, Types and Preprocessor, viewed 1 October 2026, https://gnome.pages.gitlab.gnome.org/vala/manual/
      4 # Vala 0.56 scanner (keywords, literals and escape sequences), https://gitlab.gnome.org/GNOME/vala/-/blob/0.56/vala/valascanner.vala
      5 
      6 # Detection
      7 # ‾‾‾‾‾‾‾‾‾
      8 
      9 hook global BufCreate .*[.](vala|vapi) %{
     10     set-option buffer filetype vala
     11 }
     12 
     13 # Initialization
     14 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     15 
     16 hook global WinSetOption filetype=vala %{
     17     require-module vala
     18 
     19     set-option window static_words %opt{vala_static_words}
     20 
     21     # cleanup trailing whitespaces when exiting insert mode
     22     hook window ModeChange pop:insert:.* -group vala-trim-indent c-family-trim-indent
     23     hook window InsertChar \n -group vala-insert c-family-insert-on-newline
     24     hook window InsertChar \n -group vala-indent c-family-indent-on-newline
     25     hook window InsertChar \n -group vala-indent vala-indent-on-new-line
     26     hook window InsertChar \{ -group vala-indent c-family-indent-on-opening-curly-brace
     27     hook window InsertChar \} -group vala-indent c-family-indent-on-closing-curly-brace
     28 
     29     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window vala-.+ }
     30 }
     31 
     32 hook -group vala-highlight global WinSetOption filetype=vala %{
     33     add-highlighter window/vala ref vala
     34     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/vala }
     35 }
     36 
     37 provide-module vala %§
     38 
     39 require-module c-family
     40 
     41 # Highlighters
     42 # ‾‾‾‾‾‾‾‾‾‾‾‾
     43 
     44 add-highlighter shared/vala regions
     45 add-highlighter shared/vala/code default-region group
     46 add-highlighter shared/vala/verbatim_string region '"""' '"""' fill string
     47 add-highlighter shared/vala/template_string region '@"' %{(?<!\\)(\\\\)*"} group
     48 add-highlighter shared/vala/string region %{(?<!')"} %{(?<!\\)(\\\\)*"} group
     49 add-highlighter shared/vala/character region %{'} %{(?<!\\)(\\\\)*'} fill value
     50 add-highlighter shared/vala/documentation region /\*\*(?!/) \*/ fill documentation
     51 add-highlighter shared/vala/comment region /\* \*/ fill comment
     52 add-highlighter shared/vala/line_comment region // $ fill comment
     53 add-highlighter shared/vala/regex region (?<=[=(,:!&|?])\h*/(?![/*]) (?<!\\)(\\\\)*/[imsx]* fill meta
     54 add-highlighter shared/vala/preprocessor region ^\h*# $ fill meta
     55 
     56 add-highlighter shared/vala/string/ fill string
     57 add-highlighter shared/vala/string/ regex \\(?:[\\'"0bfnrtv$]|x[0-9a-fA-F]+|u[0-9a-fA-F]{4}) 0:meta
     58 
     59 add-highlighter shared/vala/template_string/ fill string
     60 add-highlighter shared/vala/template_string/ regex \\(?:[\\'"0bfnrtv$]|x[0-9a-fA-F]+|u[0-9a-fA-F]{4}) 0:meta
     61 add-highlighter shared/vala/template_string/ regex \$(?:\w+|\([^)]*\)) 0:value
     62 
     63 add-highlighter shared/vala/code/ regex \b([a-zA-Z_]\w*)\h*(?=\() 1:function
     64 add-highlighter shared/vala/code/ regex \b[A-Z]\w*[a-z]\w*\b 0:type
     65 add-highlighter shared/vala/code/ regex (?<![\w.])(?:0x[0-9a-fA-F]+|\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)(?:[lL]{1,2}|[uU][lL]{0,2}|[fFdD])?\b 0:value
     66 add-highlighter shared/vala/code/ regex ^\h*\[\h*\K[A-Z]\w* 0:meta
     67 add-highlighter shared/vala/code/ regex \bglobal(?=::) 0:keyword
     68 
     69 # Commands
     70 # ‾‾‾‾‾‾‾‾
     71 
     72 # c-family handles if, for and while; this adds foreach
     73 define-command -hidden vala-indent-on-new-line %< evaluate-commands -draft -itersel %<
     74     execute-keys <semicolon>
     75     # indent after a foreach not followed by an opening brace
     76     try %< execute-keys -draft k x s\)\h*(?://\N+)?\n\z<ret> \
     77                                <a-semicolon>mB <a-k>\A\bforeach\b<ret> <a-semicolon>j <a-gt> >
     78     # deindent after its single line statement
     79     try %< execute-keys -draft K x <a-k>\;\h*(//\N+)?$<ret> \
     80                                K x s\)(\h+\w+)*\h*(//\N+)?\n(\N*\n){2}\z<ret> \
     81                                MB <a-k>\A\bforeach\b<ret> <a-S>1<a-&> >
     82 > >
     83 
     84 # Shell
     85 # ‾‾‾‾‾
     86 
     87 evaluate-commands %sh{
     88     values='true false null this base'
     89 
     90     # fundamental types from the reference manual, plus string, void and var
     91     types='bool char uchar short ushort int uint long ulong size_t ssize_t int8
     92         uint8 int16 uint16 int32 uint32 int64 uint64 unichar float double string
     93         void var'
     94 
     95     keywords='if else switch case default do while for foreach in break continue
     96         return try catch finally throw throws lock unlock yield new delete sizeof
     97         typeof is as out ref get set construct using namespace class struct
     98         interface enum errordomain delegate signal with requires ensures params'
     99 
    100     attributes='abstract const dynamic extern inline internal override owned
    101         partial private protected public sealed static unowned virtual volatile
    102         weak async'
    103 
    104     join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; }
    105 
    106     printf %s\\n "declare-option str-list vala_static_words $(join "${values} ${types} ${keywords} ${attributes}" ' ')"
    107 
    108     # skip @-escaped identifiers and member accesses such as map.set ()
    109     for pair in "values:value" "types:type" "keywords:keyword" "attributes:attribute"; do
    110         var=${pair%%:*} face=${pair##*:}
    111         eval words=\$$var
    112         printf 'add-highlighter shared/vala/code/ regex %s %s\n' "(?<![@.\\w])($(join "${words}" '|'))\\b" "1:$face"
    113     done
    114 }
    115 
    116 §