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#73661: [PATCH] Anchor to first sibling unless bol-sibling found Date: Sun, 06 Oct 2024 15:44:55 +0200 Message-ID: <87y131wdk8.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="32596"; mail-complaints-to="usenet@ciao.gmane.io" Cc: theo@thornhill.no,casouri@gmail.com To: 73661@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Oct 06 18:06:35 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 1sxTmF-0008IW-2v for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 06 Oct 2024 18:06:35 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sxTlj-0001Vk-Bl; Sun, 06 Oct 2024 12:06:03 -0400 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 1sxTlf-0001Ur-9H for bug-gnu-emacs@gnu.org; Sun, 06 Oct 2024 12:05:59 -0400 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 1sxTlf-0004BF-0g for bug-gnu-emacs@gnu.org; Sun, 06 Oct 2024 12:05:59 -0400 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=G7bB9JI+eM/59pmRDsmH2GdLFjDiYyQDdLIRiJENz6U=; b=tPp2u+EqYJu0W6CGlWAKtPhzWKbaCtukHEMGP6B6gsV0MGJ9vD09H640MIaAzBmJEPT+crh8f33GRRIMbzLcW36D0VFYlW2V6kDpCuXTAxwJ7EKRMxWZjDn2jBYElBm6uPDHIj39l+gnZvlfJFZQsVrQL9KjDM6ruVTXQ9ieeDUDmRf6ppR9HniuPdHiuOjH91kaDJLcQAHF8VKDJzJCPasM2rD8Ea+dV/dRo6624Ln76yQ5vSARLI7C8o3dOUIrB0Jx+coTMoPY/AB3m4AdU2iD17q4Ot9euI6PlUPjcinv55TwitzInF1zSAjfjnCaYMQaLLsSyTV96w3TniNb3w==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sxTli-0001a8-Ii; Sun, 06 Oct 2024 12:06:02 -0400 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, 06 Oct 2024 16:06:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 73661 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.17282307115678 (code B ref -1); Sun, 06 Oct 2024 16:06:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 6 Oct 2024 16:05:11 +0000 Original-Received: from localhost ([127.0.0.1]:42029 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sxTkp-0001Qg-H3 for submit@debbugs.gnu.org; Sun, 06 Oct 2024 12:05:11 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:36296) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sxRZm-0001yu-Ng for submit@debbugs.gnu.org; Sun, 06 Oct 2024 09:45:35 -0400 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 1sxRZg-0007IW-G1 for bug-gnu-emacs@gnu.org; Sun, 06 Oct 2024 09:45:28 -0400 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 1sxRZe-0006Dd-8v for bug-gnu-emacs@gnu.org; Sun, 06 Oct 2024 09:45:28 -0400 Original-Received: from localhost (unknown [127.0.0.1]) by mx.kolabnow.com (Postfix) with ESMTP id 5C7833507C10 for ; Sun, 6 Oct 2024 15:45:13 +0200 (CEST) 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:message-id:date:date :subject:subject:from:from:received:received:received; s= dkim20240523; t=1728222312; x=1730036713; bh=G7bB9JI+eM/59pmRDsm H2GdLFjDiYyQDdLIRiJENz6U=; b=JvW9FQkIkxAW2kf9FbwJxNGCHUWiGTGdU5p QXtz2bsWeaZytCR0keiLawU2moInPXKmUFGR1vR+9ir8tIZ7HXUeuO87Uii5jJot Lol+HkmcgjNUf/rA65ZDQ7QgBu7Qulq9qCYt/0Uv/YBptcvr1MtYGxVll/9RRUtT 5U/vR0q270qfA+F5LzOreFgwwjigZiFKNU7j1NjKGACa95VzWchdwqm8Nob7Hdvt TrU9P6d0Pibi+5RWGPxRY8Ae2QGbD5pz6Oto0uSks+W8cgmJDa6leKJmRVu4Ln9B iLvrOMmlXV8hMjdG2c55sEk6vkkuuAxmwVyfZGZG2Edt5qd9Ymg== 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 V0ZQgO51btPv for ; Sun, 6 Oct 2024 15:45:12 +0200 (CEST) Original-Received: from int-mx009.mykolab.com (unknown [10.9.13.9]) by mx.kolabnow.com (Postfix) with ESMTPS id 367FC3507C0F for ; Sun, 6 Oct 2024 15:45:11 +0200 (CEST) Original-Received: from ext-subm010.mykolab.com (unknown [10.9.6.10]) by int-mx009.mykolab.com (Postfix) with ESMTPS id 74E2D20AA57A for ; Sun, 6 Oct 2024 15:45:11 +0200 (CEST) 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-Mailman-Approved-At: Sun, 06 Oct 2024 12:05:07 -0400 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:293093 Archived-At: --=-=-= Content-Type: text/plain Tags: patch Indenting would give up when initializer lists and similar constructs were broken up over multiple lines where the first item was not also beginning-of-line. Items should anchor on the first sibling (like with function calls and init-lists in general). The search for a sibling to anchor on should stop at the first sibling at bol or the first sibling in the (sub)tree. This brings indent up to c-mode parity for code like: return { x1, x2, ..., y1, x2, ... }; In GNU Emacs 31.0.50 (build 1, x86_64-pc-linux-gnu) of 2024-10-06 built on ada Repository revision: c8e5f2ee9f6155ef545c2fe8ddf702c4f16a6eea Repository branch: master System Description: Debian GNU/Linux 12 (bookworm) Configured using: 'configure --with-x-toolkit=no --with-xpm=ifavailable --with-jpeg=ifavailable --with-png=ifavailable --with-gif=ifavailable --with-tiff=ifavailable --with-gnutls=ifavailable --with-tree-sitter --with-native-compilation --prefix=/home/j/src/emacs/build/root' --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-Anchor-to-first-sibling-unless-bol-sibling-found.patch >From 0bf96b008b57298ffcc63b9bb8222358ce966c21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Kvalsvik?= Date: Fri, 4 Oct 2024 21:38:33 +0200 Subject: [PATCH] Anchor to first sibling unless bol-sibling found The behavior differed between c-mode/c++-mode and *-ts-mode for initializer lists where the first element was not at beginning-of-line. The anchor-prev-sibling function gave up and returned nil, but it should (probably) anchor on the first element in the initializer list, such as this: return { v1, v2, ..., y1, y2, ... }; c-ts-mode behaved better and figured out how to align, but I added a test for a similar compound literal to prevent regressions. * lisp/progmodes/c-ts-mode.el (c-ts-mode--anchor-prev-sibling): Anchor at first sibling unless bol is found. * test/lisp/progmodes/c-ts-mode-resources/indent.erts: New initializer list and compound literal test. --- lisp/progmodes/c-ts-mode.el | 9 +++-- .../progmodes/c-ts-mode-resources/indent.erts | 39 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index 303c994637c..3f6a7422aa0 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -338,10 +338,13 @@ c-ts-mode--anchor-prev-sibling ;; If the start of the previous sibling isn't at the ;; beginning of a line, something's probably not quite ;; right, go a step further. (E.g., comment after a - ;; statement.) + ;; statement.) If the previous sibling is the first named + ;; node then anchor to that, e.g. when returning an aggregate + ;; and starting the items on the same line as {. (_ (goto-char (treesit-node-start prev-sibling)) - (if (looking-back (rx bol (* whitespace)) - (line-beginning-position)) + (if (or (looking-back (rx bol (* whitespace)) + (line-beginning-position))) + (null (treesit-node-prev-sibling prev-sibling t)) (setq continue nil) (setq prev-sibling (treesit-node-prev-sibling prev-sibling))))))) diff --git a/test/lisp/progmodes/c-ts-mode-resources/indent.erts b/test/lisp/progmodes/c-ts-mode-resources/indent.erts index 599173832b5..a13a74cf8b3 100644 --- a/test/lisp/progmodes/c-ts-mode-resources/indent.erts +++ b/test/lisp/progmodes/c-ts-mode-resources/indent.erts @@ -208,6 +208,21 @@ int main() } =-=-= +Name: Return Compund Literal + +=-= +struct pair { int fst, snd; }; +struct pair +make_pair(int long_identifier_a[], int long_identifier_b[], + int offset_a, int offset_b) +{ + int base_offset = 10; + return (struct pair) { long_identifier_a[base_offset + offset_b], + long_identifier_b[base_offset + offset_b] }; +} + +=-=-= + Name: Switch-Case statement =-= @@ -486,6 +501,30 @@ namespace A { } =-=-= +Name: Return Aggregate Initialized Struct + +=-= +struct pair { int x, y; } +pair +make_pair(int long_identifier_a[], int long_identifier_b[], + int offset_a, int offset_b) +{ + int base_offset = 10; + return { long_identifier_a[base_offset + offset_b], + long_identifier_b[base_offset + offset_b] }; +} +=-= +struct pair { int x, y; } +pair +make_pair(int long_identifier_a[], int long_identifier_b[], + int offset_a, int offset_b) +{ + int base_offset = 10; + return { long_identifier_a[base_offset + offset_b], + long_identifier_b[base_offset + offset_b] }; +} +=-=-= + Code: (lambda () (c-ts-mode) -- 2.39.5 --=-=-=--