on master help sign in
Resolve args.files before changing directory (#8676)
47b6c4bBjorn Ove Hay Andersen
helix-term/src/application.rs+15−8
@@ -162,14 +162,19 @@ impl Application {
162 // Unset path to prevent accidentally saving to the original tutor file.
162 // Unset path to prevent accidentally saving to the original tutor file.
163 doc_mut!(editor).set_path(None);
163 doc_mut!(editor).set_path(None);
164 } else if !args.files.is_empty() {
164 } else if !args.files.is_empty() {
165− if args.open_cwd {
165+ let mut files_it = args.files.into_iter().peekable();
166− // NOTE: The working directory is already set to args.files[0] in main()
166+
167− editor.new_file(Action::VerticalSplit);
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()) {
168− let picker = ui::file_picker(".".into(), &config.load().editor);
169+ let picker = ui::file_picker(first, &config.load().editor);
169 compositor.push(Box::new(overlaid(picker)));
170 compositor.push(Box::new(overlaid(picker)));
170− } else {
171+ }
172+
173+ // If there are any more files specified, open them
174+ if files_it.peek().is_some() {
171− let nr_of_files = args.files.len();
175+ let mut nr_of_files = 0;
172− for (i, (file, pos)) in args.files.into_iter().enumerate() {
176+ for (file, pos) in files_it {
177+ nr_of_files += 1;
173 if file.is_dir() {
178 if file.is_dir() {
174 return Err(anyhow::anyhow!(
179 return Err(anyhow::anyhow!(
175 "expected a path to file, found a directory. (to open a directory pass it as first argument)"
180 "expected a path to file, found a directory. (to open a directory pass it as first argument)"
@@ -181,7 +186,7 @@ impl Application {
181 // option. If neither of those two arguments are passed
186 // option. If neither of those two arguments are passed
182 // in, just load the files normally.
187 // in, just load the files normally.
183 let action = match args.split {
188 let action = match args.split {
184− _ if i == 0 => Action::VerticalSplit,
189+ _ if nr_of_files == 1 => Action::VerticalSplit,
185 Some(Layout::Vertical) => Action::VerticalSplit,
190 Some(Layout::Vertical) => Action::VerticalSplit,
186 Some(Layout::Horizontal) => Action::HorizontalSplit,
191 Some(Layout::Horizontal) => Action::HorizontalSplit,
187 None => Action::Load,
192 None => Action::Load,
@@ -208,6 +213,8 @@ impl Application {
208 // does not affect views without pos since it is at the top
213 // does not affect views without pos since it is at the top
209 let (view, doc) = current!(editor);
214 let (view, doc) = current!(editor);
210 align_view(doc, view, Align::Center);
215 align_view(doc, view, Align::Center);
216+ } else {
217+ editor.new_file(Action::VerticalSplit);
211 }
218 }
212 } else if stdin().is_tty() || cfg!(feature = "integration") {
219 } else if stdin().is_tty() || cfg!(feature = "integration") {
213 editor.new_file(Action::VerticalSplit);
220 editor.new_file(Action::VerticalSplit);
helix-term/src/args.rs−1
@@ -17,7 +17,6 @@ pub struct Args {
17 pub log_file: Option<PathBuf>,
17 pub log_file: Option<PathBuf>,
18 pub config_file: Option<PathBuf>,
18 pub config_file: Option<PathBuf>,
19 pub files: Vec<(PathBuf, Position)>,
19 pub files: Vec<(PathBuf, Position)>,
20− pub open_cwd: bool,
21 pub working_directory: Option<PathBuf>,
20 pub working_directory: Option<PathBuf>,
22}
21}
23
22
helix-term/src/main.rs+7−5
@@ -116,16 +116,18 @@ FLAGS:
116
116
117 setup_logging(args.verbosity).context("failed to initialize logging")?;
117 setup_logging(args.verbosity).context("failed to initialize logging")?;
118
118
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+
119 // NOTE: Set the working directory early so the correct configuration is loaded. Be aware that
124 // NOTE: Set the working directory early so the correct configuration is loaded. Be aware that
120 // Application::new() depends on this logic so it must be updated if this changes.
125 // Application::new() depends on this logic so it must be updated if this changes.
121 if let Some(path) = &args.working_directory {
126 if let Some(path) = &args.working_directory {
122 helix_loader::set_current_working_dir(path)?;
127 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
127 helix_loader::set_current_working_dir(path)?;
130 helix_loader::set_current_working_dir(path)?;
128− args.open_cwd = true; // Signal Application that we want to open the picker on "."
129 }
131 }
130
132
131 let config = match Config::load_default() {
133 let config = match Config::load_default() {