From 4281a46942bec7bdb778ba93f1965f0e26c43652 Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Wed, 16 Apr 2025 20:11:34 +0300 Subject: [PATCH] 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". (cherry picked from commit 7574eb82c5cd150a0706a81e9f8555962a1e2167) --- lisp/outline.el | 6 +++++- lisp/progmodes/rust-ts-mode.el | 10 ++++++++++ lisp/treesit.el | 13 +++---------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lisp/outline.el b/lisp/outline.el index d899d1df667..6cfdf1c191e 100644 --- a/lisp/outline.el +++ b/lisp/outline.el @@ -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))) diff --git a/lisp/progmodes/rust-ts-mode.el b/lisp/progmodes/rust-ts-mode.el index f756ba0c2a1..83f20c38574 100644 --- a/lisp/progmodes/rust-ts-mode.el +++ b/lisp/progmodes/rust-ts-mode.el @@ -576,6 +576,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) diff --git a/lisp/treesit.el b/lisp/treesit.el index 80304e145de..d09ecd8c618 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -4105,7 +4105,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 @@ -4403,15 +4404,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))) -- 2.39.5