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 17:07:48 -0800 Message-ID: <0EC6C5BE-321E-43E9-92F9-D829DBBD36A7@gmail.com> References: <5B538888-E454-4F75-B3B8-AB20E10B3E89@gmail.com> 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="39525"; 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 02:08:22 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 1p0Y3o-000A4t-E1 for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 01 Dec 2022 02:08:20 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p0Y3Y-0004Zd-GK; Wed, 30 Nov 2022 20:08: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 1p0Y3X-0004ZT-IR for bug-gnu-emacs@gnu.org; Wed, 30 Nov 2022 20:08: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 1p0Y3W-0005EG-VL for bug-gnu-emacs@gnu.org; Wed, 30 Nov 2022 20:08:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1p0Y3W-0006AY-GC for bug-gnu-emacs@gnu.org; Wed, 30 Nov 2022 20:08:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Yuan Fu Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 01 Dec 2022 01:08: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.166985688123711 (code B ref 59662); Thu, 01 Dec 2022 01:08:02 +0000 Original-Received: (at 59662) by debbugs.gnu.org; 1 Dec 2022 01:08:01 +0000 Original-Received: from localhost ([127.0.0.1]:36206 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p0Y3U-0006AK-K8 for submit@debbugs.gnu.org; Wed, 30 Nov 2022 20:08:01 -0500 Original-Received: from mail-pf1-f178.google.com ([209.85.210.178]:35772) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p0Y3Q-0006A7-DA for 59662@debbugs.gnu.org; Wed, 30 Nov 2022 20:07:59 -0500 Original-Received: by mail-pf1-f178.google.com with SMTP id w79so437931pfc.2 for <59662@debbugs.gnu.org>; Wed, 30 Nov 2022 17:07:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=e+xu5oFil4jpsEgi54bFrumSI63Wxo9UqfNyL3UKWlw=; b=k5DvKomjLftOeVdOFnJHlSxIlfDAtKkzqO+oHV+8sI5Q0OeA7by8iptOrocpq6RmZ0 y6gheM9kenmD2tnMCBi17GS58JAMuj8OG7LBThdcsjEUYkTdfHF845VqhQYct3wYr5f6 LpJ3ijDsPTUd16nKkc67TM9wBeSwUiRD99fSRENKuFBE7HdLQNk3bjkqaL3S8Y+rHmdx GiScTU0pd5sm7FaskmQUkXpaB+KpQB/0AXLoIlKuRQCX5IQdBE1irYtsrfAO0glIiu+q gVPxCrIgzFQH3y2FNNea/HoU8kqQXe2UMjudphNfC8ScA/o2hpOEfsmUsgb50uGRlbhd c1IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=e+xu5oFil4jpsEgi54bFrumSI63Wxo9UqfNyL3UKWlw=; b=v7ga2gGxhMDnMhOprnXyaH97m1uQy1SVutA5/EKOWAexEqssQwC0/IcZGiZgfsvxkn ILMzYNAv+wNSBqoKpM2Zbmk4AB0yYDOKSB5LpG5Ju1EcSo8WcNlLNC6SEHFX8mHwPonK ATbNj2T87snuS0Clc0gGuOeSOyN2YFrCMmC73hwLnWM0dCkzxtf8/sIDr5mJE8onASkq 8Vc45iGBLlamHo4+ycI7SonpIvjxZ5zl376IfqJtLgQ9M9eX0EI1bJj9K/ATQFKjvJpW Hgsh0phdyJdVZm/4ReGav+C/IYlfSVnWNUIKQ5hcjys5ShDS+MGTNxCArFhRasOFw7/N Gz6A== X-Gm-Message-State: ANoB5pnn3gHkvyO8IhHCZL914ZJRwHk0SlMlxlnD9GEbW89g16zKyOIo pRoYWw38wES7mlaxA3gla+8= X-Google-Smtp-Source: AA0mqf6wi7yqOqXWEd3rZHXa/T+WsKa4MapBak8fAXaGygYHitKaGxJ81dNLlUGhT3t6rRanGB6Hfw== X-Received: by 2002:a05:6a00:1c8f:b0:574:6880:e76f with SMTP id y15-20020a056a001c8f00b005746880e76fmr36451076pfw.35.1669856869989; Wed, 30 Nov 2022 17:07:49 -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 v30-20020aa799de000000b0056b3c863950sm1983888pfi.8.2022.11.30.17.07.49 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 30 Nov 2022 17:07:49 -0800 (PST) In-Reply-To: <5B538888-E454-4F75-B3B8-AB20E10B3E89@gmail.com> 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:249587 Archived-At: > On Nov 30, 2022, at 3:23 PM, Yuan Fu wrote: >=20 >=20 > Theodor Thornhill writes: >=20 >> Eli Zaretskii writes: >>=20 >>>> 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. >>>=20 >>> Sounds good, but why did you think it wouldn't be OK? Anything here = that >>> doesn't meet the eye? >>>=20 >>=20 >> Not really, but see below answer. >>=20 >>>> An alternative could be to add a 'treesit-mode-map' where we can >>>> auto-enable such constructs. >>>=20 >>> I think this is less desirable. >>>=20 >>> Yuan, WDYT? >>>=20 >>>> @@ -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))) >>>=20 >>> 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_? >>=20 >> 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. >>=20 >> In a way filling _is_ formatting/reindenting, at least that's how I = look >> at it. >=20 > 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). >=20 > 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. >=20 > 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? >=20 > I=E2=80=99m thinking something like >=20 > (defun prog-mode-indent-defun () > (interactive) > (mark-defun) > (indent-region (region-beginning) (region-end))) >=20 > (defvar prog-mode-map > (... > (define-key map (kbd "C-c C-q") #'prog-mode-indent-defun))) Tho I=E2=80=99d prefer we bind it in major mode maps first. Adding = anything to prog-mode-map should go through emacs-devel (and I don=E2=80=99= t know if we want to add any bindings to prog-mode-map), of course. Yuan=