diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-05-05 22:14:12 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-05-05 22:14:12 +0100 |
commit | c3596371d841ebb949dc8b99acc3bc902e4d808a (patch) | |
tree | e8fe32621f287079ca2e35781e6373a2acb9e5c0 /crates/ide | |
parent | b10d5e342c4ff31b50d738c37c726274c692956b (diff) | |
parent | c2cf34db90e48de6e8a10da7959a4c49f95e1ff6 (diff) |
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 <[email protected]>
Diffstat (limited to 'crates/ide')
-rwxr-xr-x[-rw-r--r--] | crates/ide/src/folding_ranges.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/crates/ide/src/folding_ranges.rs b/crates/ide/src/folding_ranges.rs index 2b9ed123c..a03988778 100644..100755 --- a/crates/ide/src/folding_ranges.rs +++ b/crates/ide/src/folding_ranges.rs | |||
@@ -20,6 +20,7 @@ pub enum FoldKind { | |||
20 | Consts, | 20 | Consts, |
21 | Statics, | 21 | Statics, |
22 | Array, | 22 | Array, |
23 | WhereClause, | ||
23 | } | 24 | } |
24 | 25 | ||
25 | #[derive(Debug)] | 26 | #[derive(Debug)] |
@@ -109,6 +110,13 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> { | |||
109 | res.push(Fold { range, kind: FoldKind::Statics }) | 110 | res.push(Fold { range, kind: FoldKind::Statics }) |
110 | } | 111 | } |
111 | } | 112 | } |
113 | |||
114 | // Fold where clause | ||
115 | if node.kind() == WHERE_CLAUSE { | ||
116 | if let Some(range) = fold_range_for_where_clause(&node) { | ||
117 | res.push(Fold { range, kind: FoldKind::WhereClause }) | ||
118 | } | ||
119 | } | ||
112 | } | 120 | } |
113 | } | 121 | } |
114 | } | 122 | } |
@@ -241,6 +249,23 @@ fn contiguous_range_for_comment( | |||
241 | } | 249 | } |
242 | } | 250 | } |
243 | 251 | ||
252 | fn fold_range_for_where_clause(node: &SyntaxNode) -> Option<TextRange> { | ||
253 | let first_where_pred = node.first_child(); | ||
254 | let last_where_pred = node.last_child(); | ||
255 | |||
256 | if first_where_pred != last_where_pred { | ||
257 | let mut it = node.descendants_with_tokens(); | ||
258 | if let (Some(_where_clause), Some(where_kw), Some(last_comma)) = | ||
259 | (it.next(), it.next(), it.last()) | ||
260 | { | ||
261 | let start = where_kw.text_range().end(); | ||
262 | let end = last_comma.text_range().end(); | ||
263 | return Some(TextRange::new(start, end)); | ||
264 | } | ||
265 | } | ||
266 | None | ||
267 | } | ||
268 | |||
244 | #[cfg(test)] | 269 | #[cfg(test)] |
245 | mod tests { | 270 | mod tests { |
246 | use test_utils::extract_tags; | 271 | use test_utils::extract_tags; |
@@ -272,6 +297,7 @@ mod tests { | |||
272 | FoldKind::Consts => "consts", | 297 | FoldKind::Consts => "consts", |
273 | FoldKind::Statics => "statics", | 298 | FoldKind::Statics => "statics", |
274 | FoldKind::Array => "array", | 299 | FoldKind::Array => "array", |
300 | FoldKind::WhereClause => "whereclause", | ||
275 | }; | 301 | }; |
276 | assert_eq!(kind, &attr.unwrap()); | 302 | assert_eq!(kind, &attr.unwrap()); |
277 | } | 303 | } |
@@ -513,4 +539,23 @@ static SECOND_STATIC: &str = "second";</fold> | |||
513 | "#, | 539 | "#, |
514 | ) | 540 | ) |
515 | } | 541 | } |
542 | |||
543 | #[test] | ||
544 | fn fold_where_clause() { | ||
545 | // fold multi-line and don't fold single line. | ||
546 | check( | ||
547 | r#" | ||
548 | fn foo() | ||
549 | where<fold whereclause> | ||
550 | A: Foo, | ||
551 | B: Foo, | ||
552 | C: Foo, | ||
553 | D: Foo,</fold> {} | ||
554 | |||
555 | fn bar() | ||
556 | where | ||
557 | A: Bar, {} | ||
558 | "#, | ||
559 | ) | ||
560 | } | ||
516 | } | 561 | } |