diff options
| author | Yuan Fu | 2022-12-04 00:22:28 -0800 |
|---|---|---|
| committer | Yuan Fu | 2022-12-04 20:03:28 -0800 |
| commit | ec00d292ec02f34a0d879767c6737fadbe24ce20 (patch) | |
| tree | 1d7ddbf47b81fbfab3f846f7f230e3960653e148 | |
| parent | 4bcdb1cc65bf779b6479f99a7aa767ab83b3bae1 (diff) | |
| download | emacs-ec00d292ec02f34a0d879767c6737fadbe24ce20.tar.gz emacs-ec00d292ec02f34a0d879767c6737fadbe24ce20.zip | |
Improve treesit-fontify-with-override
This also fixes fontification problem with c-ts-mode--fontify-defun.
Now treesit-fontify-with-override clips the fontification region for
the user, so no need for (max start node-start) shenanigans anymore.
More importantly it doesn't fontify unless the region between
node-start and node-end intersects with the region between start and
end, which fixes the problem with c-ts-mode--fontify-defun.
* lisp/treesit.el (treesit-fontify-with-override): Add optional
parameter BOUND-START and BOUND-END. Wrap the function body in a
when-form.
* lisp/progmodes/c-ts-mode.el (c-ts-mode--fontify-declarator)
(c-ts-mode--fontify-variable)
(c-ts-mode--fontify-defun)
(c-ts-fontify-error)
* lisp/progmodes/js.el (js--fontify-template-string)
* lisp/progmodes/python.el (python--treesit-fontify-string): Use the
new signature.
| -rw-r--r-- | lisp/progmodes/c-ts-mode.el | 28 | ||||
| -rw-r--r-- | lisp/progmodes/js.el | 2 | ||||
| -rw-r--r-- | lisp/progmodes/python.el | 2 | ||||
| -rw-r--r-- | lisp/treesit.el | 42 |
4 files changed, 39 insertions, 35 deletions
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index a8189a0f3da..1bd093cfa2d 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el | |||
| @@ -360,12 +360,11 @@ For NODE, OVERRIDE, START, END, and ARGS, see | |||
| 360 | override start end args)) | 360 | override start end args)) |
| 361 | ((or "identifier" "field_identifier") | 361 | ((or "identifier" "field_identifier") |
| 362 | (treesit-fontify-with-override | 362 | (treesit-fontify-with-override |
| 363 | (max (treesit-node-start node) start) | 363 | (treesit-node-start node) (treesit-node-end node) |
| 364 | (min (treesit-node-end node) end) | ||
| 365 | (pcase (treesit-node-type (treesit-node-parent node)) | 364 | (pcase (treesit-node-type (treesit-node-parent node)) |
| 366 | ("function_declarator" 'font-lock-function-name-face) | 365 | ("function_declarator" 'font-lock-function-name-face) |
| 367 | (_ 'font-lock-variable-name-face)) | 366 | (_ 'font-lock-variable-name-face)) |
| 368 | override)))) | 367 | override start end)))) |
| 369 | 368 | ||
| 370 | (defun c-ts-mode--fontify-variable (node override start end &rest _) | 369 | (defun c-ts-mode--fontify-variable (node override start end &rest _) |
| 371 | "Fontify an identifier node. | 370 | "Fontify an identifier node. |
| @@ -375,10 +374,8 @@ OVERRIDE, START, END, and ARGS, see `treesit-font-lock-rules'." | |||
| 375 | (treesit-node-parent node)) | 374 | (treesit-node-parent node)) |
| 376 | "call_expression")) | 375 | "call_expression")) |
| 377 | (treesit-fontify-with-override | 376 | (treesit-fontify-with-override |
| 378 | (max (treesit-node-start node) start) | 377 | (treesit-node-start node) (treesit-node-end node) |
| 379 | (min (treesit-node-end node) end) | 378 | 'font-lock-variable-name-face override start end))) |
| 380 | 'font-lock-variable-name-face | ||
| 381 | override))) | ||
| 382 | 379 | ||
| 383 | (defun c-ts-mode--fontify-defun (node override start end &rest _) | 380 | (defun c-ts-mode--fontify-defun (node override start end &rest _) |
| 384 | "Correctly fontify the DEFUN macro. | 381 | "Correctly fontify the DEFUN macro. |
| @@ -405,21 +402,19 @@ This function corrects the fontification on the colon in | |||
| 405 | (when (equal (treesit-node-text node t) ":") | 402 | (when (equal (treesit-node-text node t) ":") |
| 406 | (treesit-fontify-with-override | 403 | (treesit-fontify-with-override |
| 407 | (treesit-node-start node) (treesit-node-end node) | 404 | (treesit-node-start node) (treesit-node-end node) |
| 408 | 'default override))) | 405 | 'default override start end))) |
| 409 | ;; Fix the parameter list. | 406 | ;; Fix the parameter list. |
| 410 | (while arg-list-2 | 407 | (while arg-list-2 |
| 411 | (let ((type (and arg-list-2 (pop arg-list-2))) | 408 | (let ((type (and arg-list-2 (pop arg-list-2))) |
| 412 | (arg (and arg-list-2 (pop arg-list-2)))) | 409 | (arg (and arg-list-2 (pop arg-list-2)))) |
| 413 | (when type | 410 | (when type |
| 414 | (treesit-fontify-with-override | 411 | (treesit-fontify-with-override |
| 415 | (max start (treesit-node-start type)) | 412 | (treesit-node-start type) (treesit-node-end type) |
| 416 | (min end (treesit-node-end type)) | 413 | 'font-lock-type-face override start end)) |
| 417 | 'font-lock-type-face override)) | ||
| 418 | (when arg | 414 | (when arg |
| 419 | (treesit-fontify-with-override | 415 | (treesit-fontify-with-override |
| 420 | (max start (treesit-node-start arg)) | 416 | (treesit-node-start arg) (treesit-node-end arg) |
| 421 | (min end (treesit-node-end arg)) | 417 | 'default override start end)))))) |
| 422 | 'default override)))))) | ||
| 423 | 418 | ||
| 424 | (defun c-ts-fontify-error (node override start end &rest _) | 419 | (defun c-ts-fontify-error (node override start end &rest _) |
| 425 | "Fontify the error nodes. | 420 | "Fontify the error nodes. |
| @@ -428,8 +423,7 @@ For NODE, OVERRIDE, START, and END, see | |||
| 428 | (let ((parent (treesit-node-parent node)) | 423 | (let ((parent (treesit-node-parent node)) |
| 429 | (child (treesit-node-child node 0))) | 424 | (child (treesit-node-child node 0))) |
| 430 | (treesit-fontify-with-override | 425 | (treesit-fontify-with-override |
| 431 | (max start (treesit-node-start node)) | 426 | (treesit-node-start node) (treesit-node-end node) |
| 432 | (min end (treesit-node-end node)) | ||
| 433 | (cond | 427 | (cond |
| 434 | ;; This matches the case MACRO(struct a, b, c) | 428 | ;; This matches the case MACRO(struct a, b, c) |
| 435 | ;; where struct is seen as error. | 429 | ;; where struct is seen as error. |
| @@ -439,7 +433,7 @@ For NODE, OVERRIDE, START, and END, see | |||
| 439 | '("struct" "long" "short" "enum" "union"))) | 433 | '("struct" "long" "short" "enum" "union"))) |
| 440 | 'font-lock-keyword-face) | 434 | 'font-lock-keyword-face) |
| 441 | (t 'font-lock-warning-face)) | 435 | (t 'font-lock-warning-face)) |
| 442 | override))) | 436 | override start end))) |
| 443 | 437 | ||
| 444 | (defun c-ts-mode--imenu-1 (node) | 438 | (defun c-ts-mode--imenu-1 (node) |
| 445 | "Helper for `c-ts-mode--imenu'. | 439 | "Helper for `c-ts-mode--imenu'. |
diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index 389096147ac..90ab7cc924b 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el | |||
| @@ -3647,7 +3647,7 @@ OVERRIDE is the override flag described in | |||
| 3647 | (setq font-beg (max start font-beg)) | 3647 | (setq font-beg (max start font-beg)) |
| 3648 | (when (< font-beg end) | 3648 | (when (< font-beg end) |
| 3649 | (treesit-fontify-with-override | 3649 | (treesit-fontify-with-override |
| 3650 | font-beg font-end 'font-lock-string-face override))) | 3650 | font-beg font-end 'font-lock-string-face override start end))) |
| 3651 | (setq font-beg (treesit-node-end child) | 3651 | (setq font-beg (treesit-node-end child) |
| 3652 | child (treesit-node-next-sibling child))))) | 3652 | child (treesit-node-next-sibling child))))) |
| 3653 | 3653 | ||
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 4fc5d24e2fb..ebee703499a 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -1069,7 +1069,7 @@ fontified." | |||
| 1069 | (when (eq (char-after string-beg) ?f) | 1069 | (when (eq (char-after string-beg) ?f) |
| 1070 | (cl-incf string-beg)) | 1070 | (cl-incf string-beg)) |
| 1071 | (treesit-fontify-with-override | 1071 | (treesit-fontify-with-override |
| 1072 | (max start string-beg) (min end string-end) face override))) | 1072 | string-beg string-end face override start end))) |
| 1073 | 1073 | ||
| 1074 | (defvar python--treesit-settings | 1074 | (defvar python--treesit-settings |
| 1075 | (treesit-font-lock-rules | 1075 | (treesit-font-lock-rules |
diff --git a/lisp/treesit.el b/lisp/treesit.el index f3c03daf7e0..eee6eee0c7f 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el | |||
| @@ -774,25 +774,35 @@ signals the `treesit-font-lock-error' error if that happens." | |||
| 774 | ((memq feature remove-list) nil) | 774 | ((memq feature remove-list) nil) |
| 775 | (t current-value)))))) | 775 | (t current-value)))))) |
| 776 | 776 | ||
| 777 | (defun treesit-fontify-with-override (start end face override) | 777 | (defun treesit-fontify-with-override |
| 778 | (start end face override &optional bound-start bound-end) | ||
| 778 | "Apply FACE to the region between START and END. | 779 | "Apply FACE to the region between START and END. |
| 779 | OVERRIDE can be nil, t, `append', `prepend', or `keep'. | 780 | OVERRIDE can be nil, t, `append', `prepend', or `keep'. |
| 780 | See `treesit-font-lock-rules' for their semantic." | 781 | See `treesit-font-lock-rules' for their semantic. |
| 781 | (pcase override | 782 | |
| 782 | ('nil (unless (text-property-not-all | 783 | If BOUND-START and BOUND-END are non-nil, only fontify the region |
| 783 | start end 'face nil) | 784 | in between them." |
| 784 | (put-text-property start end 'face face))) | 785 | (when (or (null bound-start) (null bound-end) |
| 785 | ('t (put-text-property start end 'face face)) | 786 | (and bound-start bound-end |
| 786 | ('append (font-lock-append-text-property | 787 | (<= bound-start end) |
| 788 | (>= bound-end start))) | ||
| 789 | (when (and bound-start bound-end) | ||
| 790 | (setq start (max bound-start start) | ||
| 791 | end (min bound-end end))) | ||
| 792 | (pcase override | ||
| 793 | ('nil (unless (text-property-not-all start end 'face nil) | ||
| 794 | (put-text-property start end 'face face))) | ||
| 795 | ('t (put-text-property start end 'face face)) | ||
| 796 | ('append (font-lock-append-text-property | ||
| 797 | start end 'face face)) | ||
| 798 | ('prepend (font-lock-prepend-text-property | ||
| 799 | start end 'face face)) | ||
| 800 | ('keep (font-lock-fillin-text-property | ||
| 787 | start end 'face face)) | 801 | start end 'face face)) |
| 788 | ('prepend (font-lock-prepend-text-property | 802 | (_ (signal 'treesit-font-lock-error |
| 789 | start end 'face face)) | 803 | (list |
| 790 | ('keep (font-lock-fillin-text-property | 804 | "Unrecognized value of :override option" |
| 791 | start end 'face face)) | 805 | override)))))) |
| 792 | (_ (signal 'treesit-font-lock-error | ||
| 793 | (list | ||
| 794 | "Unrecognized value of :override option" | ||
| 795 | override))))) | ||
| 796 | 806 | ||
| 797 | (defun treesit--set-nonsticky (start end sym &optional remove) | 807 | (defun treesit--set-nonsticky (start end sym &optional remove) |
| 798 | "Set `rear-nonsticky' property between START and END. | 808 | "Set `rear-nonsticky' property between START and END. |