Integrate the new diff algorithm into the modal assistant
This commit is contained in:
parent
1ae5a909cd
commit
69b6967838
2 changed files with 101 additions and 235 deletions
|
@ -1,7 +1,7 @@
|
|||
use crate::{stream_completion, OpenAIRequest, RequestMessage, Role};
|
||||
use collections::HashMap;
|
||||
use editor::{Editor, ToOffset};
|
||||
use futures::StreamExt;
|
||||
use futures::{channel::mpsc, SinkExt, StreamExt};
|
||||
use gpui::{
|
||||
actions, elements::*, AnyViewHandle, AppContext, Entity, Task, View, ViewContext, ViewHandle,
|
||||
WeakViewHandle,
|
||||
|
@ -59,151 +59,67 @@ impl RefactoringAssistant {
|
|||
editor.id(),
|
||||
cx.spawn(|mut cx| {
|
||||
async move {
|
||||
let selection_start = selection.start.to_offset(&snapshot);
|
||||
let mut edit_start = selection.start.to_offset(&snapshot);
|
||||
|
||||
let mut new_text = String::new();
|
||||
let mut messages = response.await?;
|
||||
let (mut hunks_tx, mut hunks_rx) = mpsc::channel(1);
|
||||
let diff = cx.background().spawn(async move {
|
||||
let mut messages = response.await?.ready_chunks(4);
|
||||
let mut diff = crate::diff::Diff::new(selected_text);
|
||||
|
||||
let mut transaction = None;
|
||||
|
||||
while let Some(message) = messages.next().await {
|
||||
smol::future::yield_now().await;
|
||||
let mut message = message?;
|
||||
if let Some(choice) = message.choices.pop() {
|
||||
if let Some(text) = choice.delta.content {
|
||||
new_text.push_str(&text);
|
||||
|
||||
println!("-------------------------------------");
|
||||
|
||||
println!(
|
||||
"{}",
|
||||
similar::TextDiff::from_words(&selected_text, &new_text)
|
||||
.unified_diff()
|
||||
);
|
||||
|
||||
let mut changes =
|
||||
similar::TextDiff::from_words(&selected_text, &new_text)
|
||||
.iter_all_changes()
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut ix = 0;
|
||||
while ix < changes.len() {
|
||||
let deletion_start_ix = ix;
|
||||
let mut deletion_end_ix = ix;
|
||||
while changes
|
||||
.get(ix)
|
||||
.map_or(false, |change| change.tag() == ChangeTag::Delete)
|
||||
{
|
||||
ix += 1;
|
||||
deletion_end_ix += 1;
|
||||
while let Some(messages) = messages.next().await {
|
||||
let mut new_text = String::new();
|
||||
for message in messages {
|
||||
let mut message = message?;
|
||||
if let Some(choice) = message.choices.pop() {
|
||||
if let Some(text) = choice.delta.content {
|
||||
new_text.push_str(&text);
|
||||
}
|
||||
|
||||
let insertion_start_ix = ix;
|
||||
let mut insertion_end_ix = ix;
|
||||
while changes
|
||||
.get(ix)
|
||||
.map_or(false, |change| change.tag() == ChangeTag::Insert)
|
||||
{
|
||||
ix += 1;
|
||||
insertion_end_ix += 1;
|
||||
}
|
||||
|
||||
if deletion_end_ix > deletion_start_ix
|
||||
&& insertion_end_ix > insertion_start_ix
|
||||
{
|
||||
for _ in deletion_start_ix..deletion_end_ix {
|
||||
let deletion = changes.remove(deletion_end_ix);
|
||||
changes.insert(insertion_end_ix - 1, deletion);
|
||||
}
|
||||
}
|
||||
|
||||
ix += 1;
|
||||
}
|
||||
|
||||
while changes
|
||||
.last()
|
||||
.map_or(false, |change| change.tag() != ChangeTag::Insert)
|
||||
{
|
||||
changes.pop();
|
||||
}
|
||||
|
||||
editor.update(&mut cx, |editor, cx| {
|
||||
editor.buffer().update(cx, |buffer, cx| {
|
||||
if let Some(transaction) = transaction.take() {
|
||||
buffer.undo(cx); // TODO: Undo the transaction instead
|
||||
}
|
||||
|
||||
buffer.start_transaction(cx);
|
||||
let mut edit_start = selection_start;
|
||||
dbg!(&changes);
|
||||
for change in changes {
|
||||
let value = change.value();
|
||||
let edit_end = edit_start + value.len();
|
||||
match change.tag() {
|
||||
ChangeTag::Equal => {
|
||||
edit_start = edit_end;
|
||||
}
|
||||
ChangeTag::Delete => {
|
||||
let range = snapshot.anchor_after(edit_start)
|
||||
..snapshot.anchor_before(edit_end);
|
||||
buffer.edit([(range, "")], None, cx);
|
||||
edit_start = edit_end;
|
||||
}
|
||||
ChangeTag::Insert => {
|
||||
let insertion_start =
|
||||
snapshot.anchor_after(edit_start);
|
||||
buffer.edit(
|
||||
[(insertion_start..insertion_start, value)],
|
||||
None,
|
||||
cx,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
transaction = buffer.end_transaction(cx);
|
||||
})
|
||||
})?;
|
||||
}
|
||||
|
||||
let hunks = diff.push_new(&new_text);
|
||||
hunks_tx.send((hunks, new_text)).await?;
|
||||
}
|
||||
|
||||
if let Some(hunk) = diff.finish() {
|
||||
hunks_tx.send((vec![hunk], String::new())).await?;
|
||||
}
|
||||
|
||||
anyhow::Ok(())
|
||||
});
|
||||
|
||||
while let Some((hunks, new_text)) = hunks_rx.next().await {
|
||||
editor.update(&mut cx, |editor, cx| {
|
||||
editor.buffer().update(cx, |buffer, cx| {
|
||||
buffer.start_transaction(cx);
|
||||
let mut new_text_ix = 0;
|
||||
for hunk in hunks {
|
||||
match hunk {
|
||||
crate::diff::Hunk::Insert { len } => {
|
||||
let text = &new_text[new_text_ix..new_text_ix + len];
|
||||
let edit_start = snapshot.anchor_after(edit_start);
|
||||
buffer.edit([(edit_start..edit_start, text)], None, cx);
|
||||
new_text_ix += len;
|
||||
}
|
||||
crate::diff::Hunk::Remove { len } => {
|
||||
let edit_end = edit_start + len;
|
||||
let edit_range = snapshot.anchor_after(edit_start)
|
||||
..snapshot.anchor_before(edit_end);
|
||||
buffer.edit([(edit_range, "")], None, cx);
|
||||
edit_start = edit_end;
|
||||
}
|
||||
crate::diff::Hunk::Keep { len } => {
|
||||
edit_start += len;
|
||||
new_text_ix += len;
|
||||
}
|
||||
}
|
||||
}
|
||||
buffer.end_transaction(cx);
|
||||
})
|
||||
})?;
|
||||
}
|
||||
|
||||
editor.update(&mut cx, |editor, cx| {
|
||||
editor.buffer().update(cx, |buffer, cx| {
|
||||
if let Some(transaction) = transaction.take() {
|
||||
buffer.undo(cx); // TODO: Undo the transaction instead
|
||||
}
|
||||
|
||||
buffer.start_transaction(cx);
|
||||
let mut edit_start = selection_start;
|
||||
for change in similar::TextDiff::from_words(&selected_text, &new_text)
|
||||
.iter_all_changes()
|
||||
{
|
||||
let value = change.value();
|
||||
let edit_end = edit_start + value.len();
|
||||
match change.tag() {
|
||||
ChangeTag::Equal => {
|
||||
edit_start = edit_end;
|
||||
}
|
||||
ChangeTag::Delete => {
|
||||
let range = snapshot.anchor_after(edit_start)
|
||||
..snapshot.anchor_before(edit_end);
|
||||
buffer.edit([(range, "")], None, cx);
|
||||
edit_start = edit_end;
|
||||
}
|
||||
ChangeTag::Insert => {
|
||||
let insertion_start = snapshot.anchor_after(edit_start);
|
||||
buffer.edit(
|
||||
[(insertion_start..insertion_start, value)],
|
||||
None,
|
||||
cx,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
buffer.end_transaction(cx);
|
||||
})
|
||||
})?;
|
||||
|
||||
diff.await?;
|
||||
anyhow::Ok(())
|
||||
}
|
||||
.log_err()
|
||||
|
@ -285,75 +201,3 @@ impl RefactoringModal {
|
|||
}
|
||||
}
|
||||
}
|
||||
fn words(text: &str) -> impl Iterator<Item = (Range<usize>, &str)> {
|
||||
let mut word_start_ix = None;
|
||||
let mut chars = text.char_indices();
|
||||
iter::from_fn(move || {
|
||||
while let Some((ix, ch)) = chars.next() {
|
||||
if let Some(start_ix) = word_start_ix {
|
||||
if !ch.is_alphanumeric() {
|
||||
let word = &text[start_ix..ix];
|
||||
word_start_ix.take();
|
||||
return Some((start_ix..ix, word));
|
||||
}
|
||||
} else {
|
||||
if ch.is_alphanumeric() {
|
||||
word_start_ix = Some(ix);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
})
|
||||
}
|
||||
|
||||
fn streaming_diff<'a>(old_text: &'a str, new_text: &'a str) -> Vec<Change<'a, str>> {
|
||||
let changes = TextDiff::configure()
|
||||
.algorithm(similar::Algorithm::Patience)
|
||||
.diff_words(old_text, new_text);
|
||||
let mut changes = changes.iter_all_changes().peekable();
|
||||
|
||||
let mut result = vec![];
|
||||
|
||||
loop {
|
||||
let mut deletions = vec![];
|
||||
let mut insertions = vec![];
|
||||
|
||||
while changes
|
||||
.peek()
|
||||
.map_or(false, |change| change.tag() == ChangeTag::Delete)
|
||||
{
|
||||
deletions.push(changes.next().unwrap());
|
||||
}
|
||||
|
||||
while changes
|
||||
.peek()
|
||||
.map_or(false, |change| change.tag() == ChangeTag::Insert)
|
||||
{
|
||||
insertions.push(changes.next().unwrap());
|
||||
}
|
||||
|
||||
if !deletions.is_empty() && !insertions.is_empty() {
|
||||
result.append(&mut insertions);
|
||||
result.append(&mut deletions);
|
||||
} else {
|
||||
result.append(&mut deletions);
|
||||
result.append(&mut insertions);
|
||||
}
|
||||
|
||||
if let Some(change) = changes.next() {
|
||||
result.push(change);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove all non-inserts at the end.
|
||||
while result
|
||||
.last()
|
||||
.map_or(false, |change| change.tag() != ChangeTag::Insert)
|
||||
{
|
||||
result.pop();
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue