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 12:09:18 +0100 Message-ID: <87tu269mox.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> <83o7se2n89.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="714"; mail-complaints-to="usenet@ciao.gmane.io" Cc: larsi@gnus.org, 59662@debbugs.gnu.org, casouri@gmail.com, 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 12:10:27 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 1p3EnL-000ARv-5R for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 08 Dec 2022 12:10:27 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p3En4-0006AL-2J; Thu, 08 Dec 2022 06:10:10 -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 1p3En0-0006AC-An for bug-gnu-emacs@gnu.org; Thu, 08 Dec 2022 06:10:06 -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 1p3Emw-0003RR-GG for bug-gnu-emacs@gnu.org; Thu, 08 Dec 2022 06:10:06 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1p3Emw-0000Sc-7C for bug-gnu-emacs@gnu.org; Thu, 08 Dec 2022 06:10: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 11:10: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.16704977631755 (code B ref 59662); Thu, 08 Dec 2022 11:10:02 +0000 Original-Received: (at 59662) by debbugs.gnu.org; 8 Dec 2022 11:09:23 +0000 Original-Received: from localhost ([127.0.0.1]:56097 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p3EmJ-0000SF-5J for submit@debbugs.gnu.org; Thu, 08 Dec 2022 06:09:23 -0500 Original-Received: from out-9.mta0.migadu.com ([91.218.175.9]:52232) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p3EmH-0000S9-5v for 59662@debbugs.gnu.org; Thu, 08 Dec 2022 06:09:22 -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=1670497760; 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=5ClDanvEjBUliHSfkh66v6WP5Imn4tlkJQndJt+k7jA=; b=O6GYJ5baJKQxpLjrB/uj5xYei3ApZiPrTYXHu2g0DfYP9pK3etZIXSlalnsQiSijSGbE9T ayxxGzSUkzQ+uy5BXRc4gr/NamWERqFLHbq2Uo4XZStLN8lEUj/m/rokL+KpgJZhfppR/w cYWTNB6API6bZEfF8dWG67SYWEDHvEdMzq60tZhlXERhhbzvx56HrFuLU9wisFl9jsbdIM UkLm7eJrkq3MsmWawZe0MUwROnIsk5SRVYkfOLy1pe8SWnTg8biknyn8f+K12VV26p3LyI MwelEfheEJjhpPG/LWGM+ir+X0zpj366+DI1WBlblwy29iiUY0u/wnd3SXZJmw== In-Reply-To: <83o7se2n89.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:250272 Archived-At: Eli Zaretskii writes: >> From: Theodor Thornhill >> Cc: dgutov@yandex.ru, monnier@iro.umontreal.ca, larsi@gnus.org, >> 59662@debbugs.gnu.org, casouri@gmail.org >> Date: Thu, 08 Dec 2022 09:56:44 +0100 >> >> Eli Zaretskii writes: >> >> > 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\". ") > > The intent is that major modes set this variable? SGTM. Yeah, that was the idea. > >> (if (or (and (treesit-available-p) >> (treesit-ready-p (treesit-language-at (point))) > > I'd imagine that this kind of test should have an API, so the > treesit-ready-p call should not be made explicitly? Yuan, WDYT? > > Also, isn't it enough to check whether the buffer has a tree-sitter > parser or something? > >> The error handling here is naive, but is this something in the line of >> what you're suggesting? > > Yes, it is what I had in mind, but see above. What about this? I changed it to make a point that we should fill if inside of a string or comment, and indent if inside a code block. We one such variable for each type, or in both as suggested below. ``` (defvar-local treesit-text-type-regexp nil "A regexp that matches the node type of textual nodes. For example, \"(line|block)_comment\" in the case of a comment, or \"string_literal\" in the case of a string.") (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-parser-list) (string-match-p treesit-text-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))))) ```