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.
This commit is contained in:
parent
4bcdb1cc65
commit
ec00d292ec
4 changed files with 39 additions and 35 deletions
|
@ -360,12 +360,11 @@ For NODE, OVERRIDE, START, END, and ARGS, see
|
||||||
override start end args))
|
override start end args))
|
||||||
((or "identifier" "field_identifier")
|
((or "identifier" "field_identifier")
|
||||||
(treesit-fontify-with-override
|
(treesit-fontify-with-override
|
||||||
(max (treesit-node-start node) start)
|
(treesit-node-start node) (treesit-node-end node)
|
||||||
(min (treesit-node-end node) end)
|
|
||||||
(pcase (treesit-node-type (treesit-node-parent node))
|
(pcase (treesit-node-type (treesit-node-parent node))
|
||||||
("function_declarator" 'font-lock-function-name-face)
|
("function_declarator" 'font-lock-function-name-face)
|
||||||
(_ 'font-lock-variable-name-face))
|
(_ 'font-lock-variable-name-face))
|
||||||
override))))
|
override start end))))
|
||||||
|
|
||||||
(defun c-ts-mode--fontify-variable (node override start end &rest _)
|
(defun c-ts-mode--fontify-variable (node override start end &rest _)
|
||||||
"Fontify an identifier node.
|
"Fontify an identifier node.
|
||||||
|
@ -375,10 +374,8 @@ OVERRIDE, START, END, and ARGS, see `treesit-font-lock-rules'."
|
||||||
(treesit-node-parent node))
|
(treesit-node-parent node))
|
||||||
"call_expression"))
|
"call_expression"))
|
||||||
(treesit-fontify-with-override
|
(treesit-fontify-with-override
|
||||||
(max (treesit-node-start node) start)
|
(treesit-node-start node) (treesit-node-end node)
|
||||||
(min (treesit-node-end node) end)
|
'font-lock-variable-name-face override start end)))
|
||||||
'font-lock-variable-name-face
|
|
||||||
override)))
|
|
||||||
|
|
||||||
(defun c-ts-mode--fontify-defun (node override start end &rest _)
|
(defun c-ts-mode--fontify-defun (node override start end &rest _)
|
||||||
"Correctly fontify the DEFUN macro.
|
"Correctly fontify the DEFUN macro.
|
||||||
|
@ -405,21 +402,19 @@ This function corrects the fontification on the colon in
|
||||||
(when (equal (treesit-node-text node t) ":")
|
(when (equal (treesit-node-text node t) ":")
|
||||||
(treesit-fontify-with-override
|
(treesit-fontify-with-override
|
||||||
(treesit-node-start node) (treesit-node-end node)
|
(treesit-node-start node) (treesit-node-end node)
|
||||||
'default override)))
|
'default override start end)))
|
||||||
;; Fix the parameter list.
|
;; Fix the parameter list.
|
||||||
(while arg-list-2
|
(while arg-list-2
|
||||||
(let ((type (and arg-list-2 (pop arg-list-2)))
|
(let ((type (and arg-list-2 (pop arg-list-2)))
|
||||||
(arg (and arg-list-2 (pop arg-list-2))))
|
(arg (and arg-list-2 (pop arg-list-2))))
|
||||||
(when type
|
(when type
|
||||||
(treesit-fontify-with-override
|
(treesit-fontify-with-override
|
||||||
(max start (treesit-node-start type))
|
(treesit-node-start type) (treesit-node-end type)
|
||||||
(min end (treesit-node-end type))
|
'font-lock-type-face override start end))
|
||||||
'font-lock-type-face override))
|
|
||||||
(when arg
|
(when arg
|
||||||
(treesit-fontify-with-override
|
(treesit-fontify-with-override
|
||||||
(max start (treesit-node-start arg))
|
(treesit-node-start arg) (treesit-node-end arg)
|
||||||
(min end (treesit-node-end arg))
|
'default override start end))))))
|
||||||
'default override))))))
|
|
||||||
|
|
||||||
(defun c-ts-fontify-error (node override start end &rest _)
|
(defun c-ts-fontify-error (node override start end &rest _)
|
||||||
"Fontify the error nodes.
|
"Fontify the error nodes.
|
||||||
|
@ -428,8 +423,7 @@ For NODE, OVERRIDE, START, and END, see
|
||||||
(let ((parent (treesit-node-parent node))
|
(let ((parent (treesit-node-parent node))
|
||||||
(child (treesit-node-child node 0)))
|
(child (treesit-node-child node 0)))
|
||||||
(treesit-fontify-with-override
|
(treesit-fontify-with-override
|
||||||
(max start (treesit-node-start node))
|
(treesit-node-start node) (treesit-node-end node)
|
||||||
(min end (treesit-node-end node))
|
|
||||||
(cond
|
(cond
|
||||||
;; This matches the case MACRO(struct a, b, c)
|
;; This matches the case MACRO(struct a, b, c)
|
||||||
;; where struct is seen as error.
|
;; where struct is seen as error.
|
||||||
|
@ -439,7 +433,7 @@ For NODE, OVERRIDE, START, and END, see
|
||||||
'("struct" "long" "short" "enum" "union")))
|
'("struct" "long" "short" "enum" "union")))
|
||||||
'font-lock-keyword-face)
|
'font-lock-keyword-face)
|
||||||
(t 'font-lock-warning-face))
|
(t 'font-lock-warning-face))
|
||||||
override)))
|
override start end)))
|
||||||
|
|
||||||
(defun c-ts-mode--imenu-1 (node)
|
(defun c-ts-mode--imenu-1 (node)
|
||||||
"Helper for `c-ts-mode--imenu'.
|
"Helper for `c-ts-mode--imenu'.
|
||||||
|
|
|
@ -3647,7 +3647,7 @@ OVERRIDE is the override flag described in
|
||||||
(setq font-beg (max start font-beg))
|
(setq font-beg (max start font-beg))
|
||||||
(when (< font-beg end)
|
(when (< font-beg end)
|
||||||
(treesit-fontify-with-override
|
(treesit-fontify-with-override
|
||||||
font-beg font-end 'font-lock-string-face override)))
|
font-beg font-end 'font-lock-string-face override start end)))
|
||||||
(setq font-beg (treesit-node-end child)
|
(setq font-beg (treesit-node-end child)
|
||||||
child (treesit-node-next-sibling child)))))
|
child (treesit-node-next-sibling child)))))
|
||||||
|
|
||||||
|
|
|
@ -1069,7 +1069,7 @@ fontified."
|
||||||
(when (eq (char-after string-beg) ?f)
|
(when (eq (char-after string-beg) ?f)
|
||||||
(cl-incf string-beg))
|
(cl-incf string-beg))
|
||||||
(treesit-fontify-with-override
|
(treesit-fontify-with-override
|
||||||
(max start string-beg) (min end string-end) face override)))
|
string-beg string-end face override start end)))
|
||||||
|
|
||||||
(defvar python--treesit-settings
|
(defvar python--treesit-settings
|
||||||
(treesit-font-lock-rules
|
(treesit-font-lock-rules
|
||||||
|
|
|
@ -774,25 +774,35 @@ signals the `treesit-font-lock-error' error if that happens."
|
||||||
((memq feature remove-list) nil)
|
((memq feature remove-list) nil)
|
||||||
(t current-value))))))
|
(t current-value))))))
|
||||||
|
|
||||||
(defun treesit-fontify-with-override (start end face override)
|
(defun treesit-fontify-with-override
|
||||||
|
(start end face override &optional bound-start bound-end)
|
||||||
"Apply FACE to the region between START and END.
|
"Apply FACE to the region between START and END.
|
||||||
OVERRIDE can be nil, t, `append', `prepend', or `keep'.
|
OVERRIDE can be nil, t, `append', `prepend', or `keep'.
|
||||||
See `treesit-font-lock-rules' for their semantic."
|
See `treesit-font-lock-rules' for their semantic.
|
||||||
(pcase override
|
|
||||||
('nil (unless (text-property-not-all
|
If BOUND-START and BOUND-END are non-nil, only fontify the region
|
||||||
start end 'face nil)
|
in between them."
|
||||||
(put-text-property start end 'face face)))
|
(when (or (null bound-start) (null bound-end)
|
||||||
('t (put-text-property start end 'face face))
|
(and bound-start bound-end
|
||||||
('append (font-lock-append-text-property
|
(<= bound-start end)
|
||||||
|
(>= bound-end start)))
|
||||||
|
(when (and bound-start bound-end)
|
||||||
|
(setq start (max bound-start start)
|
||||||
|
end (min bound-end end)))
|
||||||
|
(pcase override
|
||||||
|
('nil (unless (text-property-not-all start end 'face nil)
|
||||||
|
(put-text-property start end 'face face)))
|
||||||
|
('t (put-text-property start end 'face face))
|
||||||
|
('append (font-lock-append-text-property
|
||||||
|
start end 'face face))
|
||||||
|
('prepend (font-lock-prepend-text-property
|
||||||
|
start end 'face face))
|
||||||
|
('keep (font-lock-fillin-text-property
|
||||||
start end 'face face))
|
start end 'face face))
|
||||||
('prepend (font-lock-prepend-text-property
|
(_ (signal 'treesit-font-lock-error
|
||||||
start end 'face face))
|
(list
|
||||||
('keep (font-lock-fillin-text-property
|
"Unrecognized value of :override option"
|
||||||
start end 'face face))
|
override))))))
|
||||||
(_ (signal 'treesit-font-lock-error
|
|
||||||
(list
|
|
||||||
"Unrecognized value of :override option"
|
|
||||||
override)))))
|
|
||||||
|
|
||||||
(defun treesit--set-nonsticky (start end sym &optional remove)
|
(defun treesit--set-nonsticky (start end sym &optional remove)
|
||||||
"Set `rear-nonsticky' property between START and END.
|
"Set `rear-nonsticky' property between START and END.
|
||||||
|
|
Loading…
Add table
Reference in a new issue