Use Rope instead of String for buffer diff base (#11300)
As an attempt to do things better when showing diff hunks, store diff base as Rope, not String, to have cheaper clones when the diff base text is reused, e.g. creating another buffer with the diff base text for hunk diff expanding. Release Notes: - N/A
This commit is contained in:
parent
5f0046b923
commit
b523ee6980
13 changed files with 131 additions and 65 deletions
|
@ -1,3 +1,4 @@
|
|||
use rope::Rope;
|
||||
use std::{iter, ops::Range};
|
||||
use sum_tree::SumTree;
|
||||
use text::{Anchor, BufferId, BufferSnapshot, OffsetRangeExt, Point};
|
||||
|
@ -179,11 +180,12 @@ impl BufferDiff {
|
|||
self.tree = SumTree::new();
|
||||
}
|
||||
|
||||
pub async fn update(&mut self, diff_base: &str, buffer: &text::BufferSnapshot) {
|
||||
pub async fn update(&mut self, diff_base: &Rope, buffer: &text::BufferSnapshot) {
|
||||
let mut tree = SumTree::new();
|
||||
|
||||
let diff_base_text = diff_base.to_string();
|
||||
let buffer_text = buffer.as_rope().to_string();
|
||||
let patch = Self::diff(diff_base, &buffer_text);
|
||||
let patch = Self::diff(&diff_base_text, &buffer_text);
|
||||
|
||||
if let Some(patch) = patch {
|
||||
let mut divergence = 0;
|
||||
|
@ -345,6 +347,7 @@ mod tests {
|
|||
three
|
||||
"
|
||||
.unindent();
|
||||
let diff_base_rope = Rope::from(diff_base.clone());
|
||||
|
||||
let buffer_text = "
|
||||
one
|
||||
|
@ -355,7 +358,7 @@ mod tests {
|
|||
|
||||
let mut buffer = Buffer::new(0, BufferId::new(1).unwrap(), buffer_text);
|
||||
let mut diff = BufferDiff::new();
|
||||
smol::block_on(diff.update(&diff_base, &buffer));
|
||||
smol::block_on(diff.update(&diff_base_rope, &buffer));
|
||||
assert_hunks(
|
||||
diff.hunks(&buffer),
|
||||
&buffer,
|
||||
|
@ -364,7 +367,7 @@ mod tests {
|
|||
);
|
||||
|
||||
buffer.edit([(0..0, "point five\n")]);
|
||||
smol::block_on(diff.update(&diff_base, &buffer));
|
||||
smol::block_on(diff.update(&diff_base_rope, &buffer));
|
||||
assert_hunks(
|
||||
diff.hunks(&buffer),
|
||||
&buffer,
|
||||
|
@ -391,6 +394,7 @@ mod tests {
|
|||
ten
|
||||
"
|
||||
.unindent();
|
||||
let diff_base_rope = Rope::from(diff_base.clone());
|
||||
|
||||
let buffer_text = "
|
||||
A
|
||||
|
@ -415,7 +419,7 @@ mod tests {
|
|||
|
||||
let buffer = Buffer::new(0, BufferId::new(1).unwrap(), buffer_text);
|
||||
let mut diff = BufferDiff::new();
|
||||
smol::block_on(diff.update(&diff_base, &buffer));
|
||||
smol::block_on(diff.update(&diff_base_rope, &buffer));
|
||||
assert_eq!(diff.hunks(&buffer).count(), 8);
|
||||
|
||||
assert_hunks(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue