Skip to content

Commit

Permalink
Open folds containing selections when jumping from multibuffer (#21433)
Browse files Browse the repository at this point in the history
When searching within a single buffer, activating a search result causes
any fold containing the result to be unfolded. However, this didn't
happen when jumping to a search result from a project-wide search
multibuffer. This PR fixes that.

Release Notes:

- Fixed folds not opening when jumping from search results multibuffer
  • Loading branch information
cole-miller authored Dec 3, 2024
1 parent aca23da commit dc32ab2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12876,6 +12876,7 @@ impl Editor {
None => Autoscroll::newest(),
};
let nav_history = editor.nav_history.take();
editor.unfold_ranges(&ranges, false, true, cx);
editor.change_selections(Some(autoscroll), cx, |s| {
s.select_ranges(ranges);
});
Expand Down
70 changes: 69 additions & 1 deletion crates/editor/src/editor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11567,7 +11567,7 @@ async fn test_multibuffer_reverts(cx: &mut gpui::TestAppContext) {
}

#[gpui::test]
async fn test_mutlibuffer_in_navigation_history(cx: &mut gpui::TestAppContext) {
async fn test_multibuffer_in_navigation_history(cx: &mut gpui::TestAppContext) {
init_test(cx, |_| {});

let cols = 4;
Expand Down Expand Up @@ -11856,6 +11856,74 @@ async fn test_mutlibuffer_in_navigation_history(cx: &mut gpui::TestAppContext) {
.unwrap();
}

#[gpui::test]
async fn test_multibuffer_unfold_on_jump(cx: &mut gpui::TestAppContext) {
init_test(cx, |_| {});

let texts = ["{\n\tx\n}".to_owned(), "y".to_owned()];
let buffers = texts
.clone()
.map(|txt| cx.new_model(|cx| Buffer::local(txt, cx)));
let multi_buffer = cx.new_model(|cx| {
let mut multi_buffer = MultiBuffer::new(ReadWrite);
for i in 0..2 {
multi_buffer.push_excerpts(
buffers[i].clone(),
[ExcerptRange {
context: 0..texts[i].len(),
primary: None,
}],
cx,
);
}
multi_buffer
});

let fs = FakeFs::new(cx.executor());
fs.insert_tree(
"/project",
json!({
"x": &texts[0],
"y": &texts[1],
}),
)
.await;
let project = Project::test(fs, ["/project".as_ref()], cx).await;
let workspace = cx.add_window(|cx| Workspace::test_new(project.clone(), cx));
let cx = &mut VisualTestContext::from_window(*workspace.deref(), cx);

let multi_buffer_editor = cx.new_view(|cx| {
Editor::for_multibuffer(multi_buffer.clone(), Some(project.clone()), true, cx)
});
let buffer_editor =
cx.new_view(|cx| Editor::for_buffer(buffers[0].clone(), Some(project.clone()), cx));
workspace
.update(cx, |workspace, cx| {
workspace.add_item_to_active_pane(
Box::new(multi_buffer_editor.clone()),
None,
true,
cx,
);
workspace.add_item_to_active_pane(Box::new(buffer_editor.clone()), None, false, cx);
})
.unwrap();
cx.executor().run_until_parked();
buffer_editor.update(cx, |buffer_editor, cx| {
buffer_editor.fold_at_level(&FoldAtLevel { level: 1 }, cx);
assert!(buffer_editor.snapshot(cx).fold_count() == 1);
});
cx.executor().run_until_parked();
multi_buffer_editor.update(cx, |multi_buffer_editor, cx| {
multi_buffer_editor.change_selections(None, cx, |s| s.select_ranges([3..4]));
multi_buffer_editor.open_excerpts(&OpenExcerpts, cx);
});
cx.executor().run_until_parked();
buffer_editor.update(cx, |buffer_editor, cx| {
assert!(buffer_editor.snapshot(cx).fold_count() == 0);
});
}

#[gpui::test]
async fn test_toggle_hunk_diff(executor: BackgroundExecutor, cx: &mut gpui::TestAppContext) {
init_test(cx, |_| {});
Expand Down

0 comments on commit dc32ab2

Please sign in to comment.