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

Fix merging of an update of a symbol with an insert_before operation before the same symbol #19450

Merged
merged 2 commits into from
Oct 19, 2024
Merged
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
87 changes: 85 additions & 2 deletions crates/assistant/src/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,28 @@ impl ResolvedEdit {
return false;
}

if let Some(description) = &mut self.description {
if let Some(other_description) = &other.description {
let other_offset_range = other_range.to_offset(buffer);
let offset_range = range.to_offset(buffer);

// If the other range is empty at the start of this edit's range, combine the new text
if other_offset_range.is_empty() && other_offset_range.start == offset_range.start {
self.new_text = format!("{}\n{}", other.new_text, self.new_text);
self.range.start = other_range.start;

if let Some((description, other_description)) =
self.description.as_mut().zip(other.description.as_ref())
{
*description = format!("{}\n{}", other_description, description)
}
} else {
if let Some((description, other_description)) =
self.description.as_mut().zip(other.description.as_ref())
{
description.push('\n');
description.push_str(other_description);
}
}

true
}
}
Expand Down Expand Up @@ -699,6 +715,73 @@ mod tests {
.unindent(),
cx,
);

// Ensure InsertBefore merges correctly with Update of the same text

assert_edits(
"
fn foo() {

}
"
.unindent(),
vec![
AssistantEditKind::InsertBefore {
old_text: "
fn foo() {"
.unindent(),
new_text: "
fn bar() {
qux();
}"
.unindent(),
description: "implement bar".into(),
},
AssistantEditKind::Update {
old_text: "
fn foo() {

}"
.unindent(),
new_text: "
fn foo() {
bar();
}"
.unindent(),
description: "call bar in foo".into(),
},
AssistantEditKind::InsertAfter {
old_text: "
fn foo() {

}
"
.unindent(),
new_text: "
fn qux() {
// todo
}
"
.unindent(),
description: "implement qux".into(),
},
],
"
fn bar() {
qux();
}

fn foo() {
bar();
}

fn qux() {
// todo
}
"
.unindent(),
cx,
);
}

#[track_caller]
Expand Down
Loading