Skip to content

Commit 5dd6b93

Browse files
Merge #8231
8231: Fold consecutive consts and statics r=matklad a=MozarellaMan PR to implement #8114 ![const_fold](https://user-images.githubusercontent.com/48062697/112835083-b584c600-9090-11eb-968a-a95f4e9c1f6c.gif) Co-authored-by: Ayomide Bamidele <[email protected]>
2 parents c9810b9 + 8e11796 commit 5dd6b93

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

crates/ide/src/folding_ranges.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ pub enum FoldKind {
1717
Block,
1818
ArgList,
1919
Region,
20+
Consts,
21+
Statics,
2022
}
2123

2224
#[derive(Debug)]
@@ -30,6 +32,8 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
3032
let mut visited_comments = FxHashSet::default();
3133
let mut visited_imports = FxHashSet::default();
3234
let mut visited_mods = FxHashSet::default();
35+
let mut visited_consts = FxHashSet::default();
36+
let mut visited_statics = FxHashSet::default();
3337
// regions can be nested, here is a LIFO buffer
3438
let mut regions_starts: Vec<TextSize> = vec![];
3539

@@ -91,6 +95,19 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
9195
res.push(Fold { range, kind: FoldKind::Mods })
9296
}
9397
}
98+
99+
// Fold groups of consts
100+
if node.kind() == CONST && !visited_consts.contains(&node) {
101+
if let Some(range) = contiguous_range_for_group(&node, &mut visited_consts) {
102+
res.push(Fold { range, kind: FoldKind::Consts })
103+
}
104+
}
105+
// Fold groups of consts
106+
if node.kind() == STATIC && !visited_statics.contains(&node) {
107+
if let Some(range) = contiguous_range_for_group(&node, &mut visited_statics) {
108+
res.push(Fold { range, kind: FoldKind::Statics })
109+
}
110+
}
94111
}
95112
}
96113
}
@@ -250,6 +267,8 @@ mod tests {
250267
FoldKind::Block => "block",
251268
FoldKind::ArgList => "arglist",
252269
FoldKind::Region => "region",
270+
FoldKind::Consts => "consts",
271+
FoldKind::Statics => "statics",
253272
};
254273
assert_eq!(kind, &attr.unwrap());
255274
}
@@ -457,4 +476,24 @@ calling_function(x,y);
457476
"#,
458477
)
459478
}
479+
480+
#[test]
481+
fn fold_consecutive_const() {
482+
check(
483+
r#"
484+
<fold consts>const FIRST_CONST: &str = "first";
485+
const SECOND_CONST: &str = "second";</fold>
486+
"#,
487+
)
488+
}
489+
490+
#[test]
491+
fn fold_consecutive_static() {
492+
check(
493+
r#"
494+
<fold statics>static FIRST_STATIC: &str = "first";
495+
static SECOND_STATIC: &str = "second";</fold>
496+
"#,
497+
)
498+
}
460499
}

crates/rust-analyzer/src/to_proto.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,11 @@ pub(crate) fn folding_range(
492492
FoldKind::Comment => Some(lsp_types::FoldingRangeKind::Comment),
493493
FoldKind::Imports => Some(lsp_types::FoldingRangeKind::Imports),
494494
FoldKind::Region => Some(lsp_types::FoldingRangeKind::Region),
495-
FoldKind::Mods | FoldKind::Block | FoldKind::ArgList => None,
495+
FoldKind::Mods
496+
| FoldKind::Block
497+
| FoldKind::ArgList
498+
| FoldKind::Consts
499+
| FoldKind::Statics => None,
496500
};
497501

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

0 commit comments

Comments
 (0)