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: Mon, 28 Nov 2022 20:32:01 +0100 Message-ID: <874juig9ji.fsf@thornhill.no> Reply-To: Theodor Thornhill 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="22196"; mail-complaints-to="usenet@ciao.gmane.io" Cc: eliz@gnu.org, casouri@gmail.org To: 59662@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Nov 28 20:33:23 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 1ozjsY-0005az-QC for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 28 Nov 2022 20:33:23 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ozjsG-0005PY-Mg; Mon, 28 Nov 2022 14:33: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 1ozjsE-0005NH-E2 for bug-gnu-emacs@gnu.org; Mon, 28 Nov 2022 14:33: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 1ozjsE-0001ca-39; Mon, 28 Nov 2022 14:33:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ozjsD-0001WG-O8; Mon, 28 Nov 2022 14:33:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Theodor Thornhill Original-Sender: "Debbugs-submit" Resent-CC: eliz@gnu.org, casouri@gmail.org, bug-gnu-emacs@gnu.org Resent-Date: Mon, 28 Nov 2022 19:33:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 59662 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org X-Debbugs-Original-Xcc: eliz@gnu.org, casouri@gmail.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16696639455818 (code B ref -1); Mon, 28 Nov 2022 19:33:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 28 Nov 2022 19:32:25 +0000 Original-Received: from localhost ([127.0.0.1]:50467 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ozjrc-0001Vm-ML for submit@debbugs.gnu.org; Mon, 28 Nov 2022 14:32:25 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:41730) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ozjrb-0001Vg-N7 for submit@debbugs.gnu.org; Mon, 28 Nov 2022 14:32:24 -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 1ozjrU-00053h-7Y for bug-gnu-emacs@gnu.org; Mon, 28 Nov 2022 14:32:23 -0500 Original-Received: from out-32.mta0.migadu.com ([91.218.175.32]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ozjrR-0001Zy-97 for bug-gnu-emacs@gnu.org; Mon, 28 Nov 2022 14:32:15 -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=1669663928; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=TyTHYPrsIk2rsa9W42wYAy08bqH3nrEqsjmkd1M0xUU=; b=EMFaa2tcpVHiZmxSlpddfh7XoAIQDKLOVqNGLEOmHYjes28Pxfru3qEDLicZKO95hnkqrm yxIgDZ/W9HOkvtsyN42fCbZGn4bWTtEL6cvNQGSQhQmq1LvvfkdyW0UdBRGJ0/nvoWrtUq zPRJ4XOjPTpa4aff7tRdxhMKHrAZ59MD53PyJ+gZclVwcESCxItySZl9ubYKmpCt8j+zuz TuqozQDP/rcINuHHcmmqYsZwCqQTbJtGSmJVobXETLd7skgz6DphDv7UtfHkY84ymB45u3 fgkmwGwFFcTOVf6LELG6n6djxun9IXTDNL0ciCaQ/jGfhz0R9fgIv//+uubl2Q== X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=91.218.175.32; envelope-from=theo@thornhill.no; helo=out-32.mta0.migadu.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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:249340 Archived-At: --=-=-= Content-Type: text/plain Hi Eli and Yuan! I've added a function to treesit.el, for use in treesit-major-mode-setup. Because the treesit-defun-type-regexp gives us "defuns" for free, we can set fill-paragraph-function to this new function, thus enabling quick formatting, and some sensible default for fill-paragraph. It aims to mirror c-indent-defun. I think this is a nice way to get this functionality for free, but I'm not 100% whether this is considered ok or not. An alternative could be to add a 'treesit-mode-map' where we can auto-enable such constructs. Feel free to apply if it looks ok. Theo --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Add-treesit-indent-defun.patch >From aed3f136d530c0eaa83c385cac7388c853b4f99a Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Mon, 28 Nov 2022 20:19:20 +0100 Subject: [PATCH] Add treesit--indent-defun We can piggyback on the already existing 'treesit-defun-type-regexp' to decide a scope to reindent/reformat. By setting fill-paragraph-function major modes that implement 'treesit-defun-type-regexp' will get this functionality for free, by default bound to M-q. * lisp/treesit.el (treesit--indent-defun): New function. (treesit-major-mode-setup): Enable this functionality when prereqs are fulfilled. --- lisp/treesit.el | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lisp/treesit.el b/lisp/treesit.el index 8f092f475d..685c59aedb 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -1451,6 +1451,21 @@ treesit-simple-indent (message "No matched rule")) (cons nil nil)))))) +(defun treesit--indent-defun (&optional _arg) + "Indent the current defun. + +Uses `treesit-defun-type-regexp' to determine which nodes to +reindent." + (interactive) + (when-let ((defun-node + (save-excursion + (treesit-search-forward-goto + (treesit-node-at (point)) + treesit-defun-type-regexp t t)))) + (treesit-indent-region + (treesit-node-start defun-node) + (treesit-node-end defun-node)))) + (defun treesit-check-indent (mode) "Check current buffer's indentation against a major mode MODE. @@ -1698,7 +1713,10 @@ treesit-major-mode-setup ;; Navigation. (when treesit-defun-type-regexp (setq-local beginning-of-defun-function #'treesit-beginning-of-defun) - (setq-local end-of-defun-function #'treesit-end-of-defun))) + (setq-local end-of-defun-function #'treesit-end-of-defun)) + ;; Filling + (when (and treesit-defun-type-regexp treesit-simple-indent-rules) + (setq-local fill-paragraph-function #'treesit--indent-defun))) ;;; Debugging -- 2.34.1 --=-=-=--