From d7d160757a363c75edd06b28207c72c6f973b617 Mon Sep 17 00:00:00 2001 From: Yerlan Date: Mon, 26 Apr 2021 19:29:22 +0200 Subject: [PATCH 1/3] 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 0000000000..d583f6453e --- /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" +} +§ +# ------------------------------------------------------------------------------------------------- # From 381cc1dec3a992b38cb5c40498607ec2c64a7b93 Mon Sep 17 00:00:00 2001 From: Yerlan Date: Tue, 27 Apr 2021 19:52:15 +0200 Subject: [PATCH 2/3] Addressing comments by mawww Done. Thanks! --- rc/filetype/kotlin.kak | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rc/filetype/kotlin.kak b/rc/filetype/kotlin.kak index d583f6453e..f23a939ac0 100644 --- a/rc/filetype/kotlin.kak +++ b/rc/filetype/kotlin.kak @@ -82,9 +82,9 @@ add-highlighter shared/kotlin/code/kotlin_target regex @(delegate|field|file|get 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/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 +add-highlighter shared/kotlin/code/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 --------------------------------------------------------------------------------------------- # # ‾‾‾‾ @@ -164,7 +164,7 @@ evaluate-commands %sh{ # ------------------------------------------------------------------------------------------------ # - printf %s\\n "add-highlighter shared/kotlin/code/kotlin_errors_exceptions regex \b($(join "${kotlin_errors_exceptions}" '|'))\b 0:type" + printf %s\\n "add-highlighter shared/kotlin/code/errors_exceptions regex \b($(join "${kotlin_errors_exceptions}" '|'))\b 0:type" } § # ------------------------------------------------------------------------------------------------- # From 0f99fb53bc0d82c3f03900b29450fb66236b87c7 Mon Sep 17 00:00:00 2001 From: Yerlan Date: Wed, 28 Apr 2021 19:07:32 +0200 Subject: [PATCH 3/3] Removing more kotlin_ prefix Done. Please review again :) --- rc/filetype/kotlin.kak | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/rc/filetype/kotlin.kak b/rc/filetype/kotlin.kak index f23a939ac0..b51ef06aaf 100644 --- a/rc/filetype/kotlin.kak +++ b/rc/filetype/kotlin.kak @@ -60,27 +60,27 @@ add-highlighter shared/kotlin/comment region /\* \*/ fill comment add-highlighter shared/kotlin/inline_documentation region /// $ fill documentation add-highlighter shared/kotlin/line_comment region // $ fill comment -add-highlighter shared/kotlin/code/kotlin_annotations regex @\w+\b|\b\w+@(?=\{) 0:meta -add-highlighter shared/kotlin/code/kotlin_identifiers regex \b(field|it)\b 1:variable -add-highlighter shared/kotlin/code/kotlin_fields regex \.([A-Za-z_][\w]*)\s*?\. 1:type +add-highlighter shared/kotlin/code/annotations regex @\w+\b|\b\w+@(?=\{) 0:meta +add-highlighter shared/kotlin/code/identifiers regex \b(field|it)\b 1:variable +add-highlighter shared/kotlin/code/fields regex \.([A-Za-z_][\w]*)\s*?\. 1:type -# As at 15 March 2021, kotlin_method see: https://regex101.com/r/Mhy4HG/1 -add-highlighter shared/kotlin/code/kotlin_methods regex ::([A-Za-z_][\w]*)|\.([A-Za-z_][\w]*)\s*?[\(\{]|\.([A-Za-z_][\w]*)[\s\)\}>](?=[^\(\{]) 1:function 2:function 3:function +# As at 15 March 2021, method see: https://regex101.com/r/Mhy4HG/1 +add-highlighter shared/kotlin/code/methods regex ::([A-Za-z_][\w]*)|\.([A-Za-z_][\w]*)\s*?[\(\{]|\.([A-Za-z_][\w]*)[\s\)\}>](?=[^\(\{]) 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 +add-highlighter shared/kotlin/code/fun_tests regex ^\h*?fun\s*?`(.[^<>\[\]\\\.]+?)`\h*?(?=\() 1:default+uF +add-highlighter shared/kotlin/code/delimiters regex (\(|\)|\[|\]|\{|\}|\;|') 1:operator +add-highlighter shared/kotlin/code/operators regex (\+|-|\*|&|=|\\|\?|%|\|-|!|\||->|\.|,|<|>|:|\^|/) 1:operator +add-highlighter shared/kotlin/code/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/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/target regex @(delegate|field|file|get|param|property|receiver|set|setparam)(?=:) 0:meta +add-highlighter shared/kotlin/code/soft regex \b(by|catch|constructor|dynamic|finally|get|import|init|set|where)\b 1:keyword +add-highlighter shared/kotlin/code/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/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