kotlin.kak (10977B)
1 # References --------------------------------------------------------------------------------------- # 2 # ‾‾‾‾‾‾‾‾‾‾ 3 # Team: Yerlan & Kirk Duncan 4 # 5 # Kotlin 2020, Keywords and Operators, v1.4.0, viewed 9 September 2020, https://kotlinlang.org/docs/reference/keyword-reference.html 6 # Kdoc 2020, Documenting Kotlin Code, Block Tags, v1.4.0, viewed 9 September 2020, https://kotlinlang.org/docs/reference/kotlin-doc.html 7 # 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 8 # 9 # File types --------------------------------------------------------------------------------------- # 10 # ‾‾‾‾‾‾‾‾‾‾ 11 hook global BufCreate .*[.](kt|kts) %{ 12 set-option buffer filetype kotlin 13 } 14 15 # Initialization ----------------------------------------------------------------------------------- # 16 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 17 hook global WinSetOption filetype=kotlin %{ 18 require-module kotlin 19 20 set-option window static_words %opt{kotlin_static_words} 21 22 # cleanup trailing whitespaces when exiting insert mode 23 hook window ModeChange pop:insert:.* -group kotlin-trim-indent %{ try %{ execute-keys -draft xs^\h+$<ret>d } } 24 hook window InsertChar \n -group kotlin-indent kotlin-insert-on-new-line 25 hook window InsertChar \n -group kotlin-indent kotlin-indent-on-new-line 26 hook window InsertChar \{ -group kotlin-indent kotlin-indent-on-opening-curly-brace 27 hook window InsertChar \} -group kotlin-indent kotlin-indent-on-closing-curly-brace 28 29 hook -once -always window WinSetOption filetype=.* %{ remove-hooks window kotlin-.+ } 30 } 31 32 hook -group kotlin-highlighter global WinSetOption filetype=kotlin %{ 33 add-highlighter window/kotlin ref kotlin 34 add-highlighter window/kdoc ref kdoc 35 36 hook -once -always window WinSetOption filetype=.* %{ 37 remove-highlighter window/kotlin 38 remove-highlighter window/kdoc 39 } 40 } 41 42 hook global BufSetOption filetype=kotlin %{ 43 require-module kotlin 44 45 set-option buffer comment_line '//' 46 set-option buffer comment_block_begin '/*' 47 set-option buffer comment_block_end '*/' 48 49 hook -once -always buffer BufSetOption filetype=.* %{ remove-hooks buffer kotlin-.+ } 50 } 51 52 # Module ------------------------------------------------------------------------------------------- # 53 # ‾‾‾‾‾‾ 54 provide-module kotlin %§ 55 56 add-highlighter shared/kotlin regions 57 add-highlighter shared/kotlin/code default-region group 58 add-highlighter shared/kotlin/string region %{(?<!')"} %{(?<!\\)(\\\\)*"} group 59 add-highlighter shared/kotlin/character region %{'} %{(?<!\\)'} group 60 add-highlighter shared/kotlin/comment region /\* \*/ fill comment 61 add-highlighter shared/kotlin/inline_documentation region /// $ fill documentation 62 add-highlighter shared/kotlin/line_comment region // $ fill comment 63 64 add-highlighter shared/kotlin/code/annotations regex @\w+\b|\b\w+@(?=\{) 0:meta 65 add-highlighter shared/kotlin/code/identifiers regex \b(field|it)\b 1:variable 66 add-highlighter shared/kotlin/code/fields regex \.([A-Za-z_][\w]*)\s*?\. 1:type 67 68 # String interpolation 69 add-highlighter shared/kotlin/string/ fill string 70 add-highlighter shared/kotlin/string/ regex \$\{.*?\} 0:value 71 72 # Character 73 add-highlighter shared/kotlin/character/ fill value 74 add-highlighter shared/kotlin/character/ regex ('.{1})(.+)(') 2:meta 75 76 # As at 15 March 2021, method see: https://regex101.com/r/Mhy4HG/1 77 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 78 79 # Test suite functions: fun `this is a valid character function test`() 80 add-highlighter shared/kotlin/code/fun_tests regex ^\h*?fun\s*?`(.[^<>:/\[\]\\\.]+?)`\h*?(?=\() 1:default+iuf 81 add-highlighter shared/kotlin/code/delimiters regex (\(|\)|\[|\]|\{|\}|\;|') 1:operator 82 add-highlighter shared/kotlin/code/operators regex (\+|-|\*|&|=|\\|\?|%|\|-|!|\||->|\.|,|<|>|:|\^|/) 1:operator 83 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 84 85 # Generics need improvement, as after a colon will match as a constant only. 86 # val program: IOU = XXXX; val cat: DOG = XXXX. matches IOU or DOG as a 87 # CONSTANT when it could be generics. See: https://regex101.com/r/VPO5LE/10 88 add-highlighter shared/kotlin/code/constants_and_generics regex \b((?<==\h)\([A-Z][A-Z0-9_]+(?=[<:\;])|(?<!<)[A-Z][A-Z0-9_]+\b(?!<[>\)]))|\b((?<!=\s)(?<!\.)[A-Z]+\d*?(?![\(\;:])(?=[,\)>\s]))\b 1:meta 2:type 89 90 add-highlighter shared/kotlin/code/target regex @(delegate|field|file|get|param|property|receiver|set|setparam)(?=:) 0:meta 91 add-highlighter shared/kotlin/code/soft regex \b(by|catch|constructor|dynamic|finally|get|import|init|set|where)\b 1:keyword 92 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 93 94 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 95 96 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 97 98 # Kdoc --------------------------------------------------------------------------------------------- # 99 # ‾‾‾‾ 100 add-highlighter shared/kdoc group 101 add-highlighter shared/kdoc/tag regex \*(?:\s+)?(@(author|constructor|exception|param|property|receiver|return|sample|see|since|suppress|throws))\b 1:default+ui 102 103 # Discolour ---------------------------------------------------------------------------------------- # 104 # ‾‾‾‾‾‾‾‾‾ 105 add-highlighter shared/kotlin/code/discolour regex ^(package|import)(?S)(.+) 2:default+fa 106 107 # Commands ----------------------------------------------------------------------------------------- # 108 # ‾‾‾‾‾‾‾‾ 109 define-command -hidden kotlin-insert-on-new-line %[ 110 # copy // comments prefix and following white spaces 111 try %{ execute-keys -draft <semicolon><c-s>kx s ^\h*\K/{2,}\h* <ret> y<c-o>P<esc> } 112 ] 113 114 define-command -hidden kotlin-indent-on-new-line %~ 115 evaluate-commands -draft -itersel %< 116 # preserve previous line indent 117 try %{ execute-keys -draft <semicolon>K<a-&> } 118 # indent after lines ending with { or ( 119 try %[ execute-keys -draft kx <a-k> [{(]\h*$ <ret> j<a-gt> ] 120 # cleanup trailing white spaces on the previous line 121 try %{ execute-keys -draft kx s \h+$ <ret>d } 122 # align to opening paren of previous line 123 try %{ execute-keys -draft [( <a-k> \A\(\N+\n\N*\n?\z <ret> s \A\(\h*.|.\z <ret> '<a-;>' & } 124 # indent after a pattern match on when/where statements 125 try %[ execute-keys -draft kx <a-k> ^\h*(when|where).*$ <ret> j<a-gt> ] 126 # indent after term on an expression 127 try %[ execute-keys -draft kx <a-k> =\h*?$ <ret> j<a-gt> ] 128 # indent after keywords 129 try %[ execute-keys -draft <semicolon><a-F>)MB <a-k> \A(catch|do|else|for|if|try|while)\h*\(.*\)\h*\n\h*\n?\z <ret> s \A|.\z <ret> 1<a-&>1<a-,><a-gt> ] 130 # deindent closing brace(s) when after cursor 131 try %[ execute-keys -draft x <a-k> ^\h*[})] <ret> gh / [})] <ret> m <a-S> 1<a-&> ] 132 > 133 ~ 134 135 define-command -hidden kotlin-indent-on-opening-curly-brace %[ 136 # align indent with opening paren when { is entered on a new line after the closing paren 137 try %[ execute-keys -draft -itersel h<a-F>)M <a-k> \A\(.*\)\h*\n\h*\{\z <ret> s \A|.\z <ret> 1<a-&> ] 138 ] 139 140 define-command -hidden kotlin-indent-on-closing-curly-brace %[ 141 # align to opening curly brace when alone on a line 142 try %[ execute-keys -itersel -draft <a-h><a-k>^\h+\}$<ret>hms\A|.\z<ret>1<a-&> ] 143 ] 144 145 # Exceptions, Errors, and Types -------------------------------------------------------------------- # 146 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 147 # macro: 93le<a-;>i<ret><esc><esc> 148 evaluate-commands %sh{ 149 150 kotlin_keywords='abstract actual annotation as break by catch class companion const 151 constructor continue crossinline data delegate do dynamic else enum expect external 152 false field file final finally for fun get if import in infix init inline inner 153 interface internal is lateinit noinline null object open operator out override 154 package param private property protected public receiver reified return sealed set 155 setparam super suspend tailrec this throw true try typealias val var vararg when where while' 156 157 kotlin_types='Annotation Any Boolean BooleanArray Byte ByteArray Char Character CharArray 158 CharSequence Class ClassLoader Cloneable Comparable Compiler DeprecationLevel Double 159 DoubleArray Enum Float FloatArray Function Int IntArray Integer Lazy LazyThreadSafetyMode 160 Long LongArray Math Nothing Number Object Package Pair Process Runnable Runtime 161 SecurityManager Short ShortArray StackTraceElement StrictMath String StringBuffer System 162 Thread ThreadGroup ThreadLocal Triple Unit Void' 163 164 # ------------------------------------------------------------------------------------------------ # 165 166 kotlin_kdocs='author constructor exception param property receiver return sample see since suppress throws' 167 168 # ------------------------------------------------------------------------------------------------ # 169 170 kotlin_errors_exceptions='CharacterCodingException Error AssertionError NotImplementedError 171 OutOfMemoryErrorIllegalCallableAccessException IllegalPropertyDelegateAccessException 172 NoSuchPropertyException RuntimeException Throwable' 173 174 # ------------------------------------------------------------------------------------------------ # 175 176 join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; } 177 178 # ------------------------------------------------------------------------------------------------ # 179 180 printf %s\\n "declare-option str-list kotlin_static_words $(join "${kotlin_keywords} ${kotlin_types} ${kotlin_kdocs} ${kotlin_errors_exceptions}" ' ')" 181 182 # ------------------------------------------------------------------------------------------------ # 183 184 printf %s\\n "add-highlighter shared/kotlin/code/errors_exceptions regex \b($(join "${kotlin_errors_exceptions}" '|'))\b 0:type" 185 } 186 § 187 # ------------------------------------------------------------------------------------------------- #