diff --git a/lisp/treesit.el b/lisp/treesit.el index 2616d16e800..a62f63d0d6c 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -3331,6 +3333,34 @@ treesit-outline-level (setq level (1+ level))) (if (zerop level) 1 level))) +;;; Show paren mode + +(defun treesit-show-paren-data () + "A function suitable for `show-paren-data-function' (which see)." + (let* ((node (treesit-thing-at (point) 'sexp-list)) + (beg (when node (treesit-node-start node))) + (end (when node (treesit-node-end node)))) + ;; When after the closing node, try the previous position inside the node + (unless (or (eq (point) beg) (eq (point) end) (bobp)) + (setq node (treesit-thing-at (1- (point)) 'sexp-list) + beg (when node (treesit-node-start node)) + end (when node (treesit-node-end node)))) + (when (or (eq (point) beg) (eq (point) end)) + (let ((beg-node (treesit-node-child node 0)) + (end-node (treesit-node-child node -1))) + (when (and beg-node end-node (not (eq beg-node end-node))) + (if (eq beg (point)) + (list (treesit-node-start beg-node) + (treesit-node-end beg-node) + (treesit-node-start end-node) + (treesit-node-end end-node) + nil) + (list (treesit-node-start end-node) + (treesit-node-end end-node) + (treesit-node-start beg-node) + (treesit-node-end beg-node) + nil))))))) + ;;; Activating tree-sitter (defun treesit-ready-p (language &optional quiet) @@ -3486,6 +3516,8 @@ treesit-major-mode-setup (setq-local outline-search-function #'treesit-outline-search outline-level #'treesit-outline-level)) + (setq-local show-paren-data-function 'treesit-show-paren-data) + ;; Remove existing local parsers. (dolist (ov (overlays-in (point-min) (point-max))) (when-let* ((parser (overlay-get ov 'treesit-parser)))