From 5778ab1e41b48371cf3100a397a138deda3f73f5 Mon Sep 17 00:00:00 2001 From: m5tfi <72708423+m5tfi@users.noreply.github.com> Date: Fri, 30 Apr 2021 10:18:36 +0200 Subject: add folding for where clauses --- crates/ide/src/folding_ranges.rs | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'crates/ide/src/folding_ranges.rs') diff --git a/crates/ide/src/folding_ranges.rs b/crates/ide/src/folding_ranges.rs index 2b9ed123c..e03ddaa14 100644 --- a/crates/ide/src/folding_ranges.rs +++ b/crates/ide/src/folding_ranges.rs @@ -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 { 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 = vec![]; @@ -109,6 +111,15 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec { 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, +) -> Option { + 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 @@ mod tests { FoldKind::Consts => "consts", FoldKind::Statics => "statics", FoldKind::Array => "array", + FoldKind::WhereClause => "whereclause", }; assert_eq!(kind, &attr.unwrap()); } @@ -513,4 +547,23 @@ static SECOND_STATIC: &str = "second"; "#, ) } + + #[test] + fn fold_where_clause() { + // fold multi-line and don't fold single line. + check( + r#" +fn foo() +where + A: Foo, + B: Foo, + C: Foo, + D: Foo, {} + +fn bar() +where + A: Bar, {} +"#, + ) + } } -- cgit v1.2.3