Perform initial file load git diff async
This commit is contained in:
parent
6825b6077a
commit
6633c0b328
3 changed files with 27 additions and 25 deletions
|
@ -425,8 +425,6 @@ impl Buffer {
|
||||||
UNIX_EPOCH
|
UNIX_EPOCH
|
||||||
};
|
};
|
||||||
|
|
||||||
let git_diff = smol::block_on(BufferDiff::new(head_text.clone(), &buffer));
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
saved_mtime,
|
saved_mtime,
|
||||||
saved_version: buffer.version(),
|
saved_version: buffer.version(),
|
||||||
|
@ -435,7 +433,7 @@ impl Buffer {
|
||||||
was_dirty_before_starting_transaction: None,
|
was_dirty_before_starting_transaction: None,
|
||||||
text: buffer,
|
text: buffer,
|
||||||
head_text,
|
head_text,
|
||||||
git_diff,
|
git_diff: BufferDiff::new(),
|
||||||
file,
|
file,
|
||||||
syntax_map: Mutex::new(SyntaxMap::new()),
|
syntax_map: Mutex::new(SyntaxMap::new()),
|
||||||
parsing_in_background: false,
|
parsing_in_background: false,
|
||||||
|
@ -659,16 +657,18 @@ impl Buffer {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_git(&mut self, cx: &mut ModelContext<Self>) {
|
pub fn update_git(&mut self, cx: &mut ModelContext<Self>) {
|
||||||
if self.head_text.is_some() {
|
if let Some(head_text) = &self.head_text {
|
||||||
let snapshot = self.snapshot();
|
let snapshot = self.snapshot();
|
||||||
let head_text = self.head_text.clone();
|
let head_text = head_text.clone();
|
||||||
|
|
||||||
let buffer_diff = cx
|
let mut diff = self.git_diff.clone();
|
||||||
.background()
|
let diff = cx.background().spawn(async move {
|
||||||
.spawn(async move { BufferDiff::new(head_text, &snapshot).await });
|
diff.update(&head_text, &snapshot).await;
|
||||||
|
diff
|
||||||
|
});
|
||||||
|
|
||||||
cx.spawn_weak(|this, mut cx| async move {
|
cx.spawn_weak(|this, mut cx| async move {
|
||||||
let buffer_diff = buffer_diff.await;
|
let buffer_diff = diff.await;
|
||||||
if let Some(this) = this.upgrade(&cx) {
|
if let Some(this) = this.upgrade(&cx) {
|
||||||
this.update(&mut cx, |this, cx| {
|
this.update(&mut cx, |this, cx| {
|
||||||
this.git_diff = buffer_diff;
|
this.git_diff = buffer_diff;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{ops::Range, sync::Arc};
|
use std::ops::Range;
|
||||||
|
|
||||||
use sum_tree::SumTree;
|
use sum_tree::SumTree;
|
||||||
use text::{Anchor, BufferSnapshot, OffsetRangeExt, Point, ToPoint};
|
use text::{Anchor, BufferSnapshot, OffsetRangeExt, Point, ToPoint};
|
||||||
|
@ -98,22 +98,16 @@ impl<'a> sum_tree::Dimension<'a, DiffHunkSummary> for HunkBufferEnd {
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct BufferDiff {
|
pub struct BufferDiff {
|
||||||
last_buffer_version: clock::Global,
|
last_buffer_version: Option<clock::Global>,
|
||||||
tree: SumTree<DiffHunk<Anchor>>,
|
tree: SumTree<DiffHunk<Anchor>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BufferDiff {
|
impl BufferDiff {
|
||||||
pub async fn new(head_text: Option<Arc<String>>, buffer: &text::BufferSnapshot) -> BufferDiff {
|
pub fn new() -> BufferDiff {
|
||||||
let mut instance = BufferDiff {
|
BufferDiff {
|
||||||
last_buffer_version: buffer.version().clone(),
|
last_buffer_version: None,
|
||||||
tree: SumTree::new(),
|
tree: SumTree::new(),
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(head_text) = head_text {
|
|
||||||
instance.update(&*head_text, buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
instance
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hunks_in_range<'a>(
|
pub fn hunks_in_range<'a>(
|
||||||
|
@ -142,10 +136,13 @@ impl BufferDiff {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn needs_update(&self, buffer: &text::BufferSnapshot) -> bool {
|
pub fn needs_update(&self, buffer: &text::BufferSnapshot) -> bool {
|
||||||
buffer.version().changed_since(&self.last_buffer_version)
|
match &self.last_buffer_version {
|
||||||
|
Some(last) => buffer.version().changed_since(last),
|
||||||
|
None => true,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, head_text: &str, buffer: &text::BufferSnapshot) {
|
pub async fn update(&mut self, head_text: &str, buffer: &text::BufferSnapshot) {
|
||||||
let mut tree = SumTree::new();
|
let mut tree = SumTree::new();
|
||||||
|
|
||||||
let buffer_text = buffer.as_rope().to_string();
|
let buffer_text = buffer.as_rope().to_string();
|
||||||
|
@ -160,7 +157,7 @@ impl BufferDiff {
|
||||||
}
|
}
|
||||||
|
|
||||||
self.tree = tree;
|
self.tree = tree;
|
||||||
self.last_buffer_version = buffer.version().clone();
|
self.last_buffer_version = Some(buffer.version().clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -279,7 +276,8 @@ mod tests {
|
||||||
.unindent();
|
.unindent();
|
||||||
|
|
||||||
let mut buffer = Buffer::new(0, 0, buffer_text);
|
let mut buffer = Buffer::new(0, 0, buffer_text);
|
||||||
let diff = smol::block_on(BufferDiff::new(Some(Arc::new(head_text.clone())), &buffer));
|
let mut diff = BufferDiff::new();
|
||||||
|
smol::block_on(diff.update(&head_text, &buffer));
|
||||||
assert_hunks(&diff, &buffer, &head_text, &[(1..2, "two\n")]);
|
assert_hunks(&diff, &buffer, &head_text, &[(1..2, "two\n")]);
|
||||||
|
|
||||||
buffer.edit([(0..0, "point five\n")]);
|
buffer.edit([(0..0, "point five\n")]);
|
||||||
|
|
|
@ -449,7 +449,11 @@ impl LocalWorktree {
|
||||||
let (file, contents, head_text) = this
|
let (file, contents, head_text) = this
|
||||||
.update(&mut cx, |t, cx| t.as_local().unwrap().load(&path, cx))
|
.update(&mut cx, |t, cx| t.as_local().unwrap().load(&path, cx))
|
||||||
.await?;
|
.await?;
|
||||||
Ok(cx.add_model(|cx| Buffer::from_file(0, contents, head_text, Arc::new(file), cx)))
|
Ok(cx.add_model(|cx| {
|
||||||
|
let mut buffer = Buffer::from_file(0, contents, head_text, Arc::new(file), cx);
|
||||||
|
buffer.update_git(cx);
|
||||||
|
buffer
|
||||||
|
}))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue