kakoune

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

fsharp.kak (7637B)


      1 # https://fsharp.org/
      2 #
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*[.](fs|fsx|fsi) %{
      8     set-option buffer filetype fsharp
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=fsharp %{
     15     require-module fsharp
     16 
     17     # indent on newline
     18     hook window ModeChange pop:insert:.* -group fsharp-trim-indent fsharp-trim-indent
     19     hook window InsertChar \n -group fsharp-insert fsharp-insert-on-new-line
     20     hook window InsertChar \n -group fsharp-indent fsharp-indent-on-new-line
     21 
     22     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window fsharp-.+ }
     23 }
     24 
     25 hook -group fsharp-highlight global WinSetOption filetype=fsharp %{
     26     add-highlighter window/fsharp ref fsharp
     27     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/fsharp }
     28 }
     29 
     30 provide-module fsharp %§
     31 
     32 # Highlighters & Completion
     33 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     34 
     35 add-highlighter shared/fsharp regions
     36 add-highlighter shared/fsharp/code default-region group
     37 add-highlighter shared/fsharp/docstring region \(\*(?!\)) (\*\)) regions
     38 add-highlighter shared/fsharp/double_string region @?(?<!')" (?<!\\)(\\\\)*"B? fill string
     39 add-highlighter shared/fsharp/comment region '//' '$' fill comment
     40 # https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/attributes 
     41 add-highlighter shared/fsharp/attributes region "\[<" ">\]" fill meta
     42 
     43 add-highlighter shared/fsharp/docstring/ default-region fill comment
     44 # ability to write highlighted code inside docstring:
     45 add-highlighter shared/fsharp/docstring/ region '>>> \K' '\z' ref fsharp
     46 add-highlighter shared/fsharp/docstring/ region '\.\.\. \K' '\z' ref fsharp
     47 
     48 evaluate-commands %sh{
     49     # Grammar
     50     meta="open"
     51 
     52     # exceptions taken from fsharp.vim colors (https://github.com/fsharp/vim-fsharp)
     53     exceptions="try|failwith|failwithf|finally|invalid_arg|raise|rethrow"
     54 
     55     # keywords taken from fsharp.vim colors (https://github.com/fsharp/vim-fsharp)
     56     keywords="abstract|as|assert|base|begin|class|default|delegate"
     57     keywords="${keywords}|do|done|downcast|downto|elif|else|end|exception"
     58     keywords="${keywords}|extern|for|fun|function|global|if|in|inherit|inline"
     59     keywords="${keywords}|interface|lazy|let|match|member|module|mutable"
     60     keywords="${keywords}|namespace|new|of|override|rec|static|struct|then"
     61     keywords="${keywords}|to|type|upcast|use|val|void|when|while|with"
     62     keywords="${keywords}|async|atomic|break|checked|component|const|constraint"
     63     keywords="${keywords}|constructor|continue|decimal|eager|event|external"
     64     keywords="${keywords}|fixed|functor|include|method|mixin|object|parallel"
     65     keywords="${keywords}|process|pure|return|seq|tailcall|trait|yield"
     66     # additional operator keywords (Microsoft.FSharp.Core.Operators)
     67     keywords="${keywords}|box|hash|sizeof|nameof|typeof|typedefof|unbox|ref|fst|snd"
     68     keywords="${keywords}|stdin|stdout|stderr"
     69     # math operators (Microsoft.FSharp.Core.Operators)
     70     keywords="${keywords}|abs|acos|asin|atan|atan2|ceil|cos|cosh|exp|floor|log"
     71     keywords="${keywords}|log10|pown|round|sign|sin|sinh|sqrt|tan|tanh"
     72 
     73 
     74     types="array|bool|byte|char|decimal|double|enum|exn|float"
     75     types="${types}|float32|int|int16|int32|int64|lazy_t|list|nativeint"
     76     types="${types}|obj|option|sbyte|single|string|uint|uint32|uint64"
     77     types="${types}|uint16|unativeint|unit"
     78 
     79     fsharpCoreMethod="printf|printfn|sprintf|eprintf|eprintfn|fprintf|fprintfn"
     80 
     81     # Add the language's grammar to the static completion list
     82     printf '%s\n' "hook global WinSetOption filetype=fsharp %{
     83         set-option window static_words ${values} ${meta} ${exceptions} ${keywords} ${types}
     84     }" | tr '|' ' '
     85 
     86     # Highlight keywords
     87     printf '%s\n' "
     88         add-highlighter shared/fsharp/code/ regex '\b(${meta})\b' 0:meta
     89         add-highlighter shared/fsharp/code/ regex '\b(${exceptions})\b' 0:function
     90         add-highlighter shared/fsharp/code/ regex '\b(${fsharpCoreMethod})\b' 0:function
     91         add-highlighter shared/fsharp/code/ regex '\b(${keywords})\b' 0:keyword
     92     "
     93 }
     94 
     95 # computation expression keywords prefixed with !
     96 add-highlighter shared/fsharp/code/ regex "\w+!" 0:keyword
     97 # brackets
     98 add-highlighter shared/fsharp/code/ regex "[\[\]\(\){}]" 0:bracket
     99 # accomodate typically overloaded operators
    100 add-highlighter shared/fsharp/code/ regex "\B(<<>>|<\|\|>)\B" 0:operator
    101 # fsharp operators
    102 add-highlighter shared/fsharp/code/ regex "\B(->|<-|<=|>=)\B" 0:operator
    103 add-highlighter shared/fsharp/code/ regex "(\b(not)\b|\b(and)\b)" 0:operator
    104 add-highlighter shared/fsharp/code/ regex (?<=[\w\s\d'"_])((\?)([><+-/*%=]{1,2})(\??)|(\??)([><+-/*%=]{1,2})(\?)) 0:operator
    105 add-highlighter shared/fsharp/code/ regex (?<=[\w\s\d'"_])((\|)+>|<(\|)+|<@|@>|<@@|@@>|:>|:\?|:=|(!|#)(?=\w)|:\?>|\?|~([-+]|(~){0,2})) 0:operator
    106 add-highlighter shared/fsharp/code/ regex (?<=[\w\s\d'"_])(<>|::|\h\|\h|(\|\|)+|@|\.\.|<=|>=|(<)+|(>)+|!=|==|\|\|\||(\^)+|(&)+|\+|-|(\*)+|//|/|%+|=) 0:operator
    107 add-highlighter shared/fsharp/code/ regex (?<=[\w\s\d'"_])((?<![=<>!])=(?![=])|[+*-]=) 0:builtin
    108 # integer literals
    109 add-highlighter shared/fsharp/code/ regex %{(?<!\.)\b[1-9]('?\d+)*(u?(l|L|y|s|n)?|[IMm])\b(?!\.)} 0:value
    110 add-highlighter shared/fsharp/code/ regex %{(?<!\.)\b0b[01]('?[01]+)*(u?(l|L|y|s|n)?|[IMm])\b(?!\.)} 0:value
    111 add-highlighter shared/fsharp/code/ regex %{(?<!\.)\b0('?[0-7]+)*(u?(l|L|y|s|n)?|[IMm])\b(?!\.)} 0:value
    112 add-highlighter shared/fsharp/code/ regex %{(?<!\.)\b0x[\da-fA-F]('?[\da-fA-F]+)*(u?(l|L|y|s|n)?)?\b(?!\.)} 0:value
    113 # floating point literals
    114 add-highlighter shared/fsharp/code/ regex %{(?i)(?<!\.)\b\d('?\d+)*\.((lf|[fm])\b|\B)(?!\.)} 0:value
    115 add-highlighter shared/fsharp/code/ regex %{(?i)(?<!\.)\b\d('?\d+)*\.?e[+-]?\d('?\d+)*(lf|[fm])?\b(?!\.)} 0:value
    116 add-highlighter shared/fsharp/code/ regex %{(?i)(?<!\.)(\b(\d('?\d+)*)|\B)\.\d('?[\d]+)*(e[+-]?\d('?\d+)*)?(lf|[fm])?\b(?!\.)} 0:value
    117 add-highlighter shared/fsharp/code/ regex %{(?i)(?<!\.)\b0x[\da-f]('?[\da-f]+)*((lf|[fm])\b|\B)(?!\.)} 0:value
    118 add-highlighter shared/fsharp/code/ regex %{(?i)(?<!\.)\b0x[\da-f]('?[\da-f]+)*\.?p[+-]?\d('?\d+)*)?(lf|[fm])?\b(?!\.)} 0:value
    119 add-highlighter shared/fsharp/code/ regex %{(?i)(?<!\.)\b0x([\da-f]('?[\da-f]+)*)?\.\d('?[\d]+)*(p[+-]?\d('?\d+)*)?(lf|[fm])?\b(?!\.)} 0:value
    120 # character literals
    121 add-highlighter shared/fsharp/code/char regex %{\B'((\\.)|[^'\\])'(\b(B)|\B)} 0:value
    122 # other literals
    123 add-highlighter shared/fsharp/code/ regex "\b(true|false)\b" 0:value
    124 add-highlighter shared/fsharp/code/ regex "\B(\(\))\B" 0:value
    125 
    126 # Commands
    127 # ‾‾‾‾‾‾‾‾
    128 
    129 define-command -hidden fsharp-trim-indent %{
    130     evaluate-commands -no-hooks -draft -itersel %{
    131         execute-keys x
    132         # remove trailing white spaces
    133         try %{ execute-keys -draft s \h + $ <ret> d }
    134     }
    135 }
    136 
    137 define-command -hidden fsharp-insert-on-new-line %{
    138     evaluate-commands -draft -itersel %{
    139         # copy // comments prefix and following white spaces
    140         try %{ execute-keys -draft k x s ^\h*//\h* <ret> y jgh P }
    141     }
    142 }
    143 
    144 define-command -hidden fsharp-indent-on-new-line %{
    145     evaluate-commands -draft -itersel %{
    146         # preserve previous line indent
    147         try %{ execute-keys -draft \; K <a-&> }
    148         # cleanup trailing whitespaces from previous line
    149         try %{ execute-keys -draft k x s \h+$ <ret> d }
    150         # indent after line ending with =
    151         try %{ execute-keys -draft , k x <a-k> =$ <ret> j <a-gt> }
    152         # indent after line ending with "do"
    153         try %{ execute-keys -draft , k x <a-k> \bdo$ <ret> j <a-gt> }
    154     }
    155 }
    156 
    157 §