add folding for where clauses

This commit is contained in:
m5tfi 2021-04-30 10:18:36 +02:00
parent 80bee14e14
commit 5778ab1e41
2 changed files with 54 additions and 0 deletions

View File

@ -20,6 +20,7 @@ pub enum FoldKind {
Consts,
Statics,
Array,
WhereClause,
}
#[derive(Debug)]
@ -35,6 +36,7 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
let mut visited_mods = FxHashSet::default();
let mut visited_consts = FxHashSet::default();
let mut visited_statics = FxHashSet::default();
let mut visited_where_clauses = FxHashSet::default();
// regions can be nested, here is a LIFO buffer
let mut regions_starts: Vec<TextSize> = vec![];
@ -109,6 +111,15 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
res.push(Fold { range, kind: FoldKind::Statics })
}
}
// Fold where clause
if node.kind() == WHERE_CLAUSE && !visited_where_clauses.contains(&node) {
if let Some(range) =
contiguous_range_for_where(&node, &mut visited_where_clauses)
{
res.push(Fold { range, kind: FoldKind::WhereClause })
}
}
}
}
}
@ -241,6 +252,28 @@ fn contiguous_range_for_comment(
}
}
fn contiguous_range_for_where(
node: &SyntaxNode,
visited: &mut FxHashSet<SyntaxNode>,
) -> Option<TextRange> {
let first_where_pred = node.first_child();
let last_where_pred = node.last_child();
if first_where_pred != last_where_pred {
let mut it = node.descendants_with_tokens();
if let (Some(_where_clause), Some(where_kw), Some(last_comma)) =
(it.next(), it.next(), it.last())
{
let start = where_kw.text_range().end();
let end = last_comma.text_range().end();
visited.insert(node.clone());
return Some(TextRange::new(start, end));
}
}
None
}
#[cfg(test)]
mod tests {
use test_utils::extract_tags;
@ -272,6 +305,7 @@ fn check(ra_fixture: &str) {
FoldKind::Consts => "consts",
FoldKind::Statics => "statics",
FoldKind::Array => "array",
FoldKind::WhereClause => "whereclause",
};
assert_eq!(kind, &attr.unwrap());
}
@ -513,4 +547,23 @@ fn fold_consecutive_static() {
"#,
)
}
#[test]
fn fold_where_clause() {
// fold multi-line and don't fold single line.
check(
r#"
fn foo()
where<fold whereclause>
A: Foo,
B: Foo,
C: Foo,
D: Foo,</fold> {}
fn bar()
where
A: Bar, {}
"#,
)
}
}

View File

@ -524,6 +524,7 @@ pub(crate) fn folding_range(
| FoldKind::ArgList
| FoldKind::Consts
| FoldKind::Statics
| FoldKind::WhereClause
| FoldKind::Array => None,
};