editor: Fix "Reveal in File Manager" not working with multibuffers (#18626)

Additionally, mark context menu entry as disabled when the action would
fail (untitled buffer, collab sessions).

Supersedes #18584 

Release Notes:

- Fixed "Reveal in Finder/File Manager", "Copy Path", "Copy Relative
Path" and "Copy file location" actions not working with multibuffers.
This commit is contained in:
Piotr Osiewicz 2024-10-02 13:45:07 +02:00 committed by GitHub
parent fd94c2b3fd
commit e01bc6765d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 25 deletions

View File

@ -11257,30 +11257,32 @@ impl Editor {
None
}
fn target_file<'a>(&self, cx: &'a AppContext) -> Option<&'a dyn language::LocalFile> {
self.active_excerpt(cx)?
.1
.read(cx)
.file()
.and_then(|f| f.as_local())
}
pub fn reveal_in_finder(&mut self, _: &RevealInFileManager, cx: &mut ViewContext<Self>) {
if let Some(buffer) = self.buffer().read(cx).as_singleton() {
if let Some(file) = buffer.read(cx).file().and_then(|f| f.as_local()) {
cx.reveal_path(&file.abs_path(cx));
}
if let Some(target) = self.target_file(cx) {
cx.reveal_path(&target.abs_path(cx));
}
}
pub fn copy_path(&mut self, _: &CopyPath, cx: &mut ViewContext<Self>) {
if let Some(buffer) = self.buffer().read(cx).as_singleton() {
if let Some(file) = buffer.read(cx).file().and_then(|f| f.as_local()) {
if let Some(path) = file.abs_path(cx).to_str() {
cx.write_to_clipboard(ClipboardItem::new_string(path.to_string()));
}
if let Some(file) = self.target_file(cx) {
if let Some(path) = file.abs_path(cx).to_str() {
cx.write_to_clipboard(ClipboardItem::new_string(path.to_string()));
}
}
}
pub fn copy_relative_path(&mut self, _: &CopyRelativePath, cx: &mut ViewContext<Self>) {
if let Some(buffer) = self.buffer().read(cx).as_singleton() {
if let Some(file) = buffer.read(cx).file().and_then(|f| f.as_local()) {
if let Some(path) = file.path().to_str() {
cx.write_to_clipboard(ClipboardItem::new_string(path.to_string()));
}
if let Some(file) = self.target_file(cx) {
if let Some(path) = file.path().to_str() {
cx.write_to_clipboard(ClipboardItem::new_string(path.to_string()));
}
}
}
@ -11491,12 +11493,10 @@ impl Editor {
}
pub fn copy_file_location(&mut self, _: &CopyFileLocation, cx: &mut ViewContext<Self>) {
if let Some(buffer) = self.buffer().read(cx).as_singleton() {
if let Some(file) = buffer.read(cx).file().and_then(|f| f.as_local()) {
if let Some(path) = file.path().to_str() {
let selection = self.selections.newest::<Point>(cx).start.row + 1;
cx.write_to_clipboard(ClipboardItem::new_string(format!("{path}:{selection}")));
}
if let Some(file) = self.target_file(cx) {
if let Some(path) = file.path().to_str() {
let selection = self.selections.newest::<Point>(cx).start.row + 1;
cx.write_to_clipboard(ClipboardItem::new_string(format!("{path}:{selection}")));
}
}
}

View File

@ -158,6 +158,12 @@ pub fn deploy_context_menu(
}
let focus = cx.focused();
let has_reveal_target = editor.target_file(cx).is_some();
let reveal_in_finder_label = if cfg!(target_os = "macos") {
"Reveal in Finder"
} else {
"Reveal in File Manager"
};
ui::ContextMenu::build(cx, |menu, _cx| {
let builder = menu
.on_blur_subscription(Subscription::new(|| {}))
@ -180,11 +186,13 @@ pub fn deploy_context_menu(
.action("Copy", Box::new(Copy))
.action("Paste", Box::new(Paste))
.separator()
.when(cfg!(target_os = "macos"), |builder| {
builder.action("Reveal in Finder", Box::new(RevealInFileManager))
})
.when(cfg!(not(target_os = "macos")), |builder| {
builder.action("Reveal in File Manager", Box::new(RevealInFileManager))
.map(|builder| {
if has_reveal_target {
builder.action(reveal_in_finder_label, Box::new(RevealInFileManager))
} else {
builder
.disabled_action(reveal_in_finder_label, Box::new(RevealInFileManager))
}
})
.action("Open in Terminal", Box::new(OpenInTerminal))
.action("Copy Permalink", Box::new(CopyPermalinkToLine));