all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Randy Taylor <dev@rjt.dev>
Cc: 59268@debbugs.gnu.org
Subject: bug#59268: [PATCH] Utilize new font-lock faces for C/C++ and Python tree-sitter use
Date: Tue, 15 Nov 2022 01:26:05 +0000	[thread overview]
Message-ID: <adSxp0HWi84Hvwdhjl7zIXH0L5o2tcg_NETNj1kRpNjNwGjbNCLxgdcIFRyA6lZw1IwGG1hd8lOHxV-TPoD-V7MtBLlZN0Khl__ddfCq4B4=@rjt.dev> (raw)
In-Reply-To: <ogpBJlgoBoIG6IdqUDqQ0dSnqaWW-jHeq7mB9_n5RVmu5BKwAgpbM1m0pPKnjGLQrehP9Qb3FW34Z7n_EunChv4gAkJuZ7M4tLLwzb0P0hA=@rjt.dev>


[-- Attachment #1.1: Type: text/plain, Size: 76 bytes --]

I updated the patch to apply cleanly, since recent changes caused conflicts.

[-- Attachment #1.2: Type: text/html, Size: 170 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Utilize-new-font-lock-faces-for-C-C-and-Python-tree-.patch --]
[-- Type: text/x-patch; name=0001-Utilize-new-font-lock-faces-for-C-C-and-Python-tree-.patch, Size: 8040 bytes --]

From d474d7247ec7c51d761ea629ca107464a0fa7a2f Mon Sep 17 00:00:00 2001
From: Randy Taylor <dev@rjt.dev>
Date: Sun, 13 Nov 2022 22:06:33 -0500
Subject: [PATCH] Utilize new font-lock faces for C/C++ and Python tree-sitter
 use

* lisp/progmodes/c-ts-mode.el (c-ts-mode--font-lock-settings): Use new
escape, number, property, operator, bracket, and delimiter font-lock
faces.  Simplify type matching.
(c-ts-mode--base-mode): Add them to the feature list.
(c-ts-mode): Fix typo.
* lisp/progmodes/python.el (python--treesit-settings): Use new number,
property, operator, bracket, and delimiter font-lock faces.
(python-mode): Add them to the feature list.
---
 lisp/progmodes/c-ts-mode.el | 72 ++++++++++++++++---------------------
 lisp/progmodes/python.el    | 41 +++++++++++++++++----
 2 files changed, 65 insertions(+), 48 deletions(-)

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index bb0db0e1e7..1ee5f45d35 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -231,18 +231,17 @@ c-ts-mode--font-lock-settings
    :language mode
    :override t
    :feature 'operator
-   `([,@c-ts-mode--operators] @font-lock-builtin-face)
+   `([,@c-ts-mode--operators] @font-lock-operator-face)
    :language mode
    :override t
    :feature 'string
    `((string_literal) @font-lock-string-face
      ((string_literal)) @contextual
-     (system_lib_string) @font-lock-string-face
-     (escape_sequence) @font-lock-string-face)
+     (system_lib_string) @font-lock-string-face)
    :language mode
    :override t
    :feature 'literal
-   `((number_literal) @font-lock-constant-face
+   `((number_literal) @font-lock-number-face
      (char_literal) @font-lock-constant-face)
    :language mode
    :override t
@@ -252,30 +251,13 @@ c-ts-mode--font-lock-settings
          '((type_qualifier) @font-lock-type-face
 
            (qualified_identifier
-            scope: (namespace_identifier) @font-lock-type-face)
-
-           (operator_cast) type: (type_identifier) @font-lock-type-face)))
+            scope: (namespace_identifier) @font-lock-type-face))))
    :language mode
    :override t
    :feature 'definition
    `((declaration
       declarator: (identifier) @font-lock-variable-name-face)
 
-     (declaration
-      type: (type_identifier) @font-lock-type-face)
-
-     (field_declaration
-      declarator: (field_identifier) @font-lock-variable-name-face)
-
-     (field_declaration
-      type: (type_identifier) @font-lock-type-face)
-
-     (parameter_declaration
-      type: (type_identifier) @font-lock-type-face)
-
-     (function_definition
-      type: (type_identifier) @font-lock-type-face)
-
      (function_declarator
       declarator: (identifier) @font-lock-function-name-face)
 
@@ -285,17 +267,8 @@ c-ts-mode--font-lock-settings
      (init_declarator
       declarator: (identifier) @font-lock-variable-name-face)
 
-     (struct_specifier
-      name: (type_identifier) @font-lock-type-face)
-
      (sized_type_specifier) @font-lock-type-face
 
-     (enum_specifier
-      name: (type_identifier) @font-lock-type-face)
-
-     (enumerator
-      name: (identifier) @font-lock-variable-name-face)
-
      (parameter_declaration
       type: (_) @font-lock-type-face
       declarator: (identifier) @font-lock-variable-name-face)
@@ -303,8 +276,7 @@ c-ts-mode--font-lock-settings
      (pointer_declarator
       declarator: (identifier) @font-lock-variable-name-face)
 
-     (pointer_declarator
-      declarator: (field_identifier) @font-lock-variable-name-face))
+     ((type_identifier) @font-lock-type-face))
    :language mode
    :override t
    :feature 'expression
@@ -315,11 +287,7 @@ c-ts-mode--font-lock-settings
       function: (identifier) @font-lock-function-name-face)
 
      (field_expression
-      field: (field_identifier) @font-lock-variable-name-face)
-
-     (field_expression
-      argument: (identifier) @font-lock-variable-name-face
-      field: (field_identifier) @font-lock-variable-name-face)
+      argument: (identifier) @font-lock-variable-name-face)
 
      (pointer_expression
       argument: (identifier) @font-lock-variable-name-face))
