kakoune

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

graphql.kak (3986B)


      1 # http://graphql.org
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*[.](graphqls?) %{
      8     set-option buffer filetype graphql
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=graphql %{
     15     require-module graphql
     16 
     17     hook window ModeChange pop:insert:.* -group graphql-trim-indent graphql-trim-indent
     18     hook window InsertChar .* -group graphql-indent graphql-indent-on-char
     19     hook window InsertChar \n -group graphql-indent graphql-indent-on-new-line
     20 
     21     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window graphql-.+ }
     22 }
     23 
     24 hook -group graphql-highlight global WinSetOption filetype=graphql %{
     25     add-highlighter window/graphql ref graphql
     26     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/graphql }
     27 }
     28 
     29 
     30 provide-module graphql %§ 
     31 
     32 # Highlighters
     33 # ‾‾‾‾‾‾‾‾‾‾‾‾
     34 
     35 add-highlighter shared/graphql regions
     36 add-highlighter shared/graphql/code default-region group
     37 add-highlighter shared/graphql/line-description region '#' '\n' fill comment
     38 add-highlighter shared/graphql/block-description region '"""' '"""' fill comment
     39 add-highlighter shared/graphql/description region '"' '"\s*\n' fill comment
     40 add-highlighter shared/graphql/object region -recurse \{ [{] [}] regions
     41 
     42 # Objects
     43 add-highlighter shared/graphql/object/line-description region '#' '\n' fill comment
     44 add-highlighter shared/graphql/object/block-description region '"""' '"""' fill comment
     45 add-highlighter shared/graphql/object/field default-region group
     46 add-highlighter shared/graphql/object/field/ regex ([A-Za-z][A-Za-z0-9_-]*)(?:\([^)]*\))?\h*[:{] 1:attribute
     47 add-highlighter shared/graphql/object/field/ regex ^\h*([A-Za-z][A-Za-z0-9_-]*)\h*$ 1:attribute
     48 
     49 # Values
     50 add-highlighter shared/graphql/object/field/values regex \b(true|false|null|\d+(?:\.\d+)?(?:[eE][+-]?\d*)?)\b 0:value
     51 add-highlighter shared/graphql/object/field/variables regex \$[a-zA-Z0-9]+\b 0:variable
     52 # add-highlighter shared/graphql/object/field/string regex '"([^"]|\\")*"' 0:string
     53 add-highlighter shared/graphql/object/field/string regex '"(?:[^"\\]|\\.)*"' 0:string
     54 
     55 # Meta
     56 add-highlighter shared/graphql/object/field/directives regex @(?:include|skip) 0:meta
     57 
     58 # Attributes
     59 add-highlighter shared/graphql/object/field/required regex '(?<=[\w\]])(?<bang>!)' bang:operator
     60 add-highlighter shared/graphql/object/field/assignment regex '=' 0:operator
     61 
     62 # Keywords
     63 add-highlighter shared/graphql/code/top-level regex '\bschema\b' 0:keyword
     64 add-highlighter shared/graphql/code/keywords regex '\b(?<name>enum|fragment|input|implements|interface|mutation|on|query|scalar|subscription|type|union)\h+(?:[A-Za-z]\w*)' name:keyword
     65 
     66 # Types
     67 add-highlighter shared/graphql/object/field/scalars regex \b(Boolean|Float|ID|Int|String)\b 0:type
     68 
     69 # Operators
     70 add-highlighter shared/graphql/object/field/expand-fragment regex '\.\.\.(?=\w)' 0:operator
     71 
     72 # Commands
     73 # ‾‾‾‾‾‾‾‾
     74 
     75 define-command -hidden graphql-trim-indent %{
     76     # remove trailing white spaces
     77     try %{ execute-keys -draft -itersel x s \h+$ <ret> d }
     78 }
     79 
     80 define-command -hidden graphql-indent-on-char %<
     81     evaluate-commands -draft -itersel %<
     82         # align closer token to its opener when alone on a line
     83         try %< execute-keys -draft <a-h> <a-k> ^\h+[\]}]$ <ret> m <a-S> 1<a-&> >
     84     >
     85 >
     86 
     87 define-command -hidden graphql-indent-on-new-line %<
     88     evaluate-commands -draft -itersel %<
     89         # preserve previous line indent
     90         try %{ execute-keys -draft <semicolon> K <a-&> }
     91         # filter previous line
     92         try %{ execute-keys -draft k : graphql-trim-indent <ret> }
     93         # indent after lines ending with opener token
     94         try %< execute-keys -draft k x <a-k> [[{]\h*$ <ret> j <a-gt> >
     95         # deindent closer token(s) when after cursor
     96         try %< execute-keys -draft x <a-k> ^\h*[}\]] <ret> gh / [}\]] <ret> m <a-S> 1<a-&> >
     97     >
     98 >
     99 
    100 §