editor: Add action to fold document at the current line's indentation (#66849)

## Summary

Even though the `editor: fold at level X` actions are useful, when I do
want to use those I end up having to figure out the level myself, for
example, when wanting to fold all variants of an `enum` or all
methods/functions in an `impl` block.

As such, this Pull Request introduces a `editor: fold at current
indentation level` action which will take the current line's indentation
level and fold the whole document at that level. This makes it super
easy to, for example, place the cursor in a variant of an `enum`, call
the action and have all the variants visible on screen, regardless of
whether the variants have extra arguments. Refer to the recording below
for an example.

## Testing

Tested both manually, as shown in the recording below, as well as
introduced the following tests:

* `editor::editor_tests::test_fold_at_current_level`
*
`editor::editor_tests::test_fold_at_current_level_is_column_independent_with_injection`
* `editor::editor_tests::test_fold_at_current_level_with_tabs`
*
`editor::editor_tests::test_fold_at_current_level_with_tabs_and_soft_tab_settings`
*
`editor::editor_tests::test_fold_at_current_level_with_spaces_and_hard_tab_settings`
*
`editor::editor_tests::test_fold_at_current_level_with_mixed_indentation`
* `editor::editor_tests::test_fold_at_current_level_ignores_empty_lines`
* `editor::editor_tests::test_fold_at_current_level_on_space_only_lines`
* `editor::editor_tests::test_fold_at_current_level_on_tab_only_lines`
*
`editor::editor_tests::test_fold_at_current_level_with_multiple_cursors`

### Recording


https://github.com/user-attachments/assets/20334b35-c14e-49b9-a2a5-63958709f40e

## Self-review

- [x] I've reviewed my diff for quality, security, reliability, and
performance.
- [x] UI changes follow the
[checklist](https://zed.dev/docs/development/ui-checklist).
- [x] Tests cover the new or changed behavior.

Release Notes:

- Added `editor: fold at current indentation level` to allow folding the
whole document at the current line's indentation level
This commit is contained in:
Dino
2026-11-17 20:57:13 +00:00
committed by GitHub
parent 4122f44f6f
commit df5cf7b718
4 changed files with 433 additions and 0 deletions
+3
View File
@@ -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.
+366
View File
@@ -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, |_| {});
+1
View File
@@ -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);
+63
View File
@@ -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<Self>,
) {
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::<Point>(&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<Self>) {
if self.buffer.read(cx).is_singleton() {
let mut fold_ranges = Vec::new();