Merge pull request #1417 from zed-industries/working-directory-regression

Mikayla Maki created

Fixed regression in working directory code

Change summary

assets/settings/default.json      |  3 +
crates/terminal/src/modal_view.rs | 10 +++++
crates/terminal/src/terminal.rs   | 50 ++++++++++++++++++++++++++------
3 files changed, 51 insertions(+), 12 deletions(-)

Detailed changes

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"

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::<Settings>()
+                .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));
 

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<Workspace>) {
-        let working_directory = get_working_directory(workspace, cx);
+        let wd_strategy = cx
+            .global::<Settings>()
+            .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<PathBuf> {
-    let wd_setting = cx
-        .global::<Settings>()
-        .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<PathBuf> {
+    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);
+        });
+    }
 }