| 1 | ;; Kotlin highlights — adapted from tree-sitter-kotlin-sg (nvim-treesitter) |
| 2 | ;; Remapped to the highlight names used by this project's registry. |
| 3 | ;; |
| 4 | ;; IMPORTANT: Pattern order matters. This project's highlighter keeps the |
| 5 | ;; FIRST capture name when multiple patterns match the same range. Therefore |
| 6 | ;; specific patterns (function, property, constant…) must appear BEFORE the |
| 7 | ;; generic `(simple_identifier) @variable` catch-all at the end of this file. |
| 8 | |
| 9 | ;;; Literals (no simple_identifier conflict — safe to place early) |
| 10 | |
| 11 | [ |
| 12 | (line_comment) |
| 13 | (multiline_comment) |
| 14 | (shebang_line) |
| 15 | ] @comment |
| 16 | |
| 17 | (real_literal) @number |
| 18 | [ |
| 19 | (integer_literal) |
| 20 | (long_literal) |
| 21 | (hex_literal) |
| 22 | (bin_literal) |
| 23 | (unsigned_literal) |
| 24 | ] @number |
| 25 | |
| 26 | [ |
| 27 | (null_literal) |
| 28 | (boolean_literal) |
| 29 | ] @boolean |
| 30 | |
| 31 | (character_literal) @string |
| 32 | |
| 33 | (string_literal) @string |
| 34 | |
| 35 | (character_escape_seq) @string.escape |
| 36 | |
| 37 | ; Regex: "pattern".toRegex() |
| 38 | (call_expression |
| 39 | (navigation_expression |
| 40 | ((string_literal) @string.regex) |
| 41 | (navigation_suffix |
| 42 | ((simple_identifier) @_function |
| 43 | (#eq? @_function "toRegex"))))) |
| 44 | |
| 45 | ; Regex: Regex("pattern") |
| 46 | (call_expression |
| 47 | ((simple_identifier) @_function |
| 48 | (#eq? @_function "Regex")) |
| 49 | (call_suffix |
| 50 | (value_arguments |
| 51 | (value_argument |
| 52 | (string_literal) @string.regex)))) |
| 53 | |
| 54 | ; Regex: Regex.fromLiteral("pattern") |
| 55 | (call_expression |
| 56 | (navigation_expression |
| 57 | ((simple_identifier) @_class |
| 58 | (#eq? @_class "Regex")) |
| 59 | (navigation_suffix |
| 60 | ((simple_identifier) @_function |
| 61 | (#eq? @_function "fromLiteral")))) |
| 62 | (call_suffix |
| 63 | (value_arguments |
| 64 | (value_argument |
| 65 | (string_literal) @string.regex)))) |
| 66 | |
| 67 | ;;; Keywords |
| 68 | |
| 69 | (type_alias "typealias" @keyword) |
| 70 | [ |
| 71 | (class_modifier) |
| 72 | (member_modifier) |
| 73 | (function_modifier) |
| 74 | (property_modifier) |
| 75 | (platform_modifier) |
| 76 | (variance_modifier) |
| 77 | (parameter_modifier) |
| 78 | (visibility_modifier) |
| 79 | (reification_modifier) |
| 80 | (inheritance_modifier) |
| 81 | ] @keyword |
| 82 | |
| 83 | [ |
| 84 | "val" |
| 85 | "var" |
| 86 | "enum" |
| 87 | "class" |
| 88 | "object" |
| 89 | "interface" |
| 90 | "companion" |
| 91 | "where" |
| 92 | "by" |
| 93 | ] @keyword |
| 94 | |
| 95 | ("fun") @keyword |
| 96 | |
| 97 | (jump_expression) @keyword |
| 98 | |
| 99 | [ |
| 100 | "if" |
| 101 | "else" |
| 102 | "when" |
| 103 | ] @keyword |
| 104 | |
| 105 | [ |
| 106 | "for" |
| 107 | "do" |
| 108 | "while" |
| 109 | ] @keyword |
| 110 | |
| 111 | [ |
| 112 | "try" |
| 113 | "catch" |
| 114 | "throw" |
| 115 | "finally" |
| 116 | ] @keyword |
| 117 | |
| 118 | ;;; Annotations |
| 119 | |
| 120 | (annotation |
| 121 | "@" @attribute (use_site_target)? @attribute) |
| 122 | (annotation |
| 123 | (user_type |
| 124 | (type_identifier) @attribute)) |
| 125 | (annotation |
| 126 | (constructor_invocation |
| 127 | (user_type |
| 128 | (type_identifier) @attribute))) |
| 129 | |
| 130 | (file_annotation |
| 131 | "@" @attribute "file" @attribute ":" @attribute) |
| 132 | (file_annotation |
| 133 | (user_type |
| 134 | (type_identifier) @attribute)) |
| 135 | (file_annotation |
| 136 | (constructor_invocation |
| 137 | (user_type |
| 138 | (type_identifier) @attribute))) |
| 139 | |
| 140 | ;;; Operators & Punctuation |
| 141 | |
| 142 | [ |
| 143 | "!" |
| 144 | "!=" |
| 145 | "!==" |
| 146 | "=" |
| 147 | "==" |
| 148 | "===" |
| 149 | ">" |
| 150 | ">=" |
| 151 | "<" |
| 152 | "<=" |
| 153 | "||" |
| 154 | "&&" |
| 155 | "+" |
| 156 | "++" |
| 157 | "+=" |
| 158 | "-" |
| 159 | "--" |
| 160 | "-=" |
| 161 | "*" |
| 162 | "*=" |
| 163 | "/" |
| 164 | "/=" |
| 165 | "%" |
| 166 | "%=" |
| 167 | "?." |
| 168 | "?:" |
| 169 | "!!" |
| 170 | "is" |
| 171 | "in" |
| 172 | "as" |
| 173 | "as?" |
| 174 | ".." |
| 175 | "->" |
| 176 | ] @operator |
| 177 | |
| 178 | [ |
| 179 | "(" ")" |
| 180 | "[" "]" |
| 181 | "{" "}" |
| 182 | ] @punctuation.bracket |
| 183 | |
| 184 | [ |
| 185 | "." |
| 186 | "," |
| 187 | ";" |
| 188 | ":" |
| 189 | "::" |
| 190 | ] @punctuation.delimiter |
| 191 | |
| 192 | [ |
| 193 | (interpolation_identifier_start) |
| 194 | (interpolation_expression_start) |
| 195 | (interpolation_expression_end) |
| 196 | ] @punctuation.special |
| 197 | |
| 198 | (interpolated_identifier) @variable |
| 199 | |
| 200 | ;;; Types |
| 201 | |
| 202 | (type_identifier) @type |
| 203 | |
| 204 | ;;; Package & Imports |
| 205 | |
| 206 | (package_header |
| 207 | "package" @keyword |
| 208 | . (identifier) @type) |
| 209 | |
| 210 | (import_header |
| 211 | "import" @keyword) |
| 212 | |
| 213 | ;;; Labels |
| 214 | |
| 215 | (label) @label |
| 216 | |
| 217 | ;;; Function definitions |
| 218 | |
| 219 | (function_declaration |
| 220 | . (simple_identifier) @function) |
| 221 | |
| 222 | (getter |
| 223 | ("get") @function) |
| 224 | (setter |
| 225 | ("set") @function) |
| 226 | |
| 227 | (primary_constructor) @constructor |
| 228 | (secondary_constructor |
| 229 | ("constructor") @constructor) |
| 230 | |
| 231 | (constructor_invocation |
| 232 | (user_type |
| 233 | (type_identifier) @constructor)) |
| 234 | |
| 235 | (anonymous_initializer |
| 236 | ("init") @constructor) |
| 237 | |
| 238 | ;;; Function calls — must appear before the generic @variable catch-all |
| 239 | |
| 240 | ; function() |
| 241 | (call_expression |
| 242 | . (simple_identifier) @function) |
| 243 | |
| 244 | ; object.function() or object.property.function() |
| 245 | (call_expression |
| 246 | (navigation_expression |
| 247 | (navigation_suffix |
| 248 | (simple_identifier) @function) . )) |
| 249 | |
| 250 | (call_expression |
| 251 | . (simple_identifier) @function |
| 252 | (#any-of? @function |
| 253 | "arrayOf" |
| 254 | "arrayOfNulls" |
| 255 | "byteArrayOf" |
| 256 | "shortArrayOf" |
| 257 | "intArrayOf" |
| 258 | "longArrayOf" |
| 259 | "ubyteArrayOf" |
| 260 | "ushortArrayOf" |
| 261 | "uintArrayOf" |
| 262 | "ulongArrayOf" |
| 263 | "floatArrayOf" |
| 264 | "doubleArrayOf" |
| 265 | "booleanArrayOf" |
| 266 | "charArrayOf" |
| 267 | "emptyArray" |
| 268 | "mapOf" |
| 269 | "setOf" |
| 270 | "listOf" |
| 271 | "emptyMap" |
| 272 | "emptySet" |
| 273 | "emptyList" |
| 274 | "mutableMapOf" |
| 275 | "mutableSetOf" |
| 276 | "mutableListOf" |
| 277 | "print" |
| 278 | "println" |
| 279 | "error" |
| 280 | "TODO" |
| 281 | "run" |
| 282 | "runCatching" |
| 283 | "repeat" |
| 284 | "lazy" |
| 285 | "lazyOf" |
| 286 | "enumValues" |
| 287 | "enumValueOf" |
| 288 | "assert" |
| 289 | "check" |
| 290 | "checkNotNull" |
| 291 | "require" |
| 292 | "requireNotNull" |
| 293 | "with" |
| 294 | "suspend" |
| 295 | "synchronized" |
| 296 | )) |
| 297 | |
| 298 | ;;; Identifiers — specific patterns before the catch-all |
| 299 | |
| 300 | (enum_entry |
| 301 | (simple_identifier) @constant) |
| 302 | |
| 303 | (class_parameter |
| 304 | (simple_identifier) @property) |
| 305 | |
| 306 | (class_body |
| 307 | (property_declaration |
| 308 | (variable_declaration |
| 309 | (simple_identifier) @property))) |
| 310 | |
| 311 | (_ |
| 312 | (navigation_suffix |
| 313 | (simple_identifier) @property)) |
| 314 | |
| 315 | (parameter |
| 316 | (simple_identifier) @variable) |
| 317 | |
| 318 | (parameter_with_optional_type |
| 319 | (simple_identifier) @variable) |
| 320 | |
| 321 | (lambda_literal |
| 322 | (lambda_parameters |
| 323 | (variable_declaration |
| 324 | (simple_identifier) @variable))) |
| 325 | |
| 326 | ; `this` / `super` keywords |
| 327 | (this_expression) @variable.special |
| 328 | (super_expression) @variable.special |
| 329 | |
| 330 | ; `it` keyword inside lambdas |
| 331 | ((simple_identifier) @variable.special |
| 332 | (#eq? @variable.special "it")) |
| 333 | |
| 334 | ; `field` keyword inside property getter/setter |
| 335 | ((simple_identifier) @variable.special |
| 336 | (#eq? @variable.special "field")) |
| 337 | |
| 338 | ; `where` parsed as identifier when grammar doesn't recognise the clause |
| 339 | ((simple_identifier) @keyword |
| 340 | (#eq? @keyword "where")) |
| 341 | |
| 342 | ; Generic identifier catch-all — MUST be last so specific patterns win |
| 343 | (simple_identifier) @variable |