diff --git a/crates/editor/src/actions.rs b/crates/editor/src/actions.rs index 6afc5a406ef..136539966e8 100644 --- a/crates/editor/src/actions.rs +++ b/crates/editor/src/actions.rs @@ -570,6 +570,9 @@ actions!( /// Folds all code blocks at indentation level 9. #[action(name = "FoldAtLevel_9")] FoldAtLevel9, + /// Folds all code blocks at the same indentation level of the current + /// line. + FoldAtCurrentIndentationLevel, /// Folds all function bodies in the editor. FoldFunctionBodies, /// Folds the current code block and all its children. diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index 67f75e6ead1..fa1f4939a3e 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -2389,6 +2389,372 @@ fn test_fold_at_level_with_crease_on_boundary_row(cx: &mut TestAppContext) { .unwrap(); } +#[gpui::test] +async fn test_fold_at_current_indentation_level(cx: &mut TestAppContext) { + init_test(cx, |_| {}); + let mut cx = EditorTestContext::new(cx).await; + + cx.set_state( + &" + ˇclass Foo: + def a(): + print(1) + + class Bar: + def b(): + print(2) + " + .unindent(), + ); + + cx.update_editor(|editor, window, cx| { + editor.fold_at_current_indentation_level( + &actions::FoldAtCurrentIndentationLevel, + window, + cx, + ); + }); + + cx.assert_display_state( + &" + ˇclass Foo:⋯ + + class Bar:⋯ + " + .unindent(), + ); + + cx.update_editor(|editor, window, cx| { + editor.unfold_all(&actions::UnfoldAll, window, cx); + }); + + cx.set_selections_state( + &" + class Foo: + ˇdef a(): + print(1) + + class Bar: + def b(): + print(2) + " + .unindent(), + ); + + cx.update_editor(|editor, window, cx| { + editor.fold_at_current_indentation_level( + &actions::FoldAtCurrentIndentationLevel, + window, + cx, + ); + }); + + cx.assert_display_state( + &" + class Foo: + ˇdef a():⋯ + + class Bar: + def b():⋯ + " + .unindent(), + ); + + let state = &" + class Foo: + def a(): + if x: + y() + ˇz() + + def b(): + if w: + v() + " + .unindent(); + cx.set_state(&state); + + cx.update_editor(|editor, window, cx| { + editor.fold_at_current_indentation_level( + &actions::FoldAtCurrentIndentationLevel, + window, + cx, + ); + }); + + cx.assert_editor_state(&state); + assert_eq!( + cx.display_text(), + " + class Foo: + def a(): + if x:⋯ + z() + + def b(): + if w:⋯ + " + .unindent(), + ) +} + +#[gpui::test] +async fn test_fold_at_current_indentation_level_no_ops(cx: &mut TestAppContext) { + init_test(cx, |_| {}); + let mut cx = EditorTestContext::new(cx).await; + + // Nothing should be folded if cursor is at an empty line. + let state = " + ˇ + fn work() { + run(); + } + " + .unindent(); + cx.set_state(&state); + cx.update_editor(|editor, window, cx| { + editor.fold_at_current_indentation_level( + &actions::FoldAtCurrentIndentationLevel, + window, + cx, + ); + }); + cx.assert_display_state(&state); + + // Nothing should be folded if cursor is on a blank line with only spaces. + let state = " + \x20\x20\x20\x20ˇ + fn work() { + run(); + } + " + .unindent(); + cx.set_state(&state); + cx.update_editor(|editor, window, cx| { + editor.fold_at_current_indentation_level( + &actions::FoldAtCurrentIndentationLevel, + window, + cx, + ); + }); + cx.assert_display_state(&state); + + // Nothing should be folded if cursor is on a blank line with only tabs. + let state = " + fn work() { + run(); + } + \tˇ" + .unindent(); + + cx.set_state(&state); + cx.update_editor(|editor, window, cx| { + editor.fold_at_current_indentation_level( + &actions::FoldAtCurrentIndentationLevel, + window, + cx, + ); + }); + + cx.assert_editor_state(&state); + assert_eq!( + cx.display_text(), + " + fn work() { + run(); + } + \x20\x20\x20\x20" + .unindent(), + ); +} + +#[gpui::test] +async fn test_fold_at_current_indentation_level_with_multiple_cursors(cx: &mut TestAppContext) { + init_test(cx, |_| {}); + let mut cx = EditorTestContext::new(cx).await; + + // The newest cursor is on the `b()` method, with the older one on the `Foo` + // class definition. Ensure that folding at the current indentation level + // will use the newest selection. + cx.set_state( + &" + ˇclass Foo: + def a(): + print(1) + + class Bar: + ˇdef b(): + print(2) + " + .unindent(), + ); + + cx.update_editor(|editor, window, cx| { + editor.fold_at_current_indentation_level( + &actions::FoldAtCurrentIndentationLevel, + window, + cx, + ); + }); + + assert_eq!( + cx.display_text(), + " + class Foo: + def a():⋯ + + class Bar: + def b():⋯ + " + .unindent(), + ); +} + +#[gpui::test] +async fn test_fold_at_current_indentation_level_on_less_spaces_than_tab_size( + cx: &mut TestAppContext, +) { + init_test(cx, |settings| { + settings.defaults.tab_size = NonZeroU32::new(4); + settings.defaults.hard_tabs = Some(false); + }); + + let mut cx = EditorTestContext::new(cx).await; + + cx.set_state( + &" + class Foo: + ˇdef a(): + print(1) + + class Bar: + def b(): + print(2) + " + .unindent(), + ); + + cx.update_editor(|editor, window, cx| { + editor.fold_at_current_indentation_level( + &actions::FoldAtCurrentIndentationLevel, + window, + cx, + ); + }); + + cx.assert_display_state( + &" + class Foo: + ˇdef a():⋯ + + class Bar: + def b():⋯ + " + .unindent(), + ); +} + +#[gpui::test] +async fn test_fold_at_current_indentation_level_scans_past_single_row_crease( + cx: &mut TestAppContext, +) { + init_test(cx, |_| {}); + + let mut cx = EditorTestContext::new(cx).await; + let marked_text = " + fn first() «{}» + + ˇfn second() «{ + work(); + }» + " + .unindent(); + + cx.set_state(&marked_text.replace(['«', '»'], "")); + let crease_ranges = cx.ranges(&marked_text.replace('ˇ', "")); + + cx.update_editor(|editor, window, cx| { + let snapshot = editor.buffer().read(cx).snapshot(cx); + + editor.insert_creases( + crease_ranges.iter().map(|range| { + let start = snapshot.anchor_before(MultiBufferOffset(range.start)); + let end = snapshot.anchor_after(MultiBufferOffset(range.end)); + + Crease::simple(start..end, FoldPlaceholder::test()) + }), + cx, + ); + editor.fold_at_current_indentation_level( + &actions::FoldAtCurrentIndentationLevel, + window, + cx, + ); + }); + + assert_eq!( + cx.display_text(), + " + fn first() ⋯ + + fn second() ⋯ + " + .unindent(), + ); +} + +#[gpui::test] +async fn test_fold_at_current_indentation_level_with_crease_on_shared_boundary_row( + cx: &mut TestAppContext, +) { + init_test(cx, |_| {}); + + let mut cx = EditorTestContext::new(cx).await; + let marked_text = " + fn first() «{ + work(); + ˇ}» fn second() «{}» + + fn third() «{ + work(); + }» + " + .unindent(); + + cx.set_state(&marked_text.replace(['«', '»'], "")); + let crease_ranges = cx.ranges(&marked_text.replace('ˇ', "")); + + cx.update_editor(|editor, window, cx| { + let snapshot = editor.buffer().read(cx).snapshot(cx); + + editor.insert_creases( + crease_ranges.iter().map(|range| { + let start = snapshot.anchor_before(MultiBufferOffset(range.start)); + let end = snapshot.anchor_after(MultiBufferOffset(range.end)); + + Crease::simple(start..end, FoldPlaceholder::test()) + }), + cx, + ); + editor.fold_at_current_indentation_level( + &actions::FoldAtCurrentIndentationLevel, + window, + cx, + ); + }); + + cx.assert_display_state( + &" + fn first() { + work(); + ˇ} fn second() ⋯ + + fn third() ⋯ + " + .unindent(), + ); +} + #[gpui::test] fn test_move_cursor(cx: &mut TestAppContext) { init_test(cx, |_| {}); diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index ae5aab9681d..afc15a19b05 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -583,6 +583,7 @@ impl EditorElement { register_action(editor, window, Editor::fold_at_level_7); register_action(editor, window, Editor::fold_at_level_8); register_action(editor, window, Editor::fold_at_level_9); + register_action(editor, window, Editor::fold_at_current_indentation_level); register_action(editor, window, Editor::fold_all); register_action(editor, window, Editor::fold_function_bodies); register_action(editor, window, Editor::fold_recursive); diff --git a/crates/editor/src/fold.rs b/crates/editor/src/fold.rs index 35b6be912de..34f6184a527 100644 --- a/crates/editor/src/fold.rs +++ b/crates/editor/src/fold.rs @@ -327,6 +327,69 @@ impl Editor { self.fold_at_level(&actions::FoldAtLevel(9), window, cx); } + /// Folds the document at the level derived from the newest selection's line + /// indentation. + /// + /// Empty and white-space only lines are ignored. + pub fn fold_at_current_indentation_level( + &mut self, + _: &actions::FoldAtCurrentIndentationLevel, + window: &mut Window, + cx: &mut Context, + ) { + if !self.buffer().read(cx).is_singleton() { + return; + } + + let display_snapshot = self.display_snapshot(cx); + let buffer_snapshot = self.buffer.read(cx).snapshot(cx); + let row = MultiBufferRow( + self.selections + .newest::(&display_snapshot) + .head() + .row, + ); + + if buffer_snapshot.is_line_blank(row) { + return; + } + + let mut current_level = 1; + let mut current_row = 0; + while current_row < row.0 { + match self + .snapshot(window, cx) + .crease_for_buffer_row(MultiBufferRow(current_row)) + { + Some(crease) => { + let crease_range = crease.range(); + + if row.0 > crease_range.end.row { + current_row = (current_row + 1).max(crease_range.end.row); + } else if row.0 < crease_range.end.row { + current_row += 1; + current_level += 1; + } else { + let row_indent = buffer_snapshot.line_indent_for_row(row).raw_len(); + let crease_indent = buffer_snapshot + .line_indent_for_row(MultiBufferRow(crease_range.start.row)) + .raw_len(); + + if row_indent > crease_indent { + current_row += 1; + current_level += 1; + } else { + current_row = (current_row + 1).max(crease_range.end.row); + } + } + } + None => current_row += 1, + } + } + + self.fold_at_level(&actions::FoldAtLevel(current_level), window, cx); + } + pub fn fold_all(&mut self, _: &actions::FoldAll, window: &mut Window, cx: &mut Context) { if self.buffer.read(cx).is_singleton() { let mut fold_ranges = Vec::new();