From 432522e72407a8dc9cab78fc8f6cf5decb71ef10 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 1 Apr 2023 12:21:47 +0200 Subject: [PATCH 1/2] Add update of selected file in explorer when switching buffers --- helix-term/src/ui/editor.rs | 21 ++++++++++++++++++++- helix-term/src/ui/explorer.rs | 5 +++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index ab6d3430..d6f4fce5 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -1496,7 +1496,26 @@ impl Component for EditorView { } if let Some(explore) = self.explorer.as_mut() { - if explore.is_focus() { + let needs_update = explore.is_focus() || { + if let Some(current_document_path) = doc!(cx.editor).path().cloned() { + if let Some(current_explore_path) = explore.current_file() { + if *current_explore_path != current_document_path { + let _ = explore.reveal_file(current_document_path); + + true + } else { + false + } + } else { + let _ = explore.reveal_file(current_document_path); + true + } + } else { + false + } + }; + + if needs_update { let area = if use_bufferline { area.clip_top(1) } else { diff --git a/helix-term/src/ui/explorer.rs b/helix-term/src/ui/explorer.rs index 76c43751..d527ee58 100644 --- a/helix-term/src/ui/explorer.rs +++ b/helix-term/src/ui/explorer.rs @@ -605,6 +605,11 @@ impl Explorer { } } + /// Returns the current file in the tree view + pub fn current_file(&self) -> Option<&PathBuf> { + self.tree.current_item().ok().map(|c| &c.path) + } + pub fn is_opened(&self) -> bool { self.state.open } -- 2.38.5 From bd32cb31144806d9849972490be8e4d077e228b7 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 1 Apr 2023 12:31:32 +0200 Subject: [PATCH 2/2] Add --show-explorer cli arg --- helix-term/src/application.rs | 22 ++++++++++++++++++---- helix-term/src/args.rs | 2 ++ helix-term/src/ui/explorer.rs | 2 +- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 95faa01b..1a3e1c6d 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -21,11 +21,11 @@ use tui::backend::Backend; use crate::{ args::Args, commands::apply_workspace_edit, - compositor::{Compositor, Event}, + compositor::{self, Compositor, Event}, config::Config, job::Jobs, keymap::Keymaps, - ui::{self, overlay::overlayed}, + ui::{self, overlay::overlayed, Explorer}, }; use log::{debug, error, warn}; @@ -155,7 +155,21 @@ impl Application { let keys = Box::new(Map::new(Arc::clone(&config), |config: &Config| { &config.keys })); - let editor_view = Box::new(ui::EditorView::new(Keymaps::new(keys))); + let mut editor_view = Box::new(ui::EditorView::new(Keymaps::new(keys))); + + let mut jobs = Jobs::new(); + + if args.show_explorer { + let mut context = compositor::Context { + editor: &mut editor, + scroll: None, + jobs: &mut jobs, + }; + let mut explorer = Explorer::new(&mut context)?; + explorer.unfocus(); + editor_view.explorer = Some(explorer); + } + compositor.push(editor_view); if args.load_tutor { @@ -244,7 +258,7 @@ impl Application { syn_loader, signals, - jobs: Jobs::new(), + jobs, lsp_progress: LspProgressMap::new(), last_render: Instant::now(), }; diff --git a/helix-term/src/args.rs b/helix-term/src/args.rs index dd787f1f..53920e29 100644 --- a/helix-term/src/args.rs +++ b/helix-term/src/args.rs @@ -10,6 +10,7 @@ pub struct Args { pub health: bool, pub health_arg: Option, pub load_tutor: bool, + pub show_explorer: bool, pub fetch_grammars: bool, pub build_grammars: bool, pub split: Option, @@ -32,6 +33,7 @@ impl Args { "--version" => args.display_version = true, "--help" => args.display_help = true, "--tutor" => args.load_tutor = true, + "--show-explorer" => args.show_explorer = true, "--vsplit" => match args.split { Some(_) => anyhow::bail!("can only set a split once of a specific type"), None => args.split = Some(Layout::Vertical), diff --git a/helix-term/src/ui/explorer.rs b/helix-term/src/ui/explorer.rs index d527ee58..91b18684 100644 --- a/helix-term/src/ui/explorer.rs +++ b/helix-term/src/ui/explorer.rs @@ -266,7 +266,7 @@ impl Explorer { self.state.open = true; } - fn unfocus(&mut self) { + pub fn unfocus(&mut self) { self.state.focus = false; } -- 2.38.5