diff options
| author | Yuan Fu | 2024-12-07 23:18:53 -0800 |
|---|---|---|
| committer | Yuan Fu | 2024-12-07 23:36:02 -0800 |
| commit | b953bcb17047998c9e41cede7c5e5ffec22209b2 (patch) | |
| tree | 67f674c6c27201029acdcf2e44369c8919742a10 | |
| parent | 6f6b641975e4828f96913b29b1476c6323c952d8 (diff) | |
| download | emacs-b953bcb17047998c9e41cede7c5e5ffec22209b2.tar.gz emacs-b953bcb17047998c9e41cede7c5e5ffec22209b2.zip | |
Allow treesit--font-lock-level-setter to be an alist
* lisp/treesit.el (treesit-font-lock-level): Allow the
value to be an alist mapping major modes to font lock levels.
(treesit--font-lock-level-setter): Rewrite.
(treesit--compute-font-lock-level): New function.
(treesit-font-lock-recompute-features): Use new function.
* doc/lispref/modes.texi (Parser-based Font Lock): Minor fix.
| -rw-r--r-- | doc/lispref/modes.texi | 2 | ||||
| -rw-r--r-- | lisp/treesit.el | 56 |
2 files changed, 39 insertions, 19 deletions
diff --git a/doc/lispref/modes.texi b/doc/lispref/modes.texi index ed7cc141cd5..43282c7cd30 100644 --- a/doc/lispref/modes.texi +++ b/doc/lispref/modes.texi | |||
| @@ -4415,7 +4415,7 @@ ignored. | |||
| 4415 | @defvar treesit-font-lock-feature-list | 4415 | @defvar treesit-font-lock-feature-list |
| 4416 | This is a list of lists of feature symbols. Each element of the list | 4416 | This is a list of lists of feature symbols. Each element of the list |
| 4417 | is a list that represents a decoration level. | 4417 | is a list that represents a decoration level. |
| 4418 | @code{treesit-font-lock-level} controls which levels are | 4418 | @var{treesit-font-lock-level} controls which levels are |
| 4419 | activated. | 4419 | activated. |
| 4420 | 4420 | ||
| 4421 | Each element of the list is a list of the form @w{@code{(@var{feature} | 4421 | Each element of the list is a list of the form @w{@code{(@var{feature} |
diff --git a/lisp/treesit.el b/lisp/treesit.el index db3a706f016..db8f7a7595d 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el | |||
| @@ -898,29 +898,22 @@ t, nil, append, prepend, keep. See more in | |||
| 898 | (setf (nth 1 new-setting) t) | 898 | (setf (nth 1 new-setting) t) |
| 899 | new-setting)) | 899 | new-setting)) |
| 900 | 900 | ||
| 901 | ;; FIXME: Rewrite this in more readable fashion. | ||
| 902 | (defun treesit--font-lock-level-setter (sym val) | 901 | (defun treesit--font-lock-level-setter (sym val) |
| 903 | "Custom setter for `treesit-font-lock-level'. | 902 | "Custom setter for `treesit-font-lock-level'. |
| 904 | Set the default value of SYM to VAL, recompute fontification | 903 | Set the default value of SYM to VAL, recompute fontification |
| 905 | features and refontify for every buffer where tree-sitter-based | 904 | features and refontify for every buffer where tree-sitter-based |
| 906 | fontification is enabled." | 905 | fontification is enabled." |
| 907 | (set-default sym val) | 906 | (set-default sym val) |
| 908 | (and (treesit-available-p) | 907 | (when (treesit-available-p) |
| 909 | (named-let loop ((res nil) | 908 | (dolist (buffer (buffer-list)) |
| 910 | (buffers (buffer-list))) | 909 | (with-current-buffer buffer |
| 911 | (if (null buffers) | 910 | ;; FIXME: This doesn't re-run major mode hooks, meaning any |
| 912 | (mapc (lambda (b) | 911 | ;; customization done in major mode hooks (e.g., with |
| 913 | (with-current-buffer b | 912 | ;; `treesit-font-lock-recompute-features') is lost. |
| 914 | (setq-local treesit-font-lock-level val) | 913 | (when treesit-font-lock-settings |
| 915 | (treesit-font-lock-recompute-features) | 914 | (treesit-font-lock-recompute-features) |
| 916 | (treesit-font-lock-fontify-region (point-min) | 915 | (treesit-font-lock-fontify-region |
| 917 | (point-max)))) | 916 | (point-min) (point-max))))))) |
| 918 | res) | ||
| 919 | (let ((buffer (car buffers))) | ||
| 920 | (with-current-buffer buffer | ||
| 921 | (if treesit-font-lock-settings | ||
| 922 | (loop (append res (list buffer)) (cdr buffers)) | ||
| 923 | (loop res (cdr buffers))))))))) | ||
| 924 | 917 | ||
| 925 | (defcustom treesit-font-lock-level 3 | 918 | (defcustom treesit-font-lock-level 3 |
| 926 | "Decoration level to be used by tree-sitter fontifications. | 919 | "Decoration level to be used by tree-sitter fontifications. |
| @@ -937,6 +930,15 @@ Level 4 adds everything else that can be fontified: delimiters, | |||
| 937 | operators, brackets, punctuation, all functions, properties, | 930 | operators, brackets, punctuation, all functions, properties, |
| 938 | variables, etc. | 931 | variables, etc. |
| 939 | 932 | ||
| 933 | The value of this variable can be either a number representing a level, | ||
| 934 | or an alist of (MAJOR-MODE . LEVEL), where MAJOR-MODE is major mode | ||
| 935 | symbols, or t (meaning the default), and LEVEL is the font-lock level | ||
| 936 | for that mode. For example, | ||
| 937 | |||
| 938 | ((c-ts-mode . 3) (c++-ts-mode . 4) (t . 3)) | ||
| 939 | |||
| 940 | Major mode is checked with `derived-mode-p'. | ||
| 941 | |||
| 940 | In addition to the decoration level, individual features can be | 942 | In addition to the decoration level, individual features can be |
| 941 | turned on/off by calling `treesit-font-lock-recompute-features'. | 943 | turned on/off by calling `treesit-font-lock-recompute-features'. |
| 942 | Changing the decoration level requires calling | 944 | Changing the decoration level requires calling |
| @@ -1123,6 +1125,23 @@ name, it is ignored." | |||
| 1123 | (defvar treesit--font-lock-verbose nil | 1125 | (defvar treesit--font-lock-verbose nil |
| 1124 | "If non-nil, print debug messages when fontifying.") | 1126 | "If non-nil, print debug messages when fontifying.") |
| 1125 | 1127 | ||
| 1128 | (defun treesit--compute-font-lock-level (level) | ||
| 1129 | "Compute the font-lock level for the current major mode. | ||
| 1130 | |||
| 1131 | LEVEL should be the value of `treesit-font-lock-level'. Return a number | ||
| 1132 | representing the font-lock level for the current major mode. If there's | ||
| 1133 | no match, return 3." | ||
| 1134 | (if (numberp level) | ||
| 1135 | level | ||
| 1136 | (catch 'found | ||
| 1137 | (dolist (config level) | ||
| 1138 | (let ((mode (car config)) | ||
| 1139 | (num (cdr config))) | ||
| 1140 | (when (derived-mode-p mode) | ||
| 1141 | (throw 'found num)))) | ||
| 1142 | (or (alist-get t level) | ||
| 1143 | 3)))) | ||
| 1144 | |||
| 1126 | (defun treesit-font-lock-recompute-features | 1145 | (defun treesit-font-lock-recompute-features |
| 1127 | (&optional add-list remove-list language) | 1146 | (&optional add-list remove-list language) |
| 1128 | "Enable/disable font-lock features. | 1147 | "Enable/disable font-lock features. |
| @@ -1147,7 +1166,8 @@ and leave settings for other languages unchanged." | |||
| 1147 | (signal 'treesit-font-lock-error | 1166 | (signal 'treesit-font-lock-error |
| 1148 | (list "ADD-LIST and REMOVE-LIST contain the same feature" | 1167 | (list "ADD-LIST and REMOVE-LIST contain the same feature" |
| 1149 | intersection))) | 1168 | intersection))) |
| 1150 | (let* ((level treesit-font-lock-level) | 1169 | (let* ((level (treesit--compute-font-lock-level |
| 1170 | treesit-font-lock-level)) | ||
| 1151 | (base-features (cl-loop | 1171 | (base-features (cl-loop |
| 1152 | for idx = 0 then (1+ idx) | 1172 | for idx = 0 then (1+ idx) |
| 1153 | for features in treesit-font-lock-feature-list | 1173 | for features in treesit-font-lock-feature-list |