unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Theodor Thornhill <theo@thornhill.no>
To: Yuan Fu <casouri@gmail.com>
Cc: emacs-devel@gnu.org
Subject: Re: Disable tree-sitter font-locking for smaller ranges
Date: Tue, 18 Oct 2022 22:07:06 +0200	[thread overview]
Message-ID: <87a65szyj9.fsf@thornhill.no> (raw)
In-Reply-To: <DFC06C29-93F0-4BEB-B1A8-B792E7A9ED70@thornhill.no>

[-- Attachment #1: Type: text/plain, Size: 157 bytes --]

Theodor Thornhill <theo@thornhill.no> writes:
> The separate variables was more for readability, but I can change that back :)


Something like this?

Theo


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-more-granular-features-in-font-locking.patch --]
[-- Type: text/x-diff, Size: 13083 bytes --]

From 0b44ad45133dbdff85120e056c7ff95eef07e492 Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <theo@thornhill.no>
Date: Mon, 17 Oct 2022 12:49:19 +0200
Subject: [PATCH] Add more granular features in font-locking

There is now support for three font-locking levels, 'minimal',
'moderate' and 'full'.  The richest experience is to be expected from
the 'full', and all levels are enabled by default.

* lisp/progmodes/js.el (js--treesit-font-lock-settings): New defvar
renamed from 'js--treesit-settings'.

(js--treesit-font-lock-settings): New defvar renamed from
'js--json-treesit-settings'.

* lisp/progmodes/ts-mode.el (ts-mode--font-lock-settings): New defvar
renamed from 'ts-mode--settings'.
---
 lisp/progmodes/js.el      |  82 ++++++++++--------
 lisp/progmodes/ts-mode.el | 175 +++++++++++++++++---------------------
 2 files changed, 123 insertions(+), 134 deletions(-)

diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index 667416852e..32e1e5027d 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -3454,19 +3454,34 @@ js--treesit-keywords
     "debugger" "default" "delete" "do" "else" "export" "extends" "finally"
     "for" "from" "function" "get" "if" "import" "in" "instanceof" "let" "new"
     "of" "return" "set" "static" "switch" "switch" "target" "throw" "try"
-    "typeof" "var" "void" "while" "with" "yield"))
+    "typeof" "var" "void" "while" "with" "yield")
+  "JavaScript keywords for tree-sitter font-locking.")
 
