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
pull/7190/head
Bjorn Ove Hay Andersen 1 year ago committed by GitHub
parent 3052050ee0
commit 47b6c4bc78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -162,14 +162,19 @@ impl Application {
// Unset path to prevent accidentally saving to the original tutor file. // Unset path to prevent accidentally saving to the original tutor file.
doc_mut!(editor).set_path(None); doc_mut!(editor).set_path(None);
} else if !args.files.is_empty() { } else if !args.files.is_empty() {
if args.open_cwd { let mut files_it = args.files.into_iter().peekable();
// NOTE: The working directory is already set to args.files[0] in main()
editor.new_file(Action::VerticalSplit); // If the first file is a directory, skip it and open a picker
let picker = ui::file_picker(".".into(), &config.load().editor); 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))); 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() { if file.is_dir() {
return Err(anyhow::anyhow!( return Err(anyhow::anyhow!(
"expected a path to file, found a directory. (to open a directory pass it as first argument)" "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 // option. If neither of those two arguments are passed
// in, just load the files normally. // in, just load the files normally.
let action = match args.split { let action = match args.split {
_ if i == 0 => Action::VerticalSplit, _ if nr_of_files == 1 => Action::VerticalSplit,
Some(Layout::Vertical) => Action::VerticalSplit, Some(Layout::Vertical) => Action::VerticalSplit,
Some(Layout::Horizontal) => Action::HorizontalSplit, Some(Layout::Horizontal) => Action::HorizontalSplit,
None => Action::Load, None => Action::Load,
@ -208,6 +213,8 @@ impl Application {
// does not affect views without pos since it is at the top // does not affect views without pos since it is at the top
let (view, doc) = current!(editor); let (view, doc) = current!(editor);
align_view(doc, view, Align::Center); align_view(doc, view, Align::Center);
} else {
editor.new_file(Action::VerticalSplit);
} }
} else if stdin().is_tty() || cfg!(feature = "integration") { } else if stdin().is_tty() || cfg!(feature = "integration") {
editor.new_file(Action::VerticalSplit); editor.new_file(Action::VerticalSplit);

@ -17,7 +17,6 @@ pub struct Args {
pub log_file: Option<PathBuf>, pub log_file: Option<PathBuf>,
pub config_file: Option<PathBuf>, pub config_file: Option<PathBuf>,
pub files: Vec<(PathBuf, Position)>, pub files: Vec<(PathBuf, Position)>,
pub open_cwd: bool,
pub working_directory: Option<PathBuf>, pub working_directory: Option<PathBuf>,
} }

@ -116,16 +116,18 @@ FLAGS:
setup_logging(args.verbosity).context("failed to initialize logging")?; 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 // 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. // Application::new() depends on this logic so it must be updated if this changes.
if let Some(path) = &args.working_directory { if let Some(path) = &args.working_directory {
helix_loader::set_current_working_dir(path)?; helix_loader::set_current_working_dir(path)?;
} } 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
// 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()) {
helix_loader::set_current_working_dir(path)?; 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() { let config = match Config::load_default() {

Loading…
Cancel
Save