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 <ayoeze@hotmail.com>
This commit is contained in:
bors[bot] 2021-03-29 13:40:55 +00:00 committed by GitHub
commit 5dd6b93138
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 1 deletions

View File

@ -17,6 +17,8 @@ pub enum FoldKind {
Block,
ArgList,
Region,
Consts,
Statics,
}
#[derive(Debug)]
@ -30,6 +32,8 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
let mut visited_comments = FxHashSet::default();
let mut visited_imports = FxHashSet::default();
let mut visited_mods = FxHashSet::default();
let mut visited_consts = FxHashSet::default();
let mut visited_statics = FxHashSet::default();
// regions can be nested, here is a LIFO buffer
let mut regions_starts: Vec<TextSize> = vec![];
@ -91,6 +95,19 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
res.push(Fold { range, kind: FoldKind::Mods })
}
}
// Fold groups of consts
if node.kind() == CONST && !visited_consts.contains(&node) {
if let Some(range) = contiguous_range_for_group(&node, &mut visited_consts) {
res.push(Fold { range, kind: FoldKind::Consts })
}
}
// Fold groups of consts
if node.kind() == STATIC && !visited_statics.contains(&node) {
if let Some(range) = contiguous_range_for_group(&node, &mut visited_statics) {
res.push(Fold { range, kind: FoldKind::Statics })
}
}
}
}
}
@ -250,6 +267,8 @@ mod tests {
FoldKind::Block => "block",
FoldKind::ArgList => "arglist",
FoldKind::Region => "region",
FoldKind::Consts => "consts",
FoldKind::Statics => "statics",
};
assert_eq!(kind, &attr.unwrap());
}
@ -457,4 +476,24 @@ calling_function(x,y);
"#,
)
}
#[test]
fn fold_consecutive_const() {
check(
r#"
<fold consts>const FIRST_CONST: &str = "first";
const SECOND_CONST: &str = "second";</fold>
"#,
)
}
#[test]
fn fold_consecutive_static() {
check(
r#"
<fold statics>static FIRST_STATIC: &str = "first";
static SECOND_STATIC: &str = "second";</fold>
"#,
)
}
}

View File

@ -492,7 +492,11 @@ pub(crate) fn folding_range(
FoldKind::Comment => Some(lsp_types::FoldingRangeKind::Comment),
FoldKind::Imports => Some(lsp_types::FoldingRangeKind::Imports),
FoldKind::Region => Some(lsp_types::FoldingRangeKind::Region),
FoldKind::Mods | FoldKind::Block | FoldKind::ArgList => None,
FoldKind::Mods
| FoldKind::Block
| FoldKind::ArgList
| FoldKind::Consts
| FoldKind::Statics => None,
};
let range = range(line_index, fold.range);