Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle arbitrary expression types in aref_field nodes #381

Merged
merged 4 commits into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions fixtures/small/aref_field_actual.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# As above
a[0] = b
# So below

a[puts a] = ""
a[puts(a)] = ""
a[()] = ""
a[class A; def foo; puts a; end; end] = ""
a[begin; ""; rescue StandardException; ""; end] = ""
# Users can override #[]= to not have args
a[] = ""
# Users can override #[]= to have multiple args
a[1, 2] = ""

a[puts a]
a[puts(a)]
a[()]
a[class A; def foo; puts a; end; end]
a[begin; ""; rescue StandardException; ""; end]
# Users can override #[] to not have args
a[]
# Users can override #[] to have multiple args
a[1, 2]
44 changes: 44 additions & 0 deletions fixtures/small/aref_field_expected.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
# As above
a[0] = b
# So below

a[puts(a)] = ""
a[puts(a)] = ""
a[()] = ""
a[
class A
def foo
puts(a)
end
end
] = ""
a[
begin
""
rescue StandardException
""
end
] = ""
Comment on lines +8 to +21
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm gonna call this a "reasonable" rendering? I mean, users writing this is already pretty surprising, so I think this is the most consistent way to handle it rather than special-casing aref fields to inline these or something like that 🤷‍♂️

# Users can override #[]= to not have args
a[] = ""
# Users can override #[]= to have multiple args
a[1, 2] = ""

a[puts(a)]
a[puts(a)]
a[()]
a[
class A
def foo
puts(a)
end
end
]
a[
begin
""
rescue StandardException
""
end
]
# Users can override #[] to not have args
a[]
# Users can override #[] to have multiple args
a[1, 2]
30 changes: 17 additions & 13 deletions librubyfmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1755,19 +1755,18 @@ pub fn format_aref_field(ps: &mut dyn ConcreteParserState, af: ArefField) {
false,
Box::new(|ps| {
format_expression(ps, *af.1);
let aab = af.2;
match aab.2 {
ToProcExpr::Present(_) => {
panic!("got a to_proc in an aref_field, should be impossible");
}
ToProcExpr::NotPresent(_) => {
format_array_fast_path(
ps,
end_line,
Some((aab.1).into_args_add_star_or_expression_list()),
);

let index_expr = af.2;
let arr_contents = match index_expr {
None => None,
Some(ArgsAddBlockOrExpressionList::ArgsAddBlock(aab)) => {
Some(aab.1.into_args_add_star_or_expression_list())
}
}
Some(ArgsAddBlockOrExpressionList::ExpressionList(expr_list)) => Some(
ArgsAddStarOrExpressionListOrArgsForward::ExpressionList(expr_list),
),
};
format_array_fast_path(ps, end_line, arr_contents);
}),
);

Expand Down Expand Up @@ -2299,7 +2298,12 @@ fn format_constant_body(ps: &mut dyn ConcreteParserState, bodystmt: Box<BodyStmt
}));

ps.on_line(end_line);
ps.emit_end();
ps.with_start_of_line(
true,
Box::new(|ps| {
ps.emit_end();
}),
);
if ps.at_start_of_line() {
ps.emit_newline();
}
Expand Down
15 changes: 13 additions & 2 deletions librubyfmt/src/render_targets.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::delimiters::BreakableDelims;
use crate::line_tokens::{AbstractLineToken, ConcreteLineTokenAndTargets};
use crate::line_tokens::{AbstractLineToken, ConcreteLineToken, ConcreteLineTokenAndTargets};
use crate::parser_state::FormattingContext;
use crate::types::{ColNumber, LineNumber};
use std::collections::HashSet;
Expand Down Expand Up @@ -144,7 +144,9 @@ impl AbstractTokenTarget for BreakableEntry {
}

fn is_multiline(&self) -> bool {
self.line_numbers.len() > 1 || self.any_collapsing_newline_has_heredoc_content()
self.line_numbers.len() > 1
|| self.any_collapsing_newline_has_heredoc_content()
|| self.contains_hard_newline()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think this was a latent bug we've had around forever -- some expressions are essentially always multiline (e.g. classes), so breakables that have these in them should always render multiline.

}

fn len(&self) -> usize {
Expand Down Expand Up @@ -177,4 +179,13 @@ impl BreakableEntry {
_ => false,
})
}

fn contains_hard_newline(&self) -> bool {
self.tokens.iter().any(|t| {
matches!(
t,
AbstractLineToken::ConcreteLineToken(ConcreteLineToken::HardNewLine)
)
})
}
}
2 changes: 1 addition & 1 deletion librubyfmt/src/ripper_tree_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def_tag!(aref_field_tag, "aref_field");
pub struct ArefField(
pub aref_field_tag,
pub Box<Expression>,
pub ArgsAddBlock,
pub Option<ArgsAddBlockOrExpressionList>,
pub LineCol,
);

Expand Down