diff --git a/src/component/filesystem/action/file/edit.rs b/src/component/filesystem/action/file/edit.rs index ddf16f4..c113af1 100644 --- a/src/component/filesystem/action/file/edit.rs +++ b/src/component/filesystem/action/file/edit.rs @@ -7,29 +7,27 @@ use std::process::Command; use slab_tree::NodeRef; use crate::component::dialog::message::MessageDialogLayer; +use crate::component::filesystem::action::file::{FileNode, get_selected_file}; use crate::component::filesystem::event::FsLayerEvent; use crate::component::filesystem::FsLayer; use crate::component::filesystem::tree::FsTreeViewNode; -use crate::file::FileEntry; use crate::state::action::{Action, ActionResult}; use crate::state::Environment; use crate::util::slab_tree::NodeRefExtensions; -pub struct EditSelected; +pub struct EditSelectedFileOrDirectory; -impl Action<FsLayer> for EditSelected { +impl Action<FsLayer> for EditSelectedFileOrDirectory { fn perform(&self, layer: &mut FsLayer, _environment: &Environment) -> ActionResult { - if let Some(node) = layer.selected_node() { - if let Some(entry_path) = layer.tree.get_entry(&node).and_then(FileEntry::path) { - return edit_impl(layer, &node, entry_path); - } + if let Some(FileNode { node, path, .. }) = get_selected_file(layer) { + open_default_editor(layer, &node, path) + } else { + ActionResult::Nothing } - - ActionResult::Nothing } } -fn edit_impl(layer: &FsLayer, node: &NodeRef<FsTreeViewNode>, path: &Path) -> ActionResult { +fn open_default_editor(layer: &FsLayer, node: &NodeRef<FsTreeViewNode>, path: &Path) -> ActionResult { let editor = get_editor(); let status = Command::new(&editor) .arg(path) @@ -39,9 +37,9 @@ fn edit_impl(layer: &FsLayer, node: &NodeRef<FsTreeViewNode>, path: &Path) -> Ac return ActionResult::PushLayer(Box::new(MessageDialogLayer::generic_error(layer.dialog_y(), format!("Default editor '{}' not found.", editor.to_string_lossy())))); } - if let Some(parent_node_id) = node.parent_id() { - FsLayerEvent::RefreshViewNodeChildren(parent_node_id).enqueue(&layer.pending_events); - } + // Refresh the parent directory, or the root node if this is the view root. + let node_id_to_refresh = node.parent_id().unwrap_or_else(|| node.node_id()); + FsLayerEvent::RefreshViewNodeChildren(node_id_to_refresh).enqueue(&layer.pending_events); ActionResult::Redraw } diff --git a/src/component/filesystem/action/mod.rs b/src/component/filesystem/action/mod.rs index 2010d7b..8def60c 100644 --- a/src/component/filesystem/action/mod.rs +++ b/src/component/filesystem/action/mod.rs @@ -2,7 +2,7 @@ use lazy_static::lazy_static; use crate::component::filesystem::action::application::{Quit, RedrawScreen}; use crate::component::filesystem::action::count::PushCountDigit; -use crate::component::filesystem::action::file::{CreateDirectory, CreateFile, DeleteSelected, EditSelected, RenameSelectedFileOrDirectory}; +use crate::component::filesystem::action::file::{CreateDirectory, CreateFile, DeleteSelected, EditSelectedFileOrDirectory, RenameSelectedFileOrDirectory}; use crate::component::filesystem::action::movement::{CollapseSelectedOr, ExpandSelectedOr, MoveBetweenFirstAndLastSibling, MoveDown, MovementWithCountFactory, MovementWithFallbackFactory, MoveOrTraverseUpParent, MoveToFirst, MoveToLast, MoveToLineOr, MoveToNextSibling, MoveToParent, MoveToPreviousSibling, MoveUp, ScreenHeightRatio}; use crate::component::filesystem::action::tree::{ExpandCollapse, RefreshChildrenOfSelected}; use crate::component::filesystem::FsLayer; @@ -35,7 +35,7 @@ fn create_action_map() -> ActionKeyMap { map(&mut me, "8", PushCountDigit(8)); map(&mut me, "9", PushCountDigit(9)); - map(&mut me, "e", EditSelected); + map(&mut me, "e", EditSelectedFileOrDirectory); map(&mut me, "d", DeleteSelected); map(&mut me, "gg", MoveToLineOr(MoveToFirst)); map(&mut me, "G", MoveToLineOr(MoveToLast));