Add test for new sorting behavior

Kay Simmons created

Change summary

crates/editor/src/test/editor_lsp_test_context.rs |  6 +-
crates/file_finder/src/file_finder.rs             | 43 +++++++++++++++++
crates/fuzzy/src/paths.rs                         | 12 +---
3 files changed, 50 insertions(+), 11 deletions(-)

Detailed changes

crates/editor/src/test/editor_lsp_test_context.rs 🔗

@@ -39,7 +39,7 @@ impl<'a> EditorLspTestContext<'a> {
             pane::init(cx);
         });
 
-        let params = cx.update(AppState::test);
+        let app_state = cx.update(AppState::test);
 
         let file_name = format!(
             "file.{}",
@@ -56,10 +56,10 @@ impl<'a> EditorLspTestContext<'a> {
             }))
             .await;
 
-        let project = Project::test(params.fs.clone(), [], cx).await;
+        let project = Project::test(app_state.fs.clone(), [], cx).await;
         project.update(cx, |project, _| project.languages().add(Arc::new(language)));
 
-        params
+        app_state
             .fs
             .as_fake()
             .insert_tree("/root", json!({ "dir": { file_name.clone(): "" }}))

crates/file_finder/src/file_finder.rs 🔗

@@ -544,6 +544,7 @@ mod tests {
         let (_, workspace) = cx.add_window(|cx| {
             Workspace::new(Default::default(), 0, project, |_, _| unimplemented!(), cx)
         });
+
         let (_, finder) =
             cx.add_window(|cx| FileFinder::new(workspace.read(cx).project().clone(), None, cx));
 
@@ -563,6 +564,48 @@ mod tests {
         });
     }
 
+    #[gpui::test]
+    async fn test_path_distance_ordering(cx: &mut gpui::TestAppContext) {
+        cx.foreground().forbid_parking();
+
+        let app_state = cx.update(AppState::test);
+        app_state
+            .fs
+            .as_fake()
+            .insert_tree(
+                "/root",
+                json!({
+                    "dir1": { "a.txt": "" },
+                    "dir2": {
+                        "a.txt": "",
+                        "b.txt": ""
+                    }
+                }),
+            )
+            .await;
+
+        let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await;
+        let (_, workspace) = cx.add_window(|cx| {
+            Workspace::new(Default::default(), 0, project, |_, _| unimplemented!(), cx)
+        });
+
+        // When workspace has an active item, sort items which are closer to that item
+        // first when they have the same name. In this case, b.txt is closer to dir2's a.txt
+        // so that one should be sorted earlier
+        let b_path = Some(Arc::from(Path::new("/root/dir2/b.txt")));
+        let (_, finder) =
+            cx.add_window(|cx| FileFinder::new(workspace.read(cx).project().clone(), b_path, cx));
+
+        finder
+            .update(cx, |f, cx| f.spawn_search("a.txt".into(), cx))
+            .await;
+
+        finder.read_with(cx, |f, _| {
+            assert_eq!(f.matches[0].path.as_ref(), Path::new("dir2/a.txt"));
+            assert_eq!(f.matches[1].path.as_ref(), Path::new("dir1/a.txt"));
+        });
+    }
+
     #[gpui::test]
     async fn test_search_worktree_without_files(cx: &mut gpui::TestAppContext) {
         let app_state = cx.update(AppState::test);

crates/fuzzy/src/paths.rs 🔗

@@ -194,13 +194,9 @@ fn distance_to_relative_ancestor(path: &Path, relative_to: &Option<Arc<Path>>) -
         return usize::MAX;
     };
 
-    for (path_ancestor_count, path_ancestor) in path.ancestors().enumerate() {
-        for (relative_ancestor_count, relative_ancestor) in relative_to.ancestors().enumerate() {
-            if path_ancestor == relative_ancestor {
-                return path_ancestor_count + relative_ancestor_count;
-            }
-        }
-    }
+    let mut path_components = path.components();
+    let mut relative_components = relative_to.components();
 
-    usize::MAX
+    while path_components.next() == relative_components.next() {}
+    path_components.count() + relative_components.count() + 1
 }