From f4fb8fa4ef778a6bc39a96b06ee44f204fae001b Mon Sep 17 00:00:00 2001 From: Denys Rybalka Date: Fri, 30 Aug 2024 21:12:47 +0200 Subject: [PATCH] Open file browser in buffer's directory --- helix-term/src/commands.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 8685a47d2..c4f99ee3b 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -375,7 +375,7 @@ impl MappableCommand { file_picker, "Open file picker", file_picker_in_current_buffer_directory, "Open file picker at current buffer's directory", file_picker_in_current_directory, "Open file picker at current working directory", - file_browser, "Open file browser at current working directory", + file_browser, "Open file browser at current buffer's directory", code_action, "Perform code action", buffer_picker, "Open buffer picker", jumplist_picker, "Open jumplist picker", @@ -2950,13 +2950,19 @@ fn file_picker_in_current_directory(cx: &mut Context) { } fn file_browser(cx: &mut Context) { - let cwd = helix_stdx::env::current_working_dir(); - if !cwd.exists() { - cx.editor - .set_error("Current working directory does not exist"); - return; - } - if let Ok(picker) = ui::file_browser(cwd) { + let doc_dir = doc!(cx.editor) + .path() + .and_then(|path| path.parent().map(|path| path.to_path_buf())); + + let path = match doc_dir { + Some(path) => path, + None => { + cx.editor.set_error("Current buffer has no path or parent"); + return; + } + }; + + if let Ok(picker) = ui::file_browser(path) { cx.push_layer(Box::new(overlaid(picker))); } }