aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api_light/src/folding_ranges.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-02-08 11:49:43 +0000
committerAleksey Kladov <[email protected]>2019-02-08 11:49:43 +0000
commit12e3b4c70b5ef23b2fdfc197296d483680e125f9 (patch)
tree71baa0e0a62f9f6b61450501c5f821f67badf9e4 /crates/ra_ide_api_light/src/folding_ranges.rs
parent5cb1d41a30d25cbe136402644bf5434dd667f1e5 (diff)
reformat the world
Diffstat (limited to 'crates/ra_ide_api_light/src/folding_ranges.rs')
-rw-r--r--crates/ra_ide_api_light/src/folding_ranges.rs47
1 files changed, 10 insertions, 37 deletions
diff --git a/crates/ra_ide_api_light/src/folding_ranges.rs b/crates/ra_ide_api_light/src/folding_ranges.rs
index c73637323..357a7dee1 100644
--- a/crates/ra_ide_api_light/src/folding_ranges.rs
+++ b/crates/ra_ide_api_light/src/folding_ranges.rs
@@ -30,30 +30,21 @@ pub fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
30 // Fold items that span multiple lines 30 // Fold items that span multiple lines
31 if let Some(kind) = fold_kind(node.kind()) { 31 if let Some(kind) = fold_kind(node.kind()) {
32 if node.text().contains('\n') { 32 if node.text().contains('\n') {
33 res.push(Fold { 33 res.push(Fold { range: node.range(), kind });
34 range: node.range(),
35 kind,
36 });
37 } 34 }
38 } 35 }
39 36
40 // Fold groups of comments 37 // Fold groups of comments
41 if node.kind() == COMMENT && !visited_comments.contains(&node) { 38 if node.kind() == COMMENT && !visited_comments.contains(&node) {
42 if let Some(range) = contiguous_range_for_comment(node, &mut visited_comments) { 39 if let Some(range) = contiguous_range_for_comment(node, &mut visited_comments) {
43 res.push(Fold { 40 res.push(Fold { range, kind: FoldKind::Comment })
44 range,
45 kind: FoldKind::Comment,
46 })
47 } 41 }
48 } 42 }
49 43
50 // Fold groups of imports 44 // Fold groups of imports
51 if node.kind() == USE_ITEM && !visited_imports.contains(&node) { 45 if node.kind() == USE_ITEM && !visited_imports.contains(&node) {
52 if let Some(range) = contiguous_range_for_group(node, &mut visited_imports) { 46 if let Some(range) = contiguous_range_for_group(node, &mut visited_imports) {
53 res.push(Fold { 47 res.push(Fold { range, kind: FoldKind::Imports })
54 range,
55 kind: FoldKind::Imports,
56 })
57 } 48 }
58 } 49 }
59 50
@@ -62,10 +53,7 @@ pub fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
62 if let Some(range) = 53 if let Some(range) =
63 contiguous_range_for_group_unless(node, has_visibility, &mut visited_mods) 54 contiguous_range_for_group_unless(node, has_visibility, &mut visited_mods)
64 { 55 {
65 res.push(Fold { 56 res.push(Fold { range, kind: FoldKind::Mods })
66 range,
67 kind: FoldKind::Mods,
68 })
69 } 57 }
70 } 58 }
71 } 59 }
@@ -84,9 +72,7 @@ fn fold_kind(kind: SyntaxKind) -> Option<FoldKind> {
84} 72}
85 73
86fn has_visibility(node: &SyntaxNode) -> bool { 74fn has_visibility(node: &SyntaxNode) -> bool {
87 ast::Module::cast(node) 75 ast::Module::cast(node).and_then(|m| m.visibility()).is_some()
88 .and_then(|m| m.visibility())
89 .is_some()
90} 76}
91 77
92fn contiguous_range_for_group<'a>( 78fn contiguous_range_for_group<'a>(
@@ -125,10 +111,7 @@ fn contiguous_range_for_group_unless<'a>(
125 } 111 }
126 112
127 if first != last { 113 if first != last {
128 Some(TextRange::from_to( 114 Some(TextRange::from_to(first.range().start(), last.range().end()))
129 first.range().start(),
130 last.range().end(),
131 ))
132 } else { 115 } else {
133 // The group consists of only one element, therefore it cannot be folded 116 // The group consists of only one element, therefore it cannot be folded
134 None 117 None
@@ -169,10 +152,7 @@ fn contiguous_range_for_comment<'a>(
169 } 152 }
170 153
171 if first != last { 154 if first != last {
172 Some(TextRange::from_to( 155 Some(TextRange::from_to(first.range().start(), last.range().end()))
173 first.range().start(),
174 last.range().end(),
175 ))
176 } else { 156 } else {
177 // The group consists of only one element, therefore it cannot be folded 157 // The group consists of only one element, therefore it cannot be folded
178 None 158 None
@@ -199,10 +179,8 @@ mod tests {
199 fold_kinds.len(), 179 fold_kinds.len(),
200 "The amount of fold kinds is different than the expected amount" 180 "The amount of fold kinds is different than the expected amount"
201 ); 181 );
202 for ((fold, range), fold_kind) in folds 182 for ((fold, range), fold_kind) in
203 .into_iter() 183 folds.into_iter().zip(ranges.into_iter()).zip(fold_kinds.into_iter())
204 .zip(ranges.into_iter())
205 .zip(fold_kinds.into_iter())
206 { 184 {
207 assert_eq!(fold.range.start(), range.start()); 185 assert_eq!(fold.range.start(), range.start());
208 assert_eq!(fold.range.end(), range.end()); 186 assert_eq!(fold.range.end(), range.end());
@@ -280,12 +258,7 @@ mod with_attribute_next;</fold>
280fn main() <fold>{ 258fn main() <fold>{
281}</fold>"#; 259}</fold>"#;
282 260
283 let folds = &[ 261 let folds = &[FoldKind::Mods, FoldKind::Mods, FoldKind::Mods, FoldKind::Block];
284 FoldKind::Mods,
285 FoldKind::Mods,
286 FoldKind::Mods,
287 FoldKind::Block,
288 ];
289 do_check(text, folds); 262 do_check(text, folds);
290 } 263 }
291 264