aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteven Allen2025-08-19 11:12:01 -0700
committerJuri Linkov2025-08-21 19:59:04 +0300
commitf0b987c32c358703d85d3be010bb2fe0299192be (patch)
tree59a5561eb909be9f9626ca0929fdb425a7c3c9a1
parent1a549762ed9f7cb09a1269503566837f91794ed6 (diff)
downloademacs-f0b987c32c358703d85d3be010bb2fe0299192be.tar.gz
emacs-f0b987c32c358703d85d3be010bb2fe0299192be.zip
rust-ts-mode: handle invalid rust syntax without signaling
Don't signal an error when encountering invalid rust syntax. Without this patch, invalid rust code would prevent a chunk of the buffer from being highlighted (bug#79272). * lisp/progmodes/rust-ts-mode.el (rust-ts-mode--fontify-scope): (rust-ts-mode--fontify-pattern): Avoid calling `string-match-p' on nil when a node is missing a parent. * test/lisp/progmodes/rust-ts-mode-resources/font-lock-no-parent.rs: Rust file that reproduces the issue. * test/lisp/progmodes/rust-ts-mode-tests.el: Test case to reproduce the issue.
-rw-r--r--lisp/progmodes/rust-ts-mode.el9
-rw-r--r--test/lisp/progmodes/rust-ts-mode-resources/font-lock-no-parent.rs7
-rw-r--r--test/lisp/progmodes/rust-ts-mode-tests.el7
3 files changed, 19 insertions, 4 deletions
diff --git a/lisp/progmodes/rust-ts-mode.el b/lisp/progmodes/rust-ts-mode.el
index a5c217c0a4b..a98d621af65 100644
--- a/lisp/progmodes/rust-ts-mode.el
+++ b/lisp/progmodes/rust-ts-mode.el
@@ -366,7 +366,8 @@ See https://doc.rust-lang.org/reference/tokens.html#suffixes.")
366 tail-p 366 tail-p
367 (string-match-p 367 (string-match-p
368 "\\`\\(?:use_list\\|call_expression\\|use_as_clause\\|use_declaration\\)\\'" 368 "\\`\\(?:use_list\\|call_expression\\|use_as_clause\\|use_declaration\\)\\'"
369 (treesit-node-type (treesit-node-parent (treesit-node-parent node))))) 369 (or (treesit-node-type (treesit-node-parent (treesit-node-parent node)))
370 "no_parent")))
370 nil) 371 nil)
371 (t 'font-lock-constant-face)))) 372 (t 'font-lock-constant-face))))
372 (when face 373 (when face
@@ -387,9 +388,9 @@ See https://doc.rust-lang.org/reference/tokens.html#suffixes.")
387 ,(treesit-query-compile 'rust '((identifier) @id 388 ,(treesit-query-compile 'rust '((identifier) @id
388 (shorthand_field_identifier) @id))))) 389 (shorthand_field_identifier) @id)))))
389 (pcase-dolist (`(_name . ,id) captures) 390 (pcase-dolist (`(_name . ,id) captures)
390 (unless (string-match-p "\\`scoped_\\(?:type_\\)?identifier\\'" 391 (unless (string-match-p
391 (treesit-node-type 392 "\\`scoped_\\(?:type_\\)?identifier\\'"
392 (treesit-node-parent id))) 393 (or (treesit-node-type (treesit-node-parent id)) "no_parent"))
393 (treesit-fontify-with-override 394 (treesit-fontify-with-override
394 (treesit-node-start id) (treesit-node-end id) 395 (treesit-node-start id) (treesit-node-end id)
395 'font-lock-variable-name-face override start end))))))) 396 'font-lock-variable-name-face override start end)))))))
diff --git a/test/lisp/progmodes/rust-ts-mode-resources/font-lock-no-parent.rs b/test/lisp/progmodes/rust-ts-mode-resources/font-lock-no-parent.rs
new file mode 100644
index 00000000000..85d0ccc9bf3
--- /dev/null
+++ b/test/lisp/progmodes/rust-ts-mode-resources/font-lock-no-parent.rs
@@ -0,0 +1,7 @@
1+// intentionally invalid syntax
2+const THING: [u8; 48] = [];
3
4// should recover here and highlight the text below
5trait Foo() {
6// ^ font-lock-keyword-face
7}
diff --git a/test/lisp/progmodes/rust-ts-mode-tests.el b/test/lisp/progmodes/rust-ts-mode-tests.el
index d2e28dcfbd7..32d64260a87 100644
--- a/test/lisp/progmodes/rust-ts-mode-tests.el
+++ b/test/lisp/progmodes/rust-ts-mode-tests.el
@@ -39,6 +39,13 @@
39 (ert-font-lock-test-file (ert-resource-file "font-lock-number.rs") 39 (ert-font-lock-test-file (ert-resource-file "font-lock-number.rs")
40 'rust-ts-mode))) 40 'rust-ts-mode)))
41 41
42(ert-deftest rust-ts-test-no-parent ()
43 (skip-unless (treesit-ready-p 'rust))
44 (let ((treesit-font-lock-level 4)
45 (rust-ts-mode-fontify-number-suffix-as-type t))
46 (ert-font-lock-test-file (ert-resource-file "font-lock-no-parent.rs")
47 'rust-ts-mode)))
48
42(provide 'rust-ts-mode-tests) 49(provide 'rust-ts-mode-tests)
43 50
44;;; rust-ts-mode-tests.el ends here 51;;; rust-ts-mode-tests.el ends here