diff --git a/crates/editor/src/display_map.rs b/crates/editor/src/display_map.rs index cee3b908aa..42f3c34b15 100644 --- a/crates/editor/src/display_map.rs +++ b/crates/editor/src/display_map.rs @@ -983,8 +983,23 @@ impl DisplaySnapshot { break; } } - let end = end.unwrap_or(max_point); - Some((start..end, self.fold_placeholder.clone())) + + let mut row_before_line_breaks = end.unwrap_or(max_point); + while row_before_line_breaks.row > start.row + && self + .buffer_snapshot + .is_line_blank(MultiBufferRow(row_before_line_breaks.row)) + { + row_before_line_breaks.row -= 1; + } + + row_before_line_breaks = Point::new( + row_before_line_breaks.row, + self.buffer_snapshot + .line_len(MultiBufferRow(row_before_line_breaks.row)), + ); + + Some((start..row_before_line_breaks, self.fold_placeholder.clone())) } else { None } diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index 105b07c8ec..ac32aaadd1 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -858,6 +858,175 @@ fn test_fold_action(cx: &mut TestAppContext) { }); } +#[gpui::test] +fn test_fold_action_whitespace_sensitive_language(cx: &mut TestAppContext) { + init_test(cx, |_| {}); + + let view = cx.add_window(|cx| { + let buffer = MultiBuffer::build_simple( + &" + class Foo: + # Hello! + + def a(): + print(1) + + def b(): + print(2) + + def c(): + print(3) + " + .unindent(), + cx, + ); + build_editor(buffer.clone(), cx) + }); + + _ = view.update(cx, |view, cx| { + view.change_selections(None, cx, |s| { + s.select_display_ranges([ + DisplayPoint::new(DisplayRow(7), 0)..DisplayPoint::new(DisplayRow(10), 0) + ]); + }); + view.fold(&Fold, cx); + assert_eq!( + view.display_text(cx), + " + class Foo: + # Hello! + + def a(): + print(1) + + def b():⋯ + + def c():⋯ + " + .unindent(), + ); + + view.fold(&Fold, cx); + assert_eq!( + view.display_text(cx), + " + class Foo:⋯ + " + .unindent(), + ); + + view.unfold_lines(&UnfoldLines, cx); + assert_eq!( + view.display_text(cx), + " + class Foo: + # Hello! + + def a(): + print(1) + + def b():⋯ + + def c():⋯ + " + .unindent(), + ); + + view.unfold_lines(&UnfoldLines, cx); + assert_eq!(view.display_text(cx), view.buffer.read(cx).read(cx).text()); + }); +} + +#[gpui::test] +fn test_fold_action_multiple_line_breaks(cx: &mut TestAppContext) { + init_test(cx, |_| {}); + + let view = cx.add_window(|cx| { + let buffer = MultiBuffer::build_simple( + &" + class Foo: + # Hello! + + def a(): + print(1) + + def b(): + print(2) + + + def c(): + print(3) + + + " + .unindent(), + cx, + ); + build_editor(buffer.clone(), cx) + }); + + _ = view.update(cx, |view, cx| { + view.change_selections(None, cx, |s| { + s.select_display_ranges([ + DisplayPoint::new(DisplayRow(7), 0)..DisplayPoint::new(DisplayRow(11), 0) + ]); + }); + view.fold(&Fold, cx); + assert_eq!( + view.display_text(cx), + " + class Foo: + # Hello! + + def a(): + print(1) + + def b():⋯ + + + def c():⋯ + + + " + .unindent(), + ); + + view.fold(&Fold, cx); + assert_eq!( + view.display_text(cx), + " + class Foo:⋯ + + + " + .unindent(), + ); + + view.unfold_lines(&UnfoldLines, cx); + assert_eq!( + view.display_text(cx), + " + class Foo: + # Hello! + + def a(): + print(1) + + def b():⋯ + + + def c():⋯ + + + " + .unindent(), + ); + + view.unfold_lines(&UnfoldLines, cx); + assert_eq!(view.display_text(cx), view.buffer.read(cx).read(cx).text()); + }); +} + #[gpui::test] fn test_move_cursor(cx: &mut TestAppContext) { init_test(cx, |_| {});