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

From: Dmitry Gutov <[email-masked]>
Date: Tue, 7 Feb 2023 20:16:54 +0200

Hi Randy.

On 07/02/2023 16:26, Randy Taylor wrote:

> I've given them a test spin.
> 
> c/c++-ts-modes look good to me.
> 
> go-ts-mode looks good to me.

Very good.

> rust-ts-mode looks good to me as well except the imports. Stuff like:
> use std::fmt::{Display, Formatter};
> 
> is highlighted incorrectly. In the above example, std and fmt are highlighted as variables.

This is a result of 'variable' being implemented as it is now -- 
highlighting all (identifier) nodes that no previous rule has matched.

That makes things complicated when we try to support customizable 
highlighting level where the user can mix and match the enabled features.

With imports, there was also another problem which I mentioned here: 
https://debbugs.gnu.org/cgi/bugreport.cgi?bug=61205#68

If we highlight the imports as constants on level 3, when the 'function' 
feature is disabled, the function names will get highlighted with 
font-lock-constant-face as well. That seems undesirable.

But -- and this just occurred to me today -- if we create a separate 
feature to add to level 4, with rules defined below 'function', that 
should satisfy all the constraints.

> We should give them font-lock-constant-face.
> 
> I will try to propose a patch later today unless someone beats me to it.

Try the attached patch, please.

On a distantly related note, we have terms like 'usize' which is 
normally a type (and highlighted as such), but can also feature in 
expressions like

   let row = usize::from_str_radix(row, 10).map_err(|_| error())?;

where it is now highlighted with font-lock-constant-face. Should we try 
to do anything about that? If there is a limited number of built-in 
types in that situation (e.g. all of them primitives), we could handle 
that with a regexp.

diff --git a/lisp/progmodes/rust-ts-mode.el b/lisp/progmodes/rust-ts-mode.el
index 5c71a8ad461..e2f198704f6 100644
--- a/lisp/progmodes/rust-ts-mode.el
+++ b/lisp/progmodes/rust-ts-mode.el
@@ -239,6 +239,19 @@ rust-ts-mode--font-lock-settings
    '((field_identifier) @font-lock-property-face
      (shorthand_field_initializer (identifier) @font-lock-property-face))
 
+   :language 'rust
+   :feature 'module
+   '((scoped_identifier name: (identifier) @font-lock-constant-face)
+     (scoped_identifier path: (identifier) @font-lock-constant-face)
+     (scoped_identifier
+      (scoped_identifier
+       path: (identifier) @font-lock-constant-face))
+     (scoped_identifier
+      path: [(identifier) @font-lock-constant-face
+             (scoped_identifier
+              name: (identifier) @font-lock-constant-face)])
+     (scoped_use_list path: (identifier) @font-lock-constant-face))
+
    :language 'rust
    :feature 'variable
    '((identifier) @font-lock-variable-name-face
@@ -344,7 +357,8 @@ rust-ts-mode
                   ( keyword string)
                   ( attribute builtin constant escape-sequence
                     number type)
-                  ( bracket delimiter error function operator property variable)))
+                  ( bracket delimiter error function operator property variable
+                    module)))
 
     ;; Imenu.
     (setq-local treesit-simple-imenu-settings