diff --git a/rc/filetype/pony.kak b/rc/filetype/pony.kak index f0fa8e284..6a91688be 100644 --- a/rc/filetype/pony.kak +++ b/rc/filetype/pony.kak @@ -16,10 +16,12 @@ hook global WinSetOption filetype=pony %{ set-option window static_words %opt{pony_static_words} - hook window InsertChar \n -group pony-insert pony-insert-on-new-line - hook window InsertChar \n -group pony-indent pony-indent-on-new-line # cleanup trailing whitespaces on current line insert end hook window ModeChange pop:insert:.* -group pony-trim-indent %{ try %{ execute-keys -draft x s ^\h+$ d } } + hook window InsertChar \n -group pony-indent pony-indent-on-new-line + hook window InsertChar \n -group pony-insert pony-insert-on-new-line + + set-option buffer extra_word_chars '_' "'" hook -once -always window WinSetOption filetype=.* %{ remove-hooks window pony-.+ } } @@ -29,47 +31,78 @@ hook -group pony-highlight global WinSetOption filetype=pony %{ hook -once -always window WinSetOption filetype=.* %{ remove-highlighter pony } } -provide-module pony %{ +provide-module pony %§ # Highlighters & Completion # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ add-highlighter shared/pony regions add-highlighter shared/pony/code default-region group -add-highlighter shared/pony/triple_string region '"""' '"""' fill string -add-highlighter shared/pony/double_string region '"' (?>|==|!=|<|<=|>=|>|=>|;' 0:operator +add-highlighter shared/pony/code/ regex '(\+|-|\*|/|%|%%|<<|>>|==|!=|<|<=|>=|>)~' 0:operator +add-highlighter shared/pony/code/ regex '(\+|-|\*|/|%|%%)\?' 0:operator +add-highlighter shared/pony/code/ regex '^\h*|' 0:operator +add-highlighter shared/pony/code/ regex '\b_\b' 0:operator + +# Integer literals +add-highlighter shared/pony/code/ regex '\b[0-9](_?[0-9])*\b' 0:value +add-highlighter shared/pony/code/ regex '\b0x[0-9a-fA-F](_?[0-9a-fA-F])*\b' 0:value +add-highlighter shared/pony/code/ regex '\b0b[01](_?[01])*\b' 0:value + +# Float literals +add-highlighter shared/pony/code/ regex '\b[0-9]+\.[0-9]+(?:[eE][-+]?[0-9]+)?\b' 0:value +add-highlighter shared/pony/code/ regex '\b[0-9]+[eE][-+]?[0-9]+\b' 0:value + +# Type literals +add-highlighter shared/pony/code/ regex '\b_?[A-Z][A-Za-z0-9]*\b' 0:type + +# Literal words are highlighted below to also allow for autocompletion by appending +# them to static_words. evaluate-commands %sh{ # Grammar - values="true|false|None|this" + values="true|false|this" meta='use' # Keyword list is collected using `keyword.kwlist` from `keyword` - keywords="and|as|or|break|match|continue|else|try|in|return|end|for|is" - keywords="${keywords}|recover|consume|error|do|then|if|while" - func_decl="new|fun|be|lambda" + keywords="as|break|match|continue|else|elseif|try|in|return|end|for|is" + keywords="${keywords}|recover|consume|error|do|then|if|while|where|with" + keywords="${keywords}|class|struct|object|actor|interface|trait|primitive" + keywords="${keywords}|type|var|let|embed|repeat|until|addressof" + func_decl="new|fun|be" capabilities="iso|ref|box|tag|val|trn" - struct="class|actor|interface|trait|primitive|type|var|let|embed" - + operators="and|or|xor|not" + builtin_types="String|None|Any|Array" # Add the language's grammar to the static completion list static_words="${values} ${meta} ${keywords} ${types_decl} ${capabilities}" - static_words="${static_words} ${struct}" + static_words="${static_words} ${operators} ${builtin_types}" printf %s\\n "declare-option str-list pony_static_words ${static_words}" | tr '|' ' ' # Highlight keywords + # The (?!') ensures that keywords with prime are highlighted as usual, for example try' printf %s " - add-highlighter shared/pony/code/ regex '\b(${values})\b' 0:value - add-highlighter shared/pony/code/ regex '\b(${meta})\b' 0:meta - add-highlighter shared/pony/code/ regex '\b(${func_decl})(\s+(${capabilities}))?(\s+\w+)\(' 1:type 3:builtin 4:builtin - add-highlighter shared/pony/code/ regex '\b(${func_decl})\b' 0:type - add-highlighter shared/pony/code/ regex '=>' 0:type - add-highlighter shared/pony/code/ regex '\b(${keywords})\b' 0:keyword - add-highlighter shared/pony/code/ regex ';' 0:keyword - add-highlighter shared/pony/code/ regex '^\s*|' 0:keyword - add-highlighter shared/pony/code/ regex '\b(${struct})\b' 0:variable - add-highlighter shared/pony/code/ regex '\b(${capabilities})\b(!|^)?' 1:builtin 2:builtin + add-highlighter shared/pony/code/ regex \b(${values})\b(?!') 0:value + add-highlighter shared/pony/code/ regex \b(${operators})\b(?!') 0:operator + add-highlighter shared/pony/code/ regex \b(${meta})\b(?!') 0:meta + add-highlighter shared/pony/code/ regex \b(${func_decl})(\s+(${capabilities}))?(\s+\w+)[\[(] 1:keyword 3:attribute 4:function + add-highlighter shared/pony/code/ regex \b(${func_decl})\b(?!') 0:keyword + add-highlighter shared/pony/code/ regex \b(${keywords})\b(?!') 0:keyword + add-highlighter shared/pony/code/ regex \b(${capabilities})\b(?!')(!|\\^)? 1:attribute 2:attribute " # Highlight types and attributes @@ -81,24 +114,42 @@ evaluate-commands %sh{ # Commands # ‾‾‾‾‾‾‾‾ -define-command -hidden pony-insert-on-new-line %{ - evaluate-commands -draft -itersel %{ +define-command -hidden pony-insert-on-new-line %< + evaluate-commands -no-hooks -draft -itersel %< # copy // comments prefix and following white spaces - try %{ execute-keys -draft k x s ^\h*//\h* y jgh P } - } -} + try %{ execute-keys -draft k x s ^\h*\K//\h* y jgi P } + + # wisely add `end` keyword + evaluate-commands -save-regs x %< + try %{ execute-keys -draft k x s ^ \h + \" x y } catch %{ reg x '' } + try %< + evaluate-commands -draft %< + # Check if previous line opens a block + execute-keys -draft kx ^x(try|match|recover|repeat|object|.+\bdo$|.+\bthen$)[^0-9A-Za-z_'] + # Check that we didn't put an end on the previous line that closes the block + execute-keys -draft kx \bend$ + # Check that we do not already have an end for this indent level which is first set via `pony-indent-on-new-line` hook + execute-keys -draft }i J x ^x(end|else|elseif|until)[^0-9A-Za-z_'] + > + execute-keys -draft oxend # insert a new line with containing end + > + > + > +> define-command -hidden pony-indent-on-new-line %{ - evaluate-commands -draft -itersel %{ + evaluate-commands -no-hooks -draft -itersel %{ # preserve previous line indent try %{ execute-keys -draft , K } # cleanup trailing whitespaces from previous line try %{ execute-keys -draft k x s \h+$ d } - # indent after line ending with : - try %{ execute-keys -draft , k x (\b(?:do|try|then|else)|:|=>)$ j } + # indent after line ending with: =>, do, try, then, else, =; or + # starting with: actor, class, struct, trait, interface, if, elseif, recover, repeat + # excluding: primitive, type -- because they are often written in a single line + try %{ execute-keys -draft , k x (\b(?:do|try|then|else|recover|repeat)|=>|=)$ | ^\h*(actor|class|struct|trait|interface|object)\b j } # else, end are always de-indented try %{ execute-keys -draft , k x \b(else|end):$ k x s ^\h* y j x ^" J } } } -} +§ diff --git a/rc/tools/comment.kak b/rc/tools/comment.kak index 8ebe088f7..b61007311 100644 --- a/rc/tools/comment.kak +++ b/rc/tools/comment.kak @@ -16,7 +16,7 @@ hook global BufSetOption filetype=asciidoc %{ set-option buffer comment_block_end '////' } -hook global BufSetOption filetype=(c|cpp|dart|gluon|go|java|javascript|objc|odin|php|protobuf|rust|sass|scala|scss|swift|typescript|groovy) %{ +hook global BufSetOption filetype=(c|cpp|dart|gluon|go|java|javascript|objc|odin|php|pony|protobuf|rust|sass|scala|scss|swift|typescript|groovy) %{ set-option buffer comment_line '//' set-option buffer comment_block_begin '/*' set-option buffer comment_block_end '*/'