unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Kangas <stefankangas@gmail.com>
To: Christophe TROESTLER <Christophe.TROESTLER@umons.ac.be>,
	73877@debbugs.gnu.org
Cc: Randy Taylor <dev@rjt.dev>
Subject: bug#73877: 30; rust-ts-mode: highlight the possible type suffix of number literals
Date: Sun, 20 Oct 2024 05:00:07 -0700	[thread overview]
Message-ID: <CADwFkmmPbmcFZnc88u-3SYRWdyCVPZCPA+YjTmecyH=fe2dR7g@mail.gmail.com> (raw)
In-Reply-To: <874j587459.fsf@poincare.umons.ac.be>

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





      reply	other threads:[~2024-10-20 12:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CADwFkmmPbmcFZnc88u-3SYRWdyCVPZCPA+YjTmecyH=fe2dR7g@mail.gmail.com' \
    --to=stefankangas@gmail.com \
    --cc=73877@debbugs.gnu.org \
    --cc=Christophe.TROESTLER@umons.ac.be \
    --cc=dev@rjt.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).