From 4669f57df5001691c3784cdc56b7549ba5c7c387 Mon Sep 17 00:00:00 2001 From: Bjorn Ove Hay Andersen Date: Thu, 12 Oct 2023 09:41:54 +0200 Subject: [PATCH] Made args.working_directory and args.files[0] work nicely together --- helix-term/src/application.rs | 4 ++-- helix-term/src/args.rs | 8 -------- helix-term/src/main.rs | 10 +++++++++- helix-term/tests/test/helpers.rs | 20 +++++++++++++++++--- 4 files changed, 28 insertions(+), 14 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/args.rs b/helix-term/src/args.rs index 90a658c2dadb2..f782539caa90a 100644 --- a/helix-term/src/args.rs +++ b/helix-term/src/args.rs @@ -97,14 +97,6 @@ impl Args { args.files.push(parse_file(&arg)); } - // Set args.working_directory to the first file if it is a directory + replace it with "." - if let Some((path, _)) = args.files.first_mut() { - if path.is_dir() { - // NOTE: The original logic effectively ignored -w when the first file was a directory - args.working_directory = Some(std::mem::replace(path, PathBuf::from("."))); - } - } - Ok(args) } } diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index 10afe7d8dd440..b125e1d910409 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -121,6 +121,14 @@ FLAGS: 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 => { @@ -146,7 +154,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);