-(defvar js--treesit-settings
+(defvar js--treesit-font-lock-settings
   (treesit-font-lock-rules
    :language 'javascript
-   :feature 'basic
    :override t
-   `(((identifier) @font-lock-constant-face
+   :feature 'minimal
+   `(
+     ((identifier) @font-lock-constant-face
       (:match "^[A-Z_][A-Z_\\d]*$" @font-lock-constant-face))
 
-     (new_expression
-      constructor: (identifier) @font-lock-type-face)
+     [(this) (super)] @font-lock-keyword-face
 
+     [(true) (false) (null)] @font-lock-constant-face
+     (regex pattern: (regex_pattern)) @font-lock-string-face
+     (number) @font-lock-constant-face
+
+     (string) @font-lock-string-face
+     (comment) @font-lock-comment-face
+     [,@js--treesit-keywords] @font-lock-keyword-face
+
+     (template_string) @js--fontify-template-string
+     (template_substitution ["${" "}"] @font-lock-constant-face))
+   :language 'javascript
+   :override t
+   :feature 'moderate
+   `(
      (function
       name: (identifier) @font-lock-function-name-face)
 
@@ -3479,6 +3494,21 @@ js--treesit-settings
      (method_definition
       name: (property_identifier) @font-lock-function-name-face)
 
+     (variable_declarator
+      name: (identifier) @font-lock-variable-name-face)
+
+     (new_expression
+      constructor: (identifier) @font-lock-type-face)
+
+     (for_in_statement
+      left: (identifier) @font-lock-variable-name-face)
+
+     (arrow_function
+      parameter: (identifier) @font-lock-variable-name-face))
+   :language 'javascript
+   :override t
+   :feature 'full
+   `(
      (variable_declarator
       name: (identifier) @font-lock-function-name-face
       value: [(function) (arrow_function)])
@@ -3502,20 +3532,11 @@ js--treesit-settings
                   property:
                   (property_identifier) @font-lock-function-name-face)])
 
-     (variable_declarator
-      name: (identifier) @font-lock-variable-name-face)
-
      (assignment_expression
       left: [(identifier) @font-lock-variable-name-face
              (member_expression
               property: (property_identifier) @font-lock-variable-name-face)])
 
-     (for_in_statement
-      left: (identifier) @font-lock-variable-name-face)
-
-     (arrow_function
-      parameter: (identifier) @font-lock-variable-name-face)
-
      (pair key: (property_identifier) @font-lock-variable-name-face)
 
      (pair value: (identifier) @font-lock-variable-name-face)
@@ -3546,20 +3567,8 @@ js--treesit-settings
 
      (jsx_attribute
       (property_identifier)
-      @font-lock-constant-face)
-
-     [(this) (super)] @font-lock-keyword-face
-
-     [(true) (false) (null)] @font-lock-constant-face
-     (regex pattern: (regex_pattern)) @font-lock-string-face
-     (number) @font-lock-constant-face
-
-     (string) @font-lock-string-face
-     (comment) @font-lock-comment-face
-     [,@js--treesit-keywords] @font-lock-keyword-face
-
-     (template_string) @js--fontify-template-string
-     (template_substitution ["${" "}"] @font-lock-constant-face))))
+      @font-lock-constant-face)))
+  "Tree-sitter font-lock settings.")
 
 (defun js--fontify-template-string (beg end node)
   "Fontify template string but not substitution inside it.
@@ -3655,8 +3664,8 @@ js--treesit-enable
   (setq-local end-of-defun-function #'js--treesit-end-of-defun)
 
   (setq-local font-lock-keywords-only t)
-  (setq-local treesit-font-lock-settings js--treesit-settings)
-  (setq-local treesit-font-lock-feature-list '((basic)))
+  (setq-local treesit-font-lock-settings js--treesit-font-lock-settings)
+  (setq-local treesit-font-lock-feature-list '((minimal) (moderate) (full)))
 
   (add-hook 'which-func-functions #'js-treesit-current-defun nil t)
 
@@ -3761,10 +3770,10 @@ js-json-use-tree-sitter
   :type 'boolean
   :safe 'booleanp)
 
-(defvar js--json-treesit-settings
+(defvar js-json--treesit-font-lock-settings
   (treesit-font-lock-rules
    :language 'json
-   :feature 'basic
+   :feature 'minimal
    :override t
    `(
      (pair
@@ -3778,8 +3787,8 @@ js--json-treesit-settings
 
      (escape_sequence) @font-lock-constant-face
 
-     (comment) @font-lock-comment-face
-     )))
+     (comment) @font-lock-comment-face))
+  "Font-lock settings for JSON.")
 
 
 (defvar js--json-treesit-indent-rules
@@ -3809,8 +3818,9 @@ js--json-treesit-enable
   (setq-local end-of-defun-function #'ignore)
 
   (setq-local font-lock-defaults '(nil t))
-  (setq-local treesit-font-lock-settings js--json-treesit-settings)
+  (setq-local treesit-font-lock-settings js-json--treesit-font-lock-settings)
 
+  (setq treesit-font-lock-feature-list '((minimal)))
   (treesit-font-lock-enable))
 
 
diff --git a/lisp/progmodes/ts-mode.el b/lisp/progmodes/ts-mode.el
index 6e4aeebde8..b5adf0744e 100644
--- a/lisp/progmodes/ts-mode.el
+++ b/lisp/progmodes/ts-mode.el
@@ -104,17 +104,49 @@ ts-mode--indent-rules
      (no-node parent-bol 0)))
   "Tree-sitter indent rules.")
 
-(defvar ts-mode--settings
+(defvar ts-mode--keywords
+  '("!" "abstract" "as" "async" "await" "break"
+    "case" "catch" "class" "const" "continue" "debugger"
+    "declare" "default" "delete" "do" "else" "enum"
+    "export" "extends" "finally" "for" "from" "function"
+    "get" "if" "implements" "import" "in" "instanceof" "interface"
+    "keyof" "let" "namespace" "new" "of" "private" "protected"
+    "public" "readonly" "return" "set" "static" "switch"
+    "target" "throw" "try" "type" "typeof" "var" "void"
+    "while" "with" "yield")
+  "TypeScript keywords for tree-sitter font-locking.")
+
+(defvar ts-mode--font-lock-settings
   (treesit-font-lock-rules
    :language 'tsx
    :override t
-   :feature 'basic
-   '(((identifier) @font-lock-constant-face
+   :feature 'minimal
+   `(
+     ((identifier) @font-lock-constant-face
       (:match "^[A-Z_][A-Z_\\d]*$" @font-lock-constant-face))
 
+     [,@ts-mode--keywords] @font-lock-keyword-face
+     [(this) (super)] @font-lock-keyword-face
+
+     [(true) (false) (null)] @font-lock-constant-face
+     (regex pattern: (regex_pattern)) @font-lock-string-face
+     (number) @font-lock-constant-face
+
+     (string) @font-lock-string-face
+
+     (template_string) @ts-mode--fontify-template-string
+     (template_substitution ["${" "}"] @font-lock-builtin-face)
+
+     (comment) @font-lock-comment-face)
+   :language 'tsx
+   :override t
+   :feature 'moderate
+   '(
      (nested_type_identifier
       module: (identifier) @font-lock-type-face)
+
      (type_identifier) @font-lock-type-face
+
      (predefined_type) @font-lock-type-face
 
      (new_expression
@@ -129,6 +161,29 @@ ts-mode--settings
      (method_definition
       name: (property_identifier) @font-lock-function-name-face)
 
+     (variable_declarator
+      name: (identifier) @font-lock-variable-name-face)
+
+     (enum_declaration (identifier) @font-lock-type-face)
+
+     (enum_body (property_identifier) @font-lock-type-face)
+
+     (enum_assignment name: (property_identifier) @font-lock-type-face)
+
+     (assignment_expression
+      left: [(identifier) @font-lock-variable-name-face
+             (member_expression
+              property: (property_identifier) @font-lock-variable-name-face)])
+
+     (for_in_statement
+      left: (identifier) @font-lock-variable-name-face)
+
+     (arrow_function
+      parameter: (identifier) @font-lock-variable-name-face))
+   :language 'tsx
+   :override t
+   :feature 'full
+   '(
      (variable_declarator
       name: (identifier) @font-lock-function-name-face
       value: [(function) (arrow_function)])
@@ -151,33 +206,12 @@ ts-mode--settings
        (member_expression
         property: (property_identifier) @font-lock-function-name-face)])
 
-     (variable_declarator
-      name: (identifier) @font-lock-variable-name-face)
-
-     (enum_declaration (identifier) @font-lock-type-face)
-
-     (enum_body (property_identifier) @font-lock-type-face)
-
-     (enum_assignment name: (property_identifier) @font-lock-type-face)
-
-     (assignment_expression
-      left: [(identifier) @font-lock-variable-name-face
-             (member_expression
-              property: (property_identifier) @font-lock-variable-name-face)])
-
-     (for_in_statement
-      left: (identifier) @font-lock-variable-name-face)
-
-     (arrow_function
-      parameter: (identifier) @font-lock-variable-name-face)
-
      (arrow_function
       parameters:
       [(_ (identifier) @font-lock-variable-name-face)
        (_ (_ (identifier) @font-lock-variable-name-face))
        (_ (_ (_ (identifier) @font-lock-variable-name-face)))])
 
-
      (pair key: (property_identifier) @font-lock-variable-name-face)
 
      (pair value: (identifier) @font-lock-variable-name-face)
@@ -211,80 +245,24 @@ ts-mode--settings
       [(nested_identifier (identifier)) (identifier)]
       @font-lock-function-name-face)
 
-     (jsx_attribute (property_identifier) @font-lock-constant-face)
-
-     [(this) (super)] @font-lock-keyword-face
-
-     [(true) (false) (null)] @font-lock-constant-face
-     (regex pattern: (regex_pattern)) @font-lock-string-face
-     (number) @font-lock-constant-face
-
-     (string) @font-lock-string-face
-
-     (template_string) @js--fontify-template-string
-     (template_substitution
-      ["${" "}"] @font-lock-constant-face)
-
-     ["!"
-      "abstract"
-      "as"
-      "async"
-      "await"
-      "break"
-      "case"
-      "catch"
-      "class"
-      "const"
-      "continue"
-      "debugger"
-      "declare"
-      "default"
-      "delete"
-      "do"
-      "else"
-      "enum"
-      "export"
-      "extends"
-      "finally"
-      "for"
-      "from"
-      "function"
-      "get"
-      "if"
-      "implements"
-      "import"
-      "in"
-      "instanceof"
-      "interface"
-      "keyof"
-      "let"
-      "namespace"
-      "new"
-      "of"
-      "private"
-      "protected"
-      "public"
-      "readonly"
-      "return"
-      "set"
-      "static"
-      "switch"
-      "target"
-      "throw"
-      "try"
-      "type"
-      "typeof"
-      "var"
-      "void"
-      "while"
-      "with"
-      "yield"
-      ] @font-lock-keyword-face
-
-     (comment) @font-lock-comment-face
-     ))
+     (jsx_attribute (property_identifier) @font-lock-constant-face)))
   "Tree-sitter font-lock settings.")
 
