From 38d01f3d5dcc5d73194e6f44ea7181b6a17b9a6e Mon Sep 17 00:00:00 2001 From: cbr9 Date: Fri, 11 Aug 2023 14:11:54 +0200 Subject: [PATCH 1/2] feat: open file picker on directories using goto_file (gf) --- helix-term/src/commands.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 61c647d0b821..2f366300bf55 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1190,8 +1190,13 @@ fn goto_file_impl(cx: &mut Context, action: Action) { for sel in paths { let p = sel.trim(); if !p.is_empty() { - if let Err(e) = cx.editor.open(&PathBuf::from(p), action) { - cx.editor.set_error(format!("Open file failed: {:?}", e)); + let path = Path::new(p); + if let Ok(path) = helix_core::path::get_canonicalized_path(path) { + if path.is_dir() { + file_picker_in(cx, path); + } else if let Err(e) = cx.editor.open(&path, action) { + cx.editor.set_error(format!("Open file failed: {:?}", e)); + } } } } @@ -2561,14 +2566,18 @@ fn append_mode(cx: &mut Context) { doc.set_selection(view.id, selection); } +fn file_picker_in(cx: &mut Context, root: PathBuf) { + let picker = ui::file_picker(root, &cx.editor.config()); + cx.push_layer(Box::new(overlaid(picker))); +} + fn file_picker(cx: &mut Context) { let root = find_workspace().0; if !root.exists() { cx.editor.set_error("Workspace directory does not exist"); return; } - let picker = ui::file_picker(root, &cx.editor.config()); - cx.push_layer(Box::new(overlaid(picker))); + file_picker_in(cx, root) } fn file_picker_in_current_buffer_directory(cx: &mut Context) { @@ -2584,9 +2593,9 @@ fn file_picker_in_current_buffer_directory(cx: &mut Context) { } }; - let picker = ui::file_picker(path, &cx.editor.config()); - cx.push_layer(Box::new(overlaid(picker))); + file_picker_in(cx, path) } + fn file_picker_in_current_directory(cx: &mut Context) { let cwd = helix_loader::current_working_dir(); if !cwd.exists() { @@ -2594,8 +2603,7 @@ fn file_picker_in_current_directory(cx: &mut Context) { .set_error("Current working directory does not exist"); return; } - let picker = ui::file_picker(cwd, &cx.editor.config()); - cx.push_layer(Box::new(overlaid(picker))); + file_picker_in(cx, cwd) } fn buffer_picker(cx: &mut Context) { From 57c655b1a3f70eafd8d82846fe73c4e31ef4ad62 Mon Sep 17 00:00:00 2001 From: cbr9 Date: Fri, 11 Aug 2023 14:42:20 +0200 Subject: [PATCH 2/2] remove helper and call to canonicalize --- helix-term/src/commands.rs | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 2f366300bf55..38133535edc7 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1191,12 +1191,11 @@ fn goto_file_impl(cx: &mut Context, action: Action) { let p = sel.trim(); if !p.is_empty() { let path = Path::new(p); - if let Ok(path) = helix_core::path::get_canonicalized_path(path) { - if path.is_dir() { - file_picker_in(cx, path); - } else if let Err(e) = cx.editor.open(&path, action) { - cx.editor.set_error(format!("Open file failed: {:?}", e)); - } + if path.is_dir() { + let picker = ui::file_picker(path.into(), &cx.editor.config()); + cx.push_layer(Box::new(overlaid(picker))); + } else if let Err(e) = cx.editor.open(path, action) { + cx.editor.set_error(format!("Open file failed: {:?}", e)); } } } @@ -2566,18 +2565,14 @@ fn append_mode(cx: &mut Context) { doc.set_selection(view.id, selection); } -fn file_picker_in(cx: &mut Context, root: PathBuf) { - let picker = ui::file_picker(root, &cx.editor.config()); - cx.push_layer(Box::new(overlaid(picker))); -} - fn file_picker(cx: &mut Context) { let root = find_workspace().0; if !root.exists() { cx.editor.set_error("Workspace directory does not exist"); return; } - file_picker_in(cx, root) + let picker = ui::file_picker(root, &cx.editor.config()); + cx.push_layer(Box::new(overlaid(picker))); } fn file_picker_in_current_buffer_directory(cx: &mut Context) { @@ -2593,7 +2588,8 @@ fn file_picker_in_current_buffer_directory(cx: &mut Context) { } }; - file_picker_in(cx, path) + let picker = ui::file_picker(path, &cx.editor.config()); + cx.push_layer(Box::new(overlaid(picker))); } fn file_picker_in_current_directory(cx: &mut Context) { @@ -2603,7 +2599,8 @@ fn file_picker_in_current_directory(cx: &mut Context) { .set_error("Current working directory does not exist"); return; } - file_picker_in(cx, cwd) + let picker = ui::file_picker(cwd, &cx.editor.config()); + cx.push_layer(Box::new(overlaid(picker))); } fn buffer_picker(cx: &mut Context) {