From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#59662: 29.0.50; [PATCH] Add treesit--indent-defun Date: Thu, 08 Dec 2022 09:56:44 +0100 Message-ID: <87zgbyxohf.fsf@thornhill.no> References: <5B538888-E454-4F75-B3B8-AB20E10B3E89@gmail.com> <83o7sniokt.fsf@gnu.org> <4D5A1640-4F0F-4C21-8ED6-770B88A870EA@thornhill.no> <83r0xhdluq.fsf@gnu.org> <3e25eadb-3f2b-f92d-8b18-bf1daca91b64@yandex.ru> <83wn78cqz4.fsf@gnu.org> <4d605e69-7df9-6480-c1c5-dcb22abd3bde@yandex.ru> <83y1ri2wnj.fsf@gnu.org> <875yemz55b.fsf@thornhill.no> <83r0xa2tc4.fsf@gnu.org> Reply-To: Theodor Thornhill Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="37348"; mail-complaints-to="usenet@ciao.gmane.io" Cc: casouri@gmail.org, larsi@gnus.org, 59662@debbugs.gnu.org, monnier@iro.umontreal.ca, dgutov@yandex.ru To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Dec 08 09:57:29 2022 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 1p3Cie-0009VC-If for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 08 Dec 2022 09:57:28 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p3CiG-0002rn-6B; Thu, 08 Dec 2022 03:57: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 1p3CiF-0002rc-7J for bug-gnu-emacs@gnu.org; Thu, 08 Dec 2022 03:57:03 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p3CiE-0002pz-S4 for bug-gnu-emacs@gnu.org; Thu, 08 Dec 2022 03:57:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1p3CiE-0005yL-FJ for bug-gnu-emacs@gnu.org; Thu, 08 Dec 2022 03:57:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Theodor Thornhill Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 08 Dec 2022 08:57:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 59662 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 59662-submit@debbugs.gnu.org id=B59662.167048981422951 (code B ref 59662); Thu, 08 Dec 2022 08:57:02 +0000 Original-Received: (at 59662) by debbugs.gnu.org; 8 Dec 2022 08:56:54 +0000 Original-Received: from localhost ([127.0.0.1]:55380 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p3Ci6-0005y7-35 for submit@debbugs.gnu.org; Thu, 08 Dec 2022 03:56:54 -0500 Original-Received: from out2.migadu.com ([188.165.223.204]:20503) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p3Ci2-0005y1-8p for 59662@debbugs.gnu.org; Thu, 08 Dec 2022 03:56:53 -0500 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thornhill.no; s=key1; t=1670489809; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=W1Wx3pUTS9rIdr667R3ChhTL//6u4UI+G7Zh/4fhrM0=; b=KAMath7JmkDIVdyU7vTfwVVu7/1MbxTcod0KK0EBobGRVmHy83Yn3ea/EpsQPsBW57QjEO NJRqDgZdLDfVhM/05gkk1CP92ao6/ZwpYDJFPhOPFfCn2FIgxz0F1vrqE8DV2jkucjBPEU 7UReLyPyxRA3TnHCRx906dTXCODVPY2b3yhgami8JusVbp4JUGNK2CrR43Hk7jbcq/xIP3 gd/z9rOGHxvEJcdbUClMldbXrr64FjGANr9wnG4afnBx6nCGWU+zDcDAOfUVr1Ak85qZBd H4o/L68vo6IqqUNL6p2kYtUA5npOI0cxu5I5DFl1IZ/5VPPPEu8hsdJ2++ZjNA== In-Reply-To: <83r0xa2tc4.fsf@gnu.org> X-Migadu-Flow: FLOW_OUT 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:250252 Archived-At: Eli Zaretskii writes: >> From: Theodor Thornhill >> Cc: casouri@gmail.com, 59662@debbugs.gnu.org, casouri@gmail.org >> Date: Thu, 08 Dec 2022 09:11:28 +0100 >> >> > Bonus points for adding something that could use treesit.el >> > capabilities, when those are available, instead of syntax-ppss, to >> > determine when we are in a comment or in a string. Or maybe Theo or >> > Yuan could suggest such an addition. >> >> This works perfectly as is in *-ts-modes. At least for modes where >> syntax-table is set correctly. I don't think there's a reason to enable >> treesit.el capabilities, unless there's a case where this doesn't work. > > My point is that tree-sitter in many cases parses the program better > than syntax-ppss. So if we can use its information about comments and > strings, why not do that? It's exactly the same logic that Dmitry > used: > Something like this? If we add a defvar such as the below, similar to treesit-defun-type-regexp we can use that in the function below: ``` (defvar-local treesit-comment-type-regexp nil "A regexp that matches the node type of comment nodes. For example, \"(line|block)_comment\". ") (defun prog-reindent-defun (&optional argument) "Refill paragraph or reindent the definition that the point is on. If the point is in a string, or in a comment, or there is a comment on the current line, fill the paragraph that the point is in or is on the same line. Otherwise, reindent the definition around or below point." (interactive "P") (save-excursion (if (or (and (treesit-available-p) (treesit-ready-p (treesit-language-at (point))) (string-match-p treesit-comment-type-regexp (treesit-node-type (treesit-node-at (point))))) (nth 8 (syntax-ppss)) (re-search-forward comment-start-skip (line-end-position) t)) (if (memq fill-paragraph-function '(t nil)) (lisp-fill-paragraph argument) (funcall fill-paragraph-function argument)) (end-of-defun) (let ((end (point))) (beginning-of-defun) (indent-region (point) end))))) ``` The error handling here is naive, but is this something in the line of what you're suggesting? If so, I can tweak the error handling and create a patch for it? Unless you want to do that, Dmitry >> + (if (or (nth 8 (syntax-ppss)) >> + (re-search-forward comment-start-skip (line-end-position) t)) > > which falls back on regexp search if syntax-ppss cannot help. I'm > asking why not try tree-sitter before we fall back on syntax-ppss or > regexp search? > > Does that make sense? If not, why not? > It does. >> If this ends up in master, should I backport some similar feature for >> the *ts-modes? > > Why do we need this on the release branch? A general > indent-or-fill-comment feature for all the progmodes didn't exist > until now, which is why we intend to install this on master. Yeah, sure :-) Theo