From 2cfcce96ab58ab97887b957612b599fbf2c1d1ae Mon Sep 17 00:00:00 2001 From: Bjorn Ove Hay Andersen Date: Wed, 11 Oct 2023 19:06:41 +0200 Subject: [PATCH] Fixed issue when the first file specified as an argument was a relative directory --- helix-term/src/application.rs | 4 ++-- helix-term/src/main.rs | 14 ++++++++++---- helix-term/tests/test/helpers.rs | 20 +++++++++++++++++--- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index fabd0fa4d9860..9f5ff759f5cff 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -103,6 +103,7 @@ impl Application { args: Args, config: Config, syn_loader_conf: syntax::Configuration, + first_file_is_dir: bool, ) -> Result { #[cfg(feature = "integration")] setup_integration_logging(); @@ -162,8 +163,7 @@ impl Application { // Unset path to prevent accidentally saving to the original tutor file. doc_mut!(editor).set_path(None); } else if !args.files.is_empty() { - let first = &args.files[0].0; // we know it's not empty - if first.is_dir() { + if first_file_is_dir { // NOTE: The working directory is already set to args.files[0] in main() editor.new_file(Action::VerticalSplit); let picker = ui::file_picker(".".into(), &config.load().editor); diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index 8db5f3100a01c..8161a14417429 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -118,12 +118,18 @@ FLAGS: // NOTE: Set the working directory early so the correct configuration is loaded. Be aware that // Application::new() depends on this logic so it must be updated if this changes. - if let Some((path, true)) = args.files.first().map(|(path, _)| (path, path.is_dir())) { - helix_loader::set_current_working_dir(path)?; - } else if let Some(path) = &args.working_directory { + if let Some(path) = &args.working_directory { helix_loader::set_current_working_dir(path)?; } + // If the first file is a directory, it will be the working directory and a file picker will be opened + let first_file_is_dir = if let Some((path, _)) = args.files.first().filter(|p| p.0.is_dir()) { + helix_loader::set_current_working_dir(path)?; + true + } else { + false + }; + let config = match Config::load_default() { Ok(config) => config, Err(ConfigLoadError::Error(err)) if err.kind() == std::io::ErrorKind::NotFound => { @@ -149,7 +155,7 @@ FLAGS: }); // TODO: use the thread local executor to spawn the application task separately from the work pool - let mut app = Application::new(args, config, syn_loader_conf) + let mut app = Application::new(args, config, syn_loader_conf, first_file_is_dir) .context("unable to create new application")?; let exit_code = app.run(&mut EventStream::new()).await?; diff --git a/helix-term/tests/test/helpers.rs b/helix-term/tests/test/helpers.rs index e6762baf9c6d6..7b6fdc287d91a 100644 --- a/helix-term/tests/test/helpers.rs +++ b/helix-term/tests/test/helpers.rs @@ -139,7 +139,12 @@ pub async fn test_key_sequence_with_input_text>( let test_case = test_case.into(); let mut app = match app { Some(app) => app, - None => Application::new(Args::default(), test_config(), test_syntax_conf(None))?, + None => Application::new( + Args::default(), + test_config(), + test_syntax_conf(None), + false, + )?, }; let (view, doc) = helix_view::current!(app.editor); @@ -296,7 +301,8 @@ impl AppBuilder { path: P, pos: Option, ) -> Self { - self.args.files.push((path.into(), pos.unwrap_or_default())); + let path = path.into(); + self.args.files.push((path, pos.unwrap_or_default())); self } @@ -320,7 +326,15 @@ impl AppBuilder { } pub fn build(self) -> anyhow::Result { - let mut app = Application::new(self.args, self.config, self.syn_conf)?; + if let Some(path) = &self.args.working_directory { + bail!("Changing the working directory to {path:?} is not yet supported for integration tests"); + } + + if let Some((path, _)) = self.args.files.first().filter(|p| p.0.is_dir()) { + bail!("Having the directory {path:?} in args.files[0] is not yet supported for integration tests"); + } + + let mut app = Application::new(self.args, self.config, self.syn_conf, false)?; if let Some((text, selection)) = self.input { let (view, doc) = helix_view::current!(app.editor);