2017-11-29 17:15:00 +01:00
|
|
|
# https://nim-lang.org/
|
|
|
|
#
|
|
|
|
|
|
|
|
# Detection
|
|
|
|
# ‾‾‾‾‾‾‾‾‾
|
|
|
|
|
2017-12-09 21:55:57 +01:00
|
|
|
hook global BufCreate .*\.nim(s|ble)? %{
|
2017-11-29 17:15:00 +01:00
|
|
|
set-option buffer filetype nim
|
|
|
|
}
|
|
|
|
|
2019-04-10 05:54:19 +02:00
|
|
|
# Initialization
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
hook global WinSetOption filetype=nim %{
|
2019-03-13 06:24:33 +01:00
|
|
|
require-module nim
|
2019-04-10 05:54:19 +02:00
|
|
|
|
|
|
|
set-option window static_words %opt{nim_static_words}
|
|
|
|
|
2021-04-17 10:17:01 +02:00
|
|
|
hook window InsertChar \n -group nim-insert nim-insert-on-new-line
|
2019-04-10 05:54:19 +02:00
|
|
|
hook window InsertChar \n -group nim-indent nim-indent-on-new-line
|
|
|
|
# cleanup trailing whitespaces on current line insert end
|
2022-03-16 23:20:07 +01:00
|
|
|
hook window ModeChange pop:insert:.* -group nim-trim-indent %{ try %{ exec -draft <semicolon> x s ^\h+$ <ret> d } }
|
2019-04-10 05:54:19 +02:00
|
|
|
|
|
|
|
hook -once -always window WinSetOption filetype=.* %{ remove-hooks window nim-.+ }
|
|
|
|
}
|
|
|
|
|
|
|
|
hook -group nim-highlight global WinSetOption filetype=nim %{
|
|
|
|
add-highlighter window/nim ref nim
|
|
|
|
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/nim }
|
2019-03-13 06:24:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
provide-module nim %{
|
|
|
|
|
2017-11-29 17:15:00 +01:00
|
|
|
# Highlighters
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
2018-07-01 11:53:35 +02:00
|
|
|
add-highlighter shared/nim regions
|
|
|
|
add-highlighter shared/nim/code default-region group
|
2018-12-29 08:05:11 +01:00
|
|
|
add-highlighter shared/nim/triple_string region '([A-Za-z](_?\w)*)?"""' '"""(?!")' fill string
|
2019-01-07 21:01:39 +01:00
|
|
|
add-highlighter shared/nim/raw_string region [A-Za-z](_?[A-Za-z])*" (?<!")"(?!") fill string
|
2019-08-12 16:40:35 +02:00
|
|
|
add-highlighter shared/nim/string region (?<!'\\)" ((?<!\\)(\\\\)*"|$) group
|
2018-06-23 07:38:46 +02:00
|
|
|
add-highlighter shared/nim/inline_documentation region '##' $ fill documentation
|
|
|
|
add-highlighter shared/nim/documentation region '##\[' '\]##' fill documentation
|
|
|
|
add-highlighter shared/nim/comment region '#\[' '\]#' group
|
|
|
|
add-highlighter shared/nim/comment_line region (?<![^'].')#(?!'\[) $ group
|
2019-08-08 21:17:55 +02:00
|
|
|
|
|
|
|
add-highlighter shared/nim/string/fill fill string
|
|
|
|
add-highlighter shared/nim/comment/fill fill comment
|
|
|
|
add-highlighter shared/nim/comment_line/fill fill comment
|
2017-11-29 17:15:00 +01:00
|
|
|
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2017-11-29 17:15:00 +01:00
|
|
|
# Grammar
|
2019-01-07 21:01:39 +01:00
|
|
|
opchars='[=+-/<>@$~&%|!?^.:\\*]'
|
|
|
|
opnocol='[=+-/<>@$~&%|!?^.\\*]'
|
2021-03-24 16:11:26 +01:00
|
|
|
letter='A-Za-z\u000080-\u10FFFF'
|
|
|
|
customsuffix="'[${letter}](_?[${letter}0-9])*"
|
|
|
|
suffix="(${customsuffix}|[iIuU](8|16|32|64)|[fF](32|64)?|[dDuU])?"
|
|
|
|
floatsuffix="(${customsuffix}|[fF](32|64)?|[dD])?"
|
2019-01-07 21:01:39 +01:00
|
|
|
hexdigit='[0-9a-fA-F]'
|
|
|
|
octdigit='[0-7]'
|
|
|
|
bindigit='[01]'
|
|
|
|
hexlit="0[xX]${hexdigit}(_?${hexdigit})*"
|
|
|
|
declit="\d(_?\d)*"
|
|
|
|
octlit="0o${octdigit}(_?${octdigit})*"
|
|
|
|
binlit="0[bB]${bindigit}(_?${bindigit})*"
|
|
|
|
intlit="\b(${declit}|${hexlit}|${octlit}|${binlit})${suffix}\b"
|
|
|
|
exponent="([eE][+-]?${declit})"
|
|
|
|
floatlit="\b${declit}(\.${declit}${exponent}?|${exponent})${floatsuffix}\b"
|
|
|
|
|
2020-08-07 11:59:00 +02:00
|
|
|
keywords="addr asm bind block break case cast concept const continue
|
2019-01-07 21:01:39 +01:00
|
|
|
converter defer discard distinct do elif else end enum except export
|
2020-08-07 11:59:00 +02:00
|
|
|
finally for func if import include interface iterator let macro
|
2019-01-07 21:01:39 +01:00
|
|
|
method mixin nil out proc ptr raise ref return static template try type
|
|
|
|
unsafeAddr using var when while yield with without atomic generic"
|
2020-08-07 11:59:00 +02:00
|
|
|
operators="or xor and is isnot in notin of div mod shl shr not as from"
|
2019-01-07 21:01:39 +01:00
|
|
|
types="int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 float
|
|
|
|
float32 float64 bool char object seq array cstring string tuple varargs
|
|
|
|
typedesc pointer byte set typed untyped void auto"
|
2019-08-08 21:17:55 +02:00
|
|
|
values="false true on off"
|
2019-01-07 21:01:39 +01:00
|
|
|
|
|
|
|
join() { sep=$2; set -- $1; IFS="$sep"; echo "$*"; }
|
|
|
|
|
|
|
|
static_words="$(join "${keywords} ${types} ${operator} ${values}" ' ')"
|
2017-11-29 17:15:00 +01:00
|
|
|
|
|
|
|
# Add the language's grammar to the static completion list
|
2019-04-10 05:54:19 +02:00
|
|
|
printf %s\\n "declare-option str-list nim_static_words ${static_words}"
|
2019-01-07 21:01:39 +01:00
|
|
|
|
|
|
|
keywords="$(join "${keywords}" '|')"
|
|
|
|
operators="$(join "${operators}" '|')"
|
|
|
|
types="$(join "${types}" '|')"
|
|
|
|
values="$(join "${values}" '|')"
|
2017-11-29 17:15:00 +01:00
|
|
|
|
|
|
|
# Highlight keywords
|
|
|
|
printf %s "
|
2019-01-07 21:01:39 +01:00
|
|
|
add-highlighter shared/nim/code/ regex ${opchars}+ 0:operator
|
|
|
|
add-highlighter shared/nim/code/ regex (?<!${opchars}):{1,2}(?!${opchars}) 0:meta
|
|
|
|
add-highlighter shared/nim/code/ regex :${opnocol}${opchars}* 0:operator
|
|
|
|
add-highlighter shared/nim/code/ regex (?<!${opchars})(\*)(:)(?!${opchars}) 1:operator 2:meta
|
2018-07-01 11:53:35 +02:00
|
|
|
add-highlighter shared/nim/code/ regex \b(${keywords})\b 0:keyword
|
2019-01-07 21:01:39 +01:00
|
|
|
add-highlighter shared/nim/code/ regex \b(${operators})\b 0:operator
|
2018-07-01 11:53:35 +02:00
|
|
|
add-highlighter shared/nim/code/ regex \b(${types})\b 0:type
|
|
|
|
add-highlighter shared/nim/code/ regex \b(${values})\b 0:value
|
2019-01-07 21:01:39 +01:00
|
|
|
add-highlighter shared/nim/code/ regex ${intlit} 0:value
|
|
|
|
add-highlighter shared/nim/code/ regex ${floatlit} 0:value
|
2017-11-29 17:15:00 +01:00
|
|
|
"
|
|
|
|
}
|
|
|
|
|
2019-01-07 21:01:39 +01:00
|
|
|
add-highlighter shared/nim/code/ regex '(,|;|`|\(\.?|\.?\)|\[[.:]?|\.?\]|\{\.?|\.?\})' 0:meta
|
2019-08-12 17:17:20 +02:00
|
|
|
add-highlighter shared/nim/code/ regex %{'(\\([rcnlftvabe\\"']|0*[12]?\d?\d|x[0-9a-fA-F]{2})|[^'\n])'} 0:value
|
2017-11-29 17:15:00 +01:00
|
|
|
|
2017-12-09 21:55:57 +01:00
|
|
|
# Commands
|
|
|
|
# ‾‾‾‾‾‾‾‾
|
|
|
|
|
2021-04-17 10:17:01 +02:00
|
|
|
define-command -hidden nim-insert-on-new-line %{
|
2021-04-17 11:33:00 +02:00
|
|
|
evaluate-commands -draft -itersel %{
|
2017-12-09 21:55:57 +01:00
|
|
|
# copy '#' comment prefix and following white spaces
|
2022-03-16 23:20:07 +01:00
|
|
|
try %{ exec -draft k x s ^\h*#\h* <ret> y jgh P }
|
2021-04-17 10:17:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
define-command -hidden nim-indent-on-new-line %{
|
|
|
|
evaluate-commands -draft -itersel %{
|
2017-12-09 21:55:57 +01:00
|
|
|
# preserve previous line indent
|
2019-10-22 11:02:06 +02:00
|
|
|
try %{ exec -draft <semicolon> K <a-&> }
|
2017-12-09 21:55:57 +01:00
|
|
|
# cleanup trailing whitespaces from previous line
|
2022-03-16 23:20:07 +01:00
|
|
|
try %{ exec -draft k x s \h+$ <ret> d }
|
2019-08-06 00:00:38 +02:00
|
|
|
# indent after line ending with enum, tuple, object, type, import, export, const, let, var, ':' or '='
|
2022-04-15 00:14:17 +02:00
|
|
|
try %{ exec -draft , k x <a-k> (:|=|\b(?:enum|tuple|object|const|let|var|import|export|type))$ <ret> j <a-gt> }
|
2017-12-09 21:55:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 06:24:33 +01:00
|
|
|
}
|