kakoune

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

purescript.kak (5613B)


      1 # http://purescript.org
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 # Syntax reference
      4 # https://github.com/purescript/documentation/blob/master/language/Syntax.md
      5 
      6 # Detection
      7 # ‾‾‾‾‾‾‾‾‾
      8 
      9 hook global BufCreate .*[.](purs) %{
     10     set-option buffer filetype purescript
     11 }
     12 
     13 # Initialization
     14 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     15 
     16 hook global WinSetOption filetype=purescript %{
     17     require-module purescript
     18 
     19     set-option buffer extra_word_chars '_' "'"
     20     hook window ModeChange pop:insert:.* -group purescript-trim-indent purescript-trim-indent
     21     hook window InsertChar \n -group purescript-insert purescript-insert-on-new-line
     22     hook window InsertChar \n -group purescript-indent purescript-indent-on-new-line
     23 
     24     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window purescript-.+ }
     25 }
     26 
     27 hook -group purescript-highlight global WinSetOption filetype=purescript %{
     28     add-highlighter window/purescript ref purescript
     29     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/purescript }
     30 }
     31 
     32 
     33 provide-module purescript %§
     34 
     35 # Highlighters
     36 # ‾‾‾‾‾‾‾‾‾‾‾‾
     37 
     38 add-highlighter shared/purescript regions
     39 add-highlighter shared/purescript/code default-region group
     40 add-highlighter shared/purescript/string       region (?<!'\\)(?<!')"                 (?<!\\)(\\\\)*"  fill string
     41 add-highlighter shared/purescript/comment      region -recurse \{- \{-                  -\}            fill comment
     42 add-highlighter shared/purescript/line_comment region --(?:[^!#$%&*+./<>?@\\\^|~=]|$) $                fill comment
     43 
     44 add-highlighter shared/purescript/code/ regex (?<!')\b0x+[A-Fa-f0-9]+ 0:value
     45 add-highlighter shared/purescript/code/ regex (?<!')\b\d+([.]\d+)? 0:value
     46 add-highlighter shared/purescript/code/ regex (?<!')\b(import|hiding|module)(?!')\b 0:keyword
     47 add-highlighter shared/purescript/code/ regex (?<!')\b(import)(?!')\b\N+(?<!')\b(as)(?!')\b 2:keyword
     48 add-highlighter shared/purescript/code/ regex (?<!')\b(class|data|default|derive|infix|infixl|infixr|instance|module|newtype|pattern|type|where)(?!')\b 0:keyword
     49 add-highlighter shared/purescript/code/ regex (?<!')\b(case|do|else|if|in|let|mdo|of|proc|rec|then)(?!')\b 0:attribute
     50 add-highlighter shared/purescript/code/ regex (?<!')\b(type|data)\b\s+(\bfamily\b)?(?!') 0:keyword
     51 
     52 # The complications below is because period has many uses:
     53 # As function composition operator (possibly without spaces) like "." and "f.g"
     54 # Hierarchical modules like "Data.Maybe"
     55 # Qualified imports like "Data.Maybe.Just", "Data.Maybe.maybe", "Control.Applicative.<$>"
     56 # Quantifier separator in "forall a . [a] -> [a]"
     57 # Enum comprehensions like "[1..]" and "[a..b]" (making ".." and "Module..." illegal)
     58 
     59 # matches uppercase identifiers:  Monad Control.Monad
     60 # not non-space separated dot:    Just.const
     61 add-highlighter shared/purescript/code/ regex \b([A-Z]['\w]*\.)*[A-Z]['\w]*(?!['\w])(?![.a-z]) 0:variable
     62 
     63 # matches infix identifier: `mod` `Apa._T'M`
     64 add-highlighter shared/purescript/code/ regex `\b([A-Z]['\w]*\.)*[\w]['\w]*` 0:operator
     65 # matches imported operators: M.! M.. Control.Monad.>>
     66 # not operator keywords:      M... M.->
     67 add-highlighter shared/purescript/code/ regex \b[A-Z]['\w]*\.[~<=>|:!?/.@$*&#%+\^\-\\]+ 0:operator
     68 # matches dot: .
     69 # not possibly incomplete import:  a.
     70 # not other operators:             !. .!
     71 add-highlighter shared/purescript/code/ regex (?<![\w~<=>|:!?/.@$*&#%+\^\-\\])\.(?![~<=>|:!?/.@$*&#%+\^\-\\]) 0:operator
     72 # matches other operators: ... > < <= ^ <*> <$> etc
     73 # not dot: .
     74 # not operator keywords:  @ .. -> :: ~
     75 add-highlighter shared/purescript/code/ regex (?<![~<=>|:!?/.@$*&#%+\^\-\\])[~<=>|:!?/.@$*&#%+\^\-\\]+ 0:operator
     76 
     77 # matches operator keywords: @ ->
     78 add-highlighter shared/purescript/code/ regex (?<![~<=>|:!?/.@$*&#%+\^\-\\])(@|~|<-|->|=>|::|=|:|[|])(?![~<=>|:!?/.@$*&#%+\^\-\\]) 1:keyword
     79 # matches: forall [..variables..] .
     80 # not the variables
     81 add-highlighter shared/purescript/code/ regex \b(forall|∀)\b[^.\n]*?(\.) 1:keyword 2:keyword
     82 
     83 # matches 'x' '\\' '\'' '\n' '\0'
     84 # not incomplete literals: '\'
     85 # not valid identifiers:   w' _'
     86 add-highlighter shared/purescript/code/ regex \B'([^\\]|[\\]['"\w\d\\])' 0:string
     87 # this has to come after operators so '-' etc is correct
     88 
     89 # matches function names in type signatures
     90 add-highlighter shared/purescript/code/ regex ^\s*(?:where\s+|let\s+|default\s+)?([_a-z]['\w]*#?(?:,\s*[_a-z]['\w]*#?)*)\s+::\s 1:meta
     91 
     92 # Commands
     93 # ‾‾‾‾‾‾‾‾
     94 
     95 define-command -hidden purescript-trim-indent %{
     96     # remove trailing white spaces
     97     try %{ execute-keys -draft -itersel x s \h+$ <ret> d }
     98 }
     99 
    100 define-command -hidden purescript-insert-on-new-line %{
    101     evaluate-commands -draft -itersel %{
    102         # copy -- comments prefix and following white spaces
    103         try %{ execute-keys -draft k x s ^\h*\K--\h* <ret> y gh j P }
    104     }
    105 }
    106 
    107 define-command -hidden purescript-indent-on-new-line %{
    108     evaluate-commands -draft -itersel %{
    109         # preserve previous line indent
    110         try %{ execute-keys -draft <semicolon> K <a-&> }
    111         # align to first clause
    112         try %{ execute-keys -draft <semicolon> k x X s ^\h*(if|then|else)?\h*(([\w']+\h+)+=)?\h*(case\h+[\w']+\h+of|do|let|where)\h+\K.* <ret> s \A|.\z <ret> & }
    113         # filter previous line
    114         try %{ execute-keys -draft k : purescript-trim-indent <ret> }
    115         # indent after lines beginning with condition or ending with expression or =(
    116         try %{ execute-keys -draft <semicolon> k x <a-k> ^\h*if|[=(]$|\b(case\h+[\w']+\h+of|do|let|where)$ <ret> j <a-gt> }
    117     }
    118 }
    119 
    120 §