Load a file's head text on file load just to get started

This commit is contained in:
ForLoveOfCats 2022-08-30 11:08:22 -04:00 committed by Julia
parent efdedaab53
commit 2a14af4cde
6 changed files with 105 additions and 6 deletions

View file

@ -52,6 +52,7 @@ smol = "1.2.5"
thiserror = "1.0.29"
toml = "0.5"
rocksdb = "0.18"
git2 = "0.15"
[dev-dependencies]
client = { path = "../client", features = ["test-support"] }

View file

@ -1,9 +1,11 @@
use anyhow::{anyhow, Result};
use fsevent::EventStream;
use futures::{future::BoxFuture, Stream, StreamExt};
use git2::{Repository, RepositoryOpenFlags};
use language::LineEnding;
use smol::io::{AsyncReadExt, AsyncWriteExt};
use std::{
ffi::OsStr,
io,
os::unix::fs::MetadataExt,
path::{Component, Path, PathBuf},
@ -29,6 +31,7 @@ pub trait Fs: Send + Sync {
async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()>;
async fn open_sync(&self, path: &Path) -> Result<Box<dyn io::Read>>;
async fn load(&self, path: &Path) -> Result<String>;
async fn load_head_text(&self, path: &Path) -> Option<String>;
async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()>;
async fn canonicalize(&self, path: &Path) -> Result<PathBuf>;
async fn is_file(&self, path: &Path) -> bool;
@ -161,6 +164,38 @@ impl Fs for RealFs {
Ok(text)
}
async fn load_head_text(&self, path: &Path) -> Option<String> {
fn logic(path: &Path) -> Result<Option<String>> {
let repo = Repository::open_ext(path, RepositoryOpenFlags::empty(), &[OsStr::new("")])?;
assert!(repo.path().ends_with(".git"));
let repo_root_path = match repo.path().parent() {
Some(root) => root,
None => return Ok(None),
};
let relative_path = path.strip_prefix(repo_root_path)?;
let object = repo
.head()?
.peel_to_tree()?
.get_path(relative_path)?
.to_object(&repo)?;
let content = match object.as_blob() {
Some(blob) => blob.content().to_owned(),
None => return Ok(None),
};
let head_text = String::from_utf8(content.to_owned())?;
Ok(Some(head_text))
}
match logic(path) {
Ok(value) => return value,
Err(err) => log::error!("Error loading head text: {:?}", err),
}
None
}
async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()> {
let buffer_size = text.summary().len.min(10 * 1024);
let file = smol::fs::File::create(path).await?;
@ -748,6 +783,10 @@ impl Fs for FakeFs {
entry.file_content(&path).cloned()
}
async fn load_head_text(&self, _: &Path) -> Option<String> {
None
}
async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()> {
self.simulate_random_delay().await;
let path = normalize_path(path);

View file

@ -446,10 +446,10 @@ impl LocalWorktree {
) -> Task<Result<ModelHandle<Buffer>>> {
let path = Arc::from(path);
cx.spawn(move |this, mut cx| async move {
let (file, contents) = this
let (file, contents, head_text) = this
.update(&mut cx, |t, cx| t.as_local().unwrap().load(&path, cx))
.await?;
Ok(cx.add_model(|cx| Buffer::from_file(0, contents, Arc::new(file), cx)))
Ok(cx.add_model(|cx| Buffer::from_file(0, contents, head_text, Arc::new(file), cx)))
})
}
@ -558,13 +558,19 @@ impl LocalWorktree {
}
}
fn load(&self, path: &Path, cx: &mut ModelContext<Worktree>) -> Task<Result<(File, String)>> {
fn load(
&self,
path: &Path,
cx: &mut ModelContext<Worktree>,
) -> Task<Result<(File, String, Option<String>)>> {
let handle = cx.handle();
let path = Arc::from(path);
let abs_path = self.absolutize(&path);
let fs = self.fs.clone();
cx.spawn(|this, mut cx| async move {
let text = fs.load(&abs_path).await?;
let head_text = fs.load_head_text(&abs_path).await;
// Eagerly populate the snapshot with an updated entry for the loaded file
let entry = this
.update(&mut cx, |this, cx| {
@ -573,6 +579,7 @@ impl LocalWorktree {
.refresh_entry(path, abs_path, None, cx)
})
.await?;
Ok((
File {
entry_id: Some(entry.id),
@ -582,6 +589,7 @@ impl LocalWorktree {
is_local: true,
},
text,
head_text,
))
})
}