Skip to content

Commit

Permalink
Fixed issue when the first file specified as an argument was a relati…
Browse files Browse the repository at this point in the history
…ve directory
  • Loading branch information
bjorn-ove committed Oct 12, 2023
1 parent 07a006d commit 2cfcce9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
4 changes: 2 additions & 2 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl Application {
args: Args,
config: Config,
syn_loader_conf: syntax::Configuration,
first_file_is_dir: bool,
) -> Result<Self, Error> {
#[cfg(feature = "integration")]
setup_integration_logging();
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 10 additions & 4 deletions helix-term/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,18 @@ FLAGS:

// 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, true)) = args.files.first().map(|(path, _)| (path, path.is_dir())) {
helix_loader::set_current_working_dir(path)?;
} else if let Some(path) = &args.working_directory {
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
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 => {
Expand All @@ -149,7 +155,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?;
Expand Down
20 changes: 17 additions & 3 deletions helix-term/tests/test/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
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);
Expand Down Expand Up @@ -296,7 +301,8 @@ impl AppBuilder {
path: P,
pos: Option<helix_core::Position>,
) -> 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
}

Expand All @@ -320,7 +326,15 @@ impl AppBuilder {
}

pub fn build(self) -> anyhow::Result<Application> {
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);
Expand Down

0 comments on commit 2cfcce9

Please sign in to comment.