Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
edit_prediction: Remove duplicate definition of interpolate_edits (#44485)
Piotr Osiewicz1471105643c4
diff --git a/Cargo.lock b/Cargo.lockindex 9de96dfe48c..5790df07111 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -5247,6 +5247,7 @@ dependencies = ["client","gpui","language",+ "text",][[package]]diff --git a/crates/edit_prediction/src/prediction.rs b/crates/edit_prediction/src/prediction.rsindex d169cf26e1d..8aa2a821856 100644--- a/crates/edit_prediction/src/prediction.rs+++ b/crates/edit_prediction/src/prediction.rs@@ -6,8 +6,9 @@ use std::{};use cloud_llm_client::EditPredictionRejectReason;+use edit_prediction_types::interpolate_edits;use gpui::{AsyncApp, Entity, SharedString};-use language::{Anchor, Buffer, BufferSnapshot, EditPreview, OffsetRangeExt, TextBufferSnapshot};+use language::{Anchor, Buffer, BufferSnapshot, EditPreview, TextBufferSnapshot};use serde::Serialize;#[derive(Clone, Default, Debug, PartialEq, Eq, Hash)]@@ -53,7 +54,7 @@ impl EditPredictionResult {.read_with(cx, |buffer, cx| {let new_snapshot = buffer.snapshot();let edits: Arc<[_]> =- interpolate_edits(&edited_buffer_snapshot, &new_snapshot, edits)?.into();+ interpolate_edits(&edited_buffer_snapshot, &new_snapshot, &edits)?.into();Some((edits.clone(), new_snapshot, buffer.preview_edits(edits, cx)))})@@ -109,7 +110,7 @@ impl EditPrediction {&self,new_snapshot: &TextBufferSnapshot,) -> Option<Vec<(Range<Anchor>, Arc<str>)>> {- interpolate_edits(&self.snapshot, new_snapshot, self.edits.clone())+ interpolate_edits(&self.snapshot, new_snapshot, &self.edits)}pub fn targets_buffer(&self, buffer: &Buffer) -> bool {@@ -130,52 +131,6 @@ impl std::fmt::Debug for EditPrediction {}}-pub fn interpolate_edits(- old_snapshot: &TextBufferSnapshot,- new_snapshot: &TextBufferSnapshot,- current_edits: Arc<[(Range<Anchor>, Arc<str>)]>,-) -> Option<Vec<(Range<Anchor>, Arc<str>)>> {- let mut edits = Vec::new();-- let mut model_edits = current_edits.iter().peekable();- for user_edit in new_snapshot.edits_since::<usize>(&old_snapshot.version) {- while let Some((model_old_range, _)) = model_edits.peek() {- let model_old_range = model_old_range.to_offset(old_snapshot);- if model_old_range.end < user_edit.old.start {- let (model_old_range, model_new_text) = model_edits.next().unwrap();- edits.push((model_old_range.clone(), model_new_text.clone()));- } else {- break;- }- }-- if let Some((model_old_range, model_new_text)) = model_edits.peek() {- let model_old_offset_range = model_old_range.to_offset(old_snapshot);- if user_edit.old == model_old_offset_range {- let user_new_text = new_snapshot- .text_for_range(user_edit.new.clone())- .collect::<String>();-- if let Some(model_suffix) = model_new_text.strip_prefix(&user_new_text) {- if !model_suffix.is_empty() {- let anchor = old_snapshot.anchor_after(user_edit.old.end);- edits.push((anchor..anchor, model_suffix.into()));- }-- model_edits.next();- continue;- }- }- }-- return None;- }-- edits.extend(model_edits.cloned());-- if edits.is_empty() { None } else { Some(edits) }-}-#[cfg(test)]mod tests {use super::*;diff --git a/crates/edit_prediction_types/Cargo.toml b/crates/edit_prediction_types/Cargo.tomlindex ebc09680e1d..00a8577911a 100644--- a/crates/edit_prediction_types/Cargo.toml+++ b/crates/edit_prediction_types/Cargo.toml@@ -15,3 +15,4 @@ path = "src/edit_prediction_types.rs"client.workspace = truegpui.workspace = truelanguage.workspace = true+text.workspace = truediff --git a/crates/edit_prediction_types/src/edit_prediction_types.rs b/crates/edit_prediction_types/src/edit_prediction_types.rsindex 1f63b8626d1..fbcb3c4c00e 100644--- a/crates/edit_prediction_types/src/edit_prediction_types.rs+++ b/crates/edit_prediction_types/src/edit_prediction_types.rs@@ -2,7 +2,7 @@ use std::{ops::Range, sync::Arc};use client::EditPredictionUsage;use gpui::{App, Context, Entity, SharedString};-use language::{Anchor, Buffer, BufferSnapshot, OffsetRangeExt};+use language::{Anchor, Buffer, OffsetRangeExt};// TODO: Find a better home for `Direction`.//@@ -252,8 +252,8 @@ where/// Returns edits updated based on user edits since the old snapshot. None is returned if any user/// edit is not a prefix of a predicted insertion.pub fn interpolate_edits(- old_snapshot: &BufferSnapshot,- new_snapshot: &BufferSnapshot,+ old_snapshot: &text::BufferSnapshot,+ new_snapshot: &text::BufferSnapshot,current_edits: &[(Range<Anchor>, Arc<str>)],) -> Option<Vec<(Range<Anchor>, Arc<str>)>> {let mut edits = Vec::new();