Skip to content

Commit

Permalink
Support fndptn
Browse files Browse the repository at this point in the history
  • Loading branch information
reese authored Jan 7, 2024
1 parent 31bcbed commit 1f79e4a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
2 changes: 1 addition & 1 deletion fixtures/small/aryptn_actual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
case ["And I shall have some peace there", "for peace", "comes dropping slow"]
in String,;
"Dropping from the veils of the morning to where the cricket sings;"
in Midnight[*]
in Midnight[*glow]
"There midnight's all a glimmer, and noon a purple glow,"
in 1, *, 2
"And evening full of the linnet's wings."
Expand Down
2 changes: 1 addition & 1 deletion fixtures/small/aryptn_expected.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
case ["And I shall have some peace there", "for peace", "comes dropping slow"]
in [String, *]
"Dropping from the veils of the morning to where the cricket sings;"
in Midnight[*]
in Midnight[*glow]
"There midnight's all a glimmer, and noon a purple glow,"
in [1, *, 2]
"And evening full of the linnet's wings."
Expand Down
7 changes: 7 additions & 0 deletions fixtures/small/fndptn_actual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
case ["I will arise", "and go now", "for always night and day"]
in Lake[*, "I hear lake water lapping", "with low sounds by the shore", *waves]
<<~YEATS
While I stand on the roadway, or on the pavements grey,
I hear it in the deep heart's core.
YEATS
end
7 changes: 7 additions & 0 deletions fixtures/small/fndptn_expected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
case ["I will arise", "and go now", "for always night and day"]
in Lake[*, "I hear lake water lapping", "with low sounds by the shore", *waves]
<<~YEATS
While I stand on the roadway, or on the pavements grey,
I hear it in the deep heart's core.
YEATS
end
31 changes: 27 additions & 4 deletions librubyfmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3402,6 +3402,7 @@ fn format_in_or_else(ps: &mut dyn ConcreteParserState, in_or_else: InOrElse) {
fn format_pattern(ps: &mut dyn ConcreteParserState, pattern_node: PatternNode) {
match pattern_node {
PatternNode::Aryptn(aryptn) => format_aryptn(ps, aryptn),
PatternNode::Fndptn(fndptn) => format_fndptn(ps, fndptn),
}
}

Expand All @@ -3419,10 +3420,7 @@ fn format_aryptn(ps: &mut dyn ConcreteParserState, aryptn: Aryptn) {
vals.append(&mut pre_star_list.clone());
}
if let Some(star) = maybe_star {
vals.push(Expression::Ident(Ident::new(
"*".to_string(),
LineCol(star.2.start_line(), 0),
)));
vals.push(pattern_splat_as_expr(star));
}
if let Some(post_star_list) = maybe_post_star_list {
vals.append(&mut post_star_list.clone());
Expand All @@ -3432,6 +3430,31 @@ fn format_aryptn(ps: &mut dyn ConcreteParserState, aryptn: Aryptn) {
);
}

fn format_fndptn(ps: &mut dyn ConcreteParserState, fndptn: Fndptn) {
let Fndptn(_, maybe_collection_name, pre_splat, values, post_splat) = fndptn;
if let Some(collection_name) = maybe_collection_name {
format_var_ref(ps, collection_name);
}
ps.breakable_of(
BreakableDelims::for_array(),
Box::new(|ps| {
let mut vals = values.clone();
vals.insert(0, pattern_splat_as_expr(pre_splat));
vals.push(pattern_splat_as_expr(post_splat));

format_list_like_thing_items(ps, vals, None, false);
}),
);
}

fn pattern_splat_as_expr(var_field: VarField) -> Expression {
let mut ident = "*".to_string();
if let Some(name) = var_field.1 {
ident.push_str(name.to_local_string().as_str());
}
Expression::Ident(Ident::new(ident, LineCol(var_field.2.start_line(), 0)))
}

pub fn format_retry(ps: &mut dyn ConcreteParserState, r: Retry) {
format_keyword(
ps,
Expand Down
15 changes: 13 additions & 2 deletions librubyfmt/src/ripper_tree_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2384,18 +2384,29 @@ pub struct In(
#[derive(RipperDeserialize, Debug, Clone)]
pub enum PatternNode {
Aryptn(Aryptn),
Fndptn(Fndptn),
}

def_tag!(arrayptn_tag, "aryptn");
def_tag!(aryptn_tag, "aryptn");
#[derive(Deserialize, Debug, Clone)]
pub struct Aryptn(
pub arrayptn_tag,
pub aryptn_tag,
pub Option<VarRef>, // Container type, e.g. `in Foo["a", "b"]`
pub Option<Vec<Expression>>, // list of values before the first *
pub Option<VarField>, // "*" pattern
pub Option<Vec<Expression>>, // list of values the first *
);

def_tag!(fndptn_tag, "fndptn");
#[derive(Deserialize, Debug, Clone)]
pub struct Fndptn(
pub fndptn_tag,
pub Option<VarRef>, // Container type, e.g. `in Foo["a", "b"]`
pub VarField, // leading "*" pattern
pub Vec<Expression>, // inner values
pub VarField, // trailing "*" pattern
);

#[derive(RipperDeserialize, Debug, Clone)]
pub enum WhenOrElse {
When(When),
Expand Down

0 comments on commit 1f79e4a

Please sign in to comment.