diff options
| author | Yuan Fu | 2024-12-19 19:02:59 -0800 |
|---|---|---|
| committer | Yuan Fu | 2024-12-20 00:53:00 -0800 |
| commit | 05ab13ebc7237bcf23bc84a6a345d0b563404c2d (patch) | |
| tree | 211873c7e284276828ea5ab98aa0feb8ef098b72 | |
| parent | 926a9c864adc29f056644f5e23cf59fb90e2613c (diff) | |
| download | emacs-05ab13ebc7237bcf23bc84a6a345d0b563404c2d.tar.gz emacs-05ab13ebc7237bcf23bc84a6a345d0b563404c2d.zip | |
Add keyword :reversed to treesit-font-lock-rules
This keyword will be useful for implementing
string-interpolation feature.
* doc/lispref/modes.texi (Parser-based Font Lock): Document the
new keyword.
* lisp/treesit.el (treesit-font-lock-settings): Document.
(treesit-font-lock-setting-reversed): New function.
(treesit-font-lock-rules): Add new keyword.
(treesit-font-lock-recompute-features): Handle new keyword.
| -rw-r--r-- | doc/lispref/modes.texi | 6 | ||||
| -rw-r--r-- | lisp/treesit.el | 39 |
2 files changed, 36 insertions, 9 deletions
diff --git a/doc/lispref/modes.texi b/doc/lispref/modes.texi index 2631769c51d..73edb688c85 100644 --- a/doc/lispref/modes.texi +++ b/doc/lispref/modes.texi | |||
| @@ -4378,6 +4378,8 @@ Other keywords are optional: | |||
| 4378 | @item @tab @code{append} @tab Append the new face to existing ones | 4378 | @item @tab @code{append} @tab Append the new face to existing ones |
| 4379 | @item @tab @code{prepend} @tab Prepend the new face to existing ones | 4379 | @item @tab @code{prepend} @tab Prepend the new face to existing ones |
| 4380 | @item @tab @code{keep} @tab Fill-in regions without an existing face | 4380 | @item @tab @code{keep} @tab Fill-in regions without an existing face |
| 4381 | @item @code{:reversed} @tab @code{t} | ||
| 4382 | @tab Enable @var{query} when @var{feature} is not in the feature list. | ||
| 4381 | @item @code{:default-language} @tab @var{language} | 4383 | @item @code{:default-language} @tab @var{language} |
| 4382 | @tab Every @var{query} after this keyword will use @var{language} | 4384 | @tab Every @var{query} after this keyword will use @var{language} |
| 4383 | by default. | 4385 | by default. |
| @@ -4461,6 +4463,7 @@ For this variable to take effect, a Lisp program should call | |||
| 4461 | @findex treesit-font-lock-setting-feature | 4463 | @findex treesit-font-lock-setting-feature |
| 4462 | @findex treesit-font-lock-setting-enable | 4464 | @findex treesit-font-lock-setting-enable |
| 4463 | @findex treesit-font-lock-setting-override | 4465 | @findex treesit-font-lock-setting-override |
| 4466 | @findex treesit-font-lock-setting-reversed | ||
| 4464 | @defvar treesit-font-lock-settings | 4467 | @defvar treesit-font-lock-settings |
| 4465 | A list of settings for tree-sitter based font lock. The exact format of | 4468 | A list of settings for tree-sitter based font lock. The exact format of |
| 4466 | each individual setting is considered internal. One should always use | 4469 | each individual setting is considered internal. One should always use |
| @@ -4471,7 +4474,8 @@ the setting's query, feature, enable flag and override flag: | |||
| 4471 | @code{treesit-font-lock-setting-query}, | 4474 | @code{treesit-font-lock-setting-query}, |
| 4472 | @code{treesit-font-lock-setting-feature}, | 4475 | @code{treesit-font-lock-setting-feature}, |
| 4473 | @code{treesit-font-lock-setting-enable}, | 4476 | @code{treesit-font-lock-setting-enable}, |
| 4474 | @code{treesit-font-lock-setting-override}. | 4477 | @code{treesit-font-lock-setting-override}, |
| 4478 | @code{treesit-font-lock-setting-reversed}. | ||
| 4475 | 4479 | ||
| 4476 | @c Because the format is internal, we don't document them here. Though | 4480 | @c Because the format is internal, we don't document them here. Though |
| 4477 | @c we do have it explained in the docstring. We also expose the fact | 4481 | @c we do have it explained in the docstring. We also expose the fact |
diff --git a/lisp/treesit.el b/lisp/treesit.el index 9db3d57b6e9..7d09934b3e6 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el | |||
| @@ -866,7 +866,7 @@ debugging: | |||
| 866 | 866 | ||
| 867 | Currently each SETTING has the form: | 867 | Currently each SETTING has the form: |
| 868 | 868 | ||
| 869 | (QUERY ENABLE FEATURE OVERRIDE) | 869 | (QUERY ENABLE FEATURE OVERRIDE REVERSE) |
| 870 | 870 | ||
| 871 | QUERY must be a compiled query. See Info node `(elisp)Pattern | 871 | QUERY must be a compiled query. See Info node `(elisp)Pattern |
| 872 | Matching' for how to write a query and compile it. | 872 | Matching' for how to write a query and compile it. |
| @@ -880,7 +880,10 @@ which features are enabled with `treesit-font-lock-level' and | |||
| 880 | 880 | ||
| 881 | OVERRIDE is the override flag for this query. Its value can be | 881 | OVERRIDE is the override flag for this query. Its value can be |
| 882 | t, nil, append, prepend, keep. See more in | 882 | t, nil, append, prepend, keep. See more in |
| 883 | `treesit-font-lock-rules'.") | 883 | `treesit-font-lock-rules'. |
| 884 | |||
| 885 | If REVERSED is t, enable the QUERY when FEATURE is not in the feature | ||
| 886 | list.") | ||
| 884 | 887 | ||
| 885 | ;; Follow cl-defstruct naming conventions, in case we use cl-defstruct | 888 | ;; Follow cl-defstruct naming conventions, in case we use cl-defstruct |
| 886 | ;; in the future. | 889 | ;; in the future. |
| @@ -900,6 +903,10 @@ t, nil, append, prepend, keep. See more in | |||
| 900 | "Return the OVERRIDE flag of SETTING in `treesit-font-lock-settings'." | 903 | "Return the OVERRIDE flag of SETTING in `treesit-font-lock-settings'." |
| 901 | (nth 3 setting)) | 904 | (nth 3 setting)) |
| 902 | 905 | ||
| 906 | (defsubst treesit-font-lock-setting-reversed (setting) | ||
| 907 | "Return the REVERSED flag of SETTING in `treesit-font-lock-settings'." | ||
| 908 | (nth 4 setting)) | ||
| 909 | |||
| 903 | (defsubst treesit--font-lock-setting-clone-enable (setting) | 910 | (defsubst treesit--font-lock-setting-clone-enable (setting) |
| 904 | "Return enabled SETTING." | 911 | "Return enabled SETTING." |
| 905 | (let ((new-setting (copy-tree setting))) | 912 | (let ((new-setting (copy-tree setting))) |
| @@ -1030,6 +1037,8 @@ Other keywords include: | |||
| 1030 | `append' Append the new face to existing ones. | 1037 | `append' Append the new face to existing ones. |
| 1031 | `prepend' Prepend the new face to existing ones. | 1038 | `prepend' Prepend the new face to existing ones. |
| 1032 | `keep' Fill-in regions without an existing face. | 1039 | `keep' Fill-in regions without an existing face. |
| 1040 | :reversed t Enable the query only if the feature is | ||
| 1041 | NOT in feature list. | ||
| 1033 | :default-language LANGUAGE Every QUERY after this keyword | 1042 | :default-language LANGUAGE Every QUERY after this keyword |
| 1034 | will use LANGUAGE by default. | 1043 | will use LANGUAGE by default. |
| 1035 | 1044 | ||
| @@ -1064,6 +1073,7 @@ name, it is ignored." | |||
| 1064 | ;; DEFAULT-LANGUAGE will be chosen when current-language is | 1073 | ;; DEFAULT-LANGUAGE will be chosen when current-language is |
| 1065 | ;; not set. | 1074 | ;; not set. |
| 1066 | default-language | 1075 | default-language |
| 1076 | current-reversed | ||
| 1067 | ;; The list this function returns. | 1077 | ;; The list this function returns. |
| 1068 | (result nil)) | 1078 | (result nil)) |
| 1069 | (while query-specs | 1079 | (while query-specs |
| @@ -1102,6 +1112,13 @@ name, it is ignored." | |||
| 1102 | `("Value of :feature should be a symbol" | 1112 | `("Value of :feature should be a symbol" |
| 1103 | ,var))) | 1113 | ,var))) |
| 1104 | (setq current-feature var))) | 1114 | (setq current-feature var))) |
| 1115 | (:reversed | ||
| 1116 | (let ((var (pop query-specs))) | ||
| 1117 | (when (not (memq var '(t nil))) | ||
| 1118 | (signal 'treesit-font-lock-error | ||
| 1119 | `("Value of :reversed can only be t or nil" | ||
| 1120 | ,var))) | ||
| 1121 | (setq current-reversed var))) | ||
| 1105 | ;; (2) Process query. | 1122 | ;; (2) Process query. |
| 1106 | ((pred treesit-query-p) | 1123 | ((pred treesit-query-p) |
| 1107 | (let ((lang (or default-language current-language))) | 1124 | (let ((lang (or default-language current-language))) |
| @@ -1116,12 +1133,14 @@ name, it is ignored." | |||
| 1116 | (push `(,(treesit-query-compile lang token) | 1133 | (push `(,(treesit-query-compile lang token) |
| 1117 | t | 1134 | t |
| 1118 | ,current-feature | 1135 | ,current-feature |
| 1119 | ,current-override) | 1136 | ,current-override |
| 1137 | ,current-reversed) | ||
| 1120 | result)) | 1138 | result)) |
| 1121 | ;; Clears any configurations set for this query. | 1139 | ;; Clears any configurations set for this query. |
| 1122 | (setq current-language nil | 1140 | (setq current-language nil |
| 1123 | current-override nil | 1141 | current-override nil |
| 1124 | current-feature nil))) | 1142 | current-feature nil |
| 1143 | current-reversed nil))) | ||
| 1125 | (_ (signal 'treesit-font-lock-error | 1144 | (_ (signal 'treesit-font-lock-error |
| 1126 | `("Unexpected value" ,token)))))) | 1145 | `("Unexpected value" ,token)))))) |
| 1127 | (nreverse result)))) | 1146 | (nreverse result)))) |
| @@ -1190,9 +1209,11 @@ and leave settings for other languages unchanged." | |||
| 1190 | (additive (or add-list remove-list))) | 1209 | (additive (or add-list remove-list))) |
| 1191 | (cl-loop for idx = 0 then (1+ idx) | 1210 | (cl-loop for idx = 0 then (1+ idx) |
| 1192 | for setting in treesit-font-lock-settings | 1211 | for setting in treesit-font-lock-settings |
| 1193 | for lang = (treesit-query-language (nth 0 setting)) | 1212 | for lang = (treesit-query-language |
| 1194 | for feature = (nth 2 setting) | 1213 | (treesit-font-lock-setting-query setting)) |
| 1195 | for current-value = (nth 1 setting) | 1214 | for feature = (treesit-font-lock-setting-feature setting) |
| 1215 | for current-value = (treesit-font-lock-setting-enable setting) | ||
| 1216 | for reversed = (treesit-font-lock-setting-reversed setting) | ||
| 1196 | ;; Set the ENABLE flag for the setting if its language is | 1217 | ;; Set the ENABLE flag for the setting if its language is |
| 1197 | ;; relevant. | 1218 | ;; relevant. |
| 1198 | if (or (null language) | 1219 | if (or (null language) |
| @@ -1200,7 +1221,9 @@ and leave settings for other languages unchanged." | |||
| 1200 | do (setf (nth 1 (nth idx treesit-font-lock-settings)) | 1221 | do (setf (nth 1 (nth idx treesit-font-lock-settings)) |
| 1201 | (cond | 1222 | (cond |
| 1202 | ((not additive) | 1223 | ((not additive) |
| 1203 | (if (memq feature features) t nil)) | 1224 | (if (not reversed) |
| 1225 | (if (memq feature features) t nil) | ||
| 1226 | (if (memq feature features) nil t))) | ||
| 1204 | ((memq feature add-list) t) | 1227 | ((memq feature add-list) t) |
| 1205 | ((memq feature remove-list) nil) | 1228 | ((memq feature remove-list) nil) |
| 1206 | (t current-value)))))) | 1229 | (t current-value)))))) |