From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.bugs Subject: bug#68824: treesitter support for outline-minor-mode Date: Wed, 31 Jan 2024 09:32:46 +0200 Organization: LINKOV.NET Message-ID: <86y1c6q7oh.fsf@mail.linkov.net> References: <86le8667u7.fsf@mail.linkov.net> <865xza39vp.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="30073"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) Cc: casouri@gmail.com, 68824@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Jan 31 08:37:25 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rV59x-0007cF-1r for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 31 Jan 2024 08:37:25 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rV59c-0003L0-43; Wed, 31 Jan 2024 02:37:04 -0500 Original-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 1rV59R-0003KE-I8 for bug-gnu-emacs@gnu.org; Wed, 31 Jan 2024 02:36:53 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rV59Q-0000ix-V2 for bug-gnu-emacs@gnu.org; Wed, 31 Jan 2024 02:36:53 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rV59a-0006EL-93 for bug-gnu-emacs@gnu.org; Wed, 31 Jan 2024 02:37:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 31 Jan 2024 07:37:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 68824 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 68824-submit@debbugs.gnu.org id=B68824.170668658923900 (code B ref 68824); Wed, 31 Jan 2024 07:37:02 +0000 Original-Received: (at 68824) by debbugs.gnu.org; 31 Jan 2024 07:36:29 +0000 Original-Received: from localhost ([127.0.0.1]:37364 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rV592-0006DP-6g for submit@debbugs.gnu.org; Wed, 31 Jan 2024 02:36:28 -0500 Original-Received: from relay4-d.mail.gandi.net ([2001:4b98:dc4:8::224]:41599) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rV58y-0006Cw-MA for 68824@debbugs.gnu.org; Wed, 31 Jan 2024 02:36:25 -0500 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id A37BAE0002; Wed, 31 Jan 2024 07:36:06 +0000 (UTC) In-Reply-To: <865xza39vp.fsf@gnu.org> (Eli Zaretskii's message of "Tue, 30 Jan 2024 21:21:30 +0200") X-GND-Sasl: juri@linkov.net X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:279212 Archived-At: --=-=-= Content-Type: text/plain >> As discussed on https://lists.gnu.org/archive/html/emacs-devel/2024-01/msg00916.html >> here is the patch that adds the support for outline-minor-mode to treesit.el. >> It has been tested on c-ts-mode, dockerfile-ts-mode, elixir-ts-mode, heex-ts-mode, >> java-ts-mode, js-ts-mode, typescript-ts-mode, css-ts-mode, html-ts-mode, toml-ts-mode. > > Thanks, but shouldn't this minor mode be documented more prominently > in the Emacs user manual, now that there are so many major modes that > support it? And since major modes that want to support it have to set > up its support, I would say the ELisp manual should document that > setup. > > IOW, it makes little sense to make this minor mode more and more > prolific in Emacs, and still have almost nothing about it in our > manuals. I agree. Also could add corresponding menu item to the global menu-bar, or at least to menus of major modes deriving from prog-mode/text-mode. >> +(defvar-local treesit-outline-predicate nil >> + "Predicate used to find outline headings in a sparse tree. > > How do "sparse trees" enter the scene here? AFAICS, "sparse trees" is > mentioned only once in the entire chapter dedicated to Tree Sitter > support in the ELisp manual. Is it important to mention here the fact > that the tree is sparse? Actually I realized now there is no need to use sparse trees here. Below is a new implementation that directly traverses the syntax tree. This implementation is much better than sparse trees because sparse trees can be created only once when the file is visited, but when the buffer is modified it takes too much time to regenerate sparse trees completely after every change. So a new implementation below uses the search on the syntax tree that is much faster to do after every buffer modification. >> diff --git a/lisp/progmodes/heex-ts-mode.el b/lisp/progmodes/heex-ts-mode.el >> + ;; Outline minor mode >> + ;; Restore default value for `treesit-outline-search'. >> + (kill-local-variable 'outline-regexp) >> + (kill-local-variable 'outline-heading-end-regexp) >> + (kill-local-variable 'outline-level) > > This mode seems to do something to support outline-minor-mode that is > not mentioned in the NEWS entry, and neither is treesit-outline-search. This just disables outline settings inherited from html-mode. I will add more explanations. Here is a new patch with a shorter implementation. And I will send a complete patch with documentation changes later. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=treesit-outline-search.patch diff --git a/lisp/treesit.el b/lisp/treesit.el index 96222ed81cb..72c994c257c 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -2836,6 +2836,48 @@ treesit-simple-imenu index)))) treesit-simple-imenu-settings))) +;;; Outline minor mode + +(defvar-local treesit-outline-predicate nil + "Predicate used to find outline headings in the syntax tree. +Intended to be set by a major mode. When nil, the predicate +is constructed from the value of `treesit-simple-imenu-settings' +when a major mode sets it.") + +(defun treesit-outline-search (&optional bound move backward looking-at) + "Search for the next outline heading in the syntax tree. +See the descriptions of arguments in `outline-search-function'." + (if looking-at + (treesit-parent-until (treesit-node-at (pos-bol)) treesit-outline-predicate) + (let* ((current (treesit-node-at (pos-bol))) + (current (or (treesit-parent-until current treesit-outline-predicate) + current)) + (node (treesit-search-forward + current treesit-outline-predicate backward)) + (found (when node (treesit-node-start node)))) + (if found + (if (or (not bound) (if backward (>= found bound) (<= found bound))) + (progn + (goto-char found) + (search-forward (or (treesit-defun-name node) "")) + (goto-char (pos-bol)) + (set-match-data (list (point) (pos-eol))) + t) + (when move (goto-char bound)) + nil) + (when move (goto-char (or bound (if backward (point-min) (point-max))))) + nil)))) + +(defun treesit-outline-level () + "Return the depth of the current outline heading." + (let ((node (treesit-node-at (point))) + (level 0)) + (while node + (when (funcall treesit-outline-predicate node) + (setq level (1+ level))) + (setq node (treesit-node-parent node))) + level)) + ;;; Activating tree-sitter (defun treesit-ready-p (language &optional quiet) @@ -2966,6 +3008,23 @@ treesit-major-mode-setup (setq-local imenu-create-index-function #'treesit-simple-imenu)) + ;; Outline minor mode. + (when (and (or treesit-outline-predicate treesit-simple-imenu-settings) + (not (seq-some #'local-variable-p + '(outline-search-function + outline-regexp outline-level)))) + (unless treesit-outline-predicate + (setq treesit-outline-predicate + (lambda (node) + (seq-some + (lambda (setting) + (and (string-match-p (nth 1 setting) (treesit-node-type node)) + (or (null (nth 2 setting)) + (funcall (nth 2 setting) node)))) + treesit-simple-imenu-settings)))) + (setq-local outline-search-function #'treesit-outline-search + outline-level #'treesit-outline-level)) + ;; Remove existing local parsers. (dolist (ov (overlays-in (point-min) (point-max))) (when-let ((parser (overlay-get ov 'treesit-parser))) --=-=-=--