diff --git a/assets/settings/default.json b/assets/settings/default.json index 6c34d6be70..5c05e8eaba 100644 --- a/assets/settings/default.json +++ b/assets/settings/default.json @@ -84,7 +84,8 @@ "shell": "system", // What working directory to use when launching the terminal. // May take 4 values: - // 1. Use the current file's project directory. + // 1. Use the current file's project directory. Will Fallback to the + // first project directory strategy if unsuccessful // "working_directory": "current_project_directory" // 2. Use the first project in this workspace's directory // "working_directory": "first_project_directory" diff --git a/crates/terminal/src/modal_view.rs b/crates/terminal/src/modal_view.rs index ec5280befc..9ff0507382 100644 --- a/crates/terminal/src/modal_view.rs +++ b/crates/terminal/src/modal_view.rs @@ -1,4 +1,5 @@ use gpui::{ModelHandle, ViewContext}; +use settings::{Settings, WorkingDirectory}; use workspace::Workspace; use crate::{ @@ -27,7 +28,14 @@ pub fn deploy_modal(workspace: &mut Workspace, _: &DeployModal, cx: &mut ViewCon // No connection was stored, create a new terminal if let Some(closed_terminal_handle) = workspace.toggle_modal(cx, |workspace, cx| { // No terminal modal visible, construct a new one. - let working_directory = get_working_directory(workspace, cx); + let wd_strategy = cx + .global::() + .terminal_overrides + .working_directory + .clone() + .unwrap_or(WorkingDirectory::CurrentProjectDirectory); + + let working_directory = get_working_directory(workspace, cx, wd_strategy); let this = cx.add_view(|cx| TerminalView::new(working_directory, true, cx)); diff --git a/crates/terminal/src/terminal.rs b/crates/terminal/src/terminal.rs index 4e5829559e..27339d2d00 100644 --- a/crates/terminal/src/terminal.rs +++ b/crates/terminal/src/terminal.rs @@ -80,7 +80,14 @@ impl Entity for ErrorView { impl TerminalView { ///Create a new Terminal in the current working directory or the user's home directory fn deploy(workspace: &mut Workspace, _: &Deploy, cx: &mut ViewContext) { - let working_directory = get_working_directory(workspace, cx); + let wd_strategy = cx + .global::() + .terminal_overrides + .working_directory + .clone() + .unwrap_or(WorkingDirectory::CurrentProjectDirectory); + + let working_directory = get_working_directory(workspace, cx, wd_strategy); let view = cx.add_view(|cx| TerminalView::new(working_directory, false, cx)); workspace.add_item(Box::new(view), cx); } @@ -327,15 +334,14 @@ impl Item for TerminalView { } ///Get's the working directory for the given workspace, respecting the user's settings. -fn get_working_directory(workspace: &Workspace, cx: &AppContext) -> Option { - let wd_setting = cx - .global::() - .terminal_overrides - .working_directory - .clone() - .unwrap_or(WorkingDirectory::CurrentProjectDirectory); - let res = match wd_setting { - WorkingDirectory::CurrentProjectDirectory => current_project_directory(workspace, cx), +fn get_working_directory( + workspace: &Workspace, + cx: &AppContext, + strategy: WorkingDirectory, +) -> Option { + let res = match strategy { + WorkingDirectory::CurrentProjectDirectory => current_project_directory(workspace, cx) + .or_else(|| first_project_directory(workspace, cx)), WorkingDirectory::FirstProjectDirectory => first_project_directory(workspace, cx), WorkingDirectory::AlwaysHome => None, WorkingDirectory::Always { directory } => { @@ -519,4 +525,28 @@ mod tests { assert_eq!(res, Some((Path::new("/root1/")).to_path_buf())); }); } + + //Active entry with a work tree, worktree is a file, integration test with the strategy interface + #[gpui::test] + async fn active_entry_worktree_is_file_int(cx: &mut TestAppContext) { + //Setup variables + let mut cx = TerminalTestContext::new(cx, true); + let (project, workspace) = cx.blank_workspace().await; + let (_wt, _entry) = cx.create_folder_wt(project.clone(), "/root1/").await; + let (wt2, entry2) = cx.create_file_wt(project.clone(), "/root2.txt").await; + cx.insert_active_entry_for(wt2, entry2, project.clone()); + + //Test + cx.cx.update(|cx| { + let workspace = workspace.read(cx); + let active_entry = project.read(cx).active_entry(); + + assert!(active_entry.is_some()); + + let res = + get_working_directory(workspace, cx, WorkingDirectory::CurrentProjectDirectory); + let first = first_project_directory(workspace, cx); + assert_eq!(res, first); + }); + } }