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.

editor: Fix `fold_at_level` folding function arguments and not function body (#62245)

anxyuma170a1241477
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index 8683cf3ba50..d3f9dc22822 100644
--- a/crates/editor/src/editor_tests.rs
+++ b/crates/editor/src/editor_tests.rs
@@ -2165,6 +2165,124 @@ fn test_fold_at_level(cx: &mut TestAppContext) {
});
}
+#[gpui::test]
+fn test_fold_at_level_chain_fold(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let editor = cx.add_window(|window, cx| {
+ let text = "
+ fn foo(
+ a: i32,
+ ) {
+ if true {
+ let b = a;
+ } else {
+ let b = a;
+ }
+ }
+ "
+ .unindent();
+ let buffer = cx.new(|cx| Buffer::local(&text, cx).with_language(rust_lang(), cx));
+ let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
+
+ build_editor(buffer, window, cx)
+ });
+
+ editor
+ .update(cx, |editor, window, cx| {
+ editor.fold_at_level(&FoldAtLevel(1), window, cx);
+ assert_eq!(
+ editor.display_text(cx),
+ "
+ fn foo(⋯) {⋯}
+ "
+ .unindent(),
+ );
+
+ editor.unfold_all(&UnfoldAll, window, cx);
+ editor.fold_at_level(&FoldAtLevel(2), window, cx);
+ assert_eq!(
+ editor.display_text(cx),
+ "
+ fn foo(
+ a: i32,
+ ) {
+ if true {⋯} else {⋯}
+ }
+ "
+ .unindent(),
+ );
+ })
+ .unwrap();
+}
+
+#[gpui::test]
+fn test_fold_at_level_with_single_row_crease(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let editor = cx.add_window(|window, cx| {
+ let buffer = MultiBuffer::build_simple("aaaaaa\[email protected] and more\ncccccc\n", cx);
+ build_editor(buffer, window, cx)
+ });
+
+ editor
+ .update(cx, |editor, window, cx| {
+ let snapshot = editor.buffer().read(cx).snapshot(cx);
+ let range =
+ snapshot.anchor_before(Point::new(1, 0))..snapshot.anchor_after(Point::new(1, 9));
+
+ editor.insert_creases(Some(Crease::simple(range, FoldPlaceholder::test())), cx);
+
+ editor.fold_at_level(&FoldAtLevel(1), window, cx);
+
+ assert_eq!(
+ editor.display_text(cx),
+ "
+ aaaaaa
+ ⋯ and more
+ cccccc
+ "
+ .unindent(),
+ );
+ })
+ .unwrap();
+}
+
+#[gpui::test]
+fn test_fold_at_level_with_crease_on_boundary_row(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let editor = cx.add_window(|window, cx| {
+ let buffer = MultiBuffer::build_simple("aaa\nbbb\nccc\nddd eee\nfff\n", cx);
+ build_editor(buffer, window, cx)
+ });
+
+ editor
+ .update(cx, |editor, window, cx| {
+ let snapshot = editor.buffer().read(cx).snapshot(cx);
+ let outer =
+ snapshot.anchor_before(Point::new(1, 0))..snapshot.anchor_after(Point::new(3, 7));
+ let inner =
+ snapshot.anchor_before(Point::new(3, 0))..snapshot.anchor_after(Point::new(3, 3));
+
+ editor.insert_creases(
+ [
+ Crease::simple(outer, FoldPlaceholder::test()),
+ Crease::simple(inner, FoldPlaceholder::test()),
+ ],
+ cx,
+ );
+
+ editor.fold_at_level(&FoldAtLevel(1), window, cx);
+ assert_eq!(editor.display_text(cx), "aaa\n⋯\nfff\n");
+
+ editor.unfold_all(&UnfoldAll, window, cx);
+ editor.fold_at_level(&FoldAtLevel(2), window, cx);
+ assert_eq!(editor.display_text(cx), "aaa\nbbb\nccc\nddd eee\nfff\n");
+ })
+ .unwrap();
+}
+
#[gpui::test]
fn test_move_cursor(cx: &mut TestAppContext) {
init_test(cx, |_| {});
diff --git a/crates/editor/src/fold.rs b/crates/editor/src/fold.rs
index 1367505b1d0..c5b6459bc7f 100644
--- a/crates/editor/src/fold.rs
+++ b/crates/editor/src/fold.rs
@@ -769,7 +769,7 @@ impl Editor {
if current_level < fold_at_level {
stack.push((nested_start_row, nested_end_row, current_level + 1));
} else if current_level == fold_at_level {
- // Fold iff there is no selection completely contained within the fold region
+ // Fold if and only if there is no selection completely contained within the fold region
if !row_ranges_to_keep.iter().any(|selection| {
selection.end >= nested_start_row
&& selection.start <= nested_end_row
@@ -778,7 +778,7 @@ impl Editor {
}
}
- start_row = nested_end_row + 1;
+ start_row = (start_row + 1).max(nested_end_row);
}
None => start_row += 1,
}