Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

file_finder: Fix `path:line` navigation when target file is already open (#55875)

Kunall Banerjee9dc8880b2b96
diff --git a/crates/file_finder/src/file_finder.rs b/crates/file_finder/src/file_finder.rs
index d37a419b127..18859e74a9c 100644
--- a/crates/file_finder/src/file_finder.rs
+++ b/crates/file_finder/src/file_finder.rs
@@ -1109,14 +1109,23 @@ impl FileFinderDelegate {
) {
if search_id >= self.latest_search_id {
self.latest_search_id = search_id;
- let query_changed = Some(query.path_query())
+ let path_query_changed = Some(query.path_query())
!= self
.latest_search_query
.as_ref()
.map(|query| query.path_query());
- let extend_old_matches = self.latest_search_did_cancel && !query_changed;
-
- let selected_match = if query_changed {
+ let extend_old_matches = self.latest_search_did_cancel && !path_query_changed;
+
+ // The line/column suffix doesn't affect which files match, but it
+ // does change the user's intent ("go inside this file" vs. "pick a
+ // file"). Drop the preserved selection when it appears or changes
+ // so the new intent can be honored by `calculate_selected_index`.
+ let position_changed = self
+ .latest_search_query
+ .as_ref()
+ .and_then(|q| q.path_position.row)
+ != query.path_position.row;
+ let selected_match = if path_query_changed || position_changed {
None
} else {
self.matches.get(self.selected_index).cloned()
@@ -1196,7 +1205,7 @@ impl FileFinderDelegate {
}
}
- let query_path = query.raw_query.as_str();
+ let query_path = query.path_query();
if let Ok(mut query_path) = RelPath::new(Path::new(query_path), path_style) {
let available_worktree = self
.project
@@ -1231,8 +1240,8 @@ impl FileFinderDelegate {
if let Some(worktree) = expect_worktree {
let worktree = worktree.read(cx);
if worktree.entry_for_path(&query_path).is_none()
- && !query.raw_query.ends_with("/")
- && !(path_style.is_windows() && query.raw_query.ends_with("\\"))
+ && !query.path_query().ends_with('/')
+ && !(path_style.is_windows() && query.path_query().ends_with('\\'))
{
self.matches.matches.push(Match::CreateNew(ProjectPath {
worktree_id: worktree.id(),
@@ -1247,8 +1256,9 @@ impl FileFinderDelegate {
self.selected_index = if !self.selected_matches.is_empty() {
0
} else {
+ let query_has_position = query.path_position.row.is_some();
selected_match.map_or_else(
- || self.calculate_selected_index(cx),
+ || self.calculate_selected_index(query_has_position, cx),
|m| {
self.matches
.position(&m, self.currently_opened_path.as_ref())
@@ -1515,8 +1525,17 @@ impl FileFinderDelegate {
}
/// Skips first history match (that is displayed topmost) if it's currently opened.
- fn calculate_selected_index(&self, cx: &mut Context<Picker<Self>>) -> usize {
- if FileFinderSettings::get_global(cx).skip_focus_for_active_in_search
+ ///
+ /// When the query carries a row (e.g. `foo.rs:42`), the user is asking to
+ /// navigate inside a specific file, so we never skip past the active match
+ /// even if the setting is enabled.
+ fn calculate_selected_index(
+ &self,
+ query_has_position: bool,
+ cx: &mut Context<Picker<Self>>,
+ ) -> usize {
+ if !query_has_position
+ && FileFinderSettings::get_global(cx).skip_focus_for_active_in_search
&& let Some(Match::History { path, .. }) = self.matches.get(0)
&& Some(path) == self.currently_opened_path.as_ref()
{
diff --git a/crates/file_finder/src/file_finder_tests.rs b/crates/file_finder/src/file_finder_tests.rs
index 2c6df4b1e5b..22c901c0eda 100644
--- a/crates/file_finder/src/file_finder_tests.rs
+++ b/crates/file_finder/src/file_finder_tests.rs
@@ -484,7 +484,9 @@ async fn test_row_column_numbers_query_inside_file(cx: &mut TestAppContext) {
})
.await;
picker.update(cx, |finder, _| {
- assert_match_at_position(finder, 1, &query_inside_file.to_string());
+ // The CreateNew fallback is now keyed on the parsed path (without the
+ // `:row:column` suffix), so its file_name matches `file_query`.
+ assert_match_at_position(finder, 1, file_query);
let finder = &finder.delegate;
assert_eq!(finder.matches.len(), 2);
let latest_search_query = finder
@@ -558,7 +560,9 @@ async fn test_row_column_numbers_query_inside_unicode_file(cx: &mut TestAppConte
})
.await;
picker.update(cx, |finder, _| {
- assert_match_at_position(finder, 1, &query_inside_file.to_string());
+ // The CreateNew fallback is now keyed on the parsed path (without the
+ // `:row:column` suffix), so its file_name matches `file_query`.
+ assert_match_at_position(finder, 1, file_query);
let finder = &finder.delegate;
assert_eq!(finder.matches.len(), 2);
let latest_search_query = finder
@@ -644,7 +648,9 @@ async fn test_row_column_numbers_query_outside_file(cx: &mut TestAppContext) {
})
.await;
picker.update(cx, |finder, _| {
- assert_match_at_position(finder, 1, &query_outside_file.to_string());
+ // The CreateNew fallback is now keyed on the parsed path (without the
+ // `:row:column` suffix), so its file_name matches `file_query`.
+ assert_match_at_position(finder, 1, file_query);
let delegate = &finder.delegate;
assert_eq!(delegate.matches.len(), 2);
let latest_search_query = delegate
@@ -787,9 +793,8 @@ async fn test_line_range_query_selects_lines(cx: &mut TestAppContext) {
})
.await;
picker.update(cx, |finder, _| {
- assert_eq!(finder.delegate.matches.len(), 2);
+ assert_eq!(finder.delegate.matches.len(), 1);
assert_match_at_position(finder, 0, "first.rs");
- assert_match_at_position(finder, 1, "first.rs:2-4");
let latest_search_query = finder
.delegate
@@ -876,6 +881,48 @@ async fn test_line_range_query_outside_file_clamps_to_eof(cx: &mut TestAppContex
});
}
+// Regression test for https://github.com/zed-industries/zed/issues/55551.
+//
+// Typing `path:line` for an already-open file must keep the file selected
+// rather than offering to create one or skipping past it to a fuzzy neighbor.
+#[gpui::test]
+async fn test_path_with_position_when_target_file_is_open(cx: &mut TestAppContext) {
+ let app_state = init_test(cx);
+ app_state
+ .fs
+ .as_fake()
+ .insert_tree(
+ path!("/src"),
+ json!({
+ "default.json": "line 1\nline 2\nline 3\n",
+ "seed.default.json": "",
+ }),
+ )
+ .await;
+
+ let project = Project::test(app_state.fs.clone(), [path!("/src").as_ref()], cx).await;
+ let (_, workspace, cx) = build_find_picker(project, cx);
+ open_queried_buffer("default", 2, "default.json", &workspace, cx).await;
+
+ // Type the query character by character. Appending `:3` keeps
+ // `path_query()` the same, so the previously-buggy code preserved the
+ // skipped-to selection (`seed.default.json`) instead of re-evaluating.
+ let picker = open_file_picker(&workspace, cx);
+ cx.simulate_input("default.json:3");
+ picker.update(cx, |finder, _| {
+ assert!(
+ finder
+ .delegate
+ .matches
+ .matches
+ .iter()
+ .all(|m| !matches!(m, Match::CreateNew(_))),
+ "`Create file:` row should not be offered for an existing file"
+ );
+ assert_match_selection(finder, 0, "default.json");
+ });
+}
+
#[gpui::test]
async fn test_matching_cancellation(cx: &mut TestAppContext) {
let app_state = init_test(cx);