Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7a87f81

Browse files
committed
Auto merge of rust-lang#12576 - harpsword:fold_range_non_block_match_arm, r=Veykril
feat: add fold range for multi line match arm list fix: rust-lang#11893
2 parents b1f9efa + 3a78cc5 commit 7a87f81

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

crates/ide/src/folding_ranges.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub enum FoldKind {
2424
Array,
2525
WhereClause,
2626
ReturnType,
27+
MatchArm,
2728
}
2829

2930
#[derive(Debug)]
@@ -117,6 +118,11 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
117118
res.push(Fold { range, kind: FoldKind::WhereClause })
118119
}
119120
},
121+
ast::MatchArm(match_arm) => {
122+
if let Some(range) = fold_range_for_multiline_match_arm(match_arm) {
123+
res.push(Fold {range, kind: FoldKind::MatchArm})
124+
}
125+
},
120126
_ => (),
121127
}
122128
}
@@ -264,6 +270,16 @@ fn fold_range_for_where_clause(where_clause: ast::WhereClause) -> Option<TextRan
264270
None
265271
}
266272

273+
fn fold_range_for_multiline_match_arm(match_arm: ast::MatchArm) -> Option<TextRange> {
274+
if let Some(_) = fold_kind(match_arm.expr()?.syntax().kind()) {
275+
return None;
276+
}
277+
if match_arm.expr()?.syntax().text().contains_char('\n') {
278+
return Some(match_arm.expr()?.syntax().text_range());
279+
}
280+
None
281+
}
282+
267283
#[cfg(test)]
268284
mod tests {
269285
use test_utils::extract_tags;
@@ -299,6 +315,7 @@ mod tests {
299315
FoldKind::Array => "array",
300316
FoldKind::WhereClause => "whereclause",
301317
FoldKind::ReturnType => "returntype",
318+
FoldKind::MatchArm => "matcharm",
302319
};
303320
assert_eq!(kind, &attr.unwrap());
304321
}
@@ -456,6 +473,36 @@ fn main() <fold block>{
456473
);
457474
}
458475

476+
#[test]
477+
fn test_fold_multiline_non_block_match_arm() {
478+
check(
479+
r#"
480+
fn main() <fold block>{
481+
match foo <fold block>{
482+
block => <fold block>{
483+
}</fold>,
484+
matcharm => <fold matcharm>some.
485+
call().
486+
chain()</fold>,
487+
matcharm2
488+
=> 0,
489+
match_expr => <fold matcharm>match foo2 <fold block>{
490+
bar => (),
491+
}</fold></fold>,
492+
array_list => <fold array>[
493+
1,
494+
2,
495+
3,
496+
]</fold>,
497+
strustS => <fold matcharm>StructS <fold block>{
498+
a: 31,
499+
}</fold></fold>,
500+
}</fold>
501+
}</fold>
502+
"#,
503+
)
504+
}
505+
459506
#[test]
460507
fn fold_big_calls() {
461508
check(

crates/rust-analyzer/src/to_proto.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,8 @@ pub(crate) fn folding_range(
670670
| FoldKind::Statics
671671
| FoldKind::WhereClause
672672
| FoldKind::ReturnType
673-
| FoldKind::Array => None,
673+
| FoldKind::Array
674+
| FoldKind::MatchArm => None,
674675
};
675676

676677
let range = range(line_index, fold.range);

0 commit comments

Comments
 (0)