From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuan Fu Newsgroups: gmane.emacs.bugs Subject: bug#59662: 29.0.50; [PATCH] Add treesit--indent-defun Date: Wed, 30 Nov 2022 15:23:17 -0800 Message-ID: <5B538888-E454-4F75-B3B8-AB20E10B3E89@gmail.com> References: <874juig9ji.fsf@thornhill.no> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.1\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="7572"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Eli Zaretskii , 59662@debbugs.gnu.org, casouri@gmail.org To: Theodor Thornhill Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Dec 01 00:24:21 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 1p0WRA-0001jo-Up for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 01 Dec 2022 00:24:21 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p0WQv-0008M2-Hj; Wed, 30 Nov 2022 18:24:05 -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 1p0WQs-0008Lj-UJ for bug-gnu-emacs@gnu.org; Wed, 30 Nov 2022 18:24: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 1p0WQs-0005ov-K7 for bug-gnu-emacs@gnu.org; Wed, 30 Nov 2022 18:24:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1p0WQs-0005Gk-Fc for bug-gnu-emacs@gnu.org; Wed, 30 Nov 2022 18:24:02 -0500 X-Loop: help-debbugs@gnu.org In-Reply-To: <874juig9ji.fsf@thornhill.no> Resent-From: Yuan Fu Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 30 Nov 2022 23:24: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.166985060920243 (code B ref 59662); Wed, 30 Nov 2022 23:24:02 +0000 Original-Received: (at 59662) by debbugs.gnu.org; 30 Nov 2022 23:23:29 +0000 Original-Received: from localhost ([127.0.0.1]:35725 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p0WQL-0005GR-62 for submit@debbugs.gnu.org; Wed, 30 Nov 2022 18:23:29 -0500 Original-Received: from mail-pg1-f176.google.com ([209.85.215.176]:39724) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p0WQI-0005GK-6f for 59662@debbugs.gnu.org; Wed, 30 Nov 2022 18:23:28 -0500 Original-Received: by mail-pg1-f176.google.com with SMTP id 6so110896pgm.6 for <59662@debbugs.gnu.org>; Wed, 30 Nov 2022 15:23:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:cc:date:message-id:subject:mime-version :content-transfer-encoding:from:from:to:cc:subject:date:message-id :reply-to; bh=LKt7ohb0/EJWQ49g43BzSuGaCcxuG3KwSVQVGIsSU0w=; b=Vl6AnHK6zYDk6/bBfw1JuqrsIHXAGJM4DIIeIuogG7TKizFtDMaEaT1uuNNhzQugKM GRRpVfV0120xmCn4lT36ZxtR8fA/4FOA7ScwBkL2Lj68Yq2HFTuI6RdXLScmryMMaYHn DnZKDwP43Ql7+WoS8Xp9mIUwylVPNZ6n89yyl/5XmeMgy2yqWsIJzuBhR2FMk6VSEeq4 1jmzpCyG+O3foYuS+9NFol7aT+zBlYr1tbjU8YTgyJPXCBosibsiUYyYSm5x0BpX1hA/ ttlKoLx0F1Wzv4+s2f3R10f7tSE1ZXmBBNrM0Xi11ffVFR/VY6cthwqRoalOyaZXIjwd kU4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:cc:date:message-id:subject:mime-version :content-transfer-encoding:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LKt7ohb0/EJWQ49g43BzSuGaCcxuG3KwSVQVGIsSU0w=; b=Ay3QsE9cKIg55Yuqf2lUvaSxrRPIUo8t9d3ESa+OoT3yie9OHzolX9wJnl28AEV/mN SvbQFWYWh/pefgESKFLT8D/a7yinsCRGwIduVx7/wbPw3yRCKOaD+Dr/jhyBbcMDy7A7 ZdF4e8qPTSCMLysonpWuQg5EQoX6SY+SByeUDAzL+Yy+BN63KKzMGMRxttkxMPxTXjLZ U1UYDSRa1RVWk0LTOiRSfXAcZAiHSqIFE8EN0IBhTcMwu57Np1FC+0Ik0+BdKQHQN+Yu Ld0iD/5iWk+WEhy2jGHsCJvB96/NIe+iW7zAlcaXWS502tXY8ls7XcFFMnswoWVfXSDX IiPQ== X-Gm-Message-State: ANoB5plWiX0L5y4UHOk0q8u8PN2WPiEN4V2fuC4yCwiypcE1w0LUZ4B/ /icgZiF5VwCARP6M17x+cWI= X-Google-Smtp-Source: AA0mqf4kUtOmPYHSK2c/wcs6nwFzOjNoN7OSbPyXTL11iw6x0tMt9pRyg4WvUF34BQRmEDvFnHqdoQ== X-Received: by 2002:a63:4f48:0:b0:477:4451:cf5 with SMTP id p8-20020a634f48000000b0047744510cf5mr37976848pgl.391.1669850598822; Wed, 30 Nov 2022 15:23:18 -0800 (PST) Original-Received: from smtpclient.apple (cpe-172-117-161-177.socal.res.rr.com. [172.117.161.177]) by smtp.gmail.com with ESMTPSA id c13-20020a631c0d000000b00476db6fe510sm1417254pgc.75.2022.11.30.15.23.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 30 Nov 2022 15:23:18 -0800 (PST) X-Mailer: Apple Mail (2.3696.120.41.1.1) 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:249581 Archived-At: Theodor Thornhill writes: > Eli Zaretskii writes: > >>> Cc: eliz@gnu.org, casouri@gmail.org >>> From: Theodor Thornhill >>> Date: Mon, 28 Nov 2022 20:32:01 +0100 >>>=20 >>> 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. >>>=20 >>> 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. >> >> Sounds good, but why did you think it wouldn't be OK? Anything here = that >> doesn't meet the eye? >> > > Not really, but see below answer. > >>> An alternative could be to add a 'treesit-mode-map' where we can >>> auto-enable such constructs. >> >> I think this is less desirable. >> >> Yuan, WDYT? >> >>> @@ -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))) >> >> I'm a bit confused: if the function's name is treesit--indent-defun, = and it >> uses treesit-indent-region to do its job, why do we assign it to >> fill-paragraph-function, which is supposed to _fill_, not to = _indent_? > > This is why I was thinking it would maybe be better to put it into a > treesit-mode-map that major-modes can inherit from, thus binding it to > things such as C-c C-q. The reason I put it in filling was because = that > is a common key to press in everything _but_ prog-modes. And = prog-modes > seem to mostly just turn it off if not inside of comments etc. This > would behave just like that, except we would reformat/reindent/refill > code. > > In a way filling _is_ formatting/reindenting, at least that's how I = look > at it. I see, so you want to implement C-c C-q in c-mode. But why don=E2=80=99t = we make treesit--indent-defun a command and bind it in C-c C-q in major = modes?=20 Filling is not indent IMO. If you fill a list in elisp code it wraps = long lines=20 instead of indent (IIRC). Also, this is really not tree-sitter specific, it doesn=E2=80=99t = require any tree-sitter feature to accomplish: indent-defun only needs mark-defun and indent-region, both are supported by practically any major mode. Normally this kind of thing goes into lisp.el, alongside commands like fill-paragraph, indent-region, beginning-of-defun, etc, and claim a global keybinding. But maybe we only want it to live under C-c prefix, in that case I guess we can bind it in prog-mode-map, under C-c C-q? I=E2=80=99m thinking something like (defun prog-mode-indent-defun () (interactive) (mark-defun) (indent-region (region-beginning) (region-end))) (defvar prog-mode-map (... (define-key map (kbd "C-c C-q") #'prog-mode-indent-defun))) Yuan