:lipstick: Reduce nesting in Snapshot::remove_path

Max Brunsfeld created

Change summary

zed/src/worktree.rs | 75 +++++++++++++++++++++++-----------------------
1 file changed, 37 insertions(+), 38 deletions(-)

Detailed changes

zed/src/worktree.rs 🔗

@@ -258,51 +258,50 @@ impl Snapshot {
     }
 
     fn remove_path(&mut self, path: &Path) {
-        if let Some(mut parent_entry) = path.parent().and_then(|p| self.entry_for_path(p).cloned())
-        {
-            let mut edits = Vec::new();
-
-            let parent_inode = parent_entry.inode();
-            let mut entry_inode = None;
-            if let Entry::Dir { children, .. } = &mut parent_entry {
-                let mut new_children = Vec::new();
-                for (child_inode, child_name) in children.as_ref() {
-                    if Some(child_name.as_ref()) == path.file_name() {
-                        entry_inode = Some(*child_inode);
-                    } else {
-                        new_children.push((*child_inode, child_name.clone()));
-                    }
-                }
+        let mut parent_entry = match path.parent().and_then(|p| self.entry_for_path(p).cloned()) {
+            Some(e) => e,
+            None => return,
+        };
 
-                if new_children.iter().any(|c| Some(c.0) == entry_inode) {
-                    entry_inode = None;
+        let mut edits = Vec::new();
+        let parent_inode = parent_entry.inode();
+        let mut entry_inode = None;
+        if let Entry::Dir { children, .. } = &mut parent_entry {
+            let mut new_children = Vec::new();
+            for (child_inode, child_name) in children.as_ref() {
+                if Some(child_name.as_ref()) == path.file_name() {
+                    entry_inode = Some(*child_inode);
+                } else {
+                    new_children.push((*child_inode, child_name.clone()));
                 }
+            }
 
-                *children = new_children.into();
-                edits.push(Edit::Insert(parent_entry));
-            } else {
-                unreachable!();
+            if new_children.iter().any(|c| Some(c.0) == entry_inode) {
+                entry_inode = None;
             }
 
-            if let Some(entry_inode) = entry_inode {
-                // Recursively remove the orphaned nodes' descendants.
-                let mut descendant_stack = Vec::new();
-                descendant_stack.push((entry_inode, parent_inode));
-                while let Some((inode, parent_inode)) = descendant_stack.pop() {
-                    if let Some(entry) = self.entries.get(&inode) {
-                        if entry.parent() == Some(parent_inode) {
-                            edits.push(Edit::Remove(inode));
-                            if let Entry::Dir { children, .. } = entry {
-                                descendant_stack
-                                    .extend(children.iter().map(|c| (c.0, entry.inode())));
-                            }
+            *children = new_children.into();
+            edits.push(Edit::Insert(parent_entry));
+        } else {
+            unreachable!();
+        }
+
+        if let Some(entry_inode) = entry_inode {
+            let mut descendant_stack = Vec::new();
+            descendant_stack.push((entry_inode, parent_inode));
+            while let Some((inode, parent_inode)) = descendant_stack.pop() {
+                if let Some(entry) = self.entries.get(&inode) {
+                    if entry.parent() == Some(parent_inode) {
+                        edits.push(Edit::Remove(inode));
+                        if let Entry::Dir { children, .. } = entry {
+                            descendant_stack.extend(children.iter().map(|c| (c.0, entry.inode())));
                         }
                     }
                 }
             }
-
-            self.entries.edit(edits);
         }
+
+        self.entries.edit(edits);
     }
 
     fn fmt_entry(
@@ -372,19 +371,19 @@ impl FileHandle {
 #[derive(Clone, Debug)]
 pub enum Entry {
     Dir {
-        parent: Option<u64>,
         inode: u64,
+        parent: Option<u64>,
         is_symlink: bool,
         is_ignored: bool,
         children: Arc<[(u64, Arc<OsStr>)]>,
         pending: bool,
     },
     File {
-        parent: Option<u64>,
-        path: PathEntry,
         inode: u64,
+        parent: Option<u64>,
         is_symlink: bool,
         is_ignored: bool,
+        path: PathEntry,
     },
 }