on master help sign in
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

commit47b6c4bc78d449e3586f28d26507bfc8fb8608e2 authorBjorn Ove Hay Andersen <bjrnove@gmail.com> dateTue 21 Nov 2023, 12:07 (3 years ago) parent3052050
3 files changed+22−14
helix-term/src/application.rs+15−8 helix-term/src/args.rs−1 helix-term/src/main.rs+7−5
helix-term/src/application.rs+15−8
@@ -162,14 +162,19 @@ impl Application {
162162 // Unset path to prevent accidentally saving to the original tutor file.
163163 doc_mut!(editor).set_path(None);
164164 } else if !args.files.is_empty() {
165− if args.open_cwd {
166− // NOTE: The working directory is already set to args.files[0] in main()
167− editor.new_file(Action::VerticalSplit);
168− let picker = ui::file_picker(".".into(), &config.load().editor);
165+ let mut files_it = args.files.into_iter().peekable();
166+
167+ // If the first file is a directory, skip it and open a picker
168+ if let Some((first, _)) = files_it.next_if(|(p, _)| p.is_dir()) {
169+ let picker = ui::file_picker(first, &config.load().editor);
169170 compositor.push(Box::new(overlaid(picker)));
170− } else {
171− let nr_of_files = args.files.len();
172− for (i, (file, pos)) in args.files.into_iter().enumerate() {
171+ }
172+
173+ // If there are any more files specified, open them
174+ if files_it.peek().is_some() {
175+ let mut nr_of_files = 0;
176+ for (file, pos) in files_it {
177+ nr_of_files += 1;
173178 if file.is_dir() {
174179 return Err(anyhow::anyhow!(
175180 "expected a path to file, found a directory. (to open a directory pass it as first argument)"
@@ -181,7 +186,7 @@ impl Application {
181186 // option. If neither of those two arguments are passed
182187 // in, just load the files normally.
183188 let action = match args.split {
184− _ if i == 0 => Action::VerticalSplit,
189+ _ if nr_of_files == 1 => Action::VerticalSplit,
185190 Some(Layout::Vertical) => Action::VerticalSplit,
186191 Some(Layout::Horizontal) => Action::HorizontalSplit,
187192 None => Action::Load,
@@ -208,6 +213,8 @@ impl Application {
208213 // does not affect views without pos since it is at the top
209214 let (view, doc) = current!(editor);
210215 align_view(doc, view, Align::Center);
216+ } else {
217+ editor.new_file(Action::VerticalSplit);
211218 }
212219 } else if stdin().is_tty() || cfg!(feature = "integration") {
213220 editor.new_file(Action::VerticalSplit);
helix-term/src/args.rs−1
@@ -17,7 +17,6 @@ pub struct Args {
1717 pub log_file: Option<PathBuf>,
1818 pub config_file: Option<PathBuf>,
1919 pub files: Vec<(PathBuf, Position)>,
20− pub open_cwd: bool,
2120 pub working_directory: Option<PathBuf>,
2221}
2322
helix-term/src/main.rs+7−5
@@ -116,16 +116,18 @@ FLAGS:
116116
117117 setup_logging(args.verbosity).context("failed to initialize logging")?;
118118
119+ // Before setting the working directory, resolve all the paths in args.files
120+ for (path, _) in args.files.iter_mut() {
121+ *path = helix_core::path::get_canonicalized_path(path);
122+ }
123+
119124 // NOTE: Set the working directory early so the correct configuration is loaded. Be aware that
120125 // Application::new() depends on this logic so it must be updated if this changes.
121126 if let Some(path) = &args.working_directory {
122127 helix_loader::set_current_working_dir(path)?;
123− }
124−
125− // If the first file is a directory, it will be the working directory and a file picker will be opened
126− if let Some((path, _)) = args.files.first().filter(|p| p.0.is_dir()) {
128+ } else if let Some((path, _)) = args.files.first().filter(|p| p.0.is_dir()) {
129+ // If the first file is a directory, it will be the working directory unless -w was specified
127130 helix_loader::set_current_working_dir(path)?;
128− args.open_cwd = true; // Signal Application that we want to open the picker on "."
129131 }
130132
131133 let config = match Config::load_default() {