Fix incorrect outline selections after submit (#9160)

Kirill Bulatov created

Follow-up of https://github.com/zed-industries/zed/pull/9153

Release Notes:

- N/A

Change summary

Cargo.lock                          |  2 +
crates/go_to_line/Cargo.toml        |  1 
crates/go_to_line/src/go_to_line.rs | 41 ++++++++++++++++++++++++++-
crates/outline/Cargo.toml           |  1 
crates/outline/src/outline.rs       | 46 ++++++++++++++++++++++++++++--
5 files changed, 85 insertions(+), 6 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -4291,6 +4291,7 @@ dependencies = [
  "language",
  "menu",
  "project",
+ "rope",
  "serde_json",
  "text",
  "theme",
@@ -6626,6 +6627,7 @@ dependencies = [
  "ordered-float 2.10.0",
  "picker",
  "project",
+ "rope",
  "serde_json",
  "settings",
  "smol",

crates/go_to_line/Cargo.toml 🔗

@@ -29,6 +29,7 @@ indoc.workspace = true
 language = { workspace = true, features = ["test-support"] }
 menu.workspace = true
 project = { workspace = true, features = ["test-support"] }
+rope.workspace = true
 serde_json.workspace = true
 tree-sitter-rust.workspace = true
 tree-sitter-typescript.workspace = true

crates/go_to_line/src/go_to_line.rs 🔗

@@ -278,6 +278,7 @@ mod tests {
             Vec::<u32>::new(),
             "Initially opened go to line modal should not highlight any rows"
         );
+        assert_single_caret_at_row(&editor, 0, cx);
 
         cx.simulate_input("1");
         assert_eq!(
@@ -285,6 +286,7 @@ mod tests {
             vec![0],
             "Go to line modal should highlight a row, corresponding to the query"
         );
+        assert_single_caret_at_row(&editor, 0, cx);
 
         cx.simulate_input("8");
         assert_eq!(
@@ -292,6 +294,7 @@ mod tests {
             vec![13],
             "If the query is too large, the last row should be highlighted"
         );
+        assert_single_caret_at_row(&editor, 0, cx);
 
         cx.dispatch_action(menu::Cancel);
         drop(go_to_line_view);
@@ -301,6 +304,7 @@ mod tests {
             Vec::<u32>::new(),
             "After cancelling and closing the modal, no rows should be highlighted"
         );
+        assert_single_caret_at_row(&editor, 0, cx);
 
         let go_to_line_view = open_go_to_line_view(&workspace, cx);
         assert_eq!(
@@ -308,10 +312,15 @@ mod tests {
             Vec::<u32>::new(),
             "Reopened modal should not highlight any rows"
         );
+        assert_single_caret_at_row(&editor, 0, cx);
 
+        let expected_highlighted_row = 4;
         cx.simulate_input("5");
-        assert_eq!(highlighted_display_rows(&editor, cx), vec![4]);
-
+        assert_eq!(
+            highlighted_display_rows(&editor, cx),
+            vec![expected_highlighted_row]
+        );
+        assert_single_caret_at_row(&editor, 0, cx);
         cx.dispatch_action(menu::Confirm);
         drop(go_to_line_view);
         editor.update(cx, |_, _| {});
@@ -320,6 +329,8 @@ mod tests {
             Vec::<u32>::new(),
             "After confirming and closing the modal, no rows should be highlighted"
         );
+        // On confirm, should place the caret on the highlighted row.
+        assert_single_caret_at_row(&editor, expected_highlighted_row, cx);
     }
 
     fn open_go_to_line_view(
@@ -338,6 +349,32 @@ mod tests {
         })
     }
 
+    #[track_caller]
+    fn assert_single_caret_at_row(
+        editor: &View<Editor>,
+        buffer_row: u32,
+        cx: &mut VisualTestContext,
+    ) {
+        let selections = editor.update(cx, |editor, cx| {
+            editor
+                .selections
+                .all::<rope::Point>(cx)
+                .into_iter()
+                .map(|s| s.start..s.end)
+                .collect::<Vec<_>>()
+        });
+        assert!(
+            selections.len() == 1,
+            "Expected one caret selection but got: {selections:?}"
+        );
+        let selection = &selections[0];
+        assert!(
+            selection.start == selection.end,
+            "Expected a single caret selection, but got: {selection:?}"
+        );
+        assert_eq!(selection.start.row, buffer_row);
+    }
+
     fn init_test(cx: &mut TestAppContext) -> Arc<AppState> {
         cx.update(|cx| {
             let state = AppState::test(cx);

crates/outline/Cargo.toml 🔗

@@ -33,6 +33,7 @@ indoc.workspace = true
 language = { workspace = true, features = ["test-support"] }
 menu.workspace = true
 project = { workspace = true, features = ["test-support"] }
+rope.workspace = true
 serde_json.workspace = true
 tree-sitter-rust.workspace = true
 tree-sitter-typescript.workspace = true

crates/outline/src/outline.rs 🔗

@@ -242,8 +242,9 @@ impl PickerDelegate for OutlineViewDelegate {
                 .highlighted_rows::<OutlineRowHighlights>()
                 .and_then(|highlights| highlights.into_iter().next().map(|(rows, _)| rows.clone()))
             {
-                active_editor
-                    .change_selections(Some(Autoscroll::center()), cx, |s| s.select_ranges([rows]));
+                active_editor.change_selections(Some(Autoscroll::center()), cx, |s| {
+                    s.select_ranges([rows.start..rows.start])
+                });
                 active_editor.clear_row_highlights::<OutlineRowHighlights>();
                 active_editor.focus(cx);
             }
@@ -385,6 +386,7 @@ mod tests {
             Vec::<u32>::new(),
             "Initially opened outline view should have no highlights"
         );
+        assert_single_caret_at_row(&editor, 0, cx);
 
         cx.dispatch_action(menu::SelectNext);
         ensure_outline_view_contents(&outline_view, cx);
@@ -393,6 +395,7 @@ mod tests {
             vec![2, 3, 4, 5],
             "Second struct's rows should be highlighted"
         );
+        assert_single_caret_at_row(&editor, 0, cx);
 
         cx.dispatch_action(menu::SelectPrev);
         ensure_outline_view_contents(&outline_view, cx);
@@ -401,6 +404,7 @@ mod tests {
             vec![0],
             "First struct's row should be highlighted"
         );
+        assert_single_caret_at_row(&editor, 0, cx);
 
         cx.dispatch_action(menu::Cancel);
         ensure_outline_view_contents(&outline_view, cx);
@@ -409,6 +413,7 @@ mod tests {
             Vec::<u32>::new(),
             "No rows should be highlighted after outline view is cancelled and closed"
         );
+        assert_single_caret_at_row(&editor, 0, cx);
 
         let outline_view = open_outline_view(&workspace, cx);
         ensure_outline_view_contents(&outline_view, cx);
@@ -417,11 +422,16 @@ mod tests {
             Vec::<u32>::new(),
             "Reopened outline view should have no highlights"
         );
+        assert_single_caret_at_row(&editor, 0, cx);
 
+        let expected_first_highlighted_row = 2;
         cx.dispatch_action(menu::SelectNext);
         ensure_outline_view_contents(&outline_view, cx);
-        assert_eq!(highlighted_display_rows(&editor, cx), vec![2, 3, 4, 5]);
-
+        assert_eq!(
+            highlighted_display_rows(&editor, cx),
+            vec![expected_first_highlighted_row, 3, 4, 5]
+        );
+        assert_single_caret_at_row(&editor, 0, cx);
         cx.dispatch_action(menu::Confirm);
         ensure_outline_view_contents(&outline_view, cx);
         assert_eq!(
@@ -429,6 +439,8 @@ mod tests {
             Vec::<u32>::new(),
             "No rows should be highlighted after outline view is confirmed and closed"
         );
+        // On confirm, should place the caret on the first row of the highlighted rows range.
+        assert_single_caret_at_row(&editor, expected_first_highlighted_row, cx);
     }
 
     fn open_outline_view(
@@ -568,4 +580,30 @@ mod tests {
             .unwrap(),
         )
     }
+
+    #[track_caller]
+    fn assert_single_caret_at_row(
+        editor: &View<Editor>,
+        buffer_row: u32,
+        cx: &mut VisualTestContext,
+    ) {
+        let selections = editor.update(cx, |editor, cx| {
+            editor
+                .selections
+                .all::<rope::Point>(cx)
+                .into_iter()
+                .map(|s| s.start..s.end)
+                .collect::<Vec<_>>()
+        });
+        assert!(
+            selections.len() == 1,
+            "Expected one caret selection but got: {selections:?}"
+        );
+        let selection = &selections[0];
+        assert!(
+            selection.start == selection.end,
+            "Expected a single caret selection, but got: {selection:?}"
+        );
+        assert_eq!(selection.start.row, buffer_row);
+    }
 }