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, 24 Nov 2024 10:15:12 +0100 Message-ID: <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="39304"; mail-complaints-to="usenet@ciao.gmane.io" Cc: theo@thornhill.no,casouri@gmail.com To: 74507@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Nov 24 10:16:34 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 1tF8jJ-000A8U-Qb for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 24 Nov 2024 10:16:34 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tF8it-0004tV-U2; Sun, 24 Nov 2024 04:16:07 -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 1tF8ip-0004sz-DL for bug-gnu-emacs@gnu.org; Sun, 24 Nov 2024 04:16:03 -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 1tF8ip-0002Q8-18 for bug-gnu-emacs@gnu.org; Sun, 24 Nov 2024 04:16:03 -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:Date:From:To:Subject; bh=sfwjHTu+j2xs3NK9Zb/1FrT5hCr1vNW+c1BqosFFBxc=; b=KkMxDFgr//Jrcn5DIG7oUNZXCYh+J7qkyKFBPGs7P3D8l4O/LEh2kPxJse8ROGbLjJLo8eKp13HO93iUNtClfqvLyMys68thoValjccxpckgjf57ondNtnezEyv4vAOmf7DRVlA3Y37WtdS8LMnWj8SYWNBXVOeMJzhWqtX8qGSYESGjbID4k5YNppjMnTn/FPQqQhjshKE5SXQ96fsbtx0BdvePrwhJqebK1ibnxwkUcIDOHRzGl8mY9r2YjlftYn/3D0PGOZlhJl/5I31IJzFVWJUd8l1EO1oJtmpqTBsjb/tTJ70cJoZbEz55ny08jQLVmmFRa7VJXLxOumZ4zA==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tF8io-000063-GH; Sun, 24 Nov 2024 04:16:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: =?UTF-8?Q?J=C3=B8rgen?= Kvalsvik Original-Sender: "Debbugs-submit" Resent-CC: theo@thornhill.no, casouri@gmail.com, bug-gnu-emacs@gnu.org Resent-Date: Sun, 24 Nov 2024 09:16:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 74507 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org X-Debbugs-Original-Xcc: theo@thornhill.no,casouri@gmail.com Original-Received: via spool by submit@debbugs.gnu.org id=B.1732439735326 (code B ref -1); Sun, 24 Nov 2024 09:16:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 24 Nov 2024 09:15:35 +0000 Original-Received: from localhost ([127.0.0.1]:32841 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tF8iN-00005A-3H for submit@debbugs.gnu.org; Sun, 24 Nov 2024 04:15:35 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:47092) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tF8iK-00004y-EW for submit@debbugs.gnu.org; Sun, 24 Nov 2024 04:15:33 -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 1tF8iJ-0004oS-MV for bug-gnu-emacs@gnu.org; Sun, 24 Nov 2024 04:15:32 -0500 Original-Received: from mx.kolabnow.com ([212.103.80.155]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tF8iH-0002GU-Cs for bug-gnu-emacs@gnu.org; Sun, 24 Nov 2024 04:15:31 -0500 Original-Received: from localhost (unknown [127.0.0.1]) by mx.kolabnow.com (Postfix) with ESMTP id 3A5BC3005A03 for ; Sun, 24 Nov 2024 10:15:21 +0100 (CET) Authentication-Results: ext-mx-out013.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:message-id:date:date :subject:subject:from:from:received:received:received; s= dkim20240523; t=1732439719; x=1734254120; bh=sfwjHTu+j2xs3NK9Zb/ 1FrT5hCr1vNW+c1BqosFFBxc=; b=Q8+6coCk0XnszA1Mt1wsmXz3pNSupGCuvcr +WScaXTfeIvTWtN28jGSgZbToB3ESqUFfHZjQ25Iv8WX+9udpLE8fBC8VFDIOQbW DxLu9U3AriHGVTztOdhwX0Wxyxjc+T37GDVxzX+ZsH4ovro18+hcRFsnkv0ACRz2 kTyY2UZQbUZs1wTNzlZ4OyN8JSAvn0SOSAMA1/vejp1yD7+4yQWGMGF+QXqtWQKk 4GXlFsukIND7gOlJfnFVBGMEFNmEBFqM35XrcttGD5D1yqkZMM0HYeW+kEIxWBic 5Z6NsCLblhLNtfW8CC+94SQHvWylCp6owH8D6un2XwfXNZINuxg== X-Virus-Scanned: amavis at mykolab.com Original-Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out013.mykolab.com [127.0.0.1]) (amavis, port 10024) with ESMTP id cl0O8QxCORek for ; Sun, 24 Nov 2024 10:15:19 +0100 (CET) Original-Received: from int-mx009.mykolab.com (unknown [10.9.13.9]) by mx.kolabnow.com (Postfix) with ESMTPS id 229AF30005A1 for ; Sun, 24 Nov 2024 10:15:17 +0100 (CET) Original-Received: from ext-subm010.mykolab.com (unknown [10.9.6.10]) by int-mx009.mykolab.com (Postfix) with ESMTPS id A77DB227A322 for ; Sun, 24 Nov 2024 10:15:17 +0100 (CET) Received-SPF: pass client-ip=212.103.80.155; envelope-from=j@lambda.is; helo=mx.kolabnow.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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:295898 Archived-At: --=-=-= Content-Type: text/plain Tags: patch Tags: patch I found that the coumpounded statements are anchored to the wrong object when 1. the compound is not at beginning-of-line and 2. it is not preceeded by a construct such as fndecl/for/if/while/etc., which differs from how c-mode indents it. Non-BOL compound statements is actually quite common with macros and testing frameworks (see the test case). For example, you want this to indent: TEST_CASE(1) { assert (...); } The heuristic is quite course - it simply checks if the grandparent is function-definition or not, which is really if this node is a top-level sibling in the function body. If that is the case, the old rules should apply and standalone-parent should be the guide; otherwise, it should be the parent-BOL. This feels a bit shaky but does seem to work well for the test cases, and can be refined in the future. --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-Indent-compounds-c-ts-mode-when-is-not-BOL.patch >From ad5c50851710711fa1d62708633d7bae92f82fae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Kvalsvik?= Date: Tue, 19 Nov 2024 08:01:01 +0100 Subject: [PATCH] Indent compounds 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 | 8 +++++ .../progmodes/c-ts-mode-resources/indent.erts | 30 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index 3823c553fda..80233e90241 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -402,6 +402,11 @@ 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 {} that immediately follows the 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 +483,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 +503,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 --=-=-=--