unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#73877: 30; rust-ts-mode: highlight the possible type suffix of number literals
@ 2024-10-19 10:56 Christophe TROESTLER
  2024-10-20 12:00 ` Stefan Kangas
  0 siblings, 1 reply; 14+ messages in thread
From: Christophe TROESTLER @ 2024-10-19 10:56 UTC (permalink / raw)
  To: 73877

[-- Attachment #1: Type: text/plain, Size: 199 bytes --]


Hi,

In order to make Rust number literals such as 1usize, 1.0_f64,... more legible, the type suffix should be highlighted.  The attached patch to rust-ts-mode does that.

Best,
Christophe


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-rust-ts-mode-highlight-the-possible-type-suffix-of-n.patch --]
[-- Type: text/x-diff; name="0001-rust-ts-mode-highlight-the-possible-type-suffix-of-n.patch", Size: 2706 bytes --]

From e1cf2f8c59f2abd75688721d08653327c5427da9 Mon Sep 17 00:00:00 2001
From: Christophe Troestler <Christophe.Troestler@umons.ac.be>
Date: Fri, 18 Oct 2024 23:50:06 +0200
Subject: [PATCH] rust-ts-mode: highlight the possible type suffix of number
 literals
Content-Type: text/plain; charset="utf-8"

---
 lisp/progmodes/rust-ts-mode.el | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/rust-ts-mode.el b/lisp/progmodes/rust-ts-mode.el
index 571ffa9b220..0454c46261f 100644
--- a/lisp/progmodes/rust-ts-mode.el
+++ b/lisp/progmodes/rust-ts-mode.el
@@ -116,6 +116,12 @@ rust-ts-mode--indent-rules
      ((parent-is "use_list") parent-bol rust-ts-mode-indent-offset)))
   "Tree-sitter indent rules for `rust-ts-mode'.")
 
+(defconst rust-ts-mode--number-types
+  (regexp-opt '("u8" "i8" "u16" "i16" "u32" "i32" "u64"
+                "i64" "u128" "i128" "usize" "isize" "f32" "f64"))
+  "Regexp that matches any suffix on number litterals as documented
+at https://doc.rust-lang.org/reference/tokens.html#suffixes")
+
 (defvar rust-ts-mode--builtin-macros
   '("concat_bytes" "concat_idents" "const_format_args"
     "format_args_nl" "log_syntax" "trace_macros" "assert" "assert_eq"
@@ -221,7 +227,8 @@ rust-ts-mode--font-lock-settings
 
    :language 'rust
    :feature 'number
-   '([(float_literal) (integer_literal)] @font-lock-number-face)
+   '([(float_literal) (integer_literal)]
+     @rust-ts-mode--fontify-number-literal)
 
    :language 'rust
    :feature 'operator
@@ -365,6 +372,22 @@ 'rust-ts-mode--fontify-pattern
              (treesit-node-start id) (treesit-node-end id)
              'font-lock-variable-name-face override start end)))))))
 
+(defun rust-ts-mode--fontify-number-literal (node override start stop &rest _)
+  "Fontify number literals, highlighting the optional type if present"
+  (let* ((beg (treesit-node-start node))
+         (end (treesit-node-end node)))
+    (save-excursion
+      (goto-char end)
+      (if (looking-back rust-ts-mode--number-types beg)
+          (let* ((ty (match-beginning 0))
+                 (nb (if (eq (char-before ty) ?_) (1- ty) ty)))
+            (treesit-fontify-with-override
+             ty end 'font-lock-type-face override start stop)
+            (treesit-fontify-with-override
+             beg nb 'font-lock-number-face override start stop))
+          (treesit-fontify-with-override
+           beg end 'font-lock-number-face override start stop)))))
+
 (defun rust-ts-mode--defun-name (node)
   "Return the defun name of NODE.
 Return nil if there is no name or if NODE is not a defun node."
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2024-11-23 12:58 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-19 10:56 bug#73877: 30; rust-ts-mode: highlight the possible type suffix of number literals Christophe TROESTLER
2024-10-20 12:00 ` Stefan Kangas
2024-10-22 23:05   ` Randy Taylor
2024-10-24 15:22     ` Christophe TROESTLER
2024-10-27 19:07       ` Randy Taylor
2024-10-28 21:56         ` Christophe TROESTLER
2024-11-02 11:01           ` Eli Zaretskii
2024-11-02 19:48           ` Randy Taylor
2024-11-11 12:32             ` Christophe TROESTLER
2024-11-15  0:11               ` Randy Taylor
2024-11-18 15:14                 ` Christophe TROESTLER
2024-11-22  0:57                   ` Randy Taylor
2024-11-22  7:23                     ` Christophe TROESTLER
2024-11-23 12:58                     ` Eli Zaretskii

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).