From a37762209d9ec79e4a23a9e8f5068ba1d435d59b Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Fri, 12 Jul 2024 02:07:39 +0530 Subject: [PATCH 1/5] fix isssue of danlging comma --- crates/core/src/inline_snippets.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/core/src/inline_snippets.rs b/crates/core/src/inline_snippets.rs index 0044c8d61..836d0e5a0 100644 --- a/crates/core/src/inline_snippets.rs +++ b/crates/core/src/inline_snippets.rs @@ -353,6 +353,7 @@ fn delete_hanging_comma( if Some(&index) != next_comma { result.push(c); } else { + next_comma = to_delete.next(); // Keep track of ranges we need to expand into, since we deleted code in the range // This isn't perfect, but it's good enough for tracking cell boundaries for (range, ..) in replacement_ranges.iter_mut().rev() { @@ -362,7 +363,6 @@ fn delete_hanging_comma( } } ranges_updates = update_range_shifts(index + offset, &ranges_updates, &ranges); - next_comma = to_delete.next(); } } From 4a4fb7d1985db8984e91859595c9e97b5fc0c1e5 Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Fri, 12 Jul 2024 02:54:05 +0530 Subject: [PATCH 2/5] add test to evaluate delete_hanging_comma fn --- crates/core/src/test.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/crates/core/src/test.rs b/crates/core/src/test.rs index a76b46d2b..ba38e21cd 100644 --- a/crates/core/src/test.rs +++ b/crates/core/src/test.rs @@ -6330,6 +6330,41 @@ fn inline_list_length() { .unwrap(); } +#[test] +fn test_delete_hanging_comma() { + run_test_expected(TestArgExpected { + pattern: r#" + |language python + | + |`TaskMetadata($args)` => `TaskMetadata($args)` + |"# + .trim_margin() + .unwrap(), + source: r#" + |TaskMetadata( + | name="TestTask", + | description="This is a test task.", + | type="TestType", + | , + | reference="https://example.com/test", + |) + |"# + .trim_margin() + .unwrap(), + expected: r#" + |TaskMetadata( + | name="TestTask", + | description="This is a test task.", + | type="TestType", + | reference="https://example.com/test", + |) + |"# + .trim_margin() + .unwrap(), + }) + .unwrap(); +} + #[test] fn string_length() { run_test_expected({ From a27a41fbd8aa1059cbc9c576459028b853fd19d1 Mon Sep 17 00:00:00 2001 From: chintu-777 <160070339+chintu-777@users.noreply.github.com> Date: Fri, 12 Jul 2024 14:49:12 +0530 Subject: [PATCH 3/5] refined approach --- crates/core/src/inline_snippets.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/core/src/inline_snippets.rs b/crates/core/src/inline_snippets.rs index 836d0e5a0..7046c92d0 100644 --- a/crates/core/src/inline_snippets.rs +++ b/crates/core/src/inline_snippets.rs @@ -348,12 +348,18 @@ fn delete_hanging_comma( .iter() .map(|r| (r.0.effective_range(), r.1.len())) .collect(); - + + // Flag to track if the last character was a comma + let mut last_was_comma = false; + for (index, c) in chars { if Some(&index) != next_comma { + if c == ',' && last_was_comma { + continue; + } result.push(c); + last_was_comma = c == ','; } else { - next_comma = to_delete.next(); // Keep track of ranges we need to expand into, since we deleted code in the range // This isn't perfect, but it's good enough for tracking cell boundaries for (range, ..) in replacement_ranges.iter_mut().rev() { @@ -363,6 +369,8 @@ fn delete_hanging_comma( } } ranges_updates = update_range_shifts(index + offset, &ranges_updates, &ranges); + next_comma = to_delete.next(); + last_was_comma = false; } } From 6531fca293892abe00c683cc8f716193af944c27 Mon Sep 17 00:00:00 2001 From: chintu-777 <160070339+chintu-777@users.noreply.github.com> Date: Fri, 12 Jul 2024 15:10:24 +0530 Subject: [PATCH 4/5] Update inline_snippets.rs --- crates/core/src/inline_snippets.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/crates/core/src/inline_snippets.rs b/crates/core/src/inline_snippets.rs index 7046c92d0..4c5b887be 100644 --- a/crates/core/src/inline_snippets.rs +++ b/crates/core/src/inline_snippets.rs @@ -350,28 +350,26 @@ fn delete_hanging_comma( .collect(); // Flag to track if the last character was a comma - let mut last_was_comma = false; + let mut previous_char_was_comma = false; for (index, c) in chars { if Some(&index) != next_comma { - if c == ',' && last_was_comma { + next_comma = to_delete.next(); + previous_char_was_comma = false; + continue; + } + if c == ',' { + if previous_char_was_comma { continue; } - result.push(c); - last_was_comma = c == ','; + previous_char_was_comma = true; } else { // Keep track of ranges we need to expand into, since we deleted code in the range // This isn't perfect, but it's good enough for tracking cell boundaries - for (range, ..) in replacement_ranges.iter_mut().rev() { - if range.end >= index { - range.end += 1; - break; - } + previous_char_was_comma = false; + } - ranges_updates = update_range_shifts(index + offset, &ranges_updates, &ranges); - next_comma = to_delete.next(); - last_was_comma = false; - } + result.push(c); } for (r, u) in replacements.iter_mut().zip(ranges_updates) { From adb33e8b8e869bed415c271e6fb6277a8e01230c Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Fri, 12 Jul 2024 15:43:11 +0530 Subject: [PATCH 5/5] fix lint --- crates/core/src/inline_snippets.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/crates/core/src/inline_snippets.rs b/crates/core/src/inline_snippets.rs index 4c5b887be..7046c92d0 100644 --- a/crates/core/src/inline_snippets.rs +++ b/crates/core/src/inline_snippets.rs @@ -350,26 +350,28 @@ fn delete_hanging_comma( .collect(); // Flag to track if the last character was a comma - let mut previous_char_was_comma = false; + let mut last_was_comma = false; for (index, c) in chars { if Some(&index) != next_comma { - next_comma = to_delete.next(); - previous_char_was_comma = false; - continue; - } - if c == ',' { - if previous_char_was_comma { + if c == ',' && last_was_comma { continue; } - previous_char_was_comma = true; + result.push(c); + last_was_comma = c == ','; } else { // Keep track of ranges we need to expand into, since we deleted code in the range // This isn't perfect, but it's good enough for tracking cell boundaries - previous_char_was_comma = false; - + for (range, ..) in replacement_ranges.iter_mut().rev() { + if range.end >= index { + range.end += 1; + break; + } } - result.push(c); + ranges_updates = update_range_shifts(index + offset, &ranges_updates, &ranges); + next_comma = to_delete.next(); + last_was_comma = false; + } } for (r, u) in replacements.iter_mut().zip(ranges_updates) {