Subject: bug#60376: 29.0.60; Standardize csharp-ts-mode's font-lock features

From: Jostein Kjønigsen <[email-masked]>
Date: Sun, 1 Jan 2023 19:14:02 +0100

On 01.01.2023 18:24, Jostein Kjønigsen wrote:
> Disregard previous patch.
>
> Consider instead please the patch attached to this email.
>
> It does 3 things all in one:
>
> * moves function-call fontification to level 4 only (in its own feature)
>
> * cleans up long-standing issues with "messy" rules for 
> function-invocations. Removes the needs for "overrides".
>
> * also fixes issue with fonctification of self/this-method invocations.
>
> Theo: Can you try this patch and see what you think?
>
> -- 
>
> Jostein

Disregard again (and sorry for the noise!)

I've now gone through several files, done quite a bit of testing myself, 
and found another few issues needing to be solved:

  * Inconsistent variable-name fontification (sometimes when used,
    sometimes when declared, sometimes not when used, sometimes not when
    declared)
  * Variable declaration with explicit generic types
  * new() expression fontification for generic types.
  * Bleeding type-face into brackets for generic return-types in method
    definitions
  * Types when casting through as-expressions are not fontified at all.
  * And more?

I've solved those and combined all this into this latest patch, which 
also moves function-invocation into its own (level 4) feature.

This patch should be well beyond the "85%" which Eli has requested for 
Emacs-29 :)

*Theo:* Could you give this a test-spin, and I promise to call it a day? :)

--
Jostein
From 333602ff341b8a8add15ff96054cfc5cea0116cd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jostein=20Kj=C3=B8nigsen?= <[email-masked]>
Date: Sun, 1 Jan 2023 17:27:06 +0100
Subject: [PATCH] lisp/progmodes/csharp-mode.el: adjust function-call
 fontification

- ensure method-invocations are highlighted only on level 4
- ensure consistent fontification of variable declarations
  (don't highlight usage of all variables)
- fix issues with highlighting types in new() expressions.
- fix issues with generic types in variable-declarations not using "var".
- use fewer, more general queries for function-invocations. simplify code.
---
 lisp/progmodes/csharp-mode.el | 64 +++++++++++++++++++----------------
 1 file changed, 35 insertions(+), 29 deletions(-)

diff --git a/lisp/progmodes/csharp-mode.el b/lisp/progmodes/csharp-mode.el
index da64daf9848..473e8f49fd3 100644
--- a/lisp/progmodes/csharp-mode.el
+++ b/lisp/progmodes/csharp-mode.el
@@ -696,7 +696,7 @@ csharp-ts-mode--font-lock-settings
    :feature 'expression
    '((conditional_expression (identifier) @font-lock-variable-name-face)
      (postfix_unary_expression (identifier)* @font-lock-variable-name-face)
-     (assignment_expression (identifier) @font-lock-variable-name-face))
+     (initializer_expression (assignment_expression left: (identifier) @font-lock-variable-name-face)))
 
    :language 'c-sharp
    :feature 'bracket
@@ -764,8 +764,12 @@ csharp-ts-mode--font-lock-settings
       (identifier) @font-lock-type-face)
      (type_argument_list
       (identifier) @font-lock-type-face)
-     (generic_name
-      (identifier) @font-lock-type-face)
+     (type_argument_list
+      (generic_name
+       (identifier) @font-lock-type-face))
+     (base_list
+      (generic_name
+       (identifier) @font-lock-type-face))
      (array_type
       (identifier) @font-lock-type-face)
      (cast_expression (identifier) @font-lock-type-face)
@@ -773,7 +777,12 @@ csharp-ts-mode--font-lock-settings
      (type_parameter_constraints_clause
       target: (identifier) @font-lock-type-face)
      (type_of_expression (identifier) @font-lock-type-face)
