Subject: bug#61302: 29.0.60; rust-ts-mode does not show function-invocation on field-properties

From: Randy Taylor <[email-masked]>
Date: Wed, 15 Feb 2023 02:07:19 +0000

On Tuesday, February 14th, 2023 at 17:14, Dmitry Gutov <[email-masked]> wrote:
> On 14/02/2023 14:39, Randy Taylor wrote:
> 
> > On Tuesday, February 14th, 2023 at 06:42, Jostein Kjønigsen
> > [email-masked] wrote:
> > 
> > > Haven given the latest patch a try on a less macro-ridden codebase I have to
> > > say it looks a lot better. In general I would say for this other
> > > code-base it looks favourable to rust-mode.
> > > 
> > > If there's one thing which still seems to be done better in rust-mode
> > > (not nitpicking the variable highlighting), it would be fully namespaces
> > > function calls within classes.
> > > 
> > > Consider the following code:
> > > 
> > > extern crate claxon;
> > > let result = claxon::FlacReader::open(path);
> > > 
> > > While rust-ts-mode correctly identifies that open() is a function-call,
> > > rust-mode correctly identified FlacReader as a class.
> > 
> > Thanks for testing. I've attached a patch fixing this.
> > 
> > I had deleted a query I thought was now covered :). I look forward to
> > the day we have highlight tests!
> 
> 
> Thank you, looks almost perfect (see below), I've pushed that to emacs-29.

Thanks!

> 
> Highlighting tests are a pain to write, but the initiative is always
> welcome. ;-) In the meantime, at least tree-sitter validates the
> queries, which lowers the odds of typos in font-lock rules.
> 
> Speaking of a problem, the solution with applying the explicit 'default'
> face doesn't seem ideal. Aside from the redundancy in the resulting
> buffer structure (with very little practical downside), it also makes
> the 'vc-diff' buffers look like this on the attached screenshots (the
> 'default' face's background overrides the greens and reds).
> 
> I couldn't find a quick solution to this problem, so I pushed the
> existing code for now.

The following patch fixes it for me, but it's a hack since the face doesn't exist (and I don't think the override option matters for the current patch either, I only realized these things as I was about the send the email).

I tried using 'default with all the different override options and nothing worked. Yuan, any ideas?

Basically what we have here are general scoped_identifier queries that we want to apply only to the actual code (i.e. not imports). However, these scoped_identifiers also exist in the imports and the same highlighting semantics don't apply there, so we have specific queries for the different kinds of imports that specify how they want their scoped_identifier stuff highlighted. For some of those specific queries, we want to leave the face as it and make sure nothing else gets applied to it (i.e. default is what we want). Apparently that causes issues with vc-diff buffers, and maybe other things we have yet to discover.

From bd405ab9dc1b02d76f56a94b984b7bea9e597bc2 Mon Sep 17 00:00:00 2001
From: Randy Taylor <[email-masked]>
Date: Tue, 14 Feb 2023 20:40:05 -0500
Subject: [PATCH] Fix rust-ts-mode highlights overriding vc-diff buffers
 (Bug#61302)

* lisp/progmodes/rust-ts-mode.el (rust-ts-mode--fontify-default): New
function.
(rust-ts-mode--font-lock-settings): Use it.
---
 lisp/progmodes/rust-ts-mode.el | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/lisp/progmodes/rust-ts-mode.el b/lisp/progmodes/rust-ts-mode.el
index f510b7bb6c9..0b2619c51ad 100644
--- a/lisp/progmodes/rust-ts-mode.el
+++ b/lisp/progmodes/rust-ts-mode.el
@@ -217,7 +217,8 @@ rust-ts-mode--font-lock-settings
                      (scoped_identifier path: (_)
                                         name: (identifier) @font-lock-type-face))
       (:match "^[A-Z]" @font-lock-type-face))
-     (use_as_clause path: (scoped_identifier name: (identifier) @default))
+     (use_as_clause
+      path: (scoped_identifier name: (identifier) @rust-ts-mode--fontify-default))
 
      ((use_declaration
        argument: (scoped_identifier
@@ -226,14 +227,14 @@ rust-ts-mode--font-lock-settings
       (:match "^[A-Z]" @font-lock-type-face))
      (use_declaration
       argument: (scoped_identifier
-                 name: (identifier) @default))
+                 name: (identifier) @rust-ts-mode--fontify-default))
 
      (use_declaration
       argument: (scoped_identifier
                  path: (scoped_identifier
                         path: (_) @font-lock-constant-face
                         name: (identifier) @font-lock-constant-face)
-                 name: (identifier) @default))
+                 name: (identifier) @rust-ts-mode--fontify-default))
 
      (use_declaration
       argument: (scoped_use_list
@@ -243,12 +244,12 @@ rust-ts-mode--font-lock-settings
 
      ((use_list (identifier) @font-lock-type-face)
       (:match "^[A-Z]" @font-lock-type-face))
-     (use_list (identifier) @default)
+     (use_list (identifier) @rust-ts-mode--fontify-default)
      ((use_list (scoped_identifier path: (_)
                                    name: (identifier) @font-lock-type-face))
       (:match "^[A-Z]" @font-lock-type-face))
      (use_list (scoped_identifier path: (_)
-                                  name: (identifier) @default))
+                                  name: (identifier) @rust-ts-mode--fontify-default))
      (use_wildcard (scoped_identifier
                     name: (identifier) @font-lock-constant-face))
 
@@ -286,7 +287,9 @@ rust-ts-mode--font-lock-settings
              name: (identifier) @font-lock-constant-face))
      (type_identifier) @font-lock-type-face
      ;; Ensure function calls aren't highlighted as types.
-     (call_expression function: (scoped_identifier name: (identifier) @default)))
+     (call_expression function:
+                      (scoped_identifier
+                       name: (identifier) @rust-ts-mode--fontify-default)))
 
    :language 'rust
    :feature 'property
@@ -304,7 +307,7 @@ rust-ts-mode--font-lock-settings
    :feature 'variable
    '((identifier) @font-lock-variable-name-face
      ;; Everything in a token_tree is an identifier.
-     (token_tree (identifier) @default))
+     (token_tree (identifier) @rust-ts-mode--fontify-default))
 
    :language 'rust
    :feature 'escape-sequence
@@ -317,6 +320,12 @@ rust-ts-mode--font-lock-settings
    '((ERROR) @font-lock-warning-face))
   "Tree-sitter font-lock settings for `rust-ts-mode'.")
 
+(defun rust-ts-mode--fontify-default (node _ start end &rest _)
+  "Fontify the node with the default face."
+  (treesit-fontify-with-override
+   (treesit-node-start node) (treesit-node-end node)
+   'default-face 'append start end))
+
 (defalias 'rust-ts-mode--fontify-pattern
   (and
    (treesit-available-p)
-- 
2.39.2