@@ -332,7 +300,26 @@ c-ts-mode--font-lock-settings
    :language mode
    :override t
    :feature 'error
-   '((ERROR) @font-lock-warning-face)))
+   '((ERROR) @font-lock-warning-face)
+   :feature 'escape-sequence
+   :language mode
+   :override t
+   '((escape_sequence) @font-lock-escape-face)
+   :language mode
+   :override t
+   :feature 'property
+   '((field_identifier) @font-lock-property-face
+     (enumerator
+      name: (identifier) @font-lock-property-face))
+   :language mode
+   :override t
+   :feature 'bracket
+   '((["(" ")" "[" "]" "{" "}"]) @font-lock-bracket-face)
+   :language mode
+   :override t
+   :feature 'delimiter
+   '((["," ":" ";"]) @font-lock-delimiter-face)
+   ))
 
 (defun c-ts-mode--imenu-1 (node)
   "Helper for `c-ts-mode--imenu'.
@@ -418,8 +405,9 @@ c-ts-mode--base-mode
   (setq-local which-func-functions nil)
 
   (setq-local treesit-font-lock-feature-list
-              '((comment preprocessor operator constant string literal keyword)
-                (type definition expression statement)
+              '(( comment preprocessor operator constant string literal keyword)
+                ( type definition expression statement property bracket
+                  delimiter escape-sequence)
                 ())))
 
 ;;;###autoload
@@ -447,7 +435,7 @@ c-ts-mode
 
 ;;;###autoload
 (define-derived-mode c++-ts-mode c-ts-mode--base-mode "C++"
-  "Major mode for editing C, powered by tree-sitter."
+  "Major mode for editing C++, powered by tree-sitter."
   :group 'c++
 
   (unless (treesit-ready-p nil 'cpp)
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index a5d02d0fcb..88dc31b50e 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -975,11 +975,8 @@ python--treesit-constants
     "copyright" "credits" "exit" "license" "quit"))
 
 (defvar python--treesit-operators
-  ;; This is not used. And and, or, not, is, in are fontified as
-  ;; keywords.
   '("-" "-=" "!=" "*" "**" "**=" "*=" "/" "//" "//=" "/=" "&" "%" "%="
-    "^" "+" "+=" "<" "<<" "<=" "<>" "=" "==" ">" ">=" ">>" "|" "~"
-    "and" "in" "is" "not" "or"))
+    "^" "+" "+=" "<" "<<" "<=" "<>" "=" "==" ">" ">=" ">>" "|" "~"))
 
 (defvar python--treesit-special-attributes
   '("__annotations__" "__closure__" "__code__"
@@ -1118,7 +1115,38 @@ python--treesit-settings
    :feature 'escape-sequence
    :language 'python
    :override t
-   '((escape_sequence) @font-lock-escape-face))
+   '((escape_sequence) @font-lock-escape-face)
+
+   :feature 'number
+   :language 'python
+   :override t
+   '([(integer) (float)] @font-lock-number-face)
+
+   :feature 'property
+   :language 'python
+   :override t
+   '((attribute
+      attribute: (identifier) @font-lock-property-face)
+     (class_definition
+      body: (block
+             (expression_statement
+              (assignment left:
+                          (identifier) @font-lock-property-face)))))
+
+   :feature 'operator
+   :language 'python
+   :override t
+   `([,@python--treesit-operators] @font-lock-operator-face)
+
+   :feature 'bracket
+   :language 'python
+   :override t
+   '(["(" ")" "[" "]" "{" "}"] @font-lock-bracket-face)
+
+   :feature 'delimiter
+   :language 'python
+   :override t
+   '(["," "." ":" ";" (ellipsis)] @font-lock-delimiter-face))
   "Tree-sitter font-lock settings.")
 
 \f
@@ -6553,7 +6581,8 @@ python-mode
                 '(( comment string function-name class-name)
                   ( keyword builtin constant type)
                   ( assignment decorator escape-sequence
-                    string-interpolation)))
+                    string-interpolation number property
+                    operator bracket delimiter)))
     (setq-local treesit-font-lock-settings python--treesit-settings)
     (setq-local imenu-create-index-function
                 #'python-imenu-treesit-create-index)
-- 
2.38.1


  reply	other threads:[~2022-11-15  1:26 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-14  3:27 bug#59268: [PATCH] Utilize new font-lock faces for C/C++ and Python tree-sitter use Randy Taylor
2022-11-15  1:26 ` Randy Taylor [this message]
2022-11-16  2:04   ` Randy Taylor
2022-11-17  3:26     ` Randy Taylor
2022-11-17  3:39       ` Yuan Fu
2022-11-17 13:50         ` Randy Taylor
2022-11-17 18:39           ` Yuan Fu
2022-11-17 20:02             ` Randy Taylor
2022-11-17 20:07               ` Yuan Fu
2022-11-17 20:19                 ` Randy Taylor

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

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

  git send-email \
    --in-reply-to='adSxp0HWi84Hvwdhjl7zIXH0L5o2tcg_NETNj1kRpNjNwGjbNCLxgdcIFRyA6lZw1IwGG1hd8lOHxV-TPoD-V7MtBLlZN0Khl__ddfCq4B4=@rjt.dev' \
    --to=dev@rjt.dev \
    --cc=59268@debbugs.gnu.org \
    /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 external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.