Skip to content

Commit

Permalink
fix: presence of trailing comma after replacements and refactor code (#…
Browse files Browse the repository at this point in the history
…416)

Signed-off-by: Abhishek Kumar Gupta <[email protected]>
  • Loading branch information
abhishek818 committed Jul 15, 2024
1 parent 01bc0cd commit 7118310
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
39 changes: 22 additions & 17 deletions crates/core/src/inline_snippets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,28 +234,12 @@ pub(crate) fn inline_sorted_snippets_with_offset(
}
}

let mut trailing_comma_lines = vec![];

for (range, snippet) in replacements {
let range = adjust_range(&range.effective_range(), offset, &code)?;
if range.start > code.len() || range.end > code.len() {
bail!("Range {:?} is out of bounds for code:\n{}\n", range, code);
}
code.replace_range(range.clone(), snippet);

// Check if the replacement results in any lines containing just a single comma
let line_start = code[..range.start].rfind('\n').map_or(0, |pos| pos + 1);
let line_end = code[range.start..].find('\n').map_or(code.len(), |pos| range.start + pos);

let line_content = code[line_start..line_end].trim();
if line_content == "," {
trailing_comma_lines.push(line_start..line_end+1);
}
}

// Remove lines containing a trailing comma
for range in trailing_comma_lines {
code.replace_range(range, "");
code.replace_range(range, snippet);
}

Ok((code, output_ranges, original_ranges))
Expand Down Expand Up @@ -316,6 +300,27 @@ fn delete_hanging_comma(
replacements: &mut [(EffectRange, String)],
offset: usize,
) -> Result<(String, Vec<ReplacementInfo>)> {
// Handle the case when after applying replacements, a single comma
// gets left behind on a single line
let mut temp_code = code.to_string();
for (range, snippet) in replacements.iter_mut() {
let adjusted_range = adjust_range(&range.effective_range(), offset, &temp_code)?;
if adjusted_range.start > temp_code.len() || adjusted_range.end > temp_code.len() {
bail!("Range {:?} is out of bounds for code:\n{}\n", adjusted_range, temp_code);
}

temp_code.replace_range(adjusted_range.clone(), snippet);

let line_start = temp_code[..adjusted_range.start].rfind('\n').map_or(0, |pos| pos + 1);
let line_end = temp_code[adjusted_range.start..].find('\n').map_or(temp_code.len(), |pos| adjusted_range.start + pos);
let line_content = temp_code[line_start..line_end].trim();

if line_content == "," {
// inclusion of "," index in replacements
range.range.end += 1;
}
}

let deletion_ranges = replacements
.iter()
.filter_map(|r| {
Expand Down
1 change: 1 addition & 0 deletions crates/core/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12094,6 +12094,7 @@ fn trailing_comma_after_argument_removal() {
main_score="f1",
domains=["News"],
text_creation="created",
reference="https://huggingface.co/datasets/jupyterjazz/tbilisi-city-hall-titles",
date=("2024-05-02", "2024-05-03"),
form=["written"],
Expand Down

0 comments on commit 7118310

Please sign in to comment.