From 47b6c4bc78d449e3586f28d26507bfc8fb8608e2 Mon Sep 17 00:00:00 2001 From: Bjorn Ove Hay Andersen Date: Tue, 21 Nov 2023 12:07:00 +0100 Subject: Resolve args.files before changing directory (#8676) * Resolve args.files before changing directory * Removed the open_cwd work-around now that the path is full * If -w is specified, use that as the working directory * Open the remaining files in the argument list, also when the first is a directory * Use an iterator access the files argument--- helix-term/src/application.rs | 23 +++++++++++++++-------- helix-term/src/args.rs | 1 - helix-term/src/main.rs | 12 +++++++----- 3 files changed, 22 insertions(+), 14 deletions(-) (limited to 'helix-term') diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 43e1cdfc..ed085749 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -162,14 +162,19 @@ 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() { - if args.open_cwd { - // 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); + let mut files_it = args.files.into_iter().peekable(); + + // If the first file is a directory, skip it and open a picker + if let Some((first, _)) = files_it.next_if(|(p, _)| p.is_dir()) { + let picker = ui::file_picker(first, &config.load().editor); compositor.push(Box::new(overlaid(picker))); - } else { - let nr_of_files = args.files.len(); - for (i, (file, pos)) in args.files.into_iter().enumerate() { + } + + // If there are any more files specified, open them + if files_it.peek().is_some() { + let mut nr_of_files = 0; + for (file, pos) in files_it { + nr_of_files += 1; if file.is_dir() { return Err(anyhow::anyhow!( "expected a path to file, found a directory. (to open a directory pass it as first argument)" @@ -181,7 +186,7 @@ impl Application { // option. If neither of those two arguments are passed // in, just load the files normally. let action = match args.split { - _ if i == 0 => Action::VerticalSplit, + _ if nr_of_files == 1 => Action::VerticalSplit, Some(Layout::Vertical) => Action::VerticalSplit, Some(Layout::Horizontal) => Action::HorizontalSplit, None => Action::Load, @@ -208,6 +213,8 @@ impl Application { // does not affect views without pos since it is at the top let (view, doc) = current!(editor); align_view(doc, view, Align::Center); + } else { + editor.new_file(Action::VerticalSplit); } } else if stdin().is_tty() || cfg!(feature = "integration") { editor.new_file(Action::VerticalSplit); diff --git a/helix-term/src/args.rs b/helix-term/src/args.rs index 99ce3992..6a49889b 100644 --- a/helix-term/src/args.rs +++ b/helix-term/src/args.rs @@ -17,7 +17,6 @@ pub struct Args { pub log_file: Option, pub config_file: Option, pub files: Vec<(PathBuf, Position)>, - pub open_cwd: bool, pub working_directory: Option, } diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index 6411a7c5..a62c54a4 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -116,16 +116,18 @@ FLAGS: setup_logging(args.verbosity).context("failed to initialize logging")?; + // Before setting the working directory, resolve all the paths in args.files + for (path, _) in args.files.iter_mut() { + *path = helix_core::path::get_canonicalized_path(path); + } + // 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) = &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 - if let Some((path, _)) = args.files.first().filter(|p| p.0.is_dir()) { + } else if let Some((path, _)) = args.files.first().filter(|p| p.0.is_dir()) { + // If the first file is a directory, it will be the working directory unless -w was specified helix_loader::set_current_working_dir(path)?; - args.open_cwd = true; // Signal Application that we want to open the picker on "." } let config = match Config::load_default() { -- cgit v1.2.3-70-g09d2