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#74507: [PATCH] Indent compounds c-ts-mode when { is not BOL Date: Sun, 1 Dec 2024 18:13:19 -0800 Message-ID: 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> <67232cd8-0557-4801-be76-76a965a8c60e@lambda.is> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3776.700.51\)) 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="4698"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Eli Zaretskii , Theodor Thornhill , 74507-done@debbugs.gnu.org To: =?UTF-8?Q?J=C3=B8rgen?= Kvalsvik Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Dec 02 03:16:14 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 1tHvyv-0000hi-Fu for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 02 Dec 2024 03:16:13 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tHvyA-00051F-En; Sun, 01 Dec 2024 21:15:26 -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 1tHvy2-0004zr-GC for bug-gnu-emacs@gnu.org; Sun, 01 Dec 2024 21:15:20 -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 1tHvxn-0007ru-7t for bug-gnu-emacs@gnu.org; Sun, 01 Dec 2024 21:15:13 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=References:Date:In-Reply-To:From:Mime-Version:To:Subject; bh=VUVUyt2qdGLCPI7KBAXPAPE6b8X/EAcN7z19fAbxtWY=; b=TCO9GAwXG8T6eJlU+sQoDlErrIEgzBlD1+PGyK3s1DcAuhxW/s5Rt0T7J+TzD+J0US+YHYAG61yPcYiO2WyRrp9TKfUNEObmjLl+skBZRTIEUmL7KzMYGzHmLdHLkjjz0YjDCxBR7H0JheGTU2+BiXfEge+upJ+zmmIsDN/6nsF5L4bnu8PssDb1pNSLL7y5/9spQtmwq8a+V+hP0vP+qGh3XTaZoya02n2yFodS9hd0gAMkfAAFPNplUQD8Lq6zIDqIwNJ8aTf1QAb3Mz8xOWQgBZNUqEuKqdviSF6CCzmQxfcLfqrZg6K48kBd8yLNXxJUfHh0hybHB/Jo2einzw==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tHvxm-0007RR-Ru for bug-gnu-emacs@gnu.org; Sun, 01 Dec 2024 21:15:02 -0500 Resent-From: Yuan Fu Original-Sender: "Debbugs-submit" Resent-To: bug-gnu-emacs@gnu.org Resent-Date: Mon, 02 Dec 2024 02:15:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: cc-closed 74507 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Mail-Followup-To: 74507@debbugs.gnu.org, casouri@gmail.com, j@lambda.is Original-Received: via spool by 74507-done@debbugs.gnu.org id=D74507.173310567628516 (code D ref 74507); Mon, 02 Dec 2024 02:15:02 +0000 Original-Received: (at 74507-done) by debbugs.gnu.org; 2 Dec 2024 02:14:36 +0000 Original-Received: from localhost ([127.0.0.1]:53670 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tHvxL-0007Pq-89 for submit@debbugs.gnu.org; Sun, 01 Dec 2024 21:14:35 -0500 Original-Received: from mail-pl1-f179.google.com ([209.85.214.179]:60843) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tHvxI-0007Pf-MJ for 74507-done@debbugs.gnu.org; Sun, 01 Dec 2024 21:14:33 -0500 Original-Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2129fd7b1a5so28839115ad.1 for <74507-done@debbugs.gnu.org>; Sun, 01 Dec 2024 18:14:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733105612; x=1733710412; darn=debbugs.gnu.org; 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=VUVUyt2qdGLCPI7KBAXPAPE6b8X/EAcN7z19fAbxtWY=; b=Ei2L9a02XeKXIKZXJ36oPSTymlE6+9kXGpZNqxHLIip4V37KzEvWdyDLxO1JFcMUMB 7XFuq/ffIaq899i/gqBp9FM2tbCXN6IzBCzR0Ia1fG1i8tFv4PsGUbgpSiAr1/p/+euW A3uAocDX+fIKP49I3fn+VV36MgQklLdyTjfheqKj7ed+QEMgyppQWMjL3A6NOBjD8616 tVS9eTWbwlSqPvKoqGfO7jfN4Ye3eHxGDHNHCm/VXbv0Lb7umEcHWR7g4e8fv0gEDycf WW3P0UqJN3biEA4tLq1NMdikc6YatjQgHiz+WqjvX95PO5dA13AUwTdb3kEgakU0cH+M u4/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733105612; x=1733710412; 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=VUVUyt2qdGLCPI7KBAXPAPE6b8X/EAcN7z19fAbxtWY=; b=AQItEY0zBI5Icyf9yf4EomwLiiDHAzUE0zu2w9ItvJink68yXsXkx5wNoyOqXeZwJ6 tQSvPcXIym9IrCKcF7ccdvCNfdhvTdJtZKNOQHi+pTaVv5UyMf8qe9BWpzaQRLy/X8Hr +EwHlY3FjnukbcwM3vXxUoY1Xc+Jn1EVEbTJxsLJvPTL0QiyY/hTTJj4ShynWtjN8uaR fu54/jeg3Pw04QRW9zzjVOPl6XhhXT/pEJSI6EZPWbbmw4RxUoeDO2UT1t9KUYi0yviW 3+B2ywVIUiI1j72YaTM7WdvOTXhWLzOyP3KwCXUUF/kPO3ysL8OXjePqrxG8YnmLKPV2 jFaQ== X-Forwarded-Encrypted: i=1; AJvYcCUu8YDMSA2washxz79YUCdpw708niM81/SyTX/1X0EbNIvR2vjlBknQ6t47p9duziPH1goHfTY/pzlx@debbugs.gnu.org X-Gm-Message-State: AOJu0YyCVL0ky3XhLoyVADx2JXThyvq6QtSLqmskcE0ne3IFe3mHuOII /4PsYaKMLtPp1cnvSkvKht2xdun5SMfNTYCNfAnai7AMg7Wois7O X-Gm-Gg: ASbGnctgzwYkf3R5/2Hud8/XX1jt1lO1DmQV9EQ3ic3XNQ1K129fQTE5A81qcH37Pvq tWJMfYOCCpx2aSLdiFJ/5lKilxV9qj2BTfEJ7jIKcwF+pqqM8NwEReWPGGwI9DIebK4cFXzaVvl mOXw/hm2vcQwH1cFVU3V51u7uxaZZNRbKVK9UyxdzJXq70XPzivjZo/lAJx7nnkXk7aN7z0BxGQ Lnk8CY5p2hLk6C0aJAGMn5bhXE85/qerKQOTRopryL3oVh1rBtD8yRlAaGH9jh+jXkH2heriA== X-Google-Smtp-Source: AGHT+IHRq9rcB9ptJBUQN4v/awxRcPyvUHF7oUywSbfNlNpMO1wbKP2pPn2h5kDj3b2OFU/qAp2Vrw== X-Received: by 2002:a17:902:f64e:b0:215:7893:a99f with SMTP id d9443c01a7336-2157893abbdmr74072155ad.34.1733105611619; Sun, 01 Dec 2024 18:13:31 -0800 (PST) Original-Received: from smtpclient.apple ([2601:646:8f81:6120:71b7:718f:7faa:8436]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2156344b362sm28785605ad.62.2024.12.01.18.13.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 01 Dec 2024 18:13:30 -0800 (PST) In-Reply-To: <67232cd8-0557-4801-be76-76a965a8c60e@lambda.is> X-Mailer: Apple Mail (2.3776.700.51) 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:296292 Archived-At: > On Dec 1, 2024, at 2:46=E2=80=AFPM, J=C3=B8rgen Kvalsvik = wrote: >=20 > On 12/1/24 22:48, Yuan Fu wrote: >>> On Dec 1, 2024, at 1:51=E2=80=AFAM, J=C3=B8rgen Kvalsvik = wrote: >>>=20 >>> On 12/1/24 10:25, Yuan Fu wrote: >>>>> On Nov 30, 2024, at 12:49=E2=80=AFPM, J=C3=B8rgen Kvalsvik = wrote: >>>>>=20 >>>>> On 11/30/24 01:16, Yuan Fu wrote: >>>>>>> On Nov 29, 2024, at 1:05=E2=80=AFAM, J=C3=B8rgen Kvalsvik = wrote: >>>>>>>=20 >>>>>>> On 11/29/24 08:57, Eli Zaretskii wrote: >>>>>>>>> Cc: theo@thornhill.no,casouri@gmail.com >>>>>>>>> From: J=C3=B8rgen Kvalsvik >>>>>>>>> Date: Sun, 24 Nov 2024 10:15:12 +0100 >>>>>>>>>=20 >>>>>>>>> * 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. >>>>>>>=20 >>>>>>> Ok. Yuan, would you like me to submit a revision? >>>>>> Since your assignment is already done, let=E2=80=99s just apply = your patch, and I=E2=80=99ll rebase my changes on top of yours. So yeah, = do send the revision patch, thanks! >>>>>> Yuan >>>>>=20 >>>>> Certainly - I posted it on the bug tracker. >>>> Thanks J=C3=B8rgen. What did you use to generate the patch? For = some reason I can=E2=80=99t apply it. My git skill isn=E2=80=99t that = great so it could be my problem. If you can apply it fine maybe you can = share the command you used? >>>=20 >>> 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. >>>=20 >>>> 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 >>>=20 >>> 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 =3D 0; var !=3D (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=E2=80=99t at top-level = too. Here=E2=80=99s the updated test: >> #define IOTA(var, n) for (int var =3D 0; var !=3D (n); ++var) >> int main() >> { >> IOTA (v, 10) { >> printf("%d ", v); >> } >> const char *msg =3D "Hello, world!"; { >> puts("Hello, world!"); >> } >> for (int i =3D 0; >> i < 10; >> i++) { >> IOTA (v, 10) { >> printf("%d ", v); >> } >> } >> { >> IOTA (v, 10) { >> printf("%d ", v); >> } >> } >> } >> The only test case that doesn=E2=80=99t pass right now is this one: >> const char *msg =3D "Hello, world!"; { >> puts("Hello, world!"); >> } >> Is this a real use-case? >> Yuan >=20 > 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. >=20 > That being said, it is probably rare enough in practice to prio it = less. Ok, I pushed your patch and my indentation rework to master. I end up = removing the test mentioned above since it=E2=80=99s not a real = use-case, in favor of making the heuristic indent rule more specific. Yuan