-     (object_creation_expression (identifier) @font-lock-type-face))
+     (object_creation_expression
+      type: (identifier) @font-lock-type-face)
+     (object_creation_expression
+      type: (generic_name (identifier) @font-lock-type-face))
+     (as_expression right: (identifier) @font-lock-type-face)
+     (as_expression right: (generic_name (identifier) @font-lock-type-face)))
 
    :language 'c-sharp
    :feature 'definition
@@ -793,7 +802,6 @@ csharp-ts-mode--font-lock-settings
      (record_declaration (identifier) @font-lock-type-face)
      (namespace_declaration (identifier) @font-lock-type-face)
      (base_list (identifier) @font-lock-type-face)
-     (property_declaration (generic_name))
      (property_declaration
       type: (nullable_type) @font-lock-type-face
       name: (identifier) @font-lock-variable-name-face)
@@ -807,29 +815,10 @@ csharp-ts-mode--font-lock-settings
 
      (constructor_declaration name: (_) @font-lock-type-face)
 
-     (method_declaration type: (_) @font-lock-type-face)
+     (method_declaration type: [(identifier) (void_keyword)] @font-lock-type-face)
+     (method_declaration type: (generic_name (identifier) @font-lock-type-face))
      (method_declaration name: (_) @font-lock-function-name-face)
 
-     (invocation_expression
-      (member_access_expression
-       (generic_name (identifier) @font-lock-function-name-face)))
-     (invocation_expression
-      (member_access_expression
-       ((identifier) @font-lock-variable-name-face
-        (identifier) @font-lock-function-name-face)))
-     (invocation_expression
-      (identifier) @font-lock-function-name-face)
-     (invocation_expression
-      (member_access_expression
-       expression: (identifier) @font-lock-variable-name-face))
-     (invocation_expression
-      function: [(generic_name (identifier)) @font-lock-function-name-face
-                 (generic_name (type_argument_list
-                                ["<"] @font-lock-bracket-face
-                                (identifier) @font-lock-type-face
-                                [">"] @font-lock-bracket-face)
-                               )])
-
      (catch_declaration
       ((identifier) @font-lock-type-face))
      (catch_declaration
@@ -837,13 +826,30 @@ csharp-ts-mode--font-lock-settings
        (identifier) @font-lock-variable-name-face))
 
      (variable_declaration (identifier) @font-lock-type-face)
+     (variable_declaration (generic_name (identifier) @font-lock-type-face))
      (variable_declarator (identifier) @font-lock-variable-name-face)
 
      (parameter type: (identifier) @font-lock-type-face)
+     (parameter type: (generic_name (identifier) @font-lock-type-face))
      (parameter name: (identifier) @font-lock-variable-name-face)
 
-     (binary_expression (identifier) @font-lock-variable-name-face)
-     (argument (identifier) @font-lock-variable-name-face))
+     (lambda_expression (identifier) @font-lock-variable-name-face)
+
+     (declaration_expression type: (identifier) @font-lock-type-face)
+     (declaration_expression name: (identifier) @font-lock-variable-name-face))
+
+   :language 'c-sharp
+   :feature 'function
+   '((invocation_expression
+      function: (member_access_expression
+                 name: (identifier) @font-lock-function-name-face))
+     (invocation_expression
+      function: (identifier) @font-lock-function-name-face)
+     (invocation_expression
+      function: (member_access_expression
+                 name: (generic_name (identifier) @font-lock-function-name-face)))
+     (invocation_expression
+      function: (generic_name (identifier) @font-lock-function-name-face)))
 
    :language 'c-sharp
    :feature 'escape-sequence
@@ -916,7 +922,7 @@ csharp-ts-mode
               '(( comment definition)
                 ( keyword string type)
                 ( constant escape-sequence expression literal property)
-                ( bracket delimiter error)))
+                ( function bracket delimiter error)))
 
   ;; Imenu.
   (setq-local treesit-simple-imenu-settings
-- 
2.37.2