fix for nested ternary expressions#31
Merged
Merged
Conversation
Owner
|
Imo it would work better if all the wrap points belonged to the same group, something like that : case "ternary_expr":
String parent_type = ts::node_parent(node).type();
String field = node.field_name();
bool is_chained = parent_type == "ternary_expr" && (field == "alternative" || field == "consequence");
usz expr_id = 0;
if (is_chained)
{
// Inherit the group_id from the parent's wrap point
// Should be stored in C3Fmt because there could be another wrap group in-between the parent and us...
for (int i = (int)self.buf.len() - 1; i >= 0; i--)
{
if (self.buf[i].kind == TokenKind.WRAP_POINT || self.buf[i].kind == TokenKind.BEGIN_WRAP_GROUP)
{
expr_id = self.buf[i].wrap_point.group_id;
break;
}
}
}
// If not chained generate a new wrap group
if (expr_id == 0)
{
expr_id = self.begin_wrap_group(2);
}
self.process_composite(
node,
add_wrap_point_fn: fn (String type, String field, usz id)
{
switch (type)
{
case ":":
return { .indent = 1, .group_id = id };
default:
return NO_WRAP~;
}
}, group_id: expr_id);For instance, with the test it yields: fn void foo()
{
ulong i = int_from_5_chars(r[0], r[1], r[2], r[3], r[4]);
ch = i == int_from_5_chars('l', 't', 0, 0, 0) ? '<'
: i == int_from_5_chars('g', 't', 0, 0, 0) ? '>'
: i == int_from_5_chars('a', 'm', 'p', 0, 0) ? '&'
: i == int_from_5_chars('a', 'p', 'o', 's', 0) ? '\''
: i == int_from_5_chars('q', 'u', 'o', 't', 0) ? '"'
: 0;
}
fn void bar()
{
ch = i == int_from_5_chars('l', 't', 0, 0, 0) ? '<' : 0;
}We can always tune were it wraps. We could also handle elvis statements with the same code. |
ManuLinares
marked this pull request as ready for review
June 7, 2026 19:47
Collaborator
Author
|
I'm happier with it now, what do you think? I cleaned up the code a little bit, foreach_r seemed like a better/safer fit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I'm not completely happy about this one, maybe we could find a better/proper fix.