+(defun ts-mode--fontify-template-string (beg end node)
+  "Fontify template string but not substitution inside it.
+BEG, END, NODE refers to the template_string node."
+  (ignore end)
+  ;; Stolen from `js--fontify-template-string'
+  (let ((child (treesit-node-child node 0)))
+    (while child
+      (if (equal (treesit-node-type child) "template_substitution")
+          (put-text-property beg (treesit-node-start child)
+                             'face 'font-lock-string-face)
+        (put-text-property beg (treesit-node-end child)
+                           'face 'font-lock-string-face))
+      (setq beg (treesit-node-end child)
+            child (treesit-node-next-sibling child)))))
+
 (defvar ts-mode--defun-type-regexp
   (rx (or "class_declaration"
           "method_definition"
@@ -354,9 +332,10 @@ ts-mode
     (unless font-lock-defaults
       (setq font-lock-defaults '(nil t)))
 
-    (setq-local treesit-font-lock-settings ts-mode--settings)
+    (setq-local treesit-font-lock-settings ts-mode--font-lock-settings)
+
+    (setq treesit-font-lock-feature-list '((minimal) (moderate) (full)))
 
-    (setq treesit-font-lock-feature-list '((basic)))
     (treesit-font-lock-enable))
    (t
     (message "Tree sitter for TypeScript isn't available, defaulting to js-mode")
-- 
2.34.1


  reply	other threads:[~2022-10-18 20:07 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-16 10:32 Disable tree-sitter font-locking for smaller ranges Theodor Thornhill
2022-10-17  5:04 ` Yuan Fu
2022-10-17  5:49   ` Theodor Thornhill
2022-10-17  6:01     ` Yuan Fu
2022-10-17  6:33       ` Theodor Thornhill
2022-10-17  9:00         ` Yuan Fu
2022-10-17  9:41           ` Theodor Thornhill
2022-10-17 11:02             ` Theodor Thornhill via Emacs development discussions.
2022-10-18  0:20               ` Yuan Fu
2022-10-18  5:04                 ` Theodor Thornhill
2022-10-18 20:07                   ` Theodor Thornhill [this message]
2022-10-18 20:44                     ` Yuan Fu
2022-10-18  5:06             ` Yuan Fu
2022-10-18  1:23           ` Trey Peacock

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=87a65szyj9.fsf@thornhill.no \
    --to=theo@thornhill.no \
    --cc=casouri@gmail.com \
    --cc=emacs-devel@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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).