aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjohn muhl2023-11-14 16:25:43 -0600
committerEli Zaretskii2023-12-02 15:46:47 +0200
commit4835c9913b2183af6003007f8eee2cd0ab4e697d (patch)
treee74acbc9475fc9a4d9f512510f8430191420120f
parent790a96ac994d7d07580fef7b5d054154a4ec7cc7 (diff)
downloademacs-4835c9913b2183af6003007f8eee2cd0ab4e697d.tar.gz
emacs-4835c9913b2183af6003007f8eee2cd0ab4e697d.zip
Improve font-locking in lua-ts-mode (bug#67554)
* lisp/progmodes/lua-ts-mode.el (lua-ts-mode): Move property highlighting to level 4. (lua-ts--keywords): Remove `true', `false' and `nil' from keywords. (lua-ts--font-lock-settings): Highlight assignments, functions and labels in more places. Distinguish comment delimiters. (lua-ts--comment-font-lock): New function.
-rw-r--r--lisp/progmodes/lua-ts-mode.el151
1 files changed, 79 insertions, 72 deletions
diff --git a/lisp/progmodes/lua-ts-mode.el b/lisp/progmodes/lua-ts-mode.el
index a910d759c83..7dd05b2757f 100644
--- a/lisp/progmodes/lua-ts-mode.el
+++ b/lisp/progmodes/lua-ts-mode.el
@@ -133,135 +133,141 @@
133 "Lua built-in functions for tree-sitter font-locking.") 133 "Lua built-in functions for tree-sitter font-locking.")
134 134
135(defvar lua-ts--keywords 135(defvar lua-ts--keywords
136 '("and" "do" "else" "elseif" "end" "for" "function" 136 '("and" "do" "else" "elseif" "end" "for" "function" "goto" "if"
137 "goto" "if" "in" "local" "not" "or" "repeat" 137 "in" "local" "not" "or" "repeat" "return" "then" "until" "while")
138 "return" "then" "until" "while")
139 "Lua keywords for tree-sitter font-locking and navigation.") 138 "Lua keywords for tree-sitter font-locking and navigation.")
140 139
140(defun lua-ts--comment-font-lock (node override start end &rest _)
141 "Apply font lock to comment NODE within START and END.
142Applies `font-lock-comment-delimiter-face' and
143`font-lock-comment-face' See `treesit-fontify-with-override' for
144values of OVERRIDE."
145 (let* ((node-start (treesit-node-start node))
146 (node-end (treesit-node-end node))
147 (node-text (treesit-node-text node t))
148 (delimiter-end (+ 2 node-start)))
149 (when (and (>= node-start start)
150 (<= delimiter-end end)
151 (string-match "\\`--" node-text))
152 (treesit-fontify-with-override node-start
153 delimiter-end
154 font-lock-comment-delimiter-face
155 override))
156 (treesit-fontify-with-override (max delimiter-end start)
157 (min node-end end)
158 font-lock-comment-face
159 override)))
160
141(defvar lua-ts--font-lock-settings 161(defvar lua-ts--font-lock-settings
142 (treesit-font-lock-rules 162 (treesit-font-lock-rules
143 :language 'lua 163 :default-language 'lua
144 :feature 'bracket 164 :feature 'bracket
145 '(["(" ")" "[" "]" "{" "}"] @font-lock-bracket-face) 165 '(["(" ")" "[" "]" "{" "}"] @font-lock-bracket-face)
146 166
147 :language 'lua
148 :feature 'delimiter 167 :feature 'delimiter
149 '(["," ";"] @font-lock-delimiter-face) 168 '(["," ";"] @font-lock-delimiter-face)
150 169
151 :language 'lua
152 :feature 'constant 170 :feature 'constant
153 '((variable_list 171 '([(variable_list
154 attribute: (attribute (["<" ">"] (identifier)))) 172 attribute: (attribute (["<" ">"] (identifier))))
155 @font-lock-constant-face 173 (label_statement)
156 (goto_statement (identifier) @font-lock-constant-face) 174 (true) (false) (nil)]
157 (label_statement) @font-lock-constant-face) 175 @font-lock-constant-face)
158 176
159 :language 'lua
160 :feature 'operator 177 :feature 'operator
161 '(["and" "not" "or" "+" "-" "*" "/" "%" "^" 178 '(["+" "-" "*" "/" "%" "^" "#" "==" "~=" "<=" ">="
162 "#" "==" "~=" "<=" ">=" "<" ">" "=" "&" 179 "<" ">" "=" "&" "~" "|" "<<" ">>" "//" ".."
163 "~" "|" "<<" ">>" "//" ".."] 180 (vararg_expression)]
164 @font-lock-operator-face 181 @font-lock-operator-face)
165 (vararg_expression) @font-lock-operator-face)
166 182
167 :language 'lua
168 :feature 'builtin 183 :feature 'builtin
169 `(((identifier) @font-lock-builtin-face 184 `(((identifier) @font-lock-builtin-face
170 (:match ,(regexp-opt lua-ts--builtins 'symbols) 185 (:match ,(regexp-opt lua-ts--builtins 'symbols)
171 @font-lock-builtin-face))) 186 @font-lock-builtin-face)))
172 187
173 :language 'lua
174 :feature 'function 188 :feature 'function
175 '((function_call name: (identifier) @font-lock-function-call-face) 189 '((function_call name: (identifier) @font-lock-function-call-face)
176 (function_call 190 (function_call
177 name: (method_index_expression 191 (method_index_expression
178 method: (identifier) @font-lock-function-call-face)) 192 method: (identifier) @font-lock-function-call-face))
179 (function_call 193 (function_call
180 name: (dot_index_expression (identifier) @font-lock-function-call-face))) 194 (dot_index_expression
195 field: (identifier) @font-lock-function-call-face)))
181 196
182 :language 'lua
183 :feature 'punctuation 197 :feature 'punctuation
184 '(["." ":"] @font-lock-punctuation-face) 198 '(["." ":"] @font-lock-punctuation-face)
185 199
186 :language 'lua
187 :feature 'variable 200 :feature 'variable
188 '((function_call 201 '((function_call
189 arguments: (arguments (identifier)) 202 (arguments (identifier) @font-lock-variable-use-face))
190 @font-lock-variable-use-face)
191 (function_call 203 (function_call
192 name: (method_index_expression 204 (arguments
193 table: (identifier) @font-lock-variable-use-face))) 205 (binary_expression (identifier) @font-lock-variable-use-face)))
206 (function_call
207 (arguments
208 (bracket_index_expression (identifier) @font-lock-variable-use-face)))
209 (function_declaration
210 (parameters name: (identifier) @font-lock-variable-name-face)))
194 211
195 :language 'lua
196 :feature 'number 212 :feature 'number
197 '((number) @font-lock-number-face) 213 '((number) @font-lock-number-face)
198 214
199 :language 'lua
200 :feature 'keyword 215 :feature 'keyword
201 `((break_statement) @font-lock-keyword-face 216 `([(break_statement)
202 (true) @font-lock-constant-face 217 ,(vconcat lua-ts--keywords)]
203 (false) @font-lock-constant-face 218 @font-lock-keyword-face
204 (nil) @font-lock-constant-face 219 (goto_statement ((identifier) @font-lock-constant-face)))
205 ,(vconcat lua-ts--keywords) 220
206 @font-lock-keyword-face)
207
208 :language 'lua
209 :feature 'string 221 :feature 'string
210 '((string) @font-lock-string-face) 222 '((string) @font-lock-string-face)
211 223
212 :language 'lua
213 :feature 'escape 224 :feature 'escape
214 :override t 225 :override t
215 '((escape_sequence) @font-lock-escape-face) 226 '((escape_sequence) @font-lock-escape-face)
216 227
217 :language 'lua
218 :feature 'comment 228 :feature 'comment
219 '((comment) @font-lock-comment-face 229 '((comment) @lua-ts--comment-font-lock
220 (hash_bang_line) @font-lock-comment-face) 230 (hash_bang_line) @font-lock-comment-face)
221 231
222 :language 'lua
223 :feature 'definition 232 :feature 'definition
224 '((function_declaration 233 '((function_declaration
225 name: (identifier) @font-lock-function-name-face) 234 (identifier) @font-lock-function-name-face)
226 (assignment_statement
227 (variable_list name: [(identifier)]) @font-lock-function-name-face
228 (expression_list value: (function_definition)))
229 (table_constructor
230 (field
231 name: (identifier) @font-lock-function-name-face
232 value: (function_definition)))
233 (function_declaration
234 name: (dot_index_expression (identifier) @font-lock-function-name-face))
235 (function_declaration 235 (function_declaration
236 name: (method_index_expression (identifier) @font-lock-function-name-face)) 236 (dot_index_expression
237 field: (identifier) @font-lock-function-name-face))
237 (function_declaration 238 (function_declaration
238 (method_index_expression 239 (method_index_expression
240 method: (identifier) @font-lock-function-name-face))
241 (assignment_statement
242 (variable_list
243 (identifier) @font-lock-function-name-face)
244 (expression_list value: (function_definition)))
245 (field
246 name: (identifier) @font-lock-function-name-face
247 value: (function_definition))
248 (assignment_statement
249 (variable_list
239 (dot_index_expression 250 (dot_index_expression
240 table: (identifier) @font-lock-function-name-face 251 field: (identifier) @font-lock-function-name-face))
241 field: (identifier) @font-lock-property-name-face 252 (expression_list
242 ))) 253 value:
243 (parameters 254 (function_definition))))
244 name: (identifier) @font-lock-variable-name-face) 255
256 :feature 'assignment
257 '((variable_list (identifier) @font-lock-variable-name-face)
258 (variable_list
259 (bracket_index_expression
260 field: (identifier) @font-lock-variable-name-face))
261 (variable_list
262 (dot_index_expression
263 field: (identifier) @font-lock-variable-name-face))
245 (for_numeric_clause name: (identifier) @font-lock-variable-name-face)) 264 (for_numeric_clause name: (identifier) @font-lock-variable-name-face))
246 265
247 :language 'lua
248 :feature 'property 266 :feature 'property
249 '((field name: (identifier) @font-lock-property-name-face) 267 '((field name: (identifier) @font-lock-property-name-face)
250 (dot_index_expression 268 (dot_index_expression
251 field: (identifier) @font-lock-property-use-face)) 269 field: (identifier) @font-lock-property-use-face))
252 270
253 :language 'lua
254 :feature 'assignment
255 '((variable_list
256 [(identifier)
257 (bracket_index_expression)]
258 @font-lock-variable-name-face)
259 (variable_list
260 (dot_index_expression
261 table: (identifier))
262 @font-lock-variable-name-face))
263
264 :language 'lua
265 :feature 'error 271 :feature 'error
266 :override t 272 :override t
267 '((ERROR) @font-lock-warning-face)) 273 '((ERROR) @font-lock-warning-face))
@@ -665,13 +671,14 @@ Calls REPORT-FN directly."
665 (setq-local treesit-font-lock-settings lua-ts--font-lock-settings) 671 (setq-local treesit-font-lock-settings lua-ts--font-lock-settings)
666 (setq-local treesit-font-lock-feature-list 672 (setq-local treesit-font-lock-feature-list
667 '((comment definition) 673 '((comment definition)
668 (keyword property string) 674 (keyword string)
669 (assignment builtin constant number) 675 (assignment builtin constant number)
670 (bracket 676 (bracket
671 delimiter 677 delimiter
672 escape 678 escape
673 function 679 function
674 operator 680 operator
681 property
675 punctuation 682 punctuation
676 variable))) 683 variable)))
677 684