diff options
Diffstat (limited to 'crates/ra_editor/src/folding_ranges.rs')
-rw-r--r-- | crates/ra_editor/src/folding_ranges.rs | 74 |
1 files changed, 42 insertions, 32 deletions
diff --git a/crates/ra_editor/src/folding_ranges.rs b/crates/ra_editor/src/folding_ranges.rs index 2a8fa3cda..da542ecf0 100644 --- a/crates/ra_editor/src/folding_ranges.rs +++ b/crates/ra_editor/src/folding_ranges.rs | |||
@@ -10,6 +10,7 @@ use ra_syntax::{ | |||
10 | pub enum FoldKind { | 10 | pub enum FoldKind { |
11 | Comment, | 11 | Comment, |
12 | Imports, | 12 | Imports, |
13 | Block, | ||
13 | } | 14 | } |
14 | 15 | ||
15 | #[derive(Debug)] | 16 | #[derive(Debug)] |
@@ -62,6 +63,8 @@ fn fold_kind(kind: SyntaxKind) -> Option<FoldKind> { | |||
62 | match kind { | 63 | match kind { |
63 | COMMENT => Some(FoldKind::Comment), | 64 | COMMENT => Some(FoldKind::Comment), |
64 | USE_ITEM => Some(FoldKind::Imports), | 65 | USE_ITEM => Some(FoldKind::Imports), |
66 | NAMED_FIELD_DEF_LIST | FIELD_PAT_LIST | ITEM_LIST | EXTERN_ITEM_LIST | USE_TREE_LIST | ||
67 | | BLOCK | ENUM_VARIANT_LIST => Some(FoldKind::Block), | ||
65 | _ => None, | 68 | _ => None, |
66 | } | 69 | } |
67 | } | 70 | } |
@@ -170,7 +173,7 @@ mod tests { | |||
170 | use test_utils::extract_ranges; | 173 | use test_utils::extract_ranges; |
171 | 174 | ||
172 | fn do_check(text: &str, fold_kinds: &[FoldKind]) { | 175 | fn do_check(text: &str, fold_kinds: &[FoldKind]) { |
173 | let (ranges, text) = extract_ranges(text); | 176 | let (ranges, text) = extract_ranges(text, "fold"); |
174 | let file = SourceFileNode::parse(&text); | 177 | let file = SourceFileNode::parse(&text); |
175 | let folds = folding_ranges(&file); | 178 | let folds = folding_ranges(&file); |
176 | 179 | ||
@@ -198,26 +201,27 @@ mod tests { | |||
198 | #[test] | 201 | #[test] |
199 | fn test_fold_comments() { | 202 | fn test_fold_comments() { |
200 | let text = r#" | 203 | let text = r#" |
201 | <|>// Hello | 204 | <fold>// Hello |
202 | // this is a multiline | 205 | // this is a multiline |
203 | // comment | 206 | // comment |
204 | //<|> | 207 | //</fold> |
205 | 208 | ||
206 | // But this is not | 209 | // But this is not |
207 | 210 | ||
208 | fn main() { | 211 | fn main() <fold>{ |
209 | <|>// We should | 212 | <fold>// We should |
210 | // also | 213 | // also |
211 | // fold | 214 | // fold |
212 | // this one.<|> | 215 | // this one.</fold> |
213 | <|>//! But this one is different | 216 | <fold>//! But this one is different |
214 | //! because it has another flavor<|> | 217 | //! because it has another flavor</fold> |
215 | <|>/* As does this | 218 | <fold>/* As does this |
216 | multiline comment */<|> | 219 | multiline comment */</fold> |
217 | }"#; | 220 | }</fold>"#; |
218 | 221 | ||
219 | let fold_kinds = &[ | 222 | let fold_kinds = &[ |
220 | FoldKind::Comment, | 223 | FoldKind::Comment, |
224 | FoldKind::Block, | ||
221 | FoldKind::Comment, | 225 | FoldKind::Comment, |
222 | FoldKind::Comment, | 226 | FoldKind::Comment, |
223 | FoldKind::Comment, | 227 | FoldKind::Comment, |
@@ -228,60 +232,66 @@ fn main() { | |||
228 | #[test] | 232 | #[test] |
229 | fn test_fold_imports() { | 233 | fn test_fold_imports() { |
230 | let text = r#" | 234 | let text = r#" |
231 | <|>use std::{ | 235 | <fold>use std::<fold>{ |
232 | str, | 236 | str, |
233 | vec, | 237 | vec, |
234 | io as iop | 238 | io as iop |
235 | };<|> | 239 | }</fold>;</fold> |
236 | 240 | ||
237 | fn main() { | 241 | fn main() <fold>{ |
238 | }"#; | 242 | }</fold>"#; |
239 | 243 | ||
240 | let folds = &[FoldKind::Imports]; | 244 | let folds = &[FoldKind::Imports, FoldKind::Block, FoldKind::Block]; |
241 | do_check(text, folds); | 245 | do_check(text, folds); |
242 | } | 246 | } |
243 | 247 | ||
244 | #[test] | 248 | #[test] |
245 | fn test_fold_import_groups() { | 249 | fn test_fold_import_groups() { |
246 | let text = r#" | 250 | let text = r#" |
247 | <|>use std::str; | 251 | <fold>use std::str; |
248 | use std::vec; | 252 | use std::vec; |
249 | use std::io as iop;<|> | 253 | use std::io as iop;</fold> |
250 | 254 | ||
251 | <|>use std::mem; | 255 | <fold>use std::mem; |
252 | use std::f64;<|> | 256 | use std::f64;</fold> |
253 | 257 | ||
254 | use std::collections::HashMap; | 258 | use std::collections::HashMap; |
255 | // Some random comment | 259 | // Some random comment |
256 | use std::collections::VecDeque; | 260 | use std::collections::VecDeque; |
257 | 261 | ||
258 | fn main() { | 262 | fn main() <fold>{ |
259 | }"#; | 263 | }</fold>"#; |
260 | 264 | ||
261 | let folds = &[FoldKind::Imports, FoldKind::Imports]; | 265 | let folds = &[FoldKind::Imports, FoldKind::Imports, FoldKind::Block]; |
262 | do_check(text, folds); | 266 | do_check(text, folds); |
263 | } | 267 | } |
264 | 268 | ||
265 | #[test] | 269 | #[test] |
266 | fn test_fold_import_and_groups() { | 270 | fn test_fold_import_and_groups() { |
267 | let text = r#" | 271 | let text = r#" |
268 | <|>use std::str; | 272 | <fold>use std::str; |
269 | use std::vec; | 273 | use std::vec; |
270 | use std::io as iop;<|> | 274 | use std::io as iop;</fold> |
271 | 275 | ||
272 | <|>use std::mem; | 276 | <fold>use std::mem; |
273 | use std::f64;<|> | 277 | use std::f64;</fold> |
274 | 278 | ||
275 | <|>use std::collections::{ | 279 | <fold>use std::collections::<fold>{ |
276 | HashMap, | 280 | HashMap, |
277 | VecDeque, | 281 | VecDeque, |
278 | };<|> | 282 | }</fold>;</fold> |
279 | // Some random comment | 283 | // Some random comment |
280 | 284 | ||
281 | fn main() { | 285 | fn main() <fold>{ |
282 | }"#; | 286 | }</fold>"#; |
283 | 287 | ||
284 | let folds = &[FoldKind::Imports, FoldKind::Imports, FoldKind::Imports]; | 288 | let folds = &[ |
289 | FoldKind::Imports, | ||
290 | FoldKind::Imports, | ||
291 | FoldKind::Imports, | ||
292 | FoldKind::Block, | ||
293 | FoldKind::Block, | ||
294 | ]; | ||
285 | do_check(text, folds); | 295 | do_check(text, folds); |
286 | } | 296 | } |
287 | 297 | ||