New hook 'outline-after-change-functions' (bug#77256).

* lisp/outline.el (outline-after-change-functions): New variable.
(outline--fix-buttons-after-change):
Run hook 'outline-after-change-functions'.

* lisp/treesit.el (treesit--after-change): Improve docstring.
(treesit-major-mode-setup): Simplify to just add
'treesit--after-change' to 'outline-after-change-functions' hook.

* lisp/progmodes/rust-ts-mode.el (rust-ts-mode): Add explicit
'treesit-outline-predicate' that is like
'treesit-simple-imenu-settings', but also adds "trait_item".
This commit is contained in:
Juri Linkov 2025-04-16 20:11:34 +03:00
parent 77bf9d33ee
commit 7574eb82c5
3 changed files with 18 additions and 11 deletions

View file

@ -1980,7 +1980,11 @@ With a prefix argument, show headings up to that LEVEL."
(outline--insert-button (if close-p 'close 'open))))
(or from (point-min)) (or to (point-max)))))
(defun outline--fix-buttons-after-change (beg end _len)
(defvar outline-after-change-functions nil
"List of functions to call after each text change in outline-mode.")
(defun outline--fix-buttons-after-change (beg end len)
(run-hook-with-args 'outline-after-change-functions beg end len)
;; Handle whole lines
(save-excursion (goto-char beg) (setq beg (pos-bol)))
(save-excursion (goto-char end) (setq end (pos-eol)))

View file

@ -578,6 +578,16 @@ See `prettify-symbols-compose-predicate'."
("Struct" "\\`struct_item\\'" nil nil)
("Fn" "\\`function_item\\'" nil nil)))
;; Outline.
(setq-local treesit-outline-predicate
(rx bos (or "mod_item"
"enum_item"
"impl_item"
"type_item"
"struct_item"
"function_item"
"trait_item")
eos))
;; Indent.
(setq-local indent-tabs-mode nil
treesit-simple-indent-rules rust-ts-mode--indent-rules)

View file

@ -4112,7 +4112,8 @@ For BOUND, MOVE, BACKWARD, LOOKING-AT, see the descriptions in
level))
(defun treesit--after-change (beg end _len)
"Force updating the ranges after each text change."
"Force updating the ranges in BEG...END.
Expected to be called after each text change."
(treesit-update-ranges beg end))
;;; Hideshow mode
@ -4410,15 +4411,7 @@ before calling this function."
#'treesit-outline-predicate--from-imenu))
(setq-local outline-search-function #'treesit-outline-search
outline-level #'treesit-outline-level)
(add-hook 'outline-minor-mode-hook
(lambda ()
(if (bound-and-true-p outline-minor-mode)
(add-hook 'after-change-functions
#'treesit--after-change
0 t)
(remove-hook 'after-change-functions
#'treesit--after-change t)))
nil t))
(add-hook 'outline-after-change-functions #'treesit--after-change nil t))
;; Remove existing local parsers.
(dolist (ov (overlays-in (point-min) (point-max)))