From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?Q?J=C3=B8rgen?= Kvalsvik Newsgroups: gmane.emacs.bugs Subject: bug#74507: [PATCH] Indent compounds c-ts-mode when { is not BOL Date: Sun, 1 Dec 2024 23:46:06 +0100 Message-ID: <67232cd8-0557-4801-be76-76a965a8c60e@lambda.is> References: <87r071yov3.fsf@lambda.is> <86serabhgw.fsf@gnu.org> <1e4fcc9a-4f69-4c3a-b9d6-716d61129e31@lambda.is> <60820855-5672-4083-B7B5-38FF5259BBF4@gmail.com> <2b677735-ecc0-4fb6-be60-533bfbcc962a@lambda.is> <2407F047-D3EA-4260-9987-D674EFD2932A@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25197"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Eli Zaretskii , theo@thornhill.no, 74507@debbugs.gnu.org To: Yuan Fu Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Dec 01 23:47:32 2024 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 1tHsiw-0006PA-Rc for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 01 Dec 2024 23:47:31 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tHsia-0002WB-JR; Sun, 01 Dec 2024 17:47:08 -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 1tHsiW-0002Vt-6p for bug-gnu-emacs@gnu.org; Sun, 01 Dec 2024 17:47:04 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tHsiU-0003Yj-QV for bug-gnu-emacs@gnu.org; Sun, 01 Dec 2024 17:47:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=In-Reply-To:From:References:MIME-Version:Date:To:Subject; bh=x5SHcm4phGEd9G/MrzDr7P/awdX1etPbdhrjS//Vb1k=; b=YCZZM+R8wVPJhJ010ZwDNSiS2PzJEYXlRQoj/YjZaFf2uIkeogNK6qEBhRGetJXg/T5oVFb19vBaE/cFawYox3cO09z/jLBeOxXSla7zk/vkL9RWJSotu7k3Ps6afVdHAU/2B8zZh/6tTCDojH71Q2V4SKAafnul302ri3BrJKJtFL3fvlWRkZLHb8D3kd/x9Pu1o0ZMoWPGh98g7jHk7iLY8bpmddPGIkRNhwt1Ccr2syMWRbfc2LjtebxF+lOUZ0qE9fL4xZuIhEhPgxsQRq5DxfIYX82m1F3nbFX6TANWwgzDMpMWggHrWQNGGOBht9eJO1aY7m/B2+fDp9FQuw==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tHsiU-0005Ki-FK for bug-gnu-emacs@gnu.org; Sun, 01 Dec 2024 17:47:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: =?UTF-8?Q?J=C3=B8rgen?= Kvalsvik Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 01 Dec 2024 22:47:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 74507 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 74507-submit@debbugs.gnu.org id=B74507.173309318020416 (code B ref 74507); Sun, 01 Dec 2024 22:47:02 +0000 Original-Received: (at 74507) by debbugs.gnu.org; 1 Dec 2024 22:46:20 +0000 Original-Received: from localhost ([127.0.0.1]:53401 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tHsho-0005JD-96 for submit@debbugs.gnu.org; Sun, 01 Dec 2024 17:46:20 -0500 Original-Received: from mx.kolabnow.com ([212.103.80.154]:58596) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tHshl-0005Il-9t for 74507@debbugs.gnu.org; Sun, 01 Dec 2024 17:46:18 -0500 Original-Received: from localhost (unknown [127.0.0.1]) by mx.kolabnow.com (Postfix) with ESMTP id 762D620C73F5; Sun, 1 Dec 2024 23:46:11 +0100 (CET) Authentication-Results: ext-mx-out011.mykolab.com (amavis); dkim=pass (2048-bit key) reason="pass (just generated, assumed good)" header.d=kolabnow.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:content-type:content-type:in-reply-to :from:from:content-language:references:subject:subject :mime-version:date:date:message-id:received:received:received; s=dkim20240523; t=1733093169; x=1734907570; bh=x5SHcm4phGEd9G/M rzDr7P/awdX1etPbdhrjS//Vb1k=; b=iZn+Ck/w1AbK9B1dJsZkZyL0g+eK7wK3 +rsTI3NHvd07yJ9iXtRnwXauHtRqmVjIl4CsUp/fUNdmFvVr6O7jDmTmzgV/hp5m Uzyj/EsyoomaUQbOH4gRlD2j5Fx4lDefzzlB/8W9gDcv2reqMlH7Rgc4bcHXgrwy rrANkY+cFGTACLvdL1NUnb1ADfc17PmE4LKUtuaIA0l1adB6pkheBFfgUkHSsiiz bseUx4Z2SIopm14RVm2uEGaaqmTRZv/+C1b3tuRDDcTb/KYkU9NtRH5mBzFJOnOs fRkqkJ0ETT85nivq8eDsvY/0pWE01asHXqzApnIufLt5S11vRWXUYQ== X-Virus-Scanned: amavis at mykolab.com Original-Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out011.mykolab.com [127.0.0.1]) (amavis, port 10024) with ESMTP id sBMfPrbsH9mT; Sun, 1 Dec 2024 23:46:09 +0100 (CET) Original-Received: from int-mx009.mykolab.com (unknown [10.9.13.9]) by mx.kolabnow.com (Postfix) with ESMTPS id 0718D20A5267; Sun, 1 Dec 2024 23:46:07 +0100 (CET) Original-Received: from ext-subm010.mykolab.com (unknown [10.9.6.10]) by int-mx009.mykolab.com (Postfix) with ESMTPS id 9FE1E2330B85; Sun, 1 Dec 2024 23:46:07 +0100 (CET) Content-Language: en-US In-Reply-To: <2407F047-D3EA-4260-9987-D674EFD2932A@gmail.com> 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:296285 Archived-At: On 12/1/24 22:48, Yuan Fu wrote: > > >> On Dec 1, 2024, at 1:51 AM, Jørgen Kvalsvik wrote: >> >> On 12/1/24 10:25, Yuan Fu wrote: >>>> On Nov 30, 2024, at 12:49 PM, Jørgen Kvalsvik wrote: >>>> >>>> On 11/30/24 01:16, Yuan Fu wrote: >>>>>> On Nov 29, 2024, at 1:05 AM, Jørgen Kvalsvik wrote: >>>>>> >>>>>> On 11/29/24 08:57, Eli Zaretskii wrote: >>>>>>>> Cc: theo@thornhill.no,casouri@gmail.com >>>>>>>> From: Jørgen Kvalsvik >>>>>>>> Date: Sun, 24 Nov 2024 10:15:12 +0100 >>>>>>>> >>>>>>>> * lisp/progmodes/c-ts-mode.el (c-ts-mode--parent-is-not-top-compound): >>>>>>>> New function. >>>>>>>> (c-ts-mode--indent-styles): Use it. >>>>>>>> * test/lisp/progmodes/c-ts-mode-resources/indent.erts: New compound >>>>>>>> statement test. >>>>>>> Please make sure the lines here are not too long (see CONTRIBUTE for >>>>>>> details). >>>>>>>> +(defun c-ts-mode--parent-is-not-top-compound (_n parent &rest _) >>>>>>>> + "Matches when PARENT is not the top level compound statement, >>>>>>>> +the {} that immediately follows the signature." >>>>>>> The first line of a doc string should be a single complete sentence. >>>>>>> Thanks. >>>>>> >>>>>> Ok. Yuan, would you like me to submit a revision? >>>>> Since your assignment is already done, let’s just apply your patch, and I’ll rebase my changes on top of yours. So yeah, do send the revision patch, thanks! >>>>> Yuan >>>> >>>> Certainly - I posted it on the bug tracker. >>> Thanks Jørgen. What did you use to generate the patch? For some reason I can’t apply it. My git skill isn’t that great so it could be my problem. If you can apply it fine maybe you can share the command you used? >> >> I used git format-patch HEAD~1, and I just tested applying it to master with `git am 0001-Indent-compounds-in-c-ts-mode-when-is-not-BOL.patch' which worked. >> >>> BTW, the commit title is missing. When you add the title, you can also add the bug number. For example: >>> Improve c-ts-mode indentation for macros (bug#74507) >>> Yuan >> >> Sure. I've attached a new patch with the bug in it. I tested it and it applies cleanly with `git am 0001-Improve-c-ts-mode-compound-indents-bug-74507.patch' >> <0001-Improve-c-ts-mode-compound-indents-bug-74507.patch> > > Thanks. This patch applied without a problem. > > I reworked your heuristic into a function rule for better organization, and changed the condition slightly: > > (defun c-ts-mode--macro-heuristic-rules (node parent &rest _) > "Heuristic indent rule for control flow macros. > > Eg, > > #define IOTA(var, n) for (int var = 0; var != (n); ++var) > > int main() > { > IOTA (v, 10) { > printf(\"%d \", v); <-- Here we want to indent > counter++; <-- Use baseline rule to align > } to prev sibling > > Checked by \"Compound Statement after code (Bug#74507)\" test. > > NODE and PARENT are the same as other indent rules." > (when (and (treesit-node-match-p parent "compound_statement") > (treesit-node-match-p (treesit-node-prev-sibling parent) > "expression_statement")) > (let ((parent-bol > (lambda () (save-excursion > (goto-char (treesit-node-start parent)) > (back-to-indentation) > (point))))) > (cond > ;; Closing brace. > ((treesit-node-match-p node "}") > (cons (funcall parent-bol) 0)) > ;; First sibling. > ((treesit-node-eq (treesit-node-child parent 0 'named) node) > (cons (funcall parent-bol) > c-ts-mode-indent-offset)))))) > > Instead of checking whether PARENT is top-level compound_statement, I make it check whether the previous sibling is an expression_statement. This way the heuristic works when the macro isn’t at top-level too. Here’s the updated test: > > #define IOTA(var, n) for (int var = 0; var != (n); ++var) > int main() > { > IOTA (v, 10) { > printf("%d ", v); > } > > const char *msg = "Hello, world!"; { > puts("Hello, world!"); > } > > for (int i = 0; > i < 10; > i++) { > IOTA (v, 10) { > printf("%d ", v); > } > } > > { > IOTA (v, 10) { > printf("%d ", v); > } > } > } > > The only test case that doesn’t pass right now is this one: > > const char *msg = "Hello, world!"; { > puts("Hello, world!"); > } > > Is this a real use-case? > > Yuan Maybe - I included it to show that the problem was not tied to it being a macro, but rather { not being the first thing on the line. It would be nice to support it, and c-mode also indents it properly. That being said, it is probably rare enough in practice to prio it less. Thanks, Jørgen