emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

feature/tree-sitter f6e92035a7 5/8: Allow user to add/remove tree-sitter


From: Yuan Fu
Subject: feature/tree-sitter f6e92035a7 5/8: Allow user to add/remove tree-sitter font-lock features
Date: Tue, 1 Nov 2022 16:28:16 -0400 (EDT)

branch: feature/tree-sitter
commit f6e92035a7c3c29aa980e1bebf9c48f1bb7264d8
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>

    Allow user to add/remove tree-sitter font-lock features
    
    * lisp/treesit.el (treesit-font-lock-recompute-features): Add
    ADD-LIST, REMOVE-LIST argument.
---
 lisp/treesit.el | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/lisp/treesit.el b/lisp/treesit.el
index 733d721f10..82eda595b9 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -419,6 +419,9 @@ omitted, default END to BEG."
 (defvar-local treesit-font-lock-feature-list nil
   "A list of lists of feature symbols.
 
+End-user should use `treesit-font-lock-recompute-features' and
+`font-lock-maximum-decoration' to configure enabled features.
+
 Each sublist represents a decoration level.
 `font-lock-maximum-decoration' controls which levels are
 activated.
@@ -581,20 +584,31 @@ name, it is ignored.
 (defvar treesit--font-lock-verbose nil
   "If non-nil, print debug messages when fontifying.")
 
-(defun treesit-font-lock-recompute-features ()
+(defun treesit-font-lock-recompute-features (&optional add-list remove-list)
   "Enable/disable font-lock settings according to decoration level.
-Set the ENABLE flag for each setting in
-`treesit-font-lock-settings', according to
+
+First compute the enabled features according to
 `treesit-font-lock-feature-list' and
-`font-lock-maximum-decoration'."
+`font-lock-maximum-decoration', then if ADD-LIST or REMOVE-LIST
+are not omitted, further add and remove features accordingly.
+
+ADD-LIST and REMOVE-LIST are each list of feature symbols.  The
+same feature symbol cannot not appear in both lists.  Otherwise
+signal `treesit-font-lock-error'."
+  (when-let ((intersection (cl-intersection add-list remove-list)))
+    (signal 'treesit-font-lock-error
+            (list "ADD-LIST and REMOVE-LIST contain the same feature"
+                  intersection)))
   (let* ((level (font-lock-value-in-major-mode
                  font-lock-maximum-decoration))
-         (features (cl-loop
-                    for idx = 0 then (1+ idx)
-                    for features in treesit-font-lock-feature-list
-                    if (or (eq level t)
-                           (>= level (1+ idx)))
-                    append features)))
+         (base-features (cl-loop
+                         for idx = 0 then (1+ idx)
+                         for features in treesit-font-lock-feature-list
+                         if (or (eq level t)
+                                (>= level (1+ idx)))
+                         append features))
+         (features (cl-set-difference (cl-union base-features add-list)
+                                      remove-list)))
     (cl-loop for idx = 0 then (1+ idx)
              for setting in treesit-font-lock-settings
              for feature = (nth 2 setting)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]