Hey everyone.

When using csharp-ts-mode to edit C# files, you may encounter the use of the typeof() operator in typeof()-expressions. The typeof() operater has 1 required parameter, which is always a type-name.

    public class Gnu
    {
        public void Demo()
        {
            var type = typeof(IQueryable);
        }
    }

This type parameter should be fontified using font-lock-type-face. Currently it is not.

There are explicit rules in csharp-ts-mode defined to attempt to fontify this, but they do not seem to be working:

     ,@(when (csharp-ts-mode--test-type-of-expression)
         '((type_of_expression (identifier) @font-lock-type-face))
         '((typeof_expression (identifier) @font-lock-type-face)))

Based on my inspection in treesit-explore mode, I see the grammar reports back these nodes as typeof_expression-nodes, but these rules are not activated because the preceeding (when ...) condition fails.

(defun csharp-ts-mode--test-type-of-expression ()
  "Return non-nil if (type_of_expression) is in the grammar."
  (ignore-errors
    (treesit-query-compile 'c-sharp "(type_of_expression)" t)
    t))

Changing this implementation to the following (matching the node-names I see in treesit-explore mode) makes highlighting work for me:

(defun csharp-ts-mode--test-type-of-expression ()
  "Return non-nil if (type_of_expression) is in the grammar."
  (ignore-errors
    (treesit-query-compile 'c-sharp "(typeof_expression)" t)
    t))

Attached is a patch which provides this change.

Note: This bug/fix was reproduced using tree-sitter-c-sharp grammar built on December 18th, 2024. I haven't tested with other versions.

Kind regards
Jostein