summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSal Bakraa <SalBakraa@gmail.com>2022-01-06 02:19:09 +0300
committerStephan Seitz <stephan.seitz@fau.de>2022-01-08 20:01:00 +0100
commit5cfc96effe8427870adf561f564a9170778b6e4d (patch)
tree7944414917dcad50c6f05b17133f8041b7ec3db4
parent89abe4af393e5b7000b64683915f8e2dccb26cd3 (diff)
fix(kotlin): update highlights query to match new parser
-rw-r--r--queries/kotlin/highlights.scm546
1 files changed, 380 insertions, 166 deletions
diff --git a/queries/kotlin/highlights.scm b/queries/kotlin/highlights.scm
index abbac210..ef1d2185 100644
--- a/queries/kotlin/highlights.scm
+++ b/queries/kotlin/highlights.scm
@@ -1,195 +1,409 @@
+;;; Identifiers
+
+(simple_identifier) @variable
+
+; `it` keyword inside lambdas
+; FIXME: This will highlight the keyword outside of lambdas since tree-sitter
+; does not allow us to check for arbitrary nestation
+((simple_identifier) @variable.builtin
+(#eq? @variable.builtin "it"))
+
+; `field` keyword inside property getter/setter
+; FIXME: This will highlight the keyword outside of getters and setters
+; since tree-sitter does not allow us to check for arbitrary nestation
+((simple_identifier) @variable.builtin
+(#eq? @variable.builtin "field"))
+
+; `this` this keyword inside classes
+(this_expression) @variable.builtin
+
+; `super` keyword inside classes
+(super_expression) @variable.builtin
+
+(class_parameter
+ (simple_identifier) @property)
+
+(class_body
+ (property_declaration
+ (variable_declaration
+ (simple_identifier) @property)))
+
+; id_1.id_2.id_3: `id_2` and `id_3` are assumed as object properties
+(_
+ (navigation_suffix
+ (simple_identifier) @property))
+
+; SCREAMING CASE identifiers are assumed to be constants
+((simple_identifier) @constant
+(#lua-match? @constant "^[A-Z][A-Z0-9_]*$"))
+
+(_
+ (navigation_suffix
+ (simple_identifier) @constant
+ (#lua-match? @constant "^[A-Z][A-Z0-9_]*$")))
+
+(enum_entry
+ (simple_identifier) @constant)
+
+(type_identifier) @type
+
+((type_identifier) @type.builtin
+ (#any-of? @type.builtin
+ "Byte"
+ "Short"
+ "Int"
+ "Long"
+ "UByte"
+ "UShort"
+ "UInt"
+ "ULong"
+ "Float"
+ "Double"
+ "Boolean"
+ "Char"
+ "String"
+ "Array"
+ "ByteArray"
+ "ShortArray"
+ "IntArray"
+ "LongArray"
+ "UByteArray"
+ "UShortArray"
+ "UIntArray"
+ "ULongArray"
+ "FloatArray"
+ "DoubleArray"
+ "BooleanArray"
+ "CharArray"
+ "Map"
+ "Set"
+ "List"
+ "EmptyMap"
+ "EmptySet"
+ "EmptyList"
+ "MutableMap"
+ "MutableSet"
+ "MutableList"
+))
+
+(package_header
+ . (identifier)) @namespace
+
+(import_header
+ "import" @include)
+
+; The last `simple_identifier` in a `import_header` will always either be a function
+; or a type. Classes can appear anywhere in the import path, unlike functions
+(import_header
+ (identifier
+ (simple_identifier) @type @import)
+ (import_alias
+ (type_identifier) @type)?
+ (#lua-match? @import "^[A-Z]"))
+
+(import_header
+ (identifier
+ (simple_identifier) @function @import .)
+ (import_alias
+ (type_identifier) @function)?
+ (#lua-match? @import "^[a-z]"))
+
+; TODO: Seperate labeled returns/breaks/continue/super/this
+; Must be implemented in the parser first
+(label) @label
+
+;;; Function definitions
+
+(function_declaration
+ . (simple_identifier) @function)
+
+(getter
+ ("get") @function.builtin)
+(setter
+ ("set") @function.builtin)
+
+(primary_constructor) @constructor
+(secondary_constructor
+ ("constructor") @constructor)
+
+(constructor_invocation
+ (user_type
+ (type_identifier) @constructor))
+
+(anonymous_initializer
+ ("init") @constructor)
+
+(parameter
+ (simple_identifier) @parameter)
+
+(parameter_with_optional_type
+ (simple_identifier) @parameter)
+
+; lambda parameters
+(lambda_literal
+ (lambda_parameters
+ (variable_declaration
+ (simple_identifier) @parameter)))
+
+;;; Function calls
+
+; function()
+(call_expression
+ . (simple_identifier) @function)
+
+; object.function() or object.property.function()
+(call_expression
+ (navigation_expression
+ (navigation_suffix
+ (simple_identifier) @function) . ))
+
+(call_expression
+ . (simple_identifier) @function.builtin
+ (#any-of? @function.builtin
+ "arrayOf"
+ "arrayOfNulls"
+ "byteArrayOf"
+ "shortArrayOf"
+ "intArrayOf"
+ "longArrayOf"
+ "ubyteArrayOf"
+ "ushortArrayOf"
+ "uintArrayOf"
+ "ulongArrayOf"
+ "floatArrayOf"
+ "doubleArrayOf"
+ "booleanArrayOf"
+ "charArrayOf"
+ "emptyArray"
+ "mapOf"
+ "setOf"
+ "listOf"
+ "emptyMap"
+ "emptySet"
+ "emptyList"
+ "mutableMapOf"
+ "mutableSetOf"
+ "mutableListOf"
+ "print"
+ "println"
+ "error"
+ "TODO"
+ "run"
+ "runCatching"
+ "repeat"
+ "lazy"
+ "lazyOf"
+ "enumValues"
+ "enumValueOf"
+ "assert"
+ "check"
+ "checkNotNull"
+ "require"
+ "requireNotNull"
+ "with"
+ "suspend"
+ "synchronized"
+))
+
+;;; Literals
+
[
- (comment)
- (shebang)
+ (comment)
+ (shebang_line)
] @comment
+(real_literal) @float
[
- ":"
- ","
- "."
-] @punctuation.delimiter
+ (integer_literal)
+ (long_literal)
+ (hex_literal)
+ (bin_literal)
+ (unsigned_literal)
+] @number
[
- "("
- ")"
- "{"
- "}"
-] @punctuation.bracket
+ "null" ; should be highlighted the same as booleans
+ (boolean_literal)
+] @boolean
+
+(character_literal) @character
[
- "class"
- "object"
- "fun"
- "var"
- "val"
- "try"
- "catch"
- "finally"
-] @keyword
+ (line_string_literal)
+ (multi_line_string_literal)
+] @string
-"import" @include
+; NOTE: Escapes not allowed in multi-line strings
+(line_string_literal (character_escape_seq) @string.escape)
-"return" @keyword.return
+; There are 3 ways to define a regex
+; - "[abc]?".toRegex()
+(call_expression
+ (navigation_expression
+ ([(line_string_literal) (multi_line_string_literal)] @string.regex)
+ (navigation_suffix
+ ((simple_identifier) @_function
+ (#eq? @_function "toRegex")))))
-(package_stmt
- "package" @include)
-(package_path
- (identifier) @namespace)
+; - Regex("[abc]?")
+(call_expression
+ ((simple_identifier) @_function
+ (#eq? @_function "Regex"))
+ (call_suffix
+ (value_arguments
+ (value_argument
+ [ (line_string_literal) (multi_line_string_literal) ] @string.regex))))
-(return_expr
- "return@" @keyword.return
- label: (identifier) @label)
-(break_expr
- "break@" @keyword
- label: (identifier) @label)
+; - Regex.fromLiteral("[abc]?")
+(call_expression
+ (navigation_expression
+ ((simple_identifier) @_class
+ (#eq? @_class "Regex"))
+ (navigation_suffix
+ ((simple_identifier) @_function
+ (#eq? @_function "fromLiteral"))))
+ (call_suffix
+ (value_arguments
+ (value_argument
+ [ (line_string_literal) (multi_line_string_literal) ] @string.regex))))
-(label
- name: (identifier) @label) @constant
+;;; Keywords
+(type_alias "typealias" @keyword)
[
- "for"
- "do"
- "while"
-] @repeat
+ (class_modifier)
+ (member_modifier)
+ (function_modifier)
+ (property_modifier)
+ (platform_modifier)
+ (variance_modifier)
+ (parameter_modifier)
+ (visibility_modifier)
+ (reification_modifier)
+ (inheritance_modifier)
+]@keyword
[
- "||"
- "&&"
- "!="
- "!=="
- "=="
- "==="
- "<"
- ">"
- "<="
- ">="
- ".."
- "+"
- "-"
- "%"
- "*"
- "/"
-] @operator
+ "val"
+ "var"
+ "enum"
+ "class"
+ "object"
+ "interface"
+; "typeof" ; NOTE: It is reserved for future use
+] @keyword
+
+("fun") @keyword.function
+
+(jump_expression) @keyword.return
[
- "as"
- "as?"
- "in"
-] @keyword.operator
-
-(class_decl
- name: (identifier) @type)
-(class_decl
- super: (_) @type)
-(object_decl
- name: (identifier) @type)
-
-(class_param
- name: (identifier) @parameter
- type: (_) @type)
-
-(type_constraints
- "where" @keyword
- (type_constraint
- type: (identifier) @type
- super: (_) @type))
-
-(property
- name: (identifier) @property)
-(property
- type: (_) @type)
+ "if"
+ "else"
+ "when"
+] @conditional
-(enum_entry
- name: (identifier) @constant)
+[
+ "for"
+ "do"
+ "while"
+] @repeat
-(func_decl
- name: (identifier) @function)
-(func_decl
- return: (_) @type)
+[
+ "try"
+ "catch"
+ "throw"
+ "finally"
+] @exception
-(param_decl
- name: (identifier) @parameter
- type: (_) @type)
-(type_param
- name: (identifier) @type)
+(annotation
+ "@" @annotation (use_site_target)? @annotation)
+(annotation
+ (user_type
+ (type_identifier) @annotation))
+(annotation
+ (constructor_invocation
+ (user_type
+ (type_identifier) @annotation)))
-(call
- function: (identifier) @function)
-(call
- function: (selector
- field: (identifier) @function))
+(file_annotation
+ "@" @annotation "file" @annotation ":" @annotation)
+(file_annotation
+ (user_type
+ (type_identifier) @annotation))
+(file_annotation
+ (constructor_invocation
+ (user_type
+ (type_identifier) @annotation)))
-(getter
- "get" @function.builtin)
-(setter
- "set" @function.builtin)
+;;; Operators & Punctuation
-((identifier) @function.builtin
- (#any-of? @function.builtin
- "arrayOf"
- "arrayOfNulls"
- "assert"
- "booleanArrayOf"
- "byteArrayOf"
- "Char"
- "charArrayOf"
- "check"
- "checkNotNull"
- "doubleArrayOf"
- "emptyArray"
- "enumValueOf"
- "enumValues"
- "error"
- "floatArrayOf"
- "intArrayOf"
- "lazy"
- "lazyOf"
- "longArrayOf"
- "repeat"
- "require"
- "requireNotNull"
- "run"
- "runCatching"
- "shortArrayOf"
- "suspend"
- "synchronized"
- "TODO"
- "ubyteArrayOf"
- "UintArray"
- "uintArrayOf"
- "ULongArray"
- "ulongArrayOf"
- "UShortArray"
- "ushortArrayOf"
- "with"
- "print"
- "println"
- "readLine"))
-((identifier) @constant.builtin
- (#eq? @constant.builtin "it"))
-
-(lambda
- args: (var_decl
- name: (identifier) @parameter))
-(lambda
- args: (var_decl
- type: (_) @type ))
-
-(binary_expr
- operator: (identifier) @keyword.operator)
-
-(ERROR) @error
-
-(integer) @number
-(float) @float
-
-(string) @string
-
-"null" @constant
+[
+ "!"
+ "!="
+ "!=="
+ "="
+ "=="
+ "==="
+ ">"
+ ">="
+ "<"
+ "<="
+ "||"
+ "&&"
+ "+"
+ "++"
+ "+="
+ "-"
+ "--"
+ "-="
+ "*"
+ "*="
+ "/"
+ "/="
+ "%"
+ "%="
+ "?."
+ "?:"
+ "!!"
+ "is"
+ "!is"
+ "in"
+ "!in"
+ "as"
+ "as?"
+ ".."
+ "->"
+] @operator
[
- "true"
- "false"
-] @boolean
+ "(" ")"
+ "[" "]"
+ "{" "}"
+] @punctuation.bracket
+
+[
+ "."
+ ","
+ ";"
+ ":"
+ "::"
+] @punctuation.delimiter
+
+; NOTE: `interpolated_identifier`s can be highlighted in any way
+(line_string_literal
+ "$" @punctuation.special
+ (interpolated_identifier) @none)
+(line_string_literal
+ "${" @punctuation.special
+ (interpolated_expression) @none
+ "}" @punctuation.special)
+(multi_line_string_literal
+ "$" @punctuation.special
+ (interpolated_identifier) @none)
+(multi_line_string_literal
+ "${" @punctuation.special
+ (interpolated_expression) @none
+ "}" @punctuation.special)