Skip to content

Commit d4a26b4

Browse files
committed
feat: support folding multiline arg list & fn body in one folding range
1 parent df50136 commit d4a26b4

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

crates/ide/src/folding_ranges.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
4545
let mut visited_mods = FxHashSet::default();
4646
let mut visited_consts = FxHashSet::default();
4747
let mut visited_statics = FxHashSet::default();
48+
let mut merged_fn_bodies = FxHashSet::default();
4849

4950
// regions can be nested, here is a LIFO buffer
5051
let mut region_starts: Vec<TextSize> = vec![];
@@ -57,6 +58,31 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
5758
NodeOrToken::Token(token) => token.text().contains('\n'),
5859
};
5960
if is_multiline {
61+
// for the arg list, we need to special handle
62+
if matches!(element.kind(), ARG_LIST | PARAM_LIST) {
63+
if let NodeOrToken::Node(node) = &element {
64+
if let Some(fn_node) = node.parent().and_then(ast::Fn::cast) {
65+
if let Some(body) = fn_node.body() {
66+
// just add a big fold combine the params and body
67+
res.push(Fold {
68+
range: TextRange::new(
69+
node.text_range().start(),
70+
body.syntax().text_range().end(),
71+
),
72+
kind: FoldKind::ArgList,
73+
});
74+
merged_fn_bodies.insert(body.syntax().text_range());
75+
continue;
76+
}
77+
}
78+
}
79+
}
80+
// skip the merged function body
81+
if matches!(element.kind(), BLOCK_EXPR)
82+
&& merged_fn_bodies.contains(&element.text_range())
83+
{
84+
continue;
85+
}
6086
res.push(Fold { range: element.text_range(), kind });
6187
continue;
6288
}
@@ -287,6 +313,7 @@ mod tests {
287313

288314
use super::*;
289315

316+
#[track_caller]
290317
fn check(#[rust_analyzer::rust_fixture] ra_fixture: &str) {
291318
let (ranges, text) = extract_tags(ra_fixture, "fold");
292319

@@ -537,7 +564,7 @@ const _: S = S <fold block>{
537564
fn foo<fold arglist>(
538565
x: i32,
539566
y: String,
540-
)</fold> {}
567+
) {}</fold>
541568
"#,
542569
)
543570
}

0 commit comments

Comments
 (0)