From d192b6ebc7ded22dfcbcdd40439b2191e245d6ff Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Sat, 22 Jan 2022 14:44:58 -0700 Subject: [PATCH] Remove Worktree::abs_path I'd like to only have methods related to absolute paths on local worktrees, because it's not really possible to implement them on remote worktrees since we don't know the full path being shared and wouldn't have anything to do with it anyway if we did. --- crates/editor/src/items.rs | 10 +++------- crates/language/src/buffer.rs | 2 +- crates/project/src/worktree.rs | 27 ++++++++++++--------------- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index 78f5bcc827..a2413f248a 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -122,13 +122,9 @@ impl ItemView for Editor { } fn title(&self, cx: &AppContext) -> String { - let filename = self - .buffer() - .read(cx) - .file(cx) - .and_then(|file| file.file_name(cx)); - if let Some(name) = filename { - name.to_string_lossy().into() + let file = self.buffer().read(cx).file(cx); + if let Some(file) = file { + file.file_name(cx).to_string_lossy().into() } else { "untitled".into() } diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index cceef3992e..3d0ef512d4 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -170,7 +170,7 @@ pub trait File { /// Returns the last component of this handle's absolute path. If this handle refers to the root /// of its worktree, then this method will return the name of the worktree itself. - fn file_name(&self, cx: &AppContext) -> Option; + fn file_name(&self, cx: &AppContext) -> OsString; fn is_deleted(&self) -> bool; diff --git a/crates/project/src/worktree.rs b/crates/project/src/worktree.rs index ea87a14341..bbdaaeb007 100644 --- a/crates/project/src/worktree.rs +++ b/crates/project/src/worktree.rs @@ -543,6 +543,10 @@ impl LocalWorktree { Ok((tree, scan_states_tx)) } + pub fn abs_path(&self) -> &Arc { + &self.abs_path + } + pub fn authorized_logins(&self) -> Vec { self.config.collaborators.clone() } @@ -1092,10 +1096,6 @@ impl Snapshot { } } - pub fn abs_path(&self) -> &Arc { - &self.abs_path - } - pub fn root_entry(&self) -> Option<&Entry> { self.entry_for_path("") } @@ -1360,29 +1360,26 @@ impl language::File for File { } fn abs_path(&self, cx: &AppContext) -> Option { - if self.is_local { - Some(self.worktree.read(cx).abs_path().join(&self.path)) - } else { - None - } + 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(); - if let Some(worktree_name) = self.worktree.read(cx).abs_path().file_name() { - full_path.push(worktree_name); - } + full_path.push(self.worktree.read(cx).root_name()); full_path.push(&self.path); full_path } /// Returns the last component of this handle's absolute path. If this handle refers to the root /// of its worktree, then this method will return the name of the worktree itself. - fn file_name<'a>(&'a self, cx: &AppContext) -> Option { + fn file_name(&self, cx: &AppContext) -> OsString { self.path .file_name() - .or_else(|| self.worktree.read(cx).abs_path().file_name()) - .map(Into::into) + .map(|name| name.into()) + .unwrap_or_else(|| OsString::from(&self.worktree.read(cx).root_name)) } fn is_deleted(&self) -> bool {