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 in c-ts-mode when { is not BOL Date: Sat, 30 Nov 2024 16:39:48 +0100 Message-ID: <8734j8zq63.fsf@lambda.is> References: <87r071yov3.fsf@lambda.is> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="1690"; mail-complaints-to="usenet@ciao.gmane.io" To: 74507@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Nov 30 16:41:12 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 1tHPap-0000Ev-KO for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 30 Nov 2024 16:41:11 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tHPai-0002xY-9p; Sat, 30 Nov 2024 10:41: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 1tHPag-0002xQ-RI for bug-gnu-emacs@gnu.org; Sat, 30 Nov 2024 10:41:02 -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 1tHPag-0001yT-35 for bug-gnu-emacs@gnu.org; Sat, 30 Nov 2024 10:41:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:From:Date:To:In-Reply-To:References:Subject; bh=ZeaVDHyWXRQbfTLNRMMIeNApUKNouwpVF698ECH6exQ=; b=cMkxfPpYQ19btpwB8mMNEsAwl1fOkCjXFn7Y1HlicKI+FHBpe/VobU9PrH6beMavYKzoUxaA5C2YhwaMAkHO5fWDOIjnNvsmtlslkpN5NBt6qKjfbClBFu6G8SR5YLw5NqvcELeTHDjgziOTT90pXRRdYNjkyQ9Z4lqxSrVyiTEVA95tdh8qBLxlr9sE3p0/fuuvTqofKqdDY39HR6U1lQngXgn1g0MtbFPRpyD6ofbGPmDo/jDORHSWD7Nhj3x092i54qRZ7WjTldB0/2KA8TRNgcy8jMXAFgdeZtW7ay1CcYP9lLAhDoQI0cTvsIQNS9m9vn4z3FMgAWPleFuqQQ==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tHPaf-0000MJ-Tr for bug-gnu-emacs@gnu.org; Sat, 30 Nov 2024 10:41:01 -0500 X-Loop: help-debbugs@gnu.org In-Reply-To: <87r071yov3.fsf@lambda.is> Resent-From: =?UTF-8?Q?J=C3=B8rgen?= Kvalsvik Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 30 Nov 2024 15:41:01 +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.1732981206894 (code B ref 74507); Sat, 30 Nov 2024 15:41:01 +0000 Original-Received: (at 74507) by debbugs.gnu.org; 30 Nov 2024 15:40:06 +0000 Original-Received: from localhost ([127.0.0.1]:48328 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tHPZl-0000EH-Lp for submit@debbugs.gnu.org; Sat, 30 Nov 2024 10:40:06 -0500 Original-Received: from mx.kolabnow.com ([212.103.80.154]:48298) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tHPZi-0000DL-ST for 74507@debbugs.gnu.org; Sat, 30 Nov 2024 10:40:03 -0500 Original-Received: from localhost (unknown [127.0.0.1]) by mx.kolabnow.com (Postfix) with ESMTP id B27C920B2764 for <74507@debbugs.gnu.org>; Sat, 30 Nov 2024 16:39:56 +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-type:content-type:mime-version:subject:subject:from:from :message-id:date:date:received:received:received; s= dkim20240523; t=1732981195; x=1734795596; bh=h0pykLEJoE1zd7tK9aO pOPdrpLyyFTfJzxGPN5QLl98=; b=a74Gw20xPLexjRbToDjrzPbTn8MPL8XH8ov K6ElKiUmJfDI3otg2pTfv2FhUOP836RAMtUHkwx41DhP2x8NbP2xJFemCYarsrjF gAVpo8JydwrrdCohvmvOPeKTNQfhjG61o6ZuxLFuMaEnHYvv4HBNFQv0CZouKWyL vrXGIEKGnf3lgR++sHNee8iR+GZH9Ov1sb764RmOGHFyHE0s0f+qgfiF8RG5Hdv4 Rni3doZx64YfOBhmd8MDQHxT1zUWa8+8OnJ2qb/tIjTwSyozS6mAPM2ZGtTPsUGp NPNSt0L4drglW3awLYbc/Il3NmPPCP6CKQOOlaBY/t59mR1wSQA== X-Virus-Scanned: amavis at mykolab.com X-Amavis-Alert: BAD HEADER SECTION, MIME error: error: unexpected end of header; ; error: part did not end with expected boundary; ; error: unexpected end of parts before epilogue 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 bJtd20KBCdN7 for <74507@debbugs.gnu.org>; Sat, 30 Nov 2024 16:39:55 +0100 (CET) Original-Received: from int-mx009.mykolab.com (unknown [10.9.13.9]) by mx.kolabnow.com (Postfix) with ESMTPS id 6BCF220B274D for <74507@debbugs.gnu.org>; Sat, 30 Nov 2024 16:39:54 +0100 (CET) Original-Received: from ext-subm010.mykolab.com (unknown [10.9.6.10]) by int-mx009.mykolab.com (Postfix) with ESMTPS id 1B139288545C for <74507@debbugs.gnu.org>; Sat, 30 Nov 2024 16:39:54 +0100 (CET) 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:296171 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Tags: patch Here you go. Thank you for the feedback, Eli. Thanks, J=C3=B8rgen =20 --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-Indent-compounds-in-c-ts-mode-when-is-not-BOL.patch From: =?UTF-8?q?J=C3=B8rgen=20Kvalsvik?= Date: Tue, 19 Nov 2024 08:01:01 +0100 Subject: [PATCH] Indent compounds in c-ts-mode when { is not BOL Properly indent the body of compound expressions, even when then compound expression is not at the beginning of line and the parent is not an if/for/while/etc., and matches the behavior of c-mode. This fixes a problem that is common with macros and in testing frameworks. For example, you expect this to indent: TEST_CASE(1) { assert (...); } If the compound statement is the function body itself, don't apply this new rule and instead guide by the parent and first sibling. I'm sure there are subtle interactions that aren't handled properly by checking for "function_definition" rather than something more general, but it does fix the test case and the check can be improved as more cases are found. * 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. --- lisp/progmodes/c-ts-mode.el | 9 ++++++ .../progmodes/c-ts-mode-resources/indent.erts | 30 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index 3823c553fda..b635e621b03 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -402,6 +402,12 @@ c-ts-mode--first-sibling (treesit-node-start parent) (line-end-position)))))) +(defun c-ts-mode--parent-is-not-top-compound (_n parent &rest _) + "Matches when PARENT is not the top level compound statement. +The top-level compound is the {} that immediately follows the function +signature." + (not (equal "function_definition" (treesit-node-type (treesit-node-parent parent))))) + (defun c-ts-mode--indent-styles (mode) "Indent rules supported by `c-ts-mode'. MODE is either `c' or `cpp'." @@ -478,6 +484,7 @@ c-ts-mode--indent-styles ;; Closing bracket. This should be before initializer_list ;; (and probably others) rule because that rule (and other ;; similar rules) will match the closing bracket. (Bug#61398) + ((and (node-is "}") c-ts-mode--parent-is-not-top-compound) parent-bol 0) ((node-is "}") standalone-parent 0) ,@(when (eq mode 'cpp) '(((node-is "access_specifier") parent-bol 0) @@ -497,6 +504,8 @@ c-ts-mode--indent-styles ((parent-is "field_declaration_list") c-ts-mode--anchor-prev-sibling 0) ;; Statement in {} blocks. + ((and (parent-is "compound_statement") c-ts-mode--parent-is-not-top-compound) + parent-bol c-ts-mode-indent-offset) ((or (and (parent-is "compound_statement") ;; If the previous sibling(s) are not on their ;; own line, indent as if this node is the first diff --git a/test/lisp/progmodes/c-ts-mode-resources/indent.erts b/test/lisp/progmodes/c-ts-mode-resources/indent.erts index 2f3540c3970..61e61677ed7 100644 --- a/test/lisp/progmodes/c-ts-mode-resources/indent.erts +++ b/test/lisp/progmodes/c-ts-mode-resources/indent.erts @@ -223,6 +223,36 @@ make_pair(int long_identifier_a[], int long_identifier_b[], =-=-= +Name: Compound Statement after code + +=-= +#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!"); +} +} + +=-= +#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!"); + } +} + +=-=-= + Name: Switch-Case statement =-= -- 2.39.5 --=-=-= Content-Type: text/plain Date: Sat, 30 Nov 2024 16:39:38 +0100 Message-ID: <874j3ozq6d.fsf@lambda.is> --=-=-=--