Skip to content

Commit 8b943c6

Browse files
committed
Restrict snapshot edits to a single-edit divergence from buffer
1 parent 0fa26c2 commit 8b943c6

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

crates/language/src/buffer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,15 +1245,14 @@ impl Buffer {
12451245
let registry = self.language_registry();
12461246
let language = self.language().cloned();
12471247
let old_snapshot = self.text.snapshot().clone();
1248-
let mut new_snapshot = old_snapshot.clone();
1248+
let new_snapshot = self.text.snapshot_with_edits(edits.iter().cloned());
12491249
let mut syntax_snapshot = self.syntax_map.lock().snapshot();
12501250
cx.background_spawn(async move {
12511251
if !edits.is_empty() {
12521252
if let Some(language) = language.clone() {
12531253
syntax_snapshot.reparse(&old_snapshot, registry.clone(), language);
12541254
}
12551255

1256-
new_snapshot.edit(edits.iter().cloned());
12571256
syntax_snapshot.interpolate(&new_snapshot);
12581257

12591258
if let Some(language) = language {

crates/text/src/text.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,27 @@ impl Buffer {
838838
self.snapshot
839839
}
840840

841+
/// Returns a preview snapshot derived by applying one synthetic local-branch edit.
842+
///
843+
/// This is intended for one-off preview workflows like syntax interpolation, not for
844+
/// repeatedly mutating or composing synthetic snapshot histories.
845+
pub fn snapshot_with_edits<I, S, T>(&self, edits: I) -> BufferSnapshot
846+
where
847+
I: IntoIterator<Item = (Range<S>, T)>,
848+
S: ToOffset,
849+
T: Into<Arc<str>>,
850+
{
851+
let mut snapshot = self.snapshot.clone();
852+
let timestamp = Lamport::new(ReplicaId::LOCAL_BRANCH).tick();
853+
let edits: Vec<_> = edits
854+
.into_iter()
855+
.map(|(range, new_text)| (range.to_offset(&snapshot), new_text.into()))
856+
.collect();
857+
snapshot.apply_edit_internal(edits, timestamp);
858+
snapshot.version.observe(timestamp);
859+
snapshot
860+
}
861+
841862
pub fn replica_id(&self) -> ReplicaId {
842863
self.lamport_clock.replica_id
843864
}
@@ -1854,24 +1875,6 @@ impl Deref for Buffer {
18541875
}
18551876

18561877
impl BufferSnapshot {
1857-
/// Edits the snapshot in place, applying the given edits to the text content.
1858-
/// This is useful for creating a modified snapshot without needing a full Buffer.
1859-
pub fn edit<I, S, T>(&mut self, edits: I)
1860-
where
1861-
I: IntoIterator<Item = (Range<S>, T)>,
1862-
S: ToOffset,
1863-
T: Into<Arc<str>>,
1864-
{
1865-
let mut lamport_clock = clock::Lamport::new(ReplicaId::LOCAL_BRANCH);
1866-
let timestamp = lamport_clock.tick();
1867-
let edits: Vec<_> = edits
1868-
.into_iter()
1869-
.map(|(range, new_text)| (range.to_offset(self), new_text.into()))
1870-
.collect();
1871-
self.apply_edit_internal(edits, timestamp);
1872-
self.version.observe(timestamp);
1873-
}
1874-
18751878
fn apply_edit_internal(
18761879
&mut self,
18771880
edits: Vec<(Range<usize>, Arc<str>)>,

0 commit comments

Comments
 (0)