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#68993: treesitter support for forward-sexp-default-function Date: Thu, 08 Feb 2024 19:38:17 +0200 Organization: LINKOV.NET Message-ID: <86y1bv6jsm.fsf@mail.linkov.net> 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="20859"; 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: Yuan Fu To: 68993@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Feb 08 18:42:01 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 1rY8PR-0005EP-0j for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 08 Feb 2024 18:42:01 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rY8PF-0002b8-7g; Thu, 08 Feb 2024 12:41:49 -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 1rY8PE-0002X1-AA for bug-gnu-emacs@gnu.org; Thu, 08 Feb 2024 12:41:48 -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 1rY8PD-0000Sl-Vq for bug-gnu-emacs@gnu.org; Thu, 08 Feb 2024 12:41:47 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rY8PR-0004I5-QV; Thu, 08 Feb 2024 12:42:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: casouri@gmail.com, bug-gnu-emacs@gnu.org Resent-Date: Thu, 08 Feb 2024 17:42:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 68993 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org X-Debbugs-Original-Xcc: Yuan Fu Original-Received: via spool by submit@debbugs.gnu.org id=B.170741407016422 (code B ref -1); Thu, 08 Feb 2024 17:42:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 8 Feb 2024 17:41:10 +0000 Original-Received: from localhost ([127.0.0.1]:33557 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rY8Ob-0004Go-LN for submit@debbugs.gnu.org; Thu, 08 Feb 2024 12:41:10 -0500 Original-Received: from lists.gnu.org ([2001:470:142::17]:33500) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rY8OV-0004Fu-Vm for submit@debbugs.gnu.org; Thu, 08 Feb 2024 12:41:08 -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 1rY8OB-0002D8-P9 for bug-gnu-emacs@gnu.org; Thu, 08 Feb 2024 12:40:43 -0500 Original-Received: from relay6-d.mail.gandi.net ([2001:4b98:dc4:8::226]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rY8O8-0000Hd-TG for bug-gnu-emacs@gnu.org; Thu, 08 Feb 2024 12:40:43 -0500 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id AC334C0002 for ; Thu, 8 Feb 2024 17:40:36 +0000 (UTC) X-GND-Sasl: juri@linkov.net Received-SPF: pass client-ip=2001:4b98:dc4:8::226; envelope-from=juri@linkov.net; helo=relay6-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, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action 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:279649 Archived-At: --=-=-= Content-Type: text/plain 'treesit-forward-sentence' supports the node type 'text', and for matching nodes it uses the fallback to 'forward-sentence-default-function'. This patch does exactly the same for 'treesit-forward-sexp': for nodes that match a new node type 'comment', it uses the fallback to the new function 'forward-sexp-default-function'. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=forward-sexp-default-function.patch diff --git a/lisp/treesit.el b/lisp/treesit.el index 82b2f97b4a5..284c4915f3a 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -2137,7 +2137,10 @@ treesit-forward-sexp (interactive "^p") (let ((arg (or arg 1)) (pred (or treesit-sexp-type-regexp 'sexp))) - (or (if (> arg 0) + (or (when (treesit-node-match-p (treesit-node-at (point)) 'comment t) + (funcall #'forward-sexp-default-function arg) + t) + (if (> arg 0) (treesit-end-of-thing pred (abs arg) 'restricted) (treesit-beginning-of-thing pred (abs arg) 'restricted)) ;; If we couldn't move, we should signal an error and report diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el index 4b722b4e9a7..d3c3bf55de3 100644 --- a/lisp/emacs-lisp/lisp.el +++ b/lisp/emacs-lisp/lisp.el @@ -45,7 +45,12 @@ parens-require-spaces :type 'boolean :group 'lisp) -(defvar forward-sexp-function nil +(defun forward-sexp-default-function (&optional arg) + "Default function for `forward-sexp-function'." + (goto-char (or (scan-sexps (point) arg) (buffer-end arg))) + (if (< arg 0) (backward-prefix-chars))) + +(defvar forward-sexp-function #'forward-sexp-default-function ;; FIXME: ;; - for some uses, we may want a "sexp-only" version, which only ;; jumps over a well-formed sexp, rather than some dwimish thing @@ -74,10 +79,7 @@ forward-sexp "No next sexp" "No previous sexp")))) (or arg (setq arg 1)) - (if forward-sexp-function - (funcall forward-sexp-function arg) - (goto-char (or (scan-sexps (point) arg) (buffer-end arg))) - (if (< arg 0) (backward-prefix-chars))))) + (funcall forward-sexp-function arg))) (defun backward-sexp (&optional arg interactive) "Move backward across one balanced expression (sexp). --=-=-= Content-Type: text/plain Maybe the node type 'comment' is not the best name, but it was intended to allow using the default function to be able to move with 'M-C-f' in the comments and strings there tree-sitter has no information. It makes sense to support the default movement with 'M-C-f' in the comments and strings of all ts modes. The second patch shows how this could be achieved by adding the default 'comment' match to 'treesit-thing-settings' of all modes. Or maybe this should be customizable? --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=treesit-major-mode-setup.patch diff --git a/lisp/treesit.el b/lisp/treesit.el index 82b2f97b4a5..284c4915f3a 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -3054,6 +3057,18 @@ treesit-major-mode-setup (setq-local outline-search-function #'treesit-outline-search outline-level #'treesit-outline-level)) + (dolist (parser (treesit-parser-list)) + (let ((language (treesit-parser-language parser)) + (comment (regexp-opt '("comment" "string" "string_content")))) + (unless (treesit-thing-defined-p 'comment language) + (if-let ((l (alist-get language treesit-thing-settings))) + (progn + (setf (alist-get 'comment l) (list comment)) + (setf (alist-get language treesit-thing-settings) l)) + (setq-local treesit-thing-settings + (append `((,language (comment ,comment))) + treesit-thing-settings)))))) + ;; Remove existing local parsers. (dolist (ov (overlays-in (point-min) (point-max))) (when-let ((parser (overlay-get ov 'treesit-parser))) --=-=-= Content-Type: text/plain The third patch demonstrates how it's possible to close bug#67036 that was impossible to fix without more general changes in treesit.el. The problem is that e.g. Ruby parser to such text: hash[:key] produces such syntax tree: (element_reference object: (identifier) [ (simple_symbol) ]) so when point is on [ then 'M-C-f' can't move to ]. This is fixed now by the third patch: --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=ruby-ts-mode.patch diff --git a/lisp/progmodes/ruby-ts-mode.el b/lisp/progmodes/ruby-ts-mode.el index 598eaa461ff..4d0ae2e9303 100644 --- a/lisp/progmodes/ruby-ts-mode.el +++ b/lisp/progmodes/ruby-ts-mode.el @@ -1170,7 +1170,20 @@ ruby-ts-mode "global_variable" ) eol) - #'ruby-ts--sexp-p))))) + #'ruby-ts--sexp-p)) + (comment ,(lambda (node) + (or (member (treesit-node-type node) + '("comment" "string_content")) + (and (member (treesit-node-text node) + '("[" "]")) + (equal (treesit-node-type + (treesit-node-parent node)) + "element_reference")) + (and (member (treesit-node-text node) + '("#{" "}")) + (equal (treesit-node-type + (treesit-node-parent node)) + "interpolation")))))))) ;; AFAIK, Ruby can not nest methods (setq-local treesit-defun-prefer-top-level nil) --=-=-=--