Merge #8674
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:
commit
c3596371d8
45
crates/ide/src/folding_ranges.rs
Normal file → Executable file
45
crates/ide/src/folding_ranges.rs
Normal file → Executable 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, {}
|
||||
"#,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -524,6 +524,7 @@ pub(crate) fn folding_range(
|
||||
| FoldKind::ArgList
|
||||
| FoldKind::Consts
|
||||
| FoldKind::Statics
|
||||
| FoldKind::WhereClause
|
||||
| FoldKind::Array => None,
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user