Skip to content

Commit c5d15fd

Browse files
Add FoldFunctionBodies editor action (#21504)
Related to #19424 This uses the new text object support, so will only work for languages that have `textobjects.scm`. It does not integrate with indentation-based folding for now, and the syntax-based folds don't have matching fold markers in the gutter (unless they are folded). Release Notes: - Add an editor action to fold all function bodies Co-authored-by: Conrad <[email protected]>
1 parent ce5f492 commit c5d15fd

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

crates/editor/src/actions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ gpui::actions!(
248248
FindAllReferences,
249249
Fold,
250250
FoldAll,
251+
FoldFunctionBodies,
251252
FoldRecursive,
252253
FoldSelectedRanges,
253254
ToggleFold,

crates/editor/src/editor.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11097,6 +11097,23 @@ impl Editor {
1109711097
self.fold_creases(fold_ranges, true, cx);
1109811098
}
1109911099

11100+
pub fn fold_function_bodies(
11101+
&mut self,
11102+
_: &actions::FoldFunctionBodies,
11103+
cx: &mut ViewContext<Self>,
11104+
) {
11105+
let snapshot = self.buffer.read(cx).snapshot(cx);
11106+
let Some((_, _, buffer)) = snapshot.as_singleton() else {
11107+
return;
11108+
};
11109+
let creases = buffer
11110+
.function_body_fold_ranges(0..buffer.len())
11111+
.map(|range| Crease::simple(range, self.display_map.read(cx).fold_placeholder.clone()))
11112+
.collect();
11113+
11114+
self.fold_creases(creases, true, cx);
11115+
}
11116+
1110011117
pub fn fold_recursive(&mut self, _: &actions::FoldRecursive, cx: &mut ViewContext<Self>) {
1110111118
let mut to_fold = Vec::new();
1110211119
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));

crates/editor/src/element.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ impl EditorElement {
342342
register_action(view, cx, Editor::fold);
343343
register_action(view, cx, Editor::fold_at_level);
344344
register_action(view, cx, Editor::fold_all);
345+
register_action(view, cx, Editor::fold_function_bodies);
345346
register_action(view, cx, Editor::fold_at);
346347
register_action(view, cx, Editor::fold_recursive);
347348
register_action(view, cx, Editor::toggle_fold);

crates/language/src/buffer.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3355,6 +3355,14 @@ impl BufferSnapshot {
33553355
})
33563356
}
33573357

3358+
pub fn function_body_fold_ranges<T: ToOffset>(
3359+
&self,
3360+
within: Range<T>,
3361+
) -> impl Iterator<Item = Range<usize>> + '_ {
3362+
self.text_object_ranges(within, TreeSitterOptions::default())
3363+
.filter_map(|(range, obj)| (obj == TextObject::InsideFunction).then_some(range))
3364+
}
3365+
33583366
/// For each grammar in the language, runs the provided
33593367
/// [`tree_sitter::Query`] against the given range.
33603368
pub fn matches(

0 commit comments

Comments
 (0)