* bug#73661: [PATCH] Anchor to first sibling unless bol-sibling found
@ 2024-10-06 13:44 Jørgen Kvalsvik
2024-10-08 0:12 ` Yuan Fu
0 siblings, 1 reply; 5+ messages in thread
From: Jørgen Kvalsvik @ 2024-10-06 13:44 UTC (permalink / raw)
To: 73661; +Cc: theo, casouri
[-- Attachment #1: Type: text/plain, Size: 991 bytes --]
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'
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Anchor-to-first-sibling-unless-bol-sibling-found.patch --]
[-- Type: text/patch, Size: 3682 bytes --]
From 0bf96b008b57298ffcc63b9bb8222358ce966c21 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B8rgen=20Kvalsvik?= <j@lambda.is>
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
^ permalink raw reply related [flat|nested] 5+ messages in thread
* bug#73661: [PATCH] Anchor to first sibling unless bol-sibling found
2024-10-06 13:44 bug#73661: [PATCH] Anchor to first sibling unless bol-sibling found Jørgen Kvalsvik
@ 2024-10-08 0:12 ` Yuan Fu
2024-10-08 6:17 ` Jørgen Kvalsvik
2024-10-08 11:59 ` Eli Zaretskii
0 siblings, 2 replies; 5+ messages in thread
From: Yuan Fu @ 2024-10-08 0:12 UTC (permalink / raw)
To: Jørgen Kvalsvik; +Cc: 73661, Eli Zaretskii, Theodor Thornhill
> On Oct 6, 2024, at 6:44 AM, Jørgen Kvalsvik <j@lambda.is> wrote:
>
> 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, ... };
Awesome! Thanks for the patch. Have you signed the copyright assignment? Eli, does this count as a significant contribution that requires the copyright assignment?
Yuan
^ permalink raw reply [flat|nested] 5+ messages in thread
* bug#73661: [PATCH] Anchor to first sibling unless bol-sibling found
2024-10-08 0:12 ` Yuan Fu
@ 2024-10-08 6:17 ` Jørgen Kvalsvik
2024-10-08 11:59 ` Eli Zaretskii
1 sibling, 0 replies; 5+ messages in thread
From: Jørgen Kvalsvik @ 2024-10-08 6:17 UTC (permalink / raw)
To: Yuan Fu; +Cc: 73661, Eli Zaretskii, Theodor Thornhill
On 10/8/24 02:12, Yuan Fu wrote:
>
>
>> On Oct 6, 2024, at 6:44 AM, Jørgen Kvalsvik <j@lambda.is> wrote:
>>
>> 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, ... };
>
> Awesome! Thanks for the patch. Have you signed the copyright assignment? Eli, does this count as a significant contribution that requires the copyright assignment?
>
> Yuan
I have requested the paperwork, but not signed yet.
^ permalink raw reply [flat|nested] 5+ messages in thread
* bug#73661: [PATCH] Anchor to first sibling unless bol-sibling found
2024-10-08 0:12 ` Yuan Fu
2024-10-08 6:17 ` Jørgen Kvalsvik
@ 2024-10-08 11:59 ` Eli Zaretskii
2024-10-10 7:06 ` Yuan Fu
1 sibling, 1 reply; 5+ messages in thread
From: Eli Zaretskii @ 2024-10-08 11:59 UTC (permalink / raw)
To: Yuan Fu; +Cc: 73661, theo, j
> From: Yuan Fu <casouri@gmail.com>
> Date: Mon, 7 Oct 2024 17:12:22 -0700
> Cc: 73661@debbugs.gnu.org,
> Theodor Thornhill <theo@thornhill.no>,
> Eli Zaretskii <eliz@gnu.org>
>
> Awesome! Thanks for the patch. Have you signed the copyright assignment? Eli, does this count as a significant contribution that requires the copyright assignment?
This is small enough to accept without paperwork.
^ permalink raw reply [flat|nested] 5+ messages in thread
* bug#73661: [PATCH] Anchor to first sibling unless bol-sibling found
2024-10-08 11:59 ` Eli Zaretskii
@ 2024-10-10 7:06 ` Yuan Fu
0 siblings, 0 replies; 5+ messages in thread
From: Yuan Fu @ 2024-10-10 7:06 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: 73661-done, Theodor Thornhill, Jørgen Kvalsvik
> On Oct 8, 2024, at 4:59 AM, Eli Zaretskii <eliz@gnu.org> wrote:
>
>> From: Yuan Fu <casouri@gmail.com>
>> Date: Mon, 7 Oct 2024 17:12:22 -0700
>> Cc: 73661@debbugs.gnu.org,
>> Theodor Thornhill <theo@thornhill.no>,
>> Eli Zaretskii <eliz@gnu.org>
>>
>> Awesome! Thanks for the patch. Have you signed the copyright assignment? Eli, does this count as a significant contribution that requires the copyright assignment?
>
> This is small enough to accept without paperwork.
Merged. Thanks!
Yuan
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-10-10 7:06 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-06 13:44 bug#73661: [PATCH] Anchor to first sibling unless bol-sibling found Jørgen Kvalsvik
2024-10-08 0:12 ` Yuan Fu
2024-10-08 6:17 ` Jørgen Kvalsvik
2024-10-08 11:59 ` Eli Zaretskii
2024-10-10 7:06 ` Yuan Fu
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.