From 03dc1e5aea9f00e33b32dc46cc3531a63d8d01f3 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 21 Jan 2022 16:10:26 -0800 Subject: [PATCH] Move main worktree structs adjacent to each other --- crates/project/src/worktree.rs | 266 ++++++++++++++++----------------- 1 file changed, 133 insertions(+), 133 deletions(-) diff --git a/crates/project/src/worktree.rs b/crates/project/src/worktree.rs index 5bdc2221c1..487411f97a 100644 --- a/crates/project/src/worktree.rs +++ b/crates/project/src/worktree.rs @@ -46,6 +46,57 @@ lazy_static! { static ref GITIGNORE: &'static OsStr = OsStr::new(".gitignore"); } +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)] +pub struct WorktreeId(usize); + +pub enum Worktree { + Local(LocalWorktree), + Remote(RemoteWorktree), +} + +pub struct LocalWorktree { + snapshot: Snapshot, + config: WorktreeConfig, + background_snapshot: Arc>, + last_scan_state_rx: watch::Receiver, + _background_scanner_task: Option>, + poll_task: Option>, + registration: Registration, + share: Option, + diagnostics: HashMap, Vec>>, + diagnostic_summaries: TreeMap, + queued_operations: Vec<(u64, Operation)>, + client: Arc, + fs: Arc, + weak: bool, +} + +pub struct RemoteWorktree { + pub(crate) snapshot: Snapshot, + project_id: u64, + snapshot_rx: watch::Receiver, + client: Arc, + updates_tx: postage::mpsc::Sender, + replica_id: ReplicaId, + queued_operations: Vec<(u64, Operation)>, + diagnostic_summaries: TreeMap, + weak: bool, +} + +#[derive(Clone)] +pub struct Snapshot { + id: WorktreeId, + scan_id: usize, + abs_path: Arc, + root_name: String, + root_char_bag: CharBag, + ignores: HashMap, (Arc, usize)>, + entries_by_path: SumTree, + entries_by_id: SumTree, + removed_entry_ids: HashMap, + next_entry_id: Arc, +} + #[derive(Clone, Debug)] enum ScanState { Idle, @@ -53,12 +104,22 @@ enum ScanState { Err(Arc), } -#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)] -pub struct WorktreeId(usize); +#[derive(Debug, Eq, PartialEq)] +enum Registration { + None, + Pending, + Done { project_id: u64 }, +} -pub enum Worktree { - Local(LocalWorktree), - Remote(RemoteWorktree), +struct ShareState { + project_id: u64, + snapshots_tx: Sender, + _maintain_remote_snapshot: Option>, +} + +#[derive(Default, Deserialize)] +struct WorktreeConfig { + collaborators: Vec, } pub enum Event { @@ -373,91 +434,6 @@ impl Worktree { } } -impl WorktreeId { - pub fn from_usize(handle_id: usize) -> Self { - Self(handle_id) - } - - pub(crate) fn from_proto(id: u64) -> Self { - Self(id as usize) - } - - pub fn to_proto(&self) -> u64 { - self.0 as u64 - } - - pub fn to_usize(&self) -> usize { - self.0 - } -} - -impl fmt::Display for WorktreeId { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.0.fmt(f) - } -} - -#[derive(Clone)] -pub struct Snapshot { - id: WorktreeId, - scan_id: usize, - abs_path: Arc, - root_name: String, - root_char_bag: CharBag, - ignores: HashMap, (Arc, usize)>, - entries_by_path: SumTree, - entries_by_id: SumTree, - removed_entry_ids: HashMap, - next_entry_id: Arc, -} - -pub struct LocalWorktree { - snapshot: Snapshot, - config: WorktreeConfig, - background_snapshot: Arc>, - last_scan_state_rx: watch::Receiver, - _background_scanner_task: Option>, - poll_task: Option>, - registration: Registration, - share: Option, - diagnostics: HashMap, Vec>>, - diagnostic_summaries: TreeMap, - queued_operations: Vec<(u64, Operation)>, - client: Arc, - fs: Arc, - weak: bool, -} - -#[derive(Debug, Eq, PartialEq)] -enum Registration { - None, - Pending, - Done { project_id: u64 }, -} - -struct ShareState { - project_id: u64, - snapshots_tx: Sender, - _maintain_remote_snapshot: Option>, -} - -pub struct RemoteWorktree { - pub(crate) snapshot: Snapshot, - project_id: u64, - snapshot_rx: watch::Receiver, - client: Arc, - updates_tx: postage::mpsc::Sender, - replica_id: ReplicaId, - queued_operations: Vec<(u64, Operation)>, - diagnostic_summaries: TreeMap, - weak: bool, -} - -#[derive(Default, Deserialize)] -struct WorktreeConfig { - collaborators: Vec, -} - impl LocalWorktree { async fn new( client: Arc, @@ -829,49 +805,6 @@ impl LocalWorktree { } } -fn build_gitignore(abs_path: &Path, fs: &dyn Fs) -> Result { - let contents = smol::block_on(fs.load(&abs_path))?; - let parent = abs_path.parent().unwrap_or(Path::new("/")); - let mut builder = GitignoreBuilder::new(parent); - for line in contents.lines() { - builder.add_line(Some(abs_path.into()), line)?; - } - Ok(builder.build()?) -} - -impl Deref for Worktree { - type Target = Snapshot; - - fn deref(&self) -> &Self::Target { - match self { - Worktree::Local(worktree) => &worktree.snapshot, - Worktree::Remote(worktree) => &worktree.snapshot, - } - } -} - -impl Deref for LocalWorktree { - type Target = Snapshot; - - fn deref(&self) -> &Self::Target { - &self.snapshot - } -} - -impl Deref for RemoteWorktree { - type Target = Snapshot; - - fn deref(&self) -> &Self::Target { - &self.snapshot - } -} - -impl fmt::Debug for LocalWorktree { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.snapshot.fmt(f) - } -} - impl RemoteWorktree { pub(crate) fn open_buffer( &mut self, @@ -1340,6 +1273,73 @@ impl Snapshot { } } +fn build_gitignore(abs_path: &Path, fs: &dyn Fs) -> Result { + let contents = smol::block_on(fs.load(&abs_path))?; + let parent = abs_path.parent().unwrap_or(Path::new("/")); + let mut builder = GitignoreBuilder::new(parent); + for line in contents.lines() { + builder.add_line(Some(abs_path.into()), line)?; + } + Ok(builder.build()?) +} + +impl WorktreeId { + pub fn from_usize(handle_id: usize) -> Self { + Self(handle_id) + } + + pub(crate) fn from_proto(id: u64) -> Self { + Self(id as usize) + } + + pub fn to_proto(&self) -> u64 { + self.0 as u64 + } + + pub fn to_usize(&self) -> usize { + self.0 + } +} + +impl fmt::Display for WorktreeId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +impl Deref for Worktree { + type Target = Snapshot; + + fn deref(&self) -> &Self::Target { + match self { + Worktree::Local(worktree) => &worktree.snapshot, + Worktree::Remote(worktree) => &worktree.snapshot, + } + } +} + +impl Deref for LocalWorktree { + type Target = Snapshot; + + fn deref(&self) -> &Self::Target { + &self.snapshot + } +} + +impl Deref for RemoteWorktree { + type Target = Snapshot; + + fn deref(&self) -> &Self::Target { + &self.snapshot + } +} + +impl fmt::Debug for LocalWorktree { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.snapshot.fmt(f) + } +} + impl fmt::Debug for Snapshot { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { for entry in self.entries_by_path.cursor::<()>() {