Subject: Re: bug#63708: 29.0.60; rust-ts-mode not properly handling apostrophe

From: Йордан Миладинов<[email-masked]>
Date: Fri, 26 May 2023 19:14:37 +0000

Here's a patch that implements Randy's idea.

Sent with Proton Mail secure email.

------- Original Message -------
On Friday, May 26th, 2023 at 5:21 AM, Randy Taylor <[email-masked]> wrote:

> On Thursday, May 25th, 2023 at 09:12, Eli Zaretskii [email-masked] wrote:
> 
> > > Date: Thu, 25 May 2023 12:52:18 +0000
> > 
> > > From: Randy Taylor [email-masked]
> > > Cc: Йордан Миладинов [email-masked], [email-masked], Yuan Fu [email-masked]
> > > 
> > > We probably need to augment rust-ts-mode--syntax-propertize where we apply the string syntax property to any char_literal nodes. It would probably be too late for emacs-29 though.
> > 
> > No, it won't be too late. rust-ts-mode is new in Emacs 29, so we
> > should fix it in Emacs 29. So please try to find a fix for that;
> > bonus points for making the fix as safe as possible for the release
> > branch.
> > 
> > Thanks.
> 
> 
> Sure, I'll take a look tomorrow or sometime during the weekend.

From 6159f516276a04e7e4588982e80692c62c6ea229 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=99=D0=BE=D1=80=D0=B4=D0=B0=D0=BD=20=D0=9C=D0=B8=D0=BB?=
 =?UTF-8?q?=D0=B0=D0=B4=D0=B8=D0=BD=D0=BE=D0=B2?= <[email-masked]>
Date: Fri, 26 May 2023 17:23:26 +0300
Subject: [PATCH] Fix apostrophe handling in rust-ts-mode

lisp/progmodes/rust-ts-mode.el (rust-ts-mode--syntax-propertize)

Treat the apostrophe character as string if used to define character
literals.

Treat LT and GT as pairs if used to define type parameters (formerly
they were treated as pairs only for type arguments).
---
 lisp/progmodes/rust-ts-mode.el | 46 ++++++++++++++++++++++------------
 1 file changed, 30 insertions(+), 16 deletions(-)

diff --git a/lisp/progmodes/rust-ts-mode.el b/lisp/progmodes/rust-ts-mode.el
index be06acde3e3..fb94c7e0dc8 100644
--- a/lisp/progmodes/rust-ts-mode.el
+++ b/lisp/progmodes/rust-ts-mode.el
@@ -350,26 +350,40 @@ rust-ts-mode--defun-name
       (treesit-node-child-by-field-name node "name") t))))
 
 (defun rust-ts-mode--syntax-propertize (beg end)
-  "Apply syntax text property to template delimiters between BEG and END.
+  "Apply proper syntax properties to various special characters with
+double meaning in Rust code between BEG and END.
 
-< and > are usually punctuation, e.g., as greater/less-than.  But
-when used for types, they should be considered pairs.
+Apostrophe (') is commonly treated as punctuation, but when used
+to define character literals, it should be treated as a string.
 
-This function checks for < and > in the changed RANGES and apply
-appropriate text property to alter the syntax of template
-delimiters < and >'s."
+Less-than and greater-than (<>) are usually punctuation, but when
+used for type parameters, they should be considered pairs.
+
+This function scans the changed RANGES for these special
+characters and applies the appropriate text properties to reflect
+their syntactical meaning."
+  (goto-char beg)
+  (while (search-forward "'" end t)
+    (when (string-equal "char_literal"
+                        (treesit-node-type
+                         (treesit-node-at (match-beginning 0))))
+      (put-text-property (match-beginning 0)
+                         (match-end 0)
+                         'syntax-table
+                         '(7))))
   (goto-char beg)
   (while (re-search-forward (rx (or "<" ">")) end t)
-    (pcase (treesit-node-type
-            (treesit-node-parent
-             (treesit-node-at (match-beginning 0))))
-      ("type_arguments"
-       (put-text-property (match-beginning 0)
-                          (match-end 0)
-                          'syntax-table
-                          (pcase (char-before)
-                            (?< '(4 . ?>))
-                            (?> '(5 . ?<))))))))
+    (let ((parent-type (treesit-node-type
+                        (treesit-node-parent
+                         (treesit-node-at (match-beginning 0))))))
+      (when (or (string-equal parent-type "type_arguments")
+                (string-equal parent-type "type_parameters"))
+        (put-text-property (match-beginning 0)
+                           (match-end 0)
+                           'syntax-table
+                           (pcase (char-before)
+                             (?< '(4 . ?>))
+                             (?> '(5 . ?<))))))))
 
 ;;;###autoload
 (define-derived-mode rust-ts-mode prog-mode "Rust"
-- 
2.40.1