diff --git a/Cargo.lock b/Cargo.lock index 74debbe7264cb6cb9516eea5c6e32455ee4498f7..42bb63944537c1cc9fd3ae9418f47531d340d654 100644 --- a/Cargo.lock +++ b/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", diff --git a/crates/go_to_line/Cargo.toml b/crates/go_to_line/Cargo.toml index 59a0613eed9d2253fdf9d6706aa64cbe0941c791..a697242523800bbe083591f1110347da33a4c253 100644 --- a/crates/go_to_line/Cargo.toml +++ b/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 diff --git a/crates/go_to_line/src/go_to_line.rs b/crates/go_to_line/src/go_to_line.rs index 895c383bdce7455d837b3fd1516f556d69907268..2e4402ae5bc21f975a041c37badee09235a436c9 100644 --- a/crates/go_to_line/src/go_to_line.rs +++ b/crates/go_to_line/src/go_to_line.rs @@ -278,6 +278,7 @@ mod tests { Vec::::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::::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::::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::::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, + buffer_row: u32, + cx: &mut VisualTestContext, + ) { + let selections = editor.update(cx, |editor, cx| { + editor + .selections + .all::(cx) + .into_iter() + .map(|s| s.start..s.end) + .collect::>() + }); + 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 { cx.update(|cx| { let state = AppState::test(cx); diff --git a/crates/outline/Cargo.toml b/crates/outline/Cargo.toml index 409d4bbc1e555fdee9446448c55478db4e90a742..6f385f5d8d0821ea9f879793d029ca8eba8bc03c 100644 --- a/crates/outline/Cargo.toml +++ b/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 diff --git a/crates/outline/src/outline.rs b/crates/outline/src/outline.rs index 93d5f6e9008031ae7ee0e16ebdb01df9a64cd691..3460cf29d59c860cbce96554ec0dfe749b47db5d 100644 --- a/crates/outline/src/outline.rs +++ b/crates/outline/src/outline.rs @@ -242,8 +242,9 @@ impl PickerDelegate for OutlineViewDelegate { .highlighted_rows::() .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::(); active_editor.focus(cx); } @@ -385,6 +386,7 @@ mod tests { Vec::::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::::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::::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::::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, + buffer_row: u32, + cx: &mut VisualTestContext, + ) { + let selections = editor.update(cx, |editor, cx| { + editor + .selections + .all::(cx) + .into_iter() + .map(|s| s.start..s.end) + .collect::>() + }); + 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); + } }