From e12573cd42b4602b8a4f0f3efdfe87d86dad1016 Mon Sep 17 00:00:00 2001 From: Reese Williams Date: Mon, 8 Jan 2024 00:39:18 +0000 Subject: [PATCH] Fix multiline pattern idempotency --- librubyfmt/rubyfmt_lib.rb | 8 ++++++++ librubyfmt/src/format.rs | 14 +++++++++++--- librubyfmt/src/ripper_tree_types.rs | 2 ++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/librubyfmt/rubyfmt_lib.rb b/librubyfmt/rubyfmt_lib.rb index a8a82449..195e6628 100644 --- a/librubyfmt/rubyfmt_lib.rb +++ b/librubyfmt/rubyfmt_lib.rb @@ -415,6 +415,14 @@ def on_var_field(ident) super + start_end end + def on_aryptn(*_) + with_lineno { super } + end + + def on_fndptn(*_) + with_lineno { super } + end + def on_tlambda(*args) @tlambda_stack << lineno super diff --git a/librubyfmt/src/format.rs b/librubyfmt/src/format.rs index c33dc67a..55e5a424 100644 --- a/librubyfmt/src/format.rs +++ b/librubyfmt/src/format.rs @@ -3408,8 +3408,14 @@ fn format_pattern(ps: &mut dyn ConcreteParserState, pattern_node: PatternNode) { fn format_aryptn(ps: &mut dyn ConcreteParserState, aryptn: Aryptn) { // Making this `mut` for - let Aryptn(_, maybe_collection_name, maybe_pre_star_list, maybe_star, maybe_post_star_list) = - aryptn; + let Aryptn( + _, + maybe_collection_name, + maybe_pre_star_list, + maybe_star, + maybe_post_star_list, + start_end, + ) = aryptn; if let Some(collection_name) = maybe_collection_name { format_var_ref(ps, collection_name); } @@ -3441,10 +3447,11 @@ fn format_aryptn(ps: &mut dyn ConcreteParserState, aryptn: Aryptn) { }), ); })); + ps.wind_dumping_comments_until_line(start_end.end_line()); } fn format_fndptn(ps: &mut dyn ConcreteParserState, fndptn: Fndptn) { - let Fndptn(_, maybe_collection_name, pre_splat, values, post_splat) = fndptn; + let Fndptn(_, maybe_collection_name, pre_splat, values, post_splat, start_end) = fndptn; if let Some(collection_name) = maybe_collection_name { format_var_ref(ps, collection_name); } @@ -3463,6 +3470,7 @@ fn format_fndptn(ps: &mut dyn ConcreteParserState, fndptn: Fndptn) { }), ); })); + ps.wind_dumping_comments_until_line(start_end.end_line()); } fn pattern_splat_as_expr(var_field: VarField) -> Expression { diff --git a/librubyfmt/src/ripper_tree_types.rs b/librubyfmt/src/ripper_tree_types.rs index 1a7914cb..201e7db5 100644 --- a/librubyfmt/src/ripper_tree_types.rs +++ b/librubyfmt/src/ripper_tree_types.rs @@ -2436,6 +2436,7 @@ pub struct Aryptn( pub Option>, // list of values before the first * pub Option, // "*" pattern pub Option>, // list of values the first * + pub StartEnd, ); def_tag!(fndptn_tag, "fndptn"); @@ -2446,6 +2447,7 @@ pub struct Fndptn( pub VarField, // leading "*" pattern pub Vec, // inner values pub VarField, // trailing "*" pattern + pub StartEnd, ); #[derive(RipperDeserialize, Debug, Clone)]