Skip to content

Commit a24fae3

Browse files
authored
Update tree-sitter-lua and add textobjects for Lua (#3552)
1 parent 68909dc commit a24fae3

File tree

7 files changed

+72
-47
lines changed

7 files changed

+72
-47
lines changed

book/src/generated/lang-support.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
| llvm |||| |
6767
| llvm-mir |||| |
6868
| llvm-mir-yaml || || |
69-
| lua || || `lua-language-server` |
69+
| lua || || `lua-language-server` |
7070
| make || | | |
7171
| markdown || | | `marksman` |
7272
| markdown.inline || | | |

languages.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ language-server = { command = "lua-language-server", args = [] }
667667

668668
[[grammar]]
669669
name = "lua"
670-
source = { git = "https://github.com/nvim-treesitter/tree-sitter-lua", rev = "6f5d40190ec8a0aa8c8410699353d820f4f7d7a6" }
670+
source = { git = "https://github.com/MunifTanjim/tree-sitter-lua", rev = "887dfd4e83c469300c279314ff1619b1d0b85b91" }
671671

672672
[[language]]
673673
name = "svelte"

runtime/queries/lua/folds.scm

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
(do_statement)
3+
(while_statement)
4+
(repeat_statement)
5+
(if_statement)
6+
(for_statement)
7+
(function_declaration)
8+
(function_definition)
9+
(table_constructor)
10+
] @fold

runtime/queries/lua/highlights.scm

+40-37
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
;;; Highlighting for lua
22

33
;;; Builtins
4-
(self) @variable.builtin
4+
((identifier) @variable.builtin
5+
(#eq? @variable.builtin "self"))
56

67
;; Keywords
78

@@ -12,20 +13,20 @@
1213
"end"
1314
] @keyword.control.conditional)
1415

16+
(elseif_statement
1517
[
16-
"else"
1718
"elseif"
1819
"then"
19-
] @keyword.control.conditional
20+
"end"
21+
] @keyword.control.conditional)
2022

21-
(for_statement
23+
(else_statement
2224
[
23-
"for"
24-
"do"
25+
"else"
2526
"end"
26-
] @keyword.control.repeat)
27+
] @keyword.control.conditional)
2728

28-
(for_in_statement
29+
(for_statement
2930
[
3031
"for"
3132
"do"
@@ -51,21 +52,34 @@
5152
"end"
5253
] @keyword)
5354

55+
"return" @keyword.control.return
56+
5457
[
5558
"in"
5659
"local"
5760
(break_statement)
5861
"goto"
59-
"return"
6062
] @keyword
6163

64+
(function_declaration
65+
[
66+
"function"
67+
"end"
68+
] @keyword.function)
69+
70+
(function_definition
71+
[
72+
"function"
73+
"end"
74+
] @keyword.function)
75+
6276
;; Operators
6377

6478
[
6579
"not"
6680
"and"
6781
"or"
68-
] @operator
82+
] @keyword.operator
6983

7084
[
7185
"="
@@ -95,6 +109,7 @@
95109
["," "." ":" ";"] @punctuation.delimiter
96110

97111
;; Brackets
112+
98113
[
99114
"("
100115
")"
@@ -110,7 +125,8 @@
110125
(true)
111126
] @constant.builtin.boolean
112127
(nil) @constant.builtin
113-
(spread) @constant ;; "..."
128+
(vararg_expression) @constant
129+
114130
((identifier) @constant
115131
(#match? @constant "^[A-Z][A-Z_0-9]*$"))
116132

@@ -119,45 +135,32 @@
119135
(identifier) @variable.parameter)
120136

121137
; ;; Functions
122-
(function [(function_name) (identifier)] @function)
123-
(function ["function" "end"] @keyword.function)
124-
125-
(function
126-
(function_name
127-
(function_name_field
128-
(property_identifier) @function .)))
129-
130-
(local_function (identifier) @function)
131-
(local_function ["function" "end"] @keyword.function)
138+
(function_declaration name: (identifier) @function)
139+
(function_call name: (identifier) @function.call)
132140

133-
(variable_declaration
134-
(variable_declarator (identifier) @function) (function_definition))
135-
(local_variable_declaration
136-
(variable_declarator (identifier) @function) (function_definition))
141+
(function_declaration name: (dot_index_expression field: (identifier) @function))
142+
(function_call name: (dot_index_expression field: (identifier) @function.call))
137143

138-
(function_definition ["function" "end"] @keyword.function)
144+
; TODO: incorrectly highlights variable N in `N, nop = 42, function() end`
145+
(assignment_statement
146+
(variable_list
147+
name: (identifier) @function)
148+
(expression_list
149+
value: (function_definition)))
139150

140-
(function_call
141-
[
142-
((identifier) @variable (method) @function.method)
143-
((_) (method) @function.method)
144-
(identifier) @function
145-
(field_expression (property_identifier) @function)
146-
]
147-
. (arguments))
151+
(method_index_expression method: (identifier) @function.method)
148152

149153
;; Nodes
150-
(table ["{" "}"] @constructor)
151154
(comment) @comment
152155
(string) @string
153156
(number) @constant.numeric.integer
154157
(label_statement) @label
155158
; A bit of a tricky one, this will only match field names
156159
(field . (identifier) @variable.other.member (_))
157-
(shebang) @comment
160+
(hash_bang_line) @comment
158161

159162
;; Property
160-
(property_identifier) @variable.other.member
163+
(dot_index_expression field: (identifier) @variable.other.member)
161164

162165
;; Variable
163166
(identifier) @variable

runtime/queries/lua/indents.scm

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
[
22
(function_definition)
3-
(variable_declaration)
4-
(local_variable_declaration)
3+
(function_declaration)
4+
(method_index_expression)
55
(field)
6-
(local_function)
7-
(function)
86
(if_statement)
97
(for_statement)
10-
(for_in_statement)
118
(repeat_statement)
12-
(return_statement)
139
(while_statement)
14-
(table)
10+
(table_constructor)
1511
(arguments)
1612
(do_statement)
1713
] @indent

runtime/queries/lua/injections.scm

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
((comment) @injection.content
2-
(#set! injection.language "comment"))
2+
(#set! injection.language "comment")
3+
(#set! injection.include-children))

runtime/queries/lua/textobjects.scm

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(function_definition
2+
body: (_) @function.inside) @function.around
3+
4+
(function_declaration
5+
body: (_) @function.inside) @function.around
6+
7+
(parameters
8+
((_) @parameter.inside . ","? @parameter.around) @parameter.around)
9+
10+
(arguments
11+
((_) @parameter.inside . ","? @parameter.around) @parameter.around)
12+
13+
(comment) @comment.inside
14+
15+
(comment)+ @comment.around

0 commit comments

Comments
 (0)