Add indents and textobjects for Kotlin (#12925)

This commit is contained in:
Sebastian Dörner 2025-02-20 18:58:27 +01:00 committed by GitHub
parent 3d7e2730e7
commit 0deb8bbce6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 106 additions and 24 deletions

View file

@ -121,7 +121,7 @@
| just | ✓ | ✓ | ✓ | |
| kdl | ✓ | ✓ | ✓ | |
| koka | ✓ | | ✓ | `koka` |
| kotlin | ✓ | | | `kotlin-language-server` |
| kotlin | ✓ | | | `kotlin-language-server` |
| koto | ✓ | ✓ | ✓ | `koto-ls` |
| latex | ✓ | ✓ | | `texlab` |
| ld | ✓ | | ✓ | |

View file

@ -1940,7 +1940,7 @@ language-servers = [ "kotlin-language-server" ]
[[grammar]]
name = "kotlin"
source = { git = "https://github.com/fwcd/tree-sitter-kotlin", rev = "a4f71eb9b8c9b19ded3e0e9470be4b1b77c2b569" }
source = { git = "https://github.com/fwcd/tree-sitter-kotlin", rev = "c4ddea359a7ff4d92360b2efcd6cfce5dc25afe6" }
[[language]]
name = "hcl"

View file

@ -68,19 +68,10 @@
"->"
] @operator
(multi_line_string_literal
(string_literal
"$" @punctuation
(interpolated_identifier) @none)
(multi_line_string_literal
"${" @punctuation
(interpolated_expression) @none
"}" @punctuation.)
; NOTE: `interpolated_identifier`s can be highlighted in any way
(line_string_literal
"$" @punctuation
(interpolated_identifier) @none)
(line_string_literal
(string_literal
"${" @punctuation
(interpolated_expression) @none
"}" @punctuation)
@ -108,6 +99,7 @@
"class"
"object"
"interface"
"companion"
; "typeof" ; NOTE: It is reserved for future use
] @keyword
@ -156,17 +148,14 @@
;;; Literals
; NOTE: Escapes not allowed in multi-line strings
(line_string_literal (character_escape_seq) @constant.character.escape)
(character_literal (character_escape_seq) @constant.character.escape)
[
(line_string_literal)
(multi_line_string_literal)
] @string
(string_literal) @string
(character_literal) @constant.character
[
"null" ; should be highlighted the same as booleans
(null_literal) ; should be highlighted the same as booleans
(boolean_literal)
] @constant.builtin.boolean
@ -180,7 +169,8 @@
] @constant.numeric.integer
[
(comment)
(line_comment)
(multiline_comment)
(shebang_line)
] @comment

View file

@ -0,0 +1,44 @@
[
(class_body)
(enum_class_body)
(lambda_literal)
; _block is hidden in the grammar, so list all public wrappers explicitly.
(function_body)
(anonymous_initializer)
(control_structure_body)
(secondary_constructor)
(try_expression)
(catch_block)
(finally_block)
(property_declaration)
(assignment)
(when_expression)
(call_expression)
(if_expression)
; Binary expressions
(multiplicative_expression)
(additive_expression)
(range_expression)
(infix_expression)
(elvis_expression)
(check_expression)
(comparison_expression)
(equality_expression)
(comparison_expression)
(equality_expression)
(conjunction_expression)
(disjunction_expression)
(call_suffix)
(function_value_parameters)
] @indent
[
"}"
")"
"]"
] @outdent

View file

@ -1,11 +1,15 @@
((comment) @injection.content
([
(line_comment)
(multiline_comment)
] @injection.content
(#set! injection.language "comment"))
; There are 3 ways to define a regex
; - "[abc]?".toRegex()
((call_expression
(navigation_expression
([(line_string_literal) (multi_line_string_literal)] @injection.content)
(string_literal
(string_content) @injection.content)
(navigation_suffix
((simple_identifier) @_function
(#eq? @_function "toRegex")))))
@ -18,7 +22,8 @@
(call_suffix
(value_arguments
(value_argument
[ (line_string_literal) (multi_line_string_literal) ] @injection.content))))
(string_literal
(string_content) @injection.content)))))
(#set! injection.language "regex"))
; - Regex.fromLiteral("[abc]?")
@ -32,5 +37,6 @@
(call_suffix
(value_arguments
(value_argument
[ (line_string_literal) (multi_line_string_literal) ] @injection.content))))
(string_literal
(string_content) @injection.content)))))
(#set! injection.language "regex"))

View file

@ -0,0 +1,42 @@
(function_declaration
(function_body)? @function.inside) @function.around
; Unlike function_body above, the constructor body is does not have its own
; symbol in the current grammar.
(secondary_constructor) @function.around
(class_declaration
(class_body)? @class.inside) @class.around
(class_declaration
(enum_class_body) @class.inside) @class.around
[
(line_comment)
(multiline_comment)
] @comment.inside
(line_comment)+ @comment.around
(multiline_comment) @comment.around
(enum_entry) @entry.around
(lambda_literal) @entry.around
(property_declaration) @entry.around
(object_declaration) @entry.around
(assignment) @entry.around
; TODO: This doesn't work with annotations yet, but fixing it without breaking
; the case of multiple parameters is non-trivial.
(function_value_parameters
((_) @parameter.inside . ","? @parameter.around) @parameter.around)
; secondary constructor uses function_value_parameters above
(primary_constructor
((_)@parameter.inside . ","? @parameter.around) @parameter.around)
(function_type_parameters
((_)@parameter.inside . ","? @parameter.around) @parameter.around)
(value_arguments
((_)@parameter.inside . ","? @parameter.around) @parameter.around)