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 13:48:24 -0800 Message-ID: <2407F047-D3EA-4260-9987-D674EFD2932A@gmail.com> 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> 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="28929"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Eli Zaretskii , theo@thornhill.no, 74507@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 Sun Dec 01 22:50:26 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 1tHrph-0007OW-I0 for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 01 Dec 2024 22:50:25 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tHrpO-0005Ga-8Q; Sun, 01 Dec 2024 16:50:06 -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 1tHrpL-0005GI-RA for bug-gnu-emacs@gnu.org; Sun, 01 Dec 2024 16:50: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 1tHrpL-0001yd-HB for bug-gnu-emacs@gnu.org; Sun, 01 Dec 2024 16:50:03 -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=SusWqjVnxP1zdbnVEFMN4Epygz+IdADdbPJ6vm1Y0nw=; b=S7jViJoppkbR/nHsQEA8nDjE6CEdpi0NKZZqFjCO7gY0OZgPrvW+U8QYCWeZhx6/nrBxmW2b1kzoAPQTU6TVklSdw0GHFx/vgyqheqGyY4fXUqlIi9JAvaf7A5Il6lNLvjKHtdMxm8U0vwAv0pTyfknc4hjj6nxEisyGbZ/lTXOX9QsoX0ORTRorpeWGZvsE7vdaUEm8bvYNAvaZgZTEUah/4lhgk7HVV49nszU8le0Q5dhbu8JEZnaLqLF7Hib+V81YcRHdVWU1CBiWHF/FOC5NgZotcpgYfEqzNVyN8GlCFkWnz8ZuIda26OWvZ3U1gWdnvShG8tz+MWKvpMM5aA==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tHrpK-0002NU-BZ for bug-gnu-emacs@gnu.org; Sun, 01 Dec 2024 16:50: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: Sun, 01 Dec 2024 21:50: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.17330897859101 (code B ref 74507); Sun, 01 Dec 2024 21:50:02 +0000 Original-Received: (at 74507) by debbugs.gnu.org; 1 Dec 2024 21:49:45 +0000 Original-Received: from localhost ([127.0.0.1]:53333 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tHrp3-0002Mi-9B for submit@debbugs.gnu.org; Sun, 01 Dec 2024 16:49:45 -0500 Original-Received: from mail-pl1-f171.google.com ([209.85.214.171]:60550) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tHrp0-0002MT-Kh for 74507@debbugs.gnu.org; Sun, 01 Dec 2024 16:49:43 -0500 Original-Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-21288402a26so25247775ad.0 for <74507@debbugs.gnu.org>; Sun, 01 Dec 2024 13:49:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733089717; x=1733694517; 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=SusWqjVnxP1zdbnVEFMN4Epygz+IdADdbPJ6vm1Y0nw=; b=hSfXJyggmsMGtCE8dqDhQCjlW6t4KXBjJ1oPb890jPLzztnaBOEEwl+RkQUXODGjHm PNRq8dFxogo1uvmdwBJyJBNRgVqjQcs5WxOnFosTMwf9+pC0aheGmrTBuvYNSxT4+3+h EVO6ybeR6rLNEFnx0B+7dOXHFfbk76YFhz7VOFO8mDZ9rUKpU8b9LQDYMMiaGcL4hE97 jmN8I403Go6xbWkTImKqaBt1eoqIZlnmDh8Li1saIqcGaST1phbPEveTDsMwAp8zaR8K Vy1PNQP5OgzlUprLnzR+3s4jU6nU06Lq5nQkV0HVbP8D/jWEcpOo+QwnBxGaVPMmc1ux 64+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733089717; x=1733694517; 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=SusWqjVnxP1zdbnVEFMN4Epygz+IdADdbPJ6vm1Y0nw=; b=iFmoSNSnqxUMPKBW8RNiFQVpKoPfzc1Cyr6zfGIyPEr98PrHd5uEUV2b5dVnicwl2R IT0tFX82c1Y2ZSwC16v7Io/iZBnWimpS9M/MAfkcopVpa8pjI4i6xMIZVr6LtA0EXtKN xJOPtHBu9zs07inHk/Ju5p6NyBfDcr33t+DSS5AoXQQKEe6CiYEjsaMGmrUCni/JbfvZ BrPEZzhL90EvhgCZ52A+FAcIsJPHwN4eEk9/9DY0afq6W26I1dAlJEe0bbYgS/YGw+O6 1SCx2t4EFlZTN6KCHGw5dPGrCKGR4P9nlAR2uGdNY57o4LdPr+WpSADqtP6GyWeawcQ2 usHA== X-Forwarded-Encrypted: i=1; AJvYcCVbFLBFOjf3r06gBMUqWkPhEX9Sj0Hku9SuI5/wKprOEefnjawN4gbE6QyH4OMSFDBCHh5o6w==@debbugs.gnu.org X-Gm-Message-State: AOJu0YyUFen+W/fD3Xi/PwUG3qg0HThmNB5+mJfr1MVkWCw7WOfp9vhz eoBi72rNmiiDmcSDXL6odalK/EpYUt6KFzE4HzVqNGkBKmaLOltt X-Gm-Gg: ASbGncuA56pUxkBkLisCCi0pSUmbuJmqPvsGAc9a4y6pKS5CZ92rHjJO+PJ5gyBzwYx RugjXis1rgu9ROUgTBfLnE113aqnNlIP6ZUagoB3rfDo/KKwZEersSBUlbdHUI2hX6VA6yzicxA x3adsG0Juv7r6sziBCXGRR94AxSLkzMy9JGM8FfqWSWpzr5gFC2Y9dOlAleB7Fb9Vs91TVQxjTI UbzpVnxZAmSmBy9pC547ILE0/5FftwLIuWO6haU3qymfLjdb3L6vfbYYJoysUI0n6UjRHcbMw== X-Google-Smtp-Source: AGHT+IHUZL2a+9w0ZQFyGV68G2vHfURcm42NVzT/tNzQOJg/seAgruMW78HPbGqoVi+76uEI2PoYLA== X-Received: by 2002:a17:902:f546:b0:215:6fcd:6cd1 with SMTP id d9443c01a7336-2156fcd6e39mr71650935ad.7.1733089716647; Sun, 01 Dec 2024 13:48:36 -0800 (PST) Original-Received: from smtpclient.apple ([2601:646:8f81:6120:71b7:718f:7faa:8436]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2155249b0cesm33765985ad.211.2024.12.01.13.48.35 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 01 Dec 2024 13:48:35 -0800 (PST) In-Reply-To: <2b677735-ecc0-4fb6-be60-533bfbcc962a@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:296282 Archived-At: > 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.=20 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=