(yas--all-parents): Fix stale cache issue #1180

* yasnippet.el (yas--all-parents): Record children in `yas--all-children`.
(yas--define-parents): Use it to flush the caches of the affected modes.
This commit is contained in:
Stefan Monnier 2024-01-08 11:42:46 -05:00
parent ae36504a5f
commit d7f55c7501

View File

@ -819,28 +819,31 @@ which decides on the snippet to expand.")
(or (get mode 'yas--all-parents) ;; FIXME: Use `with-memoization'? (or (get mode 'yas--all-parents) ;; FIXME: Use `with-memoization'?
(progn (progn
(put mode 'yas--all-parents (list mode)) ;; Stop inf-loop with cycles. (put mode 'yas--all-parents (list mode)) ;; Stop inf-loop with cycles.
(put mode 'yas--all-parents (let ((all-parents
(if (fboundp 'derived-mode-all-parents) (if (fboundp 'derived-mode-all-parents)
(let* ((ap (derived-mode-all-parents mode)) (let* ((ap (derived-mode-all-parents mode))
(extras (extras
(mapcar (lambda (parent) (mapcar (lambda (parent)
(yas--merge-ordered-lists (yas--merge-ordered-lists
(mapcar #'yas--all-parents (mapcar #'yas--all-parents
(gethash parent yas--parents)))) (gethash parent yas--parents))))
ap))) ap)))
(yas--merge-ordered-lists
(cons (append ap '(fundamental-mode)) extras)))
(cons mode
(yas--merge-ordered-lists (yas--merge-ordered-lists
(mapcar #'yas--all-parents (cons (append ap '(fundamental-mode)) extras)))
(remq nil (cons mode
`(,(or (get mode 'derived-mode-parent) (yas--merge-ordered-lists
;; Consider `fundamental-mode' (mapcar #'yas--all-parents
;; as ultimate ancestor. (remq nil
'fundamental-mode) `(,(or (get mode 'derived-mode-parent)
,(let ((alias (symbol-function mode))) ;; Consider `fundamental-mode'
(when (symbolp alias) alias)) ;; as ultimate ancestor.
,@(gethash mode yas--parents))))))))))) 'fundamental-mode)
,(let ((alias (symbol-function mode)))
(when (symbolp alias) alias))
,@(gethash mode yas--parents)))))))))
(dolist (parent all-parents)
(cl-pushnew mode (get parent 'yas--all-children)))
(put mode 'yas--all-parents all-parents)))))
(defun yas--modes-to-activate (&optional mode) (defun yas--modes-to-activate (&optional mode)
"Compute list of mode symbols that are active for `yas-expand' and friends." "Compute list of mode symbols that are active for `yas-expand' and friends."
@ -1854,6 +1857,8 @@ the current buffers contents."
(defun yas--define-parents (mode parents) (defun yas--define-parents (mode parents)
"Add PARENTS to the list of MODE's parents." "Add PARENTS to the list of MODE's parents."
(dolist (child (get mode 'yas--all-children))
(put child 'yas--all-parents nil)) ;Flush the cache for all children.
(puthash mode (cl-remove-duplicates (puthash mode (cl-remove-duplicates
(append parents (append parents
(gethash mode yas--parents))) (gethash mode yas--parents)))