* 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
* bug#73877: 30; rust-ts-mode: highlight the possible type suffix of number literals 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 0 siblings, 1 reply; 14+ messages in thread From: Stefan Kangas @ 2024-10-20 12:00 UTC (permalink / raw) To: Christophe TROESTLER, 73877; +Cc: Randy Taylor Christophe TROESTLER <Christophe.TROESTLER@umons.ac.be> writes: > 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 Thanks. Randy, do you have any comments on the below patch? > 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 [flat|nested] 14+ messages in thread
* bug#73877: 30; rust-ts-mode: highlight the possible type suffix of number literals 2024-10-20 12:00 ` Stefan Kangas @ 2024-10-22 23:05 ` Randy Taylor 2024-10-24 15:22 ` Christophe TROESTLER 0 siblings, 1 reply; 14+ messages in thread From: Randy Taylor @ 2024-10-22 23:05 UTC (permalink / raw) To: Christophe TROESTLER; +Cc: 73877, Stefan Kangas > Christophe TROESTLER Christophe.TROESTLER@umons.ac.be writes: > > > 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 Thanks for working on this Christophe. I think this will need to be gated behind a variable which should retain the existing behaviour by default. Would you also be able to add some tests for this? You can see how the font-lock tests are done in the test/lisp/progmodes/rust-ts-mode-tests.el file, and add some to the rust-ts-mode-resources/font-lock.rs file. We might need a new file for this one, actually, since we'd want to test both cases. I have some comments below: + "Regexp that matches any suffix on number litterals as documented ^ literals +at https://doc.rust-lang.org/reference/tokens.html#suffixes") Add a period at the end. + "Fontify number literals, highlighting the optional type if present" Add a period at the end. We may also want to say something like: "highlighting the optional type based on the value of `name-of-the-variable'." once the variable to control it is added. ^ permalink raw reply [flat|nested] 14+ messages in thread
* bug#73877: 30; rust-ts-mode: highlight the possible type suffix of number literals 2024-10-22 23:05 ` Randy Taylor @ 2024-10-24 15:22 ` Christophe TROESTLER 2024-10-27 19:07 ` Randy Taylor 0 siblings, 1 reply; 14+ messages in thread From: Christophe TROESTLER @ 2024-10-24 15:22 UTC (permalink / raw) To: Randy Taylor; +Cc: 73877@debbugs.gnu.org, Stefan Kangas [-- Attachment #1: Type: text/plain, Size: 406 bytes --] Hi, Thanks for your comments. Here is an updated patch (against emacs-30, let me know if you would prefer the emacs-29 branch). Best, C. On 23 October 2024 at 01:05 +02, Randy Taylor <dev@rjt.dev> wrote: > […] I think this will need to be gated behind a variable which should > retain the existing behaviour by default. > > Would you also be able to add some tests for this? […] [-- 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: 6559 bytes --] From a1b54f1074c6568a4aaa8c168a2e2639cdd9907c 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 | 35 ++++++++++++++++++- .../font-lock-number.rs | 18 ++++++++++ .../rust-ts-mode-resources/font-lock.rs | 18 ++++++++++ test/lisp/progmodes/rust-ts-mode-tests.el | 13 +++++-- 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs diff --git a/lisp/progmodes/rust-ts-mode.el b/lisp/progmodes/rust-ts-mode.el index 571ffa9b220..2f8bc9c1586 100644 --- a/lisp/progmodes/rust-ts-mode.el +++ b/lisp/progmodes/rust-ts-mode.el @@ -62,6 +62,14 @@ rust-ts-flymake-command (repeat :tag "Custom command" string)) :group 'rust) +(defcustom rust-ts-mode-highlight-number-literal-type nil + "If non-nil, highlight the optional type suffix of number literals." + :version "30.1" + :type 'boolean + :group 'rust) +(put 'rust-ts-mode-highlight-number-literal-type 'safe-local-variable + 'booleanp) + (defvar rust-ts-mode-prettify-symbols-alist '(("&&" . ?∧) ("||" . ?∨) ("<=" . ?≤) (">=" . ?≥) ("!=" . ?≠) @@ -116,6 +124,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 literals 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 +235,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 +380,24 @@ '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 based on the +value of `rust-ts-mode-highlight-number-literal-type'." + (let* ((beg (treesit-node-start node)) + (end (treesit-node-end node))) + (save-excursion + (goto-char end) + (if (and rust-ts-mode-highlight-number-literal-type + (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." diff --git a/test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs b/test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs new file mode 100644 index 00000000000..1c9a38bd683 --- /dev/null +++ b/test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs @@ -0,0 +1,18 @@ + +fn main() { + let x = 1usize; +// ^ font-lock-number-face +// ^ font-lock-type-face + let x = 1_usize; +// ^ font-lock-number-face +// ^ font-lock-type-face + let x = 1_f64; +// ^ font-lock-number-face +// ^ font-lock-type-face + let x = 1.0f64; +// ^ font-lock-number-face +// ^ font-lock-type-face + let x = 1.0_f64; +// ^ font-lock-number-face +// ^ font-lock-type-face +} diff --git a/test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs b/test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs index 377cda0e3b9..1f549085e3f 100644 --- a/test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs +++ b/test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs @@ -23,3 +23,21 @@ macro_rules! unsafe_foo { // ^ font-lock-operator-face } }; + +fn main() { + let x = 1usize; +// ^ font-lock-number-face +// ^ font-lock-number-face + let x = 1_usize; +// ^ font-lock-number-face +// ^ font-lock-number-face + let x = 1_f64; +// ^ font-lock-number-face +// ^ font-lock-number-face + let x = 1.0f64; +// ^ font-lock-number-face +// ^ font-lock-number-face + let x = 1.0_f64; +// ^ font-lock-number-face +// ^ font-lock-number-face +} diff --git a/test/lisp/progmodes/rust-ts-mode-tests.el b/test/lisp/progmodes/rust-ts-mode-tests.el index f718a57fc9e..95ca4a32afd 100644 --- a/test/lisp/progmodes/rust-ts-mode-tests.el +++ b/test/lisp/progmodes/rust-ts-mode-tests.el @@ -26,8 +26,17 @@ (ert-deftest rust-ts-test-font-lock () (skip-unless (treesit-ready-p 'rust)) - (let ((treesit-font-lock-level 4)) - (ert-font-lock-test-file (ert-resource-file "font-lock.rs") 'rust-ts-mode))) + (let ((treesit-font-lock-level 4) + (h rust-ts-mode-highlight-number-literal-type)) + (setq rust-ts-mode-highlight-number-literal-type nil) + (ert-font-lock-test-file (ert-resource-file "font-lock.rs") 'rust-ts-mode) + (setq rust-ts-mode-highlight-number-literal-type h)) + (let ((treesit-font-lock-level 4) + (h rust-ts-mode-highlight-number-literal-type)) + (setq rust-ts-mode-highlight-number-literal-type t) + (ert-font-lock-test-file (ert-resource-file "font-lock-number.rs") + 'rust-ts-mode) + (setq rust-ts-mode-highlight-number-literal-type h))) (provide 'rust-ts-mode-tests) -- 2.45.2 ^ permalink raw reply related [flat|nested] 14+ messages in thread
* bug#73877: 30; rust-ts-mode: highlight the possible type suffix of number literals 2024-10-24 15:22 ` Christophe TROESTLER @ 2024-10-27 19:07 ` Randy Taylor 2024-10-28 21:56 ` Christophe TROESTLER 0 siblings, 1 reply; 14+ messages in thread From: Randy Taylor @ 2024-10-27 19:07 UTC (permalink / raw) To: Christophe TROESTLER; +Cc: 73877@debbugs.gnu.org, Stefan Kangas On Thursday, October 24th, 2024 at 11:22, Christophe TROESTLER <Christophe.TROESTLER@umons.ac.be> wrote: > > > Hi, > > Thanks for your comments. Here is an updated patch (against emacs-30, let me know if you would prefer the emacs-29 branch). > > Best, > C. Thanks Christophe. This should be against the master branch. It will need a commit message (see the CONTRIBUTE file) and a NEWS entry. +(defcustom rust-ts-mode-highlight-number-literal-type nil I'm unsure about this name since it's a little misleading. The type is still highlighted if this is nil, but the same as the number. I don't really have a better suggestion though, and anything else would be longer and probably a little unruly. I was thinking about rust-ts-mode-highlight-number-literal-type-separately but... Maybe we drop the literal part? Does anyone else have any thoughts/suggestions? + :version "30.1" Should be "31.1". For rust-ts-test-font-lock, I'm wondering if we should split that up to a new ert-deftest. If we have more customizations in the future we'd probably end up doing that anyway. Also, I'm not sure we need to bother resetting the rust-ts-mode-highlight-number-literal-type variable. ^ permalink raw reply [flat|nested] 14+ messages in thread
* bug#73877: 30; rust-ts-mode: highlight the possible type suffix of number literals 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 0 siblings, 2 replies; 14+ messages in thread From: Christophe TROESTLER @ 2024-10-28 21:56 UTC (permalink / raw) To: Randy Taylor; +Cc: 73877@debbugs.gnu.org, Stefan Kangas [-- Attachment #1: Type: text/plain, Size: 1398 bytes --] Hi, Here is an updated patch that (hopefully) addresses all of your remarks except the last one: when executing the tests interactively in the instance of Emacs one works with, it is good IMHO that global variables are not changed under our feet. That being said, it is not a strong opinion and I can remove the reset if you wish. Best, C. On 27 October 2024 at 20:07 +01, Randy Taylor <dev@rjt.dev> wrote: > […] This should be against the master branch. > > It will need a commit message (see the CONTRIBUTE file) and a NEWS entry. > > +(defcustom rust-ts-mode-highlight-number-literal-type nil > I'm unsure about this name since it's a little misleading. The type > is still highlighted if this is nil, but the same as the number. > I don't really have a better suggestion though, and anything else > would be longer and probably a little unruly. I was thinking about > rust-ts-mode-highlight-number-literal-type-separately but... > > Maybe we drop the literal part? > > Does anyone else have any thoughts/suggestions? > > + :version "30.1" > Should be "31.1". > > For rust-ts-test-font-lock, I'm wondering if we should split that up to > a new ert-deftest. If we have more customizations in the future we'd > probably end up doing that anyway. > Also, I'm not sure we need to bother resetting the > rust-ts-mode-highlight-number-literal-type variable. [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #2: 0001-Rust-ts-fontify-as-type-the-possible-suffix-of-numbe.patch --] [-- Type: text/x-diff; name="0001-Rust-ts-fontify-as-type-the-possible-suffix-of-numbe.patch", Size: 7730 bytes --] From a2aa35a72a11387db8f0784de1e832220c187966 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: fontify as type the possible suffix of number literals Content-Type: text/plain; charset="utf-8" * lisp/progmodes/rust-ts-mode.el (rust-ts-mode--fontify-number-literal): perform the improved fontification of numbers. * test/lisp/progmodes/rust-ts-mode-tests.el: * test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs: * test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs: Add tests for the new optional fontification of the possible type suffix of numbers. --- etc/NEWS | 7 ++++ lisp/progmodes/rust-ts-mode.el | 36 ++++++++++++++++++- .../font-lock-number.rs | 18 ++++++++++ .../rust-ts-mode-resources/font-lock.rs | 18 ++++++++++ test/lisp/progmodes/rust-ts-mode-tests.el | 16 +++++++-- 5 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs diff --git a/etc/NEWS b/etc/NEWS index d1c7303f976..4a987f11f4b 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -630,6 +630,13 @@ cloning, or prompts for that, too. When the argument is non-nil, the function switches to a buffer visiting the directory into which the repository was cloned. +** Rust-ts mode + +--- +*** New user option 'rust-ts-mode-fontify-number-suffix-as-type'. +Rust number literals may have an optional type suffix. When this option +is non-nil, this suffix is fontified as a type. + \f * New Modes and Packages in Emacs 31.1 diff --git a/lisp/progmodes/rust-ts-mode.el b/lisp/progmodes/rust-ts-mode.el index e52ea3b125a..4314fa9aeed 100644 --- a/lisp/progmodes/rust-ts-mode.el +++ b/lisp/progmodes/rust-ts-mode.el @@ -62,6 +62,15 @@ rust-ts-flymake-command (repeat :tag "Custom command" string)) :group 'rust) +(defcustom rust-ts-mode-fontify-number-suffix-as-type nil + "If non-nil, fontify the optional type suffix of number literals with +`font-lock-type-face' instead of `font-lock-number-face'." + :version "31.1" + :type 'boolean + :group 'rust) +(put 'rust-ts-mode-fontify-number-suffix-as-type 'safe-local-variable + 'booleanp) + (defvar rust-ts-mode-prettify-symbols-alist '(("&&" . ?∧) ("||" . ?∨) ("<=" . ?≤) (">=" . ?≥) ("!=" . ?≠) @@ -116,6 +125,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 literals 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 +236,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 @@ -369,6 +385,24 @@ '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 suffix as a type +based on the value of `rust-ts-mode-highlight-number-literal-type'." + (let* ((beg (treesit-node-start node)) + (end (treesit-node-end node))) + (save-excursion + (goto-char end) + (if (and rust-ts-mode-fontify-number-suffix-as-type + (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." diff --git a/test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs b/test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs new file mode 100644 index 00000000000..1c9a38bd683 --- /dev/null +++ b/test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs @@ -0,0 +1,18 @@ + +fn main() { + let x = 1usize; +// ^ font-lock-number-face +// ^ font-lock-type-face + let x = 1_usize; +// ^ font-lock-number-face +// ^ font-lock-type-face + let x = 1_f64; +// ^ font-lock-number-face +// ^ font-lock-type-face + let x = 1.0f64; +// ^ font-lock-number-face +// ^ font-lock-type-face + let x = 1.0_f64; +// ^ font-lock-number-face +// ^ font-lock-type-face +} diff --git a/test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs b/test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs index 377cda0e3b9..1f549085e3f 100644 --- a/test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs +++ b/test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs @@ -23,3 +23,21 @@ macro_rules! unsafe_foo { // ^ font-lock-operator-face } }; + +fn main() { + let x = 1usize; +// ^ font-lock-number-face +// ^ font-lock-number-face + let x = 1_usize; +// ^ font-lock-number-face +// ^ font-lock-number-face + let x = 1_f64; +// ^ font-lock-number-face +// ^ font-lock-number-face + let x = 1.0f64; +// ^ font-lock-number-face +// ^ font-lock-number-face + let x = 1.0_f64; +// ^ font-lock-number-face +// ^ font-lock-number-face +} diff --git a/test/lisp/progmodes/rust-ts-mode-tests.el b/test/lisp/progmodes/rust-ts-mode-tests.el index f718a57fc9e..fe82ce7ad7e 100644 --- a/test/lisp/progmodes/rust-ts-mode-tests.el +++ b/test/lisp/progmodes/rust-ts-mode-tests.el @@ -26,8 +26,20 @@ (ert-deftest rust-ts-test-font-lock () (skip-unless (treesit-ready-p 'rust)) - (let ((treesit-font-lock-level 4)) - (ert-font-lock-test-file (ert-resource-file "font-lock.rs") 'rust-ts-mode))) + (let ((treesit-font-lock-level 4) + (h rust-ts-mode-fontify-number-suffix-as-type)) + (setq rust-ts-mode-fontify-number-suffix-as-type nil) + (ert-font-lock-test-file (ert-resource-file "font-lock.rs") 'rust-ts-mode) + (setq rust-ts-mode-fontify-number-suffix-as-type h))) + +(ert-deftest rust-ts-test-font-lock-number () + (skip-unless (treesit-ready-p 'rust)) + (let ((treesit-font-lock-level 4) + (h rust-ts-mode-fontify-number-suffix-as-type)) + (setq rust-ts-mode-fontify-number-suffix-as-type t) + (ert-font-lock-test-file (ert-resource-file "font-lock-number.rs") + 'rust-ts-mode) + (setq rust-ts-mode-fontify-number-suffix-as-type h))) (provide 'rust-ts-mode-tests) -- 2.45.2 ^ permalink raw reply related [flat|nested] 14+ messages in thread
* bug#73877: 30; rust-ts-mode: highlight the possible type suffix of number literals 2024-10-28 21:56 ` Christophe TROESTLER @ 2024-11-02 11:01 ` Eli Zaretskii 2024-11-02 19:48 ` Randy Taylor 1 sibling, 0 replies; 14+ messages in thread From: Eli Zaretskii @ 2024-11-02 11:01 UTC (permalink / raw) To: Christophe TROESTLER; +Cc: dev, 73877, stefankangas > Cc: "73877@debbugs.gnu.org" <73877@debbugs.gnu.org>, > Stefan Kangas <stefankangas@gmail.com> > From: Christophe TROESTLER <Christophe.TROESTLER@umons.ac.be> > Date: Mon, 28 Oct 2024 21:56:52 +0000 > > Here is an updated patch that (hopefully) addresses all of your remarks except the last one: when executing the tests interactively in the instance of Emacs one works with, it is good IMHO that global variables are not changed under our feet. That being said, it is not a strong opinion and I can remove the reset if you wish. Thanks, a few more nits to be fixed: > * lisp/progmodes/rust-ts-mode.el (rust-ts-mode--fontify-number-literal): > perform the improved fontification of numbers. The description should start with a capital letter. Also, please mention the bug number somewhere in the log message. > +--- > +*** New user option 'rust-ts-mode-fontify-number-suffix-as-type'. > +Rust number literals may have an optional type suffix. When this option > +is non-nil, this suffix is fontified as a type. ^^^^^^^^^ I think "using the 'font-lock-type-face'" is a more clear description than "as a type". > +(defcustom rust-ts-mode-fontify-number-suffix-as-type nil > + "If non-nil, fontify the optional type suffix of number literals with > +`font-lock-type-face' instead of `font-lock-number-face'." The first line of a doc string should be a single complete sentence. So please make it a summary of what this option does, and describe the details in the subsequent sentences. > +(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 literals as documented > +at https://doc.rust-lang.org/reference/tokens.html#suffixes.") Same here. > +(defun rust-ts-mode--fontify-number-literal (node override start stop &rest _) > + "Fontify number literals, highlighting the optional suffix as a type > +based on the value of `rust-ts-mode-highlight-number-literal-type'." And here. ^ permalink raw reply [flat|nested] 14+ messages in thread
* bug#73877: 30; rust-ts-mode: highlight the possible type suffix of number literals 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 1 sibling, 1 reply; 14+ messages in thread From: Randy Taylor @ 2024-11-02 19:48 UTC (permalink / raw) To: Christophe TROESTLER; +Cc: Eli Zaretskii, 73877@debbugs.gnu.org, Stefan Kangas On Monday, October 28th, 2024 at 17:56, Christophe TROESTLER <Christophe.TROESTLER@umons.ac.be> wrote: > > > Hi, > > Here is an updated patch that (hopefully) addresses all of your remarks except the last one Sorry for the delay. The patch looks good to me with Eli's points addressed (thanks Eli for taking a look) and the below addressed. >...: when executing the tests interactively in the instance of Emacs one works with, it is good IMHO that global variables are not changed under our feet. That being said, it is not a strong opinion and I can remove the reset if you wish. Unless I am missing something, the variables should reset automatically. They are only changed in the context of the test. How are you running them that you are seeing otherwise? > > Best, > C. > ^ permalink raw reply [flat|nested] 14+ messages in thread
* bug#73877: 30; rust-ts-mode: highlight the possible type suffix of number literals 2024-11-02 19:48 ` Randy Taylor @ 2024-11-11 12:32 ` Christophe TROESTLER 2024-11-15 0:11 ` Randy Taylor 0 siblings, 1 reply; 14+ messages in thread From: Christophe TROESTLER @ 2024-11-11 12:32 UTC (permalink / raw) To: Randy Taylor; +Cc: Eli Zaretskii, 73877@debbugs.gnu.org, Stefan Kangas [-- Attachment #1: Type: text/plain, Size: 576 bytes --] Thanks Eli Zaretskii and Randy Taylor for your remarks. An undated patch is attached. On 2 November 2024 at 20:48 +01, Randy Taylor <dev@rjt.dev> wrote: > […] Unless I am missing something, the variables should reset > automatically. They are only changed in the context of the test. > > How are you running them that you are seeing otherwise? I now am using the command line but, if I evaluate the tests (C-xC-e) and then run them with M-x ert, the variable 'rust-ts-mode-fontify-number-suffix-as-type' is not reset to its original value. Best, C. [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #2: 0001-Rust-ts-fontify-as-type-the-possible-suffix-of-numbe.patch --] [-- Type: text/x-diff; name="0001-Rust-ts-fontify-as-type-the-possible-suffix-of-numbe.patch", Size: 7675 bytes --] From be22fcf187ccea26706f832ca768d3f8c15e0bfd 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: fontify as type the possible suffix of number literals Content-Type: text/plain; charset="utf-8" * lisp/progmodes/rust-ts-mode.el (rust-ts-mode--fontify-number-literal): Perform the improved fontification of numbers. (Bug#73877) * test/lisp/progmodes/rust-ts-mode-tests.el: * test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs: * test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs: Add tests for the new optional fontification of the possible type suffix of numbers. --- etc/NEWS | 7 ++++ lisp/progmodes/rust-ts-mode.el | 39 ++++++++++++++++++- .../font-lock-number.rs | 18 +++++++++ .../rust-ts-mode-resources/font-lock.rs | 18 +++++++++ test/lisp/progmodes/rust-ts-mode-tests.el | 11 +++++- 5 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs diff --git a/etc/NEWS b/etc/NEWS index e63132efeda..1b7d2cb95b4 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -683,6 +683,13 @@ cloning, or prompts for that, too. When the argument is non-nil, the function switches to a buffer visiting the directory into which the repository was cloned. +** Rust-ts mode + +--- +*** New user option 'rust-ts-mode-fontify-number-suffix-as-type'. +Rust number literals may have an optional type suffix. When this option +is non-nil, this suffix is fontified using the 'font-lock-type-face'. + \f * New Modes and Packages in Emacs 31.1 diff --git a/lisp/progmodes/rust-ts-mode.el b/lisp/progmodes/rust-ts-mode.el index e52ea3b125a..7e0ed65974b 100644 --- a/lisp/progmodes/rust-ts-mode.el +++ b/lisp/progmodes/rust-ts-mode.el @@ -62,6 +62,17 @@ rust-ts-flymake-command (repeat :tag "Custom command" string)) :group 'rust) +(defcustom rust-ts-mode-fontify-number-suffix-as-type nil + "If non-nil, suffixes of number literals are fontified as types. +In Rust, number literals can possess an optional type suffix. When this +variable is non-nil, these suffixes are fontified using +`font-lock-type-face' instead of `font-lock-number-face'." + :version "31.1" + :type 'boolean + :group 'rust) +(put 'rust-ts-mode-fontify-number-suffix-as-type 'safe-local-variable + 'booleanp) + (defvar rust-ts-mode-prettify-symbols-alist '(("&&" . ?∧) ("||" . ?∨) ("<=" . ?≤) (">=" . ?≥) ("!=" . ?≠) @@ -116,6 +127,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 matching type suffixes of number literals. +See 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 +238,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 @@ -369,6 +387,25 @@ '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 suffix. +If `rust-ts-mode-fontify-number-suffix-as-type' is non-nil, use +`font-lock-type-face' to highlight the suffix." + (let* ((beg (treesit-node-start node)) + (end (treesit-node-end node))) + (save-excursion + (goto-char end) + (if (and rust-ts-mode-fontify-number-suffix-as-type + (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." diff --git a/test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs b/test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs new file mode 100644 index 00000000000..1c9a38bd683 --- /dev/null +++ b/test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs @@ -0,0 +1,18 @@ + +fn main() { + let x = 1usize; +// ^ font-lock-number-face +// ^ font-lock-type-face + let x = 1_usize; +// ^ font-lock-number-face +// ^ font-lock-type-face + let x = 1_f64; +// ^ font-lock-number-face +// ^ font-lock-type-face + let x = 1.0f64; +// ^ font-lock-number-face +// ^ font-lock-type-face + let x = 1.0_f64; +// ^ font-lock-number-face +// ^ font-lock-type-face +} diff --git a/test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs b/test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs index 377cda0e3b9..1f549085e3f 100644 --- a/test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs +++ b/test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs @@ -23,3 +23,21 @@ macro_rules! unsafe_foo { // ^ font-lock-operator-face } }; + +fn main() { + let x = 1usize; +// ^ font-lock-number-face +// ^ font-lock-number-face + let x = 1_usize; +// ^ font-lock-number-face +// ^ font-lock-number-face + let x = 1_f64; +// ^ font-lock-number-face +// ^ font-lock-number-face + let x = 1.0f64; +// ^ font-lock-number-face +// ^ font-lock-number-face + let x = 1.0_f64; +// ^ font-lock-number-face +// ^ font-lock-number-face +} diff --git a/test/lisp/progmodes/rust-ts-mode-tests.el b/test/lisp/progmodes/rust-ts-mode-tests.el index f718a57fc9e..6fad11a4ca4 100644 --- a/test/lisp/progmodes/rust-ts-mode-tests.el +++ b/test/lisp/progmodes/rust-ts-mode-tests.el @@ -27,7 +27,16 @@ (ert-deftest rust-ts-test-font-lock () (skip-unless (treesit-ready-p 'rust)) (let ((treesit-font-lock-level 4)) - (ert-font-lock-test-file (ert-resource-file "font-lock.rs") 'rust-ts-mode))) + (setq rust-ts-mode-fontify-number-suffix-as-type nil) + (ert-font-lock-test-file (ert-resource-file "font-lock.rs") + 'rust-ts-mode))) + +(ert-deftest rust-ts-test-font-lock-number () + (skip-unless (treesit-ready-p 'rust)) + (let ((treesit-font-lock-level 4)) + (setq rust-ts-mode-fontify-number-suffix-as-type t) + (ert-font-lock-test-file (ert-resource-file "font-lock-number.rs") + 'rust-ts-mode))) (provide 'rust-ts-mode-tests) -- 2.45.2 ^ permalink raw reply related [flat|nested] 14+ messages in thread
* bug#73877: 30; rust-ts-mode: highlight the possible type suffix of number literals 2024-11-11 12:32 ` Christophe TROESTLER @ 2024-11-15 0:11 ` Randy Taylor 2024-11-18 15:14 ` Christophe TROESTLER 0 siblings, 1 reply; 14+ messages in thread From: Randy Taylor @ 2024-11-15 0:11 UTC (permalink / raw) To: Christophe TROESTLER; +Cc: Eli Zaretskii, 73877@debbugs.gnu.org, Stefan Kangas On Monday, November 11th, 2024 at 07:32, Christophe TROESTLER <Christophe.TROESTLER@umons.ac.be> wrote: > > > > Thanks Eli Zaretskii and Randy Taylor for your remarks. An undated patch is attached. > > On 2 November 2024 at 20:48 +01, Randy Taylor dev@rjt.dev wrote: > > > […] Unless I am missing something, the variables should reset > > automatically. They are only changed in the context of the test. > > > > How are you running them that you are seeing otherwise? > > > I now am using the command line but, if I evaluate the tests (C-xC-e) and then run them with M-x ert, the variable 'rust-ts-mode-fontify-number-suffix-as-type' is not reset to its original value. > > Best, > C. Ah, sorry. I had only tried with treesit-font-lock-level myself since the patch didn't apply cleanly for me and I didn't have a chance to manually apply it, and I glossed over how the variables are set differently. If you do the same as treesit-font-lock-level, i.e., place it in the let instead of setq, then it will reset properly. You will also need to: (require 'rust-ts-mode) After you make that change, I think you should be able to get rid of this: +(put 'rust-ts-mode-fontify-number-suffix-as-type 'safe-local-variable + 'booleanp) In the font-lock-number.rs file, it looks like there is an extraneous newline at the top of the file. BTW the current patch still doesn't apply against the latest master for me. ^ permalink raw reply [flat|nested] 14+ messages in thread
* bug#73877: 30; rust-ts-mode: highlight the possible type suffix of number literals 2024-11-15 0:11 ` Randy Taylor @ 2024-11-18 15:14 ` Christophe TROESTLER 2024-11-22 0:57 ` Randy Taylor 0 siblings, 1 reply; 14+ messages in thread From: Christophe TROESTLER @ 2024-11-18 15:14 UTC (permalink / raw) To: Randy Taylor; +Cc: Eli Zaretskii, 73877@debbugs.gnu.org, Stefan Kangas [-- Attachment #1: Type: text/plain, Size: 1139 bytes --] On 15 November 2024 at 01:11 +01, Randy Taylor <dev@rjt.dev> wrote: > […] the patch didn't apply cleanly for me […] Here is an updated version of the patch against today's master branch. > […] If you do the same as treesit-font-lock-level, […] For some reason it did not work in the past but it does now, so that's how I proceeded. > You will also need to: (require 'rust-ts-mode) Ok — some tests do that, others don't, so I did not originally change that. I've now added the “require”. > After you make that change, I think you should be able to get rid of > this: > +(put 'rust-ts-mode-fontify-number-suffix-as-type 'safe-local-variable > + 'booleanp) I first thought that one may want to set that change for specific file but, you are right, it is more of a user choice that should be set in your own config. files. > In the font-lock-number.rs file, it looks like there is an extraneous > newline at the top of the file. Fixed. > BTW the current patch still doesn't apply against the latest master > for me. I have rebased it on the current origin/master. Best, C. [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #2: 0001-Rust-ts-fontify-as-type-the-possible-suffix-of-numbe.patch --] [-- Type: text/x-diff; name="0001-Rust-ts-fontify-as-type-the-possible-suffix-of-numbe.patch", Size: 7717 bytes --] From 616a0fc2a563b0c5b4fd74361540004560364305 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: fontify as type the possible suffix of number literals Content-Type: text/plain; charset="utf-8" * lisp/progmodes/rust-ts-mode.el (rust-ts-mode--fontify-number-literal): Perform the improved fontification of numbers. (Bug#73877) * test/lisp/progmodes/rust-ts-mode-tests.el: * test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs: * test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs: Add tests for the new optional fontification of the possible type suffix of numbers. --- etc/NEWS | 7 ++++ lisp/progmodes/rust-ts-mode.el | 37 ++++++++++++++++++- .../font-lock-number.rs | 17 +++++++++ .../rust-ts-mode-resources/font-lock.rs | 18 +++++++++ test/lisp/progmodes/rust-ts-mode-tests.el | 14 ++++++- 5 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs diff --git a/etc/NEWS b/etc/NEWS index b6c21018779..1696c17892d 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -719,6 +719,13 @@ cloning, or prompts for that, too. When the argument is non-nil, the function switches to a buffer visiting the directory into which the repository was cloned. +** Rust-ts mode + +--- +*** New user option 'rust-ts-mode-fontify-number-suffix-as-type'. +Rust number literals may have an optional type suffix. When this option +is non-nil, this suffix is fontified using the 'font-lock-type-face'. + \f * New Modes and Packages in Emacs 31.1 diff --git a/lisp/progmodes/rust-ts-mode.el b/lisp/progmodes/rust-ts-mode.el index e52ea3b125a..7a421eb506b 100644 --- a/lisp/progmodes/rust-ts-mode.el +++ b/lisp/progmodes/rust-ts-mode.el @@ -62,6 +62,15 @@ rust-ts-flymake-command (repeat :tag "Custom command" string)) :group 'rust) +(defcustom rust-ts-mode-fontify-number-suffix-as-type nil + "If non-nil, suffixes of number literals are fontified as types. +In Rust, number literals can possess an optional type suffix. When this +variable is non-nil, these suffixes are fontified using +`font-lock-type-face' instead of `font-lock-number-face'." + :version "31.1" + :type 'boolean + :group 'rust) + (defvar rust-ts-mode-prettify-symbols-alist '(("&&" . ?∧) ("||" . ?∨) ("<=" . ?≤) (">=" . ?≥) ("!=" . ?≠) @@ -116,6 +125,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 matching type suffixes of number literals. +See 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 +236,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 @@ -369,6 +385,25 @@ '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 suffix. +If `rust-ts-mode-fontify-number-suffix-as-type' is non-nil, use +`font-lock-type-face' to highlight the suffix." + (let* ((beg (treesit-node-start node)) + (end (treesit-node-end node))) + (save-excursion + (goto-char end) + (if (and rust-ts-mode-fontify-number-suffix-as-type + (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." diff --git a/test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs b/test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs new file mode 100644 index 00000000000..ed5b222d5ff --- /dev/null +++ b/test/lisp/progmodes/rust-ts-mode-resources/font-lock-number.rs @@ -0,0 +1,17 @@ +fn main() { + let x = 1usize; +// ^ font-lock-number-face +// ^ font-lock-type-face + let x = 1_usize; +// ^ font-lock-number-face +// ^ font-lock-type-face + let x = 1_f64; +// ^ font-lock-number-face +// ^ font-lock-type-face + let x = 1.0f64; +// ^ font-lock-number-face +// ^ font-lock-type-face + let x = 1.0_f64; +// ^ font-lock-number-face +// ^ font-lock-type-face +} diff --git a/test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs b/test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs index 377cda0e3b9..1f549085e3f 100644 --- a/test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs +++ b/test/lisp/progmodes/rust-ts-mode-resources/font-lock.rs @@ -23,3 +23,21 @@ macro_rules! unsafe_foo { // ^ font-lock-operator-face } }; + +fn main() { + let x = 1usize; +// ^ font-lock-number-face +// ^ font-lock-number-face + let x = 1_usize; +// ^ font-lock-number-face +// ^ font-lock-number-face + let x = 1_f64; +// ^ font-lock-number-face +// ^ font-lock-number-face + let x = 1.0f64; +// ^ font-lock-number-face +// ^ font-lock-number-face + let x = 1.0_f64; +// ^ font-lock-number-face +// ^ font-lock-number-face +} diff --git a/test/lisp/progmodes/rust-ts-mode-tests.el b/test/lisp/progmodes/rust-ts-mode-tests.el index f718a57fc9e..3a710b3af3b 100644 --- a/test/lisp/progmodes/rust-ts-mode-tests.el +++ b/test/lisp/progmodes/rust-ts-mode-tests.el @@ -23,11 +23,21 @@ (require 'ert-font-lock) (require 'ert-x) (require 'treesit) +(require 'rust-ts-mode) (ert-deftest rust-ts-test-font-lock () (skip-unless (treesit-ready-p 'rust)) - (let ((treesit-font-lock-level 4)) - (ert-font-lock-test-file (ert-resource-file "font-lock.rs") 'rust-ts-mode))) + (let ((treesit-font-lock-level 4) + (rust-ts-mode-fontify-number-suffix-as-type nil)) + (ert-font-lock-test-file (ert-resource-file "font-lock.rs") + 'rust-ts-mode))) + +(ert-deftest rust-ts-test-font-lock-number () + (skip-unless (treesit-ready-p 'rust)) + (let ((treesit-font-lock-level 4) + (rust-ts-mode-fontify-number-suffix-as-type t)) + (ert-font-lock-test-file (ert-resource-file "font-lock-number.rs") + 'rust-ts-mode))) (provide 'rust-ts-mode-tests) -- 2.45.2 ^ permalink raw reply related [flat|nested] 14+ messages in thread
* bug#73877: 30; rust-ts-mode: highlight the possible type suffix of number literals 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 0 siblings, 2 replies; 14+ messages in thread From: Randy Taylor @ 2024-11-22 0:57 UTC (permalink / raw) To: Christophe TROESTLER; +Cc: Eli Zaretskii, 73877@debbugs.gnu.org, Stefan Kangas On Monday, November 18th, 2024 at 10:14, Christophe TROESTLER <Christophe.TROESTLER@umons.ac.be> wrote: > > On 15 November 2024 at 01:11 +01, Randy Taylor dev@rjt.dev wrote: > > > […] the patch didn't apply cleanly for me […] > > > Here is an updated version of the patch against today's master branch. For some reason it still doesn't apply for me - not sure why. Maybe it's just me. I always do "git format-patch -1" to generate my patches. > > > […] If you do the same as treesit-font-lock-level, […] > > > For some reason it did not work in the past but it does now, so that's how I proceeded. > > > You will also need to: (require 'rust-ts-mode) > > > Ok — some tests do that, others don't, so I did not originally change that. I've now added the “require”. > > > After you make that change, I think you should be able to get rid of > > this: > > +(put 'rust-ts-mode-fontify-number-suffix-as-type 'safe-local-variable > > + 'booleanp) > > > I first thought that one may want to set that change for specific file but, you are right, it is more of a user choice that should be set in your own config. files. > > > In the font-lock-number.rs file, it looks like there is an extraneous > > newline at the top of the file. > > > Fixed. > > > BTW the current patch still doesn't apply against the latest master > > for me. > > > I have rebased it on the current origin/master. > > Best, > C. Thanks for working on this and putting up with me - the patch looks good to me. Eli, please install if you have no further comments. ^ permalink raw reply [flat|nested] 14+ messages in thread
* bug#73877: 30; rust-ts-mode: highlight the possible type suffix of number literals 2024-11-22 0:57 ` Randy Taylor @ 2024-11-22 7:23 ` Christophe TROESTLER 2024-11-23 12:58 ` Eli Zaretskii 1 sibling, 0 replies; 14+ messages in thread From: Christophe TROESTLER @ 2024-11-22 7:23 UTC (permalink / raw) To: Randy Taylor; +Cc: Eli Zaretskii, 73877@debbugs.gnu.org, Stefan Kangas On 22 November 2024 at 01:57 +01, Randy Taylor <dev@rjt.dev> wrote: > On Monday, November 18th, 2024 at 10:14, Christophe TROESTLER > <Christophe.TROESTLER@umons.ac.be> wrote: >> >> On 15 November 2024 at 01:11 +01, Randy Taylor dev@rjt.dev wrote: >> >> > […] the patch didn't apply cleanly for me […] >> >> >> Here is an updated version of the patch against today's master branch. > > For some reason it still doesn't apply for me - not sure why. Maybe > it's just me. > I always do "git format-patch -1" to generate my patches. I used magit to do it. “git format-patch -1” generated an identical one. Best, C. ^ permalink raw reply [flat|nested] 14+ messages in thread
* bug#73877: 30; rust-ts-mode: highlight the possible type suffix of number literals 2024-11-22 0:57 ` Randy Taylor 2024-11-22 7:23 ` Christophe TROESTLER @ 2024-11-23 12:58 ` Eli Zaretskii 1 sibling, 0 replies; 14+ messages in thread From: Eli Zaretskii @ 2024-11-23 12:58 UTC (permalink / raw) To: Randy Taylor; +Cc: 73877-done, Christophe.TROESTLER, stefankangas > Date: Fri, 22 Nov 2024 00:57:32 +0000 > From: Randy Taylor <dev@rjt.dev> > Cc: "73877@debbugs.gnu.org" <73877@debbugs.gnu.org>, Stefan Kangas <stefankangas@gmail.com>, Eli Zaretskii <eliz@gnu.org> > > On Monday, November 18th, 2024 at 10:14, Christophe TROESTLER <Christophe.TROESTLER@umons.ac.be> wrote: > > > > On 15 November 2024 at 01:11 +01, Randy Taylor dev@rjt.dev wrote: > > > > > […] the patch didn't apply cleanly for me […] > > > > > > Here is an updated version of the patch against today's master branch. > > For some reason it still doesn't apply for me - not sure why. Maybe it's just me. > I always do "git format-patch -1" to generate my patches. > > > > > > […] If you do the same as treesit-font-lock-level, […] > > > > > > For some reason it did not work in the past but it does now, so that's how I proceeded. > > > > > You will also need to: (require 'rust-ts-mode) > > > > > > Ok — some tests do that, others don't, so I did not originally change that. I've now added the “require”. > > > > > After you make that change, I think you should be able to get rid of > > > this: > > > +(put 'rust-ts-mode-fontify-number-suffix-as-type 'safe-local-variable > > > + 'booleanp) > > > > > > I first thought that one may want to set that change for specific file but, you are right, it is more of a user choice that should be set in your own config. files. > > > > > In the font-lock-number.rs file, it looks like there is an extraneous > > > newline at the top of the file. > > > > > > Fixed. > > > > > BTW the current patch still doesn't apply against the latest master > > > for me. > > > > > > I have rebased it on the current origin/master. > > > > Best, > > C. > > Thanks for working on this and putting up with me - the patch looks > good to me. Eli, please install if you have no further comments. Thanks, installed on master, and closing the bug. ^ permalink raw reply [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).