From debbugs-submit-bounces@debbugs.gnu.org Thu Jan 16 12:49:19 2025 Received: (at submit) by debbugs.gnu.org; 16 Jan 2025 17:49:19 +0000 Received: from localhost ([127.0.0.1]:34556 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tYTza-00010O-LQ for submit@debbugs.gnu.org; Thu, 16 Jan 2025 12:49:19 -0500 Received: from lists.gnu.org ([2001:470:142::17]:50564) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1tYTzX-000105-TT for submit@debbugs.gnu.org; Thu, 16 Jan 2025 12:49:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tYTzP-0005Pc-ID for bug-gnu-emacs@gnu.org; Thu, 16 Jan 2025 12:49:07 -0500 Received: from relay3-d.mail.gandi.net ([217.70.183.195]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tYTzM-0004jL-1b for bug-gnu-emacs@gnu.org; Thu, 16 Jan 2025 12:49:06 -0500 Received: by mail.gandi.net (Postfix) with ESMTPSA id 1A2A86000A for ; Thu, 16 Jan 2025 17:48:58 +0000 (UTC) From: Juri Linkov To: bug-gnu-emacs@gnu.org Subject: Hideshow support for treesitter Organization: LINKOV.NET X-Debbugs-Cc: Yuan Fu Date: Thu, 16 Jan 2025 19:45:11 +0200 Message-ID: <87tt9yu00k.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-GND-Sasl: juri@linkov.net Received-SPF: pass client-ip=217.70.183.195; envelope-from=juri@linkov.net; helo=relay3-d.mail.gandi.net X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: 0.7 (/) X-Debbugs-Envelope-To: submit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.3 (/) --=-=-= Content-Type: text/plain Tags: patch Now that we have the new list thing in tree-sitter, it became possible to implement the hideshow support to hide the list things, i.e. exactly what hs-minor-mode did in non-ts modes: --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=treesit-hideshow.patch diff --git a/lisp/treesit.el b/lisp/treesit.el index ac34edaf84d..0e48fb91b44 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -3420,6 +3420,65 @@ treesit-outline-level (setq level (1+ level))) (if (zerop level) 1 level))) +;;; Hideshow mode + +(defun treesit-hs-block-end () + (let* ((pred 'list) + (thing (treesit-thing-at + (if (bobp) (point) (1- (point))) pred)) + (end (when thing (treesit-node-end thing))) + (last (when thing (treesit-node-child thing -1))) + (beg (if last (treesit-node-start last) + (if (bobp) (point) (1- (point)))))) + (when (and thing (eq (point) end)) + (set-match-data (list beg end)) + t))) + +(defun treesit-hs-find-block-beginning () + (let* ((pred 'list) + (thing (treesit-thing-at (point) pred)) + (thing (or thing (treesit-parent-until (treesit-node-at (point)) pred))) + (beg (when thing (treesit-node-start thing))) + (end (when thing (treesit-node-end thing)))) + (when thing + (goto-char beg) + (set-match-data (list beg end)) + t))) + +(defun treesit-hs-find-next-block (_regexp _maxp comments) + (let* ((pred (if comments '(or list "comment") 'list)) + ;; `treesit-navigate-thing' can't find a thing at bobp, + ;; so use `treesit-thing-at' to match at bobp. + (current (treesit-thing-at (point) pred)) + (beg (or (and current (eq (point) (treesit-node-start current)) (point)) + (treesit-navigate-thing (point) 1 'beg pred))) + (thing (when beg (treesit-thing-at beg pred))) + (end (when thing (treesit-node-end thing)))) + (when thing + (goto-char end) + (set-match-data + (if (and comments (equal (treesit-node-type thing) "comment")) + (list beg end nil nil beg end) + (list beg end beg end))) + t))) + +(defun treesit-hs-looking-at-block-start-p () + (let* ((pred 'list) + (thing (treesit-thing-at (point) pred)) + (beg (when thing (treesit-node-start thing))) + (first (when thing (treesit-node-child thing 0))) + (end (if first (treesit-node-end first) (1+ (point))))) + (when (and thing (eq (point) beg)) + (set-match-data (list beg end)) + t))) + +(defun treesit-hs-inside-comment-p () + (let ((thing (or (treesit-thing-at (point) "comment") + (unless (bobp) + (treesit-thing-at (1- (point)) "comment"))))) + (when thing + (list (treesit-node-start thing) (treesit-node-end thing))))) + ;;; Show paren mode (defun treesit-show-paren-data--categorize (pos &optional end-p) @@ -3603,7 +3662,17 @@ treesit-major-mode-setup (setq-local forward-list-function #'treesit-forward-list) (setq-local down-list-function #'treesit-down-list) (setq-local up-list-function #'treesit-up-list) - (setq-local show-paren-data-function 'treesit-show-paren-data)) + (setq-local show-paren-data-function #'treesit-show-paren-data) + (setq hs-block-start-regexp nil + hs-block-start-mdata-select 0 + hs-block-end-regexp #'treesit-hs-block-end + hs-c-start-regexp nil + hs-forward-sexp-func #'forward-list + hs-adjust-block-beginning nil + hs-find-block-beginning-func #'treesit-hs-find-block-beginning + hs-find-next-block-func #'treesit-hs-find-next-block + hs-looking-at-block-start-p-func #'treesit-hs-looking-at-block-start-p + hs-inside-comment-p-func #'treesit-hs-inside-comment-p)) (when (treesit-thing-defined-p 'sentence nil) (setq-local forward-sentence-function #'treesit-forward-sentence)) diff --git a/lisp/progmodes/hideshow.el b/lisp/progmodes/hideshow.el index 823eb0527c6..ea7bc738a4d 100644 --- a/lisp/progmodes/hideshow.el +++ b/lisp/progmodes/hideshow.el @@ -259,14 +259,11 @@ hs-special-modes-alist ;; to the mode hierarchy. (mapcar #'purecopy '((c-mode "{" "}" "/[*/]" nil nil) - (c-ts-mode "{" "}" "/[*/]" nil nil) (c++-mode "{" "}" "/[*/]" nil nil) - (c++-ts-mode "{" "}" "/[*/]" nil nil) (bibtex-mode ("@\\S(*\\(\\s(\\)" 1)) (java-mode "{" "}" "/[*/]" nil nil) (java-ts-mode "{" "}" "/[*/]" nil nil) (js-mode "{" "}" "/[*/]" nil) - (js-ts-mode "{" "}" "/[*/]" nil) (lua-ts-mode "{\\|\\[\\[" "}\\|\\]\\]" "--" nil) (mhtml-mode "{\\|<[^/>]*?" "}\\|]*[^/]>" "