Allow opening non-extant files (#9256)

Fixes #7400



Release Notes:

- Improved the `zed` command to not create files until you save them in
the editor ([#7400](https://github.com/zed-industries/zed/issues/7400)).
This commit is contained in:
Conrad Irwin 2024-03-12 22:30:04 -06:00 committed by GitHub
parent e792c1a5c5
commit 646f69583a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 242 additions and 152 deletions

View file

@ -56,6 +56,7 @@ pub trait Fs: Send + Sync {
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;
async fn is_dir(&self, path: &Path) -> bool;
async fn metadata(&self, path: &Path) -> Result<Option<Metadata>>;
async fn read_link(&self, path: &Path) -> Result<PathBuf>;
async fn read_dir(
@ -264,6 +265,12 @@ impl Fs for RealFs {
.map_or(false, |metadata| metadata.is_file())
}
async fn is_dir(&self, path: &Path) -> bool {
smol::fs::metadata(path)
.await
.map_or(false, |metadata| metadata.is_dir())
}
async fn metadata(&self, path: &Path) -> Result<Option<Metadata>> {
let symlink_metadata = match smol::fs::symlink_metadata(path).await {
Ok(metadata) => metadata,
@ -500,7 +507,12 @@ impl FakeFsState {
fn read_path(&self, target: &Path) -> Result<Arc<Mutex<FakeFsEntry>>> {
Ok(self
.try_read_path(target, true)
.ok_or_else(|| anyhow!("path does not exist: {}", target.display()))?
.ok_or_else(|| {
anyhow!(io::Error::new(
io::ErrorKind::NotFound,
format!("not found: {}", target.display())
))
})?
.0)
}
@ -1260,6 +1272,12 @@ impl Fs for FakeFs {
}
}
async fn is_dir(&self, path: &Path) -> bool {
self.metadata(path)
.await
.is_ok_and(|metadata| metadata.is_some_and(|metadata| metadata.is_dir))
}
async fn metadata(&self, path: &Path) -> Result<Option<Metadata>> {
self.simulate_random_delay().await;
let path = normalize_path(path);