8674: fix for #8664: Emit folding ranges for multi-line where clauses r=matklad a=m5tfi

#8664 

I added a test that assert folding multi-line where clauses while leaving single lined one. Please, let me know if the code needs further improvements.

Co-authored-by: m5tfi <72708423+m5tfi@users.noreply.github.com>
This commit is contained in:
bors[bot] 2021-05-05 21:14:12 +00:00 committed by GitHub
commit c3596371d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 0 deletions

45
crates/ide/src/folding_ranges.rs Normal file → Executable file
View File

@ -20,6 +20,7 @@ pub enum FoldKind {
Consts,
Statics,
Array,
WhereClause,
}
#[derive(Debug)]
@ -109,6 +110,13 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
res.push(Fold { range, kind: FoldKind::Statics })
}
}
// Fold where clause
if node.kind() == WHERE_CLAUSE {
if let Some(range) = fold_range_for_where_clause(&node) {
res.push(Fold { range, kind: FoldKind::WhereClause })
}
}
}
}
}
@ -241,6 +249,23 @@ fn contiguous_range_for_comment(
}
}
fn fold_range_for_where_clause(node: &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();
return Some(TextRange::new(start, end));
}
}
None
}
#[cfg(test)]
mod tests {
use test_utils::extract_tags;
@ -272,6 +297,7 @@ mod tests {
FoldKind::Consts => "consts",
FoldKind::Statics => "statics",
FoldKind::Array => "array",
FoldKind::WhereClause => "whereclause",
};
assert_eq!(kind, &attr.unwrap());
}
@ -513,4 +539,23 @@ static SECOND_STATIC: &str = "second";</fold>
"#,
)
}
#[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,
};