diff options
author | wongjiahau | 2024-05-01 20:42:31 +0000 |
---|---|---|
committer | JJ | 2024-05-01 23:06:08 +0000 |
commit | 3bff36ab90aba7de8bb5bff7dbb8230d81cdf582 (patch) | |
tree | 733bdb4ff30a35c2212910d48a635ddf4c23b2e3 /helix-term/src/commands.rs | |
parent | 2cadec0b1182332338a5a1cc3062776f834d8835 (diff) |
Add file explorer and tree helper
ref: https://github.com/helix-editor/helix/issues/200
ref: https://github.com/helix-editor/helix/pull/2377
ref: https://github.com/helix-editor/helix/pull/5566
ref: https://github.com/helix-editor/helix/pull/5768
Co-authored-by: cossonleo <cossonleo@foxmail.com>
Co-authored-by: JJ <git@toki.la>
Co-authored-by: Quan Tong <quantonganh@gmail.com>
Diffstat (limited to 'helix-term/src/commands.rs')
-rw-r--r-- | helix-term/src/commands.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index d545480b..5c59962d 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -507,6 +507,8 @@ impl MappableCommand { command_palette, "Open command palette", goto_word, "Jump to a two-character label", extend_to_word, "Extend to a two-character label", + open_or_focus_explorer, "Open or focus explorer", + reveal_current_file, "Reveal current file in explorer", ); } @@ -2834,6 +2836,49 @@ fn file_picker_in_current_directory(cx: &mut Context) { cx.push_layer(Box::new(overlaid(picker))); } +fn open_or_focus_explorer(cx: &mut Context) { + cx.callback.push(Box::new( + |compositor: &mut Compositor, cx: &mut compositor::Context| { + if let Some(editor) = compositor.find::<ui::EditorView>() { + match editor.explorer.as_mut() { + Some(explore) => explore.focus(), + None => match ui::Explorer::new(cx) { + Ok(explore) => editor.explorer = Some(explore), + Err(err) => cx.editor.set_error(format!("{}", err)), + }, + } + } + }, + )); +} + +fn reveal_file_in_explorer(cx: &mut Context, path: Option<PathBuf>) { + cx.callback.push(Box::new( + |compositor: &mut Compositor, cx: &mut compositor::Context| { + if let Some(editor) = compositor.find::<ui::EditorView>() { + (|| match editor.explorer.as_mut() { + Some(explorer) => match path { + Some(path) => explorer.reveal_file(path), + None => explorer.reveal_current_file(cx), + }, + None => { + editor.explorer = Some(ui::Explorer::new(cx)?); + if let Some(explorer) = editor.explorer.as_mut() { + explorer.reveal_current_file(cx)?; + } + Ok(()) + } + })() + .unwrap_or_else(|err| cx.editor.set_error(err.to_string())) + } + }, + )); +} + +fn reveal_current_file(cx: &mut Context) { + reveal_file_in_explorer(cx, None) +} + fn buffer_picker(cx: &mut Context) { let current = view!(cx.editor).doc; |