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 10:08:17 +0100 Message-ID: <87wn72xny6.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> <87zgbyxohf.fsf@thornhill.no> 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="19413"; mail-complaints-to="usenet@ciao.gmane.io" Cc: larsi@gnus.org, 59662@debbugs.gnu.org, Yuan Fu , 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 10:09:28 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 1p3CuF-0004oF-Br for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 08 Dec 2022 10:09:27 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p3Ctw-0004iM-Bg; Thu, 08 Dec 2022 04:09:13 -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 1p3Ctq-0004hI-2S for bug-gnu-emacs@gnu.org; Thu, 08 Dec 2022 04:09:02 -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 1p3Ctp-00068d-QE for bug-gnu-emacs@gnu.org; Thu, 08 Dec 2022 04:09:01 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1p3Ctp-00066L-L7 for bug-gnu-emacs@gnu.org; Thu, 08 Dec 2022 04:09:01 -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 09:09:01 +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.167049051023440 (code B ref 59662); Thu, 08 Dec 2022 09:09:01 +0000 Original-Received: (at 59662) by debbugs.gnu.org; 8 Dec 2022 09:08:30 +0000 Original-Received: from localhost ([127.0.0.1]:55440 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p3CtK-000660-9t for submit@debbugs.gnu.org; Thu, 08 Dec 2022 04:08:30 -0500 Original-Received: from out-213.mta0.migadu.com ([91.218.175.213]:36651) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p3CtB-00065u-Qq for 59662@debbugs.gnu.org; Thu, 08 Dec 2022 04:08:28 -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=1670490499; 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=1WWCf2pmkCA5F3PnFK5XhLThFMtYdfLFdKaFpmR1NOo=; b=iUDGxzlOX7WwH8AcNtv4Vl0KxexQZyVQLyJHXNU8XuJkix1XUHiXt5iqNpzlGFL7lGi8B4 TYAJln/TSnr4ilH9GIDoIJZwPzVfIdXht9kUI0el1EhMZve+dWEW9vEs/1FWHnMGQ+L/5z 7/Vwd23FElcjPKSEezrVufRx2C3djGg2jNSD1XDcGjXVpRhCA8V/K8Rh/ggRsxeshn+Y9o wsmVee5LTOunosNLw8GQHQvzfRf0fY9FZj5wJ1q83SbJXYoGYxuqyRIJXLDtax2pl92Rod aABOHSySvJ+gD8WBW8YxWutk4o6b/7Q1pC7d7SQAl2SEz+C7J+OniAGtz4ZpCA== In-Reply-To: <87zgbyxohf.fsf@thornhill.no> 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:250255 Archived-At: Resending because I removed the wrong email to Yuan... Theodor Thornhill writes: > 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