Add File.disk_state enum to clarify filesystem states (#20776)

Motivation for this is to make things more understandable while figuring
out #20775.

This is intended to be a refactoring that does not affect behavior, but
there are a few tricky spots:

* Previously `File.mtime()` (now `File.disk_state().mtime()`) would
return last known modification time for deleted files. Looking at uses,
I believe this will not affect anything. If there are behavior changes
here I believe they would be improvements.

* `BufferEvent::DirtyChanged` is now only emitted if dirtiness actually
changed, rather than if it may have changed. This should only be an
efficiency improvement.

Release Notes:

- N/A

Co-authored-by: Mikayla Maki <mikayla@zed.dev>
This commit is contained in:
Michael Sloan 2024-11-18 10:30:08 -08:00 committed by GitHub
parent df1d0dec0a
commit d99f5fe83e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 161 additions and 134 deletions

View file

@ -9,7 +9,7 @@ use gpui::{
hash, prelude::*, AppContext, EventEmitter, Img, Model, ModelContext, Subscription, Task,
WeakModel,
};
use language::File;
use language::{DiskState, File};
use rpc::{AnyProtoClient, ErrorExt as _};
use std::ffi::OsStr;
use std::num::NonZeroU64;
@ -74,11 +74,12 @@ impl ImageItem {
file_changed = true;
}
if !new_file.is_deleted() {
let new_mtime = new_file.mtime();
if new_mtime != old_file.mtime() {
file_changed = true;
cx.emit(ImageItemEvent::ReloadNeeded);
let old_state = old_file.disk_state();
let new_state = new_file.disk_state();
if old_state != new_state {
file_changed = true;
if matches!(new_state, DiskState::Present { .. }) {
cx.emit(ImageItemEvent::ReloadNeeded)
}
}
@ -503,37 +504,30 @@ impl LocalImageStore {
return;
}
let new_file = if let Some(entry) = old_file
let snapshot_entry = old_file
.entry_id
.and_then(|entry_id| snapshot.entry_for_id(entry_id))
{
.or_else(|| snapshot.entry_for_path(old_file.path.as_ref()));
let new_file = if let Some(entry) = snapshot_entry {
worktree::File {
disk_state: match entry.mtime {
Some(mtime) => DiskState::Present { mtime },
None => old_file.disk_state,
},
is_local: true,
entry_id: Some(entry.id),
mtime: entry.mtime,
path: entry.path.clone(),
worktree: worktree.clone(),
is_deleted: false,
is_private: entry.is_private,
}
} else if let Some(entry) = snapshot.entry_for_path(old_file.path.as_ref()) {
worktree::File {
is_local: true,
entry_id: Some(entry.id),
mtime: entry.mtime,
path: entry.path.clone(),
worktree: worktree.clone(),
is_deleted: false,
is_private: entry.is_private,
}
} else {
worktree::File {
disk_state: DiskState::Deleted,
is_local: true,
entry_id: old_file.entry_id,
path: old_file.path.clone(),
mtime: old_file.mtime,
worktree: worktree.clone(),
is_deleted: true,
is_private: old_file.is_private,
}
};