Introduce LocalFile trait
If you want to call `abs_path` or `load`, the file needs to be local. You call `as_local` which returns `Option<dyn LocalFile>` with those local-only methods. I think this makes it more explicit what works only locally vs everywhere.
This commit is contained in:
parent
ea9c5b0686
commit
66fce5ec8e
3 changed files with 51 additions and 35 deletions
|
@ -938,7 +938,7 @@ impl Project {
|
|||
let buffer_abs_path;
|
||||
if let Some(file) = File::from_dyn(buffer.file()) {
|
||||
worktree = file.worktree.clone();
|
||||
buffer_abs_path = file.abs_path(cx);
|
||||
buffer_abs_path = file.as_local().map(|f| f.abs_path(cx));
|
||||
} else {
|
||||
return Task::ready(Err(anyhow!("buffer does not belong to any worktree")));
|
||||
};
|
||||
|
@ -2181,8 +2181,8 @@ mod tests {
|
|||
cx.update(|cx| {
|
||||
let target_buffer = definition.target_buffer.read(cx);
|
||||
assert_eq!(
|
||||
target_buffer.file().unwrap().abs_path(cx),
|
||||
Some(dir.path().join("a.rs"))
|
||||
target_buffer.file().unwrap().as_local().unwrap().abs_path(cx),
|
||||
dir.path().join("a.rs")
|
||||
);
|
||||
assert_eq!(definition.target_range.to_offset(target_buffer), 9..10);
|
||||
assert_eq!(
|
||||
|
|
|
@ -1366,6 +1366,14 @@ pub struct File {
|
|||
}
|
||||
|
||||
impl language::File for File {
|
||||
fn as_local(&self) -> Option<&dyn language::LocalFile> {
|
||||
if self.is_local {
|
||||
Some(self)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn mtime(&self) -> SystemTime {
|
||||
self.mtime
|
||||
}
|
||||
|
@ -1374,13 +1382,6 @@ impl language::File for File {
|
|||
&self.path
|
||||
}
|
||||
|
||||
fn abs_path(&self, cx: &AppContext) -> Option<PathBuf> {
|
||||
self.worktree
|
||||
.read(cx)
|
||||
.as_local()
|
||||
.map(|local_worktree| local_worktree.abs_path.join(&self.path))
|
||||
}
|
||||
|
||||
fn full_path(&self, cx: &AppContext) -> PathBuf {
|
||||
let mut full_path = PathBuf::new();
|
||||
full_path.push(self.worktree.read(cx).root_name());
|
||||
|
@ -1448,16 +1449,6 @@ impl language::File for File {
|
|||
})
|
||||
}
|
||||
|
||||
fn load_local(&self, cx: &AppContext) -> Option<Task<Result<String>>> {
|
||||
let worktree = self.worktree.read(cx).as_local()?;
|
||||
let abs_path = worktree.absolutize(&self.path);
|
||||
let fs = worktree.fs.clone();
|
||||
Some(
|
||||
cx.background()
|
||||
.spawn(async move { fs.load(&abs_path).await }),
|
||||
)
|
||||
}
|
||||
|
||||
fn format_remote(
|
||||
&self,
|
||||
buffer_id: u64,
|
||||
|
@ -1510,6 +1501,25 @@ impl language::File for File {
|
|||
}
|
||||
}
|
||||
|
||||
impl language::LocalFile for File {
|
||||
fn abs_path(&self, cx: &AppContext) -> PathBuf {
|
||||
self.worktree
|
||||
.read(cx)
|
||||
.as_local()
|
||||
.unwrap()
|
||||
.abs_path
|
||||
.join(&self.path)
|
||||
}
|
||||
|
||||
fn load(&self, cx: &AppContext) -> Task<Result<String>> {
|
||||
let worktree = self.worktree.read(cx).as_local().unwrap();
|
||||
let abs_path = worktree.absolutize(&self.path);
|
||||
let fs = worktree.fs.clone();
|
||||
cx.background()
|
||||
.spawn(async move { fs.load(&abs_path).await })
|
||||
}
|
||||
}
|
||||
|
||||
impl File {
|
||||
pub fn from_dyn(file: Option<&dyn language::File>) -> Option<&Self> {
|
||||
file.and_then(|f| f.as_any().downcast_ref())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue