kakoune

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

prql.kak (7253B)


      1 # https://prql-lang.org/
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*[.](prql) %{
      8     set-option buffer filetype prql
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=prql %{
     15     require-module prql
     16 
     17     set-option window static_words %opt{prql_static_words}
     18 
     19     hook window InsertChar \n -group prql-insert prql-insert-on-new-line
     20     hook window InsertChar \n -group prql-indent prql-indent-on-new-line
     21     # cleanup trailing whitespaces on current line insert end
     22     hook window ModeChange pop:insert:.* -group prql-trim-indent %{ try %{ execute-keys -draft <semicolon> x s ^\h+$ <ret> d } }
     23     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window prql-.+ }
     24 }
     25 
     26 hook -group prql-highlight global WinSetOption filetype=prql %{
     27     add-highlighter window/prql ref prql
     28     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/prql }
     29 }
     30 
     31 provide-module prql %§
     32 
     33 # Highlighters & Completion
     34 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     35 
     36 add-highlighter shared/prql regions
     37 add-highlighter shared/prql/code default-region group
     38 
     39 add-highlighter shared/prql/documentation region '##'  '$'              fill documentation
     40 add-highlighter shared/prql/comment       region '#'   '$'              fill comment
     41 
     42 # String interpolation
     43 add-highlighter shared/prql/f_triple_string region -match-capture f("""|''') (?<!\\)(?:\\\\)*("""|''') group
     44 add-highlighter shared/prql/f_triple_string/ fill string
     45 add-highlighter shared/prql/f_triple_string/ regex \{.*?\} 0:value
     46 
     47 add-highlighter shared/prql/f_double_string region 'f"'   (?<!\\)(\\\\)*" group
     48 add-highlighter shared/prql/f_double_string/ fill string
     49 add-highlighter shared/prql/f_double_string/ regex \{.*?\} 0:value
     50 
     51 add-highlighter shared/prql/f_single_string region "f'"   (?<!\\)(\\\\)*' group
     52 add-highlighter shared/prql/f_single_string/ fill string
     53 add-highlighter shared/prql/f_single_string/ regex \{.*?\} 0:value
     54 
     55 
     56 # Regular string
     57 add-highlighter shared/prql/triple_string region -match-capture ("""|''') (?<!\\)(?:\\\\)*("""|''') fill string
     58 add-highlighter shared/prql/double_string region '"'   (?<!\\)(\\\\)*" fill string
     59 add-highlighter shared/prql/single_string region "'"   (?<!\\)(\\\\)*' fill string
     60 
     61 # Parameter references
     62 add-highlighter shared/prql/code/ regex '\$\d+\b' 0:value
     63 
     64 # Integer formats
     65 add-highlighter shared/prql/code/ regex '(?i)\b0b[01]+l?\b' 0:value
     66 add-highlighter shared/prql/code/ regex '(?i)\b0x[\da-f]+l?\b' 0:value
     67 add-highlighter shared/prql/code/ regex '(?i)\b0o?[0-7]+l?\b' 0:value
     68 add-highlighter shared/prql/code/ regex '(?i)\b([1-9]\d*|0)l?\b' 0:value
     69 # Float formats
     70 add-highlighter shared/prql/code/ regex '\b\d+[eE][+-]?\d+\b' 0:value
     71 add-highlighter shared/prql/code/ regex '(\b\d+)?\.\d+\b' 0:value
     72 add-highlighter shared/prql/code/ regex '\b\d+\.' 0:value
     73 
     74 evaluate-commands %sh{
     75     # Grammar
     76     values="true false null this that"
     77     meta="prql module"
     78 
     79     keywords="case let type alias in loop"
     80 
     81     types="bool float int int8 int16 int32 int64 int128 text date time timestamp"
     82 
     83     functions="aggregate derive filter from group join select sort take window"
     84 
     85     join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; }
     86 
     87     # Add the language's grammar to the static completion list
     88     printf %s\\n "declare-option str-list prql_static_words $(join "${values} ${meta} ${keywords} ${types} ${functions}" ' ')"
     89 
     90     # Highlight keywords
     91     printf %s "
     92         add-highlighter shared/prql/code/ regex '\b($(join "${values}" '|'))\b' 0:value
     93         add-highlighter shared/prql/code/ regex '\b($(join "${meta}" '|'))\b' 0:meta
     94         add-highlighter shared/prql/code/ regex '\b($(join "${keywords}" '|'))\b' 0:keyword
     95         add-highlighter shared/prql/code/ regex '\b($(join "${functions}" '|'))\b' 0:builtin
     96         add-highlighter shared/prql/code/ regex '\b($(join "${types}" '|'))\b' 0:type
     97         add-highlighter shared/prql/code/ regex '^\h*(@\{\w+(?:=\w+)?(?:,\w+(?:=\w+)?)*\})' 1:attribute
     98     "
     99 }
    100 
    101 add-highlighter shared/prql/code/ regex (?<=[\w\s\d\)\]'"_])(\?\?|\|\||&&|<=|>=|<>?|>|!=|==|~=|\||\^|&|\+|-|\*\*?|//?|%|~) 0:operator
    102 add-highlighter shared/prql/code/ regex (?<=[\w\s\d'"_])((?<![=<>!]):?=(?![=])|[+*-]=) 0:builtin
    103 add-highlighter shared/prql/code/ regex ^\h*(?:module)\h+(\S+) 1:module
    104 
    105 # Date/time literals: @2024-12-20, @2024-12-20T14:30:00, @14:30, @14:30:00
    106 # These are added last so they override number and operator faces within the literal.
    107 add-highlighter shared/prql/code/ regex '@\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?)?' 0:value
    108 add-highlighter shared/prql/code/ regex '@\d{2}:\d{2}(?::\d{2})?' 0:value
    109 
    110 # Commands
    111 # ‾‾‾‾‾‾‾‾
    112 
    113 define-command -hidden prql-insert-on-new-line %{ evaluate-commands -itersel -draft %{
    114     execute-keys <semicolon>
    115     try %{
    116         evaluate-commands -draft -save-regs '/"' %{
    117             # Ensure previous line is a comment
    118             execute-keys -draft kxs^\h*#+\h*<ret>
    119 
    120             # now handle the comment continuation logic
    121             try %{
    122                 # try and match a regular block comment, copying the prefix
    123                 execute-keys -draft -save-regs '' k x 1s^(\h*#+\h*)\S.*$ <ret> y
    124                 execute-keys -draft P
    125             } catch %{
    126                 try %{
    127                     # try and match a regular block comment followed by a single
    128                     # empty comment line
    129                     execute-keys -draft -save-regs '' kKx 1s^(\h*#+\h*)\S+\n\h*#+\h*$ <ret> y
    130                     execute-keys -draft P
    131                 } catch %{
    132                     try %{
    133                         # try and match a pair of empty comment lines, and delete
    134                         # them if we match
    135                         execute-keys -draft kKx <a-k> ^\h*#+\h*\n\h*#+\h*$ <ret> <a-d>
    136                     } catch %{
    137                         # finally, we need a special case for a new line inserted
    138                         # into a file that consists of a single empty comment - in
    139                         # that case we can't expect to copy the trailing whitespace,
    140                         # so we add our own
    141                         execute-keys -draft -save-regs '' k x1s^(\h*#+)\h*$<ret> y
    142                         execute-keys -draft P
    143                         execute-keys -draft i<space>
    144                     }
    145                 }
    146             }
    147         }
    148 
    149         # trim trailing whitespace on the previous line
    150         try %{ execute-keys -draft k x s\h+$<ret> d }
    151     }
    152 } }
    153 
    154 define-command -hidden prql-indent-on-new-line %<
    155     evaluate-commands -draft -itersel %<
    156         # preserve previous line indent
    157         try %{ execute-keys -draft <semicolon> K <a-&> }
    158         # cleanup trailing whitespaces from previous line
    159         try %{ execute-keys -draft k x s \h+$ <ret> d }
    160         # indent after line ending with :
    161         try %{ execute-keys -draft , k x <a-k> :$ <ret> <a-K> ^\h*# <ret> j <a-gt> }
    162         # deindent closing brace/bracket when after cursor (for arrays and dictionaries)
    163         try %< execute-keys -draft x <a-k> ^\h*[}\]] <ret> gh / [}\]] <ret> m <a-S> 1<a-&> >
    164     >
    165 >
    166 
    167 §