From d7d160757a363c75edd06b28207c72c6f973b617 Mon Sep 17 00:00:00 2001 From: Yerlan Date: Mon, 26 Apr 2021 19:29:22 +0200 Subject: [PATCH] Kotlin.kak new attempt Updates to address these comments: "I still dont quite see why we need to introduce all those options, especially with names that dont say anything about kotlin. I would expect a single kotlin_static_words option to be enough. Similarly, a single highlighter should be enough here." --- rc/filetype/kotlin.kak | 170 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 rc/filetype/kotlin.kak diff --git a/rc/filetype/kotlin.kak b/rc/filetype/kotlin.kak new file mode 100644 index 00000000..d583f645 --- /dev/null +++ b/rc/filetype/kotlin.kak @@ -0,0 +1,170 @@ +# References --------------------------------------------------------------------------------------- # +# ‾‾‾‾‾‾‾‾‾‾ +# Team: Yerlan & Kirk Duncan +# +# Kotlin 2020, Keywords and Operators, v1.4.0, viewed 9 September 2020, https://kotlinlang.org/docs/reference/keyword-reference.html +# Kdoc 2020, Documenting Kotlin Code, Block Tags, v1.4.0, viewed 9 September 2020, https://kotlinlang.org/docs/reference/kotlin-doc.html +# Oracle 2020, Java Platform, Standard Edition & Java Development Kit, Version 14 API Specification, viewed 8 September 2020, https://docs.oracle.com/en/java/javase/14/docs/api/index.html +# +# File types --------------------------------------------------------------------------------------- # +# ‾‾‾‾‾‾‾‾‾‾ +hook global BufCreate .*[.](kt|kts) %{ + set-option buffer filetype kotlin +} + +# Initialization ----------------------------------------------------------------------------------- # +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +hook global WinSetOption filetype=kotlin %{ + require-module kotlin + + set-option window static_words %opt{kotlin_static_words} + + # cleanup trailing whitespaces when exiting insert mode + hook window ModeChange pop:insert:.* -group kotlin-trim-indent %{ try %{ execute-keys -draft s^\h+$d } } + hook window InsertChar \n -group kotlin-indent kotlin-insert-on-new-line + hook window InsertChar \n -group kotlin-indent kotlin-indent-on-new-line + hook window InsertChar \{ -group kotlin-indent kotlin-indent-on-opening-curly-brace + hook window InsertChar \} -group kotlin-indent kotlin-indent-on-closing-curly-brace + + hook -once -always window WinSetOption filetype=.* %{ remove-hooks window kotlin-.+ } +} + +hook -group kotlin-highlighter global WinSetOption filetype=kotlin %{ + add-highlighter window/kotlin ref kotlin + add-highlighter window/kdoc ref kdoc + + hook -once -always window WinSetOption filetype=.* %{ + remove-highlighter window/kotlin + remove-highlighter window/kdoc + } +} + +hook global BufSetOption filetype=kotlin %{ + require-module kotlin + + set-option buffer comment_line '//' + set-option buffer comment_block_begin '/*' + set-option buffer comment_block_end '*/' + + hook -once -always buffer BufSetOption filetype=.* %{ remove-hooks buffer kotlin-.+ } +} + +# Module ------------------------------------------------------------------------------------------- # +# ‾‾‾‾‾‾ +provide-module kotlin %§ + +add-highlighter shared/kotlin regions +add-highlighter shared/kotlin/code default-region group +add-highlighter shared/kotlin/string region %{(?](?=[^\(\{]) 1:function 2:function 3:function + +# Test suite functions: fun `this is a valid character function test`() +add-highlighter shared/kotlin/code/kotlin_fun_tests regex ^\h*?fun\s*?`(.[^<>\[\]\\\.]+?)`\h*?(?=\() 1:default+uF +add-highlighter shared/kotlin/code/kotlin_delimiters regex (\(|\)|\[|\]|\{|\}|\;|') 1:operator +add-highlighter shared/kotlin/code/kotlin_operators regex (\+|-|\*|&|=|\\|\?|%|\|-|!|\||->|\.|,|<|>|:|\^|/) 1:operator +add-highlighter shared/kotlin/code/kotlin_numbers regex \b((0(x|X)[0-9a-fA-F]*)|(([0-9]+\.?[0-9]*)|(\.[0-9]+))((e|E)(\+|-)?[0-9]+)?)([LlFf])?\b 0:value + +# Generics need improvement, as after a colon will match as a constant only. +# val program: IOU = XXXX; val cat: DOG = XXXX. matches IOU or DOG as a +# CONSTANT when it could be generics. See: https://regex101.com/r/VPO5LE/7 +add-highlighter shared/kotlin/code/kotlin_constants_and_generics regex ((?<==\h)\([A-Z][A-Z0-9_]+\b(?=[<:\;])|\b(?\)]))\b|\b((?\s]))\b 1:meta 2:type + +add-highlighter shared/kotlin/code/kotlin_target regex @(delegate|field|file|get|param|property|receiver|set|setparam)(?=:) 0:meta +add-highlighter shared/kotlin/code/kotlin_soft regex \b(by|catch|constructor|dynamic|finally|get|import|init|set|where)\b 1:keyword +add-highlighter shared/kotlin/code/kotlin_hard regex \b(as|as\?|break|class|continue|do|else|false|for|fun|if|in|!in|interface|is|!is|null|object|package|return|super|this|throw|true|try|typealias|val|var|when|while)\b 1:keyword + +add-highlighter shared/kotlin/code/kotlin_modifier regex \b(actual|abstract|annotation|companion|const|crossinline|data|enum|expect|external|final|infix|inline|inner|internal|lateinit|noinline|open|operator|out|override|private|protected|public|reified|sealed|suspend|tailrec|vararg)\b(?=[\s\n]) 1:attribute + +add-highlighter shared/kotlin/code/kotlin_type regex \b(Annotation|Any|Boolean|BooleanArray|Byte|ByteArray|Char|Character|CharArray|CharSequence|Class|ClassLoader|Cloneable|Comparable|Compiler|DeprecationLevel|Double|DoubleArray|Enum|Float|FloatArray|Function|Int|IntArray|Integer|Lazy|LazyThreadSafetyMode|Long|LongArray|Math|Nothing|Number|Object|Package|Pair|Process|Runnable|Runtime|SecurityManager|Short|ShortArray|StackTraceElement|StrictMath|String|StringBuffer|System|Thread|ThreadGroup|ThreadLocal|Triple|Unit|Void)\b(?=[^<]) 1:type + +# Kdoc --------------------------------------------------------------------------------------------- # +# ‾‾‾‾ +add-highlighter shared/kdoc group +add-highlighter shared/kdoc/tag regex \*(?:\s+)?(@(author|constructor|exception|param|property|receiver|return|sample|see|since|suppress|throws))\b 1:default+ui + +# Discolour ---------------------------------------------------------------------------------------- # +# ‾‾‾‾‾‾‾‾‾ +add-highlighter shared/kotlin/code/discolour regex ^(package|import)(?S)(.+) 2:default+fa + +# Commands ----------------------------------------------------------------------------------------- # +# ‾‾‾‾‾‾‾‾ +define-command -hidden kotlin-insert-on-new-line %[ + # copy // comments prefix and following white spaces + try %{ execute-keys -draft k s ^\h*\K/{2,}\h* yP } +] + +define-command -hidden kotlin-indent-on-new-line %~ + evaluate-commands -draft -itersel %< + # preserve previous line indent + try %{ execute-keys -draft K } + # indent after lines ending with { or ( + try %[ execute-keys -draft k [{(]\h*$ j ] + # cleanup trailing white spaces on the previous line + try %{ execute-keys -draft k s \h+$ d } + # align to opening paren of previous line + try %{ execute-keys -draft [( \A\([^\n]+\n[^\n]*\n?\z s \A\(\h*.|.\z '' & } + # indent after a pattern match on when/where statements + try %[ execute-keys -draft k ^\h*(when|where).*$ j ] + # indent after term on an expression + try %[ execute-keys -draft k =\h*?$ j ] + # indent after keywords + try %[ execute-keys -draft )MB \A(catch|do|else|for|if|try|while)\h*\(.*\)\h*\n\h*\n?\z s \A|.\z 11 ] + # deindent closing brace(s) when after cursor + try %[ execute-keys -draft ^\h*[})] gh / [})] m 1 ] + > +~ + +define-command -hidden kotlin-indent-on-opening-curly-brace %[ + # align indent with opening paren when { is entered on a new line after the closing paren + try %[ execute-keys -draft -itersel h)M \A\(.*\)\h*\n\h*\{\z s \A|.\z 1 ] +] + +define-command -hidden kotlin-indent-on-closing-curly-brace %[ + # align to opening curly brace when alone on a line + try %[ execute-keys -itersel -draft ^\h+\}$hms\A|.\z1 ] +] + +# Exceptions, Errors, and Types -------------------------------------------------------------------- # +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +# macro: 93lei +evaluate-commands %sh{ + kotlin_types='Annotation Any Boolean BooleanArray Byte ByteArray Char Character CharArray + CharSequence Class ClassLoader Cloneable Comparable Compiler DeprecationLevel Double + DoubleArray Enum Float FloatArray Function Int IntArray Integer Lazy LazyThreadSafetyMode + Long LongArray Math Nothing Number Object Package Pair Process Runnable Runtime + SecurityManager Short ShortArray StackTraceElement StrictMath String StringBuffer System + Thread ThreadGroup ThreadLocal Triple Unit Void' + + # ------------------------------------------------------------------------------------------------ # + + kotlin_kdocs='author constructor exception param property receiver return sample see since suppress throws' + + # ------------------------------------------------------------------------------------------------ # + + kotlin_errors_exceptions='CharacterCodingException Error AssertionError NotImplementedError + OutOfMemoryErrorIllegalCallableAccessException IllegalPropertyDelegateAccessException + NoSuchPropertyException RuntimeException Throwable' + + # ------------------------------------------------------------------------------------------------ # + + join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; } + + # ------------------------------------------------------------------------------------------------ # + + printf %s\\n "declare-option str-list kotlin_static_words $(join "${kotlin_types} ${kotlin_kdocs} ${kotlin_errors_exceptions}" ' ')" + + # ------------------------------------------------------------------------------------------------ # + + printf %s\\n "add-highlighter shared/kotlin/code/kotlin_errors_exceptions regex \b($(join "${kotlin_errors_exceptions}" '|'))\b 0:type" +} +§ +# ------------------------------------------------------------------------------------------------- #