kakoune

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

haskell.kak (6048B)


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