Add BufferSnapshot::edited_ranges_for_transaction
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
1ee15e1a59
commit
ca697e1bba
2 changed files with 75 additions and 9 deletions
|
@ -502,7 +502,7 @@ fn test_history() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_avoid_grouping_next_transaction() {
|
fn test_finalize_last_transaction() {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
let mut buffer = Buffer::new(0, 0, History::new("123456".into()));
|
let mut buffer = Buffer::new(0, 0, History::new("123456".into()));
|
||||||
|
|
||||||
|
@ -536,6 +536,44 @@ fn test_avoid_grouping_next_transaction() {
|
||||||
assert_eq!(buffer.text(), "ab2cde6");
|
assert_eq!(buffer.text(), "ab2cde6");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_edited_ranges_for_transaction() {
|
||||||
|
let now = Instant::now();
|
||||||
|
let mut buffer = Buffer::new(0, 0, History::new("1234567".into()));
|
||||||
|
|
||||||
|
buffer.start_transaction_at(now);
|
||||||
|
buffer.edit(vec![2..4], "cd");
|
||||||
|
buffer.edit(vec![6..6], "efg");
|
||||||
|
buffer.end_transaction_at(now);
|
||||||
|
assert_eq!(buffer.text(), "12cd56efg7");
|
||||||
|
|
||||||
|
let tx = buffer.finalize_last_transaction().unwrap().clone();
|
||||||
|
assert_eq!(
|
||||||
|
buffer
|
||||||
|
.edited_ranges_for_transaction::<usize>(&tx)
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
[2..4, 6..9]
|
||||||
|
);
|
||||||
|
|
||||||
|
buffer.edit(vec![5..5], "hijk");
|
||||||
|
assert_eq!(buffer.text(), "12cd5hijk6efg7");
|
||||||
|
assert_eq!(
|
||||||
|
buffer
|
||||||
|
.edited_ranges_for_transaction::<usize>(&tx)
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
[2..4, 10..13]
|
||||||
|
);
|
||||||
|
|
||||||
|
buffer.edit(vec![4..4], "l");
|
||||||
|
assert_eq!(buffer.text(), "12cdl5hijk6efg7");
|
||||||
|
assert_eq!(
|
||||||
|
buffer
|
||||||
|
.edited_ranges_for_transaction::<usize>(&tx)
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
[2..4, 11..14]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_concurrent_edits() {
|
fn test_concurrent_edits() {
|
||||||
let text = "abcdef";
|
let text = "abcdef";
|
||||||
|
|
|
@ -1730,14 +1730,6 @@ impl BufferSnapshot {
|
||||||
self.visible_text.clip_point_utf16(point, bias)
|
self.visible_text.clip_point_utf16(point, bias)
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn point_for_offset(&self, offset: usize) -> Result<Point> {
|
|
||||||
// if offset <= self.len() {
|
|
||||||
// Ok(self.text_summary_for_range(0..offset))
|
|
||||||
// } else {
|
|
||||||
// Err(anyhow!("offset out of bounds"))
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
pub fn edits_since<'a, D>(
|
pub fn edits_since<'a, D>(
|
||||||
&'a self,
|
&'a self,
|
||||||
since: &'a clock::Global,
|
since: &'a clock::Global,
|
||||||
|
@ -1748,6 +1740,42 @@ impl BufferSnapshot {
|
||||||
self.edits_since_in_range(since, Anchor::min()..Anchor::max())
|
self.edits_since_in_range(since, Anchor::min()..Anchor::max())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn edited_ranges_for_transaction<'a, D>(
|
||||||
|
&'a self,
|
||||||
|
transaction: &'a Transaction,
|
||||||
|
) -> impl 'a + Iterator<Item = Range<D>>
|
||||||
|
where
|
||||||
|
D: TextDimension,
|
||||||
|
{
|
||||||
|
let mut cursor = self.fragments.cursor::<(VersionedFullOffset, usize)>();
|
||||||
|
let mut rope_cursor = self.visible_text.cursor(0);
|
||||||
|
let cx = Some(transaction.end.clone());
|
||||||
|
let mut position = D::default();
|
||||||
|
transaction.ranges.iter().map(move |range| {
|
||||||
|
cursor.seek_forward(&VersionedFullOffset::Offset(range.start), Bias::Right, &cx);
|
||||||
|
let mut start_offset = cursor.start().1;
|
||||||
|
if cursor
|
||||||
|
.item()
|
||||||
|
.map_or(false, |fragment| fragment.is_visible(&self.undo_map))
|
||||||
|
{
|
||||||
|
start_offset += range.start - cursor.start().0.full_offset()
|
||||||
|
}
|
||||||
|
position.add_assign(&rope_cursor.summary(start_offset));
|
||||||
|
let start = position.clone();
|
||||||
|
|
||||||
|
cursor.seek_forward(&VersionedFullOffset::Offset(range.end), Bias::Left, &cx);
|
||||||
|
let mut end_offset = cursor.start().1;
|
||||||
|
if cursor
|
||||||
|
.item()
|
||||||
|
.map_or(false, |fragment| fragment.is_visible(&self.undo_map))
|
||||||
|
{
|
||||||
|
end_offset += range.end - cursor.start().0.full_offset();
|
||||||
|
}
|
||||||
|
position.add_assign(&rope_cursor.summary(end_offset));
|
||||||
|
start..position.clone()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub fn edits_since_in_range<'a, D>(
|
pub fn edits_since_in_range<'a, D>(
|
||||||
&'a self,
|
&'a self,
|
||||||
since: &'a clock::Global,
|
since: &'a clock::Global,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue