|
| 1 | +;; Based on the nvim-treesitter highlighting, which is under the Apache license. |
| 2 | +;; See https://github.com/nvim-treesitter/nvim-treesitter/blob/f8ab59861eed4a1c168505e3433462ed800f2bae/queries/kotlin/highlights.scm |
| 3 | +;; |
| 4 | +;; The only difference in this file is that queries using #lua-match? |
| 5 | +;; have been removed. |
| 6 | + |
| 7 | +;;; Identifiers |
| 8 | + |
| 9 | +(simple_identifier) @variable |
| 10 | + |
| 11 | +; `it` keyword inside lambdas |
| 12 | +; FIXME: This will highlight the keyword outside of lambdas since tree-sitter |
| 13 | +; does not allow us to check for arbitrary nestation |
| 14 | +((simple_identifier) @variable.builtin |
| 15 | +(#eq? @variable.builtin "it")) |
| 16 | + |
| 17 | +; `field` keyword inside property getter/setter |
| 18 | +; FIXME: This will highlight the keyword outside of getters and setters |
| 19 | +; since tree-sitter does not allow us to check for arbitrary nestation |
| 20 | +((simple_identifier) @variable.builtin |
| 21 | +(#eq? @variable.builtin "field")) |
| 22 | + |
| 23 | +; `this` this keyword inside classes |
| 24 | +(this_expression) @variable.builtin |
| 25 | + |
| 26 | +; `super` keyword inside classes |
| 27 | +(super_expression) @variable.builtin |
| 28 | + |
| 29 | +(class_parameter |
| 30 | + (simple_identifier) @property) |
| 31 | + |
| 32 | +(class_body |
| 33 | + (property_declaration |
| 34 | + (variable_declaration |
| 35 | + (simple_identifier) @property))) |
| 36 | + |
| 37 | +; id_1.id_2.id_3: `id_2` and `id_3` are assumed as object properties |
| 38 | +(_ |
| 39 | + (navigation_suffix |
| 40 | + (simple_identifier) @property)) |
| 41 | + |
| 42 | +(enum_entry |
| 43 | + (simple_identifier) @constant) |
| 44 | + |
| 45 | +(type_identifier) @type |
| 46 | + |
| 47 | +((type_identifier) @type.builtin |
| 48 | + (#any-of? @type.builtin |
| 49 | + "Byte" |
| 50 | + "Short" |
| 51 | + "Int" |
| 52 | + "Long" |
| 53 | + "UByte" |
| 54 | + "UShort" |
| 55 | + "UInt" |
| 56 | + "ULong" |
| 57 | + "Float" |
| 58 | + "Double" |
| 59 | + "Boolean" |
| 60 | + "Char" |
| 61 | + "String" |
| 62 | + "Array" |
| 63 | + "ByteArray" |
| 64 | + "ShortArray" |
| 65 | + "IntArray" |
| 66 | + "LongArray" |
| 67 | + "UByteArray" |
| 68 | + "UShortArray" |
| 69 | + "UIntArray" |
| 70 | + "ULongArray" |
| 71 | + "FloatArray" |
| 72 | + "DoubleArray" |
| 73 | + "BooleanArray" |
| 74 | + "CharArray" |
| 75 | + "Map" |
| 76 | + "Set" |
| 77 | + "List" |
| 78 | + "EmptyMap" |
| 79 | + "EmptySet" |
| 80 | + "EmptyList" |
| 81 | + "MutableMap" |
| 82 | + "MutableSet" |
| 83 | + "MutableList" |
| 84 | +)) |
| 85 | + |
| 86 | +(package_header |
| 87 | + . (identifier)) @namespace |
| 88 | + |
| 89 | +(import_header |
| 90 | + "import" @include) |
| 91 | + |
| 92 | + |
| 93 | +; TODO: Seperate labeled returns/breaks/continue/super/this |
| 94 | +; Must be implemented in the parser first |
| 95 | +(label) @label |
| 96 | + |
| 97 | +;;; Function definitions |
| 98 | + |
| 99 | +(function_declaration |
| 100 | + . (simple_identifier) @function) |
| 101 | + |
| 102 | +(getter |
| 103 | + ("get") @function.builtin) |
| 104 | +(setter |
| 105 | + ("set") @function.builtin) |
| 106 | + |
| 107 | +(primary_constructor) @constructor |
| 108 | +(secondary_constructor |
| 109 | + ("constructor") @constructor) |
| 110 | + |
| 111 | +(constructor_invocation |
| 112 | + (user_type |
| 113 | + (type_identifier) @constructor)) |
| 114 | + |
| 115 | +(anonymous_initializer |
| 116 | + ("init") @constructor) |
| 117 | + |
| 118 | +(parameter |
| 119 | + (simple_identifier) @parameter) |
| 120 | + |
| 121 | +(parameter_with_optional_type |
| 122 | + (simple_identifier) @parameter) |
| 123 | + |
| 124 | +; lambda parameters |
| 125 | +(lambda_literal |
| 126 | + (lambda_parameters |
| 127 | + (variable_declaration |
| 128 | + (simple_identifier) @parameter))) |
| 129 | + |
| 130 | +;;; Function calls |
| 131 | + |
| 132 | +; function() |
| 133 | +(call_expression |
| 134 | + . (simple_identifier) @function) |
| 135 | + |
| 136 | +; object.function() or object.property.function() |
| 137 | +(call_expression |
| 138 | + (navigation_expression |
| 139 | + (navigation_suffix |
| 140 | + (simple_identifier) @function) . )) |
| 141 | + |
| 142 | +(call_expression |
| 143 | + . (simple_identifier) @function.builtin |
| 144 | + (#any-of? @function.builtin |
| 145 | + "arrayOf" |
| 146 | + "arrayOfNulls" |
| 147 | + "byteArrayOf" |
| 148 | + "shortArrayOf" |
| 149 | + "intArrayOf" |
| 150 | + "longArrayOf" |
| 151 | + "ubyteArrayOf" |
| 152 | + "ushortArrayOf" |
| 153 | + "uintArrayOf" |
| 154 | + "ulongArrayOf" |
| 155 | + "floatArrayOf" |
| 156 | + "doubleArrayOf" |
| 157 | + "booleanArrayOf" |
| 158 | + "charArrayOf" |
| 159 | + "emptyArray" |
| 160 | + "mapOf" |
| 161 | + "setOf" |
| 162 | + "listOf" |
| 163 | + "emptyMap" |
| 164 | + "emptySet" |
| 165 | + "emptyList" |
| 166 | + "mutableMapOf" |
| 167 | + "mutableSetOf" |
| 168 | + "mutableListOf" |
| 169 | + "print" |
| 170 | + "println" |
| 171 | + "error" |
| 172 | + "TODO" |
| 173 | + "run" |
| 174 | + "runCatching" |
| 175 | + "repeat" |
| 176 | + "lazy" |
| 177 | + "lazyOf" |
| 178 | + "enumValues" |
| 179 | + "enumValueOf" |
| 180 | + "assert" |
| 181 | + "check" |
| 182 | + "checkNotNull" |
| 183 | + "require" |
| 184 | + "requireNotNull" |
| 185 | + "with" |
| 186 | + "suspend" |
| 187 | + "synchronized" |
| 188 | +)) |
| 189 | + |
| 190 | +;;; Literals |
| 191 | + |
| 192 | +[ |
| 193 | + (line_comment) |
| 194 | + (multiline_comment) |
| 195 | + (shebang_line) |
| 196 | +] @comment |
| 197 | + |
| 198 | +(real_literal) @float |
| 199 | +[ |
| 200 | + (integer_literal) |
| 201 | + (long_literal) |
| 202 | + (hex_literal) |
| 203 | + (bin_literal) |
| 204 | + (unsigned_literal) |
| 205 | +] @number |
| 206 | + |
| 207 | +[ |
| 208 | + "null" ; should be highlighted the same as booleans |
| 209 | + (boolean_literal) |
| 210 | +] @boolean |
| 211 | + |
| 212 | +(character_literal) @character |
| 213 | + |
| 214 | +(string_literal) @string |
| 215 | + |
| 216 | +(character_escape_seq) @string.escape |
| 217 | + |
| 218 | +; There are 3 ways to define a regex |
| 219 | +; - "[abc]?".toRegex() |
| 220 | +(call_expression |
| 221 | + (navigation_expression |
| 222 | + ((string_literal) @string.regex) |
| 223 | + (navigation_suffix |
| 224 | + ((simple_identifier) @_function |
| 225 | + (#eq? @_function "toRegex"))))) |
| 226 | + |
| 227 | +; - Regex("[abc]?") |
| 228 | +(call_expression |
| 229 | + ((simple_identifier) @_function |
| 230 | + (#eq? @_function "Regex")) |
| 231 | + (call_suffix |
| 232 | + (value_arguments |
| 233 | + (value_argument |
| 234 | + (string_literal) @string.regex)))) |
| 235 | + |
| 236 | +; - Regex.fromLiteral("[abc]?") |
| 237 | +(call_expression |
| 238 | + (navigation_expression |
| 239 | + ((simple_identifier) @_class |
| 240 | + (#eq? @_class "Regex")) |
| 241 | + (navigation_suffix |
| 242 | + ((simple_identifier) @_function |
| 243 | + (#eq? @_function "fromLiteral")))) |
| 244 | + (call_suffix |
| 245 | + (value_arguments |
| 246 | + (value_argument |
| 247 | + (string_literal) @string.regex)))) |
| 248 | + |
| 249 | +;;; Keywords |
| 250 | + |
| 251 | +(type_alias "typealias" @keyword) |
| 252 | +[ |
| 253 | + (class_modifier) |
| 254 | + (member_modifier) |
| 255 | + (function_modifier) |
| 256 | + (property_modifier) |
| 257 | + (platform_modifier) |
| 258 | + (variance_modifier) |
| 259 | + (parameter_modifier) |
| 260 | + (visibility_modifier) |
| 261 | + (reification_modifier) |
| 262 | + (inheritance_modifier) |
| 263 | +]@keyword |
| 264 | + |
| 265 | +[ |
| 266 | + "val" |
| 267 | + "var" |
| 268 | + "enum" |
| 269 | + "class" |
| 270 | + "object" |
| 271 | + "interface" |
| 272 | +; "typeof" ; NOTE: It is reserved for future use |
| 273 | +] @keyword |
| 274 | + |
| 275 | +("fun") @keyword.function |
| 276 | + |
| 277 | +(jump_expression) @keyword.return |
| 278 | + |
| 279 | +[ |
| 280 | + "if" |
| 281 | + "else" |
| 282 | + "when" |
| 283 | +] @conditional |
| 284 | + |
| 285 | +[ |
| 286 | + "for" |
| 287 | + "do" |
| 288 | + "while" |
| 289 | +] @repeat |
| 290 | + |
| 291 | +[ |
| 292 | + "try" |
| 293 | + "catch" |
| 294 | + "throw" |
| 295 | + "finally" |
| 296 | +] @exception |
| 297 | + |
| 298 | + |
| 299 | +(annotation |
| 300 | + "@" @attribute (use_site_target)? @attribute) |
| 301 | +(annotation |
| 302 | + (user_type |
| 303 | + (type_identifier) @attribute)) |
| 304 | +(annotation |
| 305 | + (constructor_invocation |
| 306 | + (user_type |
| 307 | + (type_identifier) @attribute))) |
| 308 | + |
| 309 | +(file_annotation |
| 310 | + "@" @attribute "file" @attribute ":" @attribute) |
| 311 | +(file_annotation |
| 312 | + (user_type |
| 313 | + (type_identifier) @attribute)) |
| 314 | +(file_annotation |
| 315 | + (constructor_invocation |
| 316 | + (user_type |
| 317 | + (type_identifier) @attribute))) |
| 318 | + |
| 319 | +;;; Operators & Punctuation |
| 320 | + |
| 321 | +[ |
| 322 | + "!" |
| 323 | + "!=" |
| 324 | + "!==" |
| 325 | + "=" |
| 326 | + "==" |
| 327 | + "===" |
| 328 | + ">" |
| 329 | + ">=" |
| 330 | + "<" |
| 331 | + "<=" |
| 332 | + "||" |
| 333 | + "&&" |
| 334 | + "+" |
| 335 | + "++" |
| 336 | + "+=" |
| 337 | + "-" |
| 338 | + "--" |
| 339 | + "-=" |
| 340 | + "*" |
| 341 | + "*=" |
| 342 | + "/" |
| 343 | + "/=" |
| 344 | + "%" |
| 345 | + "%=" |
| 346 | + "?." |
| 347 | + "?:" |
| 348 | + "!!" |
| 349 | + "is" |
| 350 | + "!is" |
| 351 | + "in" |
| 352 | + "!in" |
| 353 | + "as" |
| 354 | + "as?" |
| 355 | + ".." |
| 356 | + "->" |
| 357 | +] @operator |
| 358 | + |
| 359 | +[ |
| 360 | + "(" ")" |
| 361 | + "[" "]" |
| 362 | + "{" "}" |
| 363 | +] @punctuation.bracket |
| 364 | + |
| 365 | +[ |
| 366 | + "." |
| 367 | + "," |
| 368 | + ";" |
| 369 | + ":" |
| 370 | + "::" |
| 371 | +] @punctuation.delimiter |
| 372 | + |
| 373 | +; NOTE: `interpolated_identifier`s can be highlighted in any way |
| 374 | +(string_literal |
| 375 | + "$" @punctuation.special |
| 376 | + (interpolated_identifier) @none) |
| 377 | +(string_literal |
| 378 | + "${" @punctuation.special |
| 379 | + (interpolated_expression) @none |
| 380 | + "}" @punctuation.special) |
0 commit comments