aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/folding_ranges.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/folding_ranges.rs')
-rw-r--r--crates/ra_ide/src/folding_ranges.rs174
1 files changed, 94 insertions, 80 deletions
diff --git a/crates/ra_ide/src/folding_ranges.rs b/crates/ra_ide/src/folding_ranges.rs
index 8657377de..e7ec9953f 100644
--- a/crates/ra_ide/src/folding_ranges.rs
+++ b/crates/ra_ide/src/folding_ranges.rs
@@ -15,6 +15,7 @@ pub enum FoldKind {
15 Imports, 15 Imports,
16 Mods, 16 Mods,
17 Block, 17 Block,
18 ArgList,
18} 19}
19 20
20#[derive(Debug)] 21#[derive(Debug)]
@@ -83,8 +84,10 @@ fn fold_kind(kind: SyntaxKind) -> Option<FoldKind> {
83 match kind { 84 match kind {
84 COMMENT => Some(FoldKind::Comment), 85 COMMENT => Some(FoldKind::Comment),
85 USE_ITEM => Some(FoldKind::Imports), 86 USE_ITEM => Some(FoldKind::Imports),
87 ARG_LIST => Some(FoldKind::ArgList),
86 RECORD_FIELD_DEF_LIST 88 RECORD_FIELD_DEF_LIST
87 | RECORD_FIELD_PAT_LIST 89 | RECORD_FIELD_PAT_LIST
90 | RECORD_FIELD_LIST
88 | ITEM_LIST 91 | ITEM_LIST
89 | EXTERN_ITEM_LIST 92 | EXTERN_ITEM_LIST
90 | USE_TREE_LIST 93 | USE_TREE_LIST
@@ -196,89 +199,85 @@ fn contiguous_range_for_comment(
196 199
197#[cfg(test)] 200#[cfg(test)]
198mod tests { 201mod tests {
202 use test_utils::extract_tags;
203
199 use super::*; 204 use super::*;
200 use test_utils::extract_ranges;
201 205
202 fn do_check(text: &str, fold_kinds: &[FoldKind]) { 206 fn check(ra_fixture: &str) {
203 let (ranges, text) = extract_ranges(text, "fold"); 207 let (ranges, text) = extract_tags(ra_fixture, "fold");
208
204 let parse = SourceFile::parse(&text); 209 let parse = SourceFile::parse(&text);
205 let folds = folding_ranges(&parse.tree()); 210 let folds = folding_ranges(&parse.tree());
206
207 assert_eq!( 211 assert_eq!(
208 folds.len(), 212 folds.len(),
209 ranges.len(), 213 ranges.len(),
210 "The amount of folds is different than the expected amount" 214 "The amount of folds is different than the expected amount"
211 ); 215 );
212 assert_eq!( 216
213 folds.len(), 217 for (fold, (range, attr)) in folds.iter().zip(ranges.into_iter()) {
214 fold_kinds.len(),
215 "The amount of fold kinds is different than the expected amount"
216 );
217 for ((fold, range), fold_kind) in
218 folds.iter().zip(ranges.into_iter()).zip(fold_kinds.iter())
219 {
220 assert_eq!(fold.range.start(), range.start()); 218 assert_eq!(fold.range.start(), range.start());
221 assert_eq!(fold.range.end(), range.end()); 219 assert_eq!(fold.range.end(), range.end());
222 assert_eq!(&fold.kind, fold_kind); 220
221 let kind = match fold.kind {
222 FoldKind::Comment => "comment",
223 FoldKind::Imports => "imports",
224 FoldKind::Mods => "mods",
225 FoldKind::Block => "block",
226 FoldKind::ArgList => "arglist",
227 };
228 assert_eq!(kind, &attr.unwrap());
223 } 229 }
224 } 230 }
225 231
226 #[test] 232 #[test]
227 fn test_fold_comments() { 233 fn test_fold_comments() {
228 let text = r#" 234 check(
229<fold>// Hello 235 r#"
236<fold comment>// Hello
230// this is a multiline 237// this is a multiline
231// comment 238// comment
232//</fold> 239//</fold>
233 240
234// But this is not 241// But this is not
235 242
236fn main() <fold>{ 243fn main() <fold block>{
237 <fold>// We should 244 <fold comment>// We should
238 // also 245 // also
239 // fold 246 // fold
240 // this one.</fold> 247 // this one.</fold>
241 <fold>//! But this one is different 248 <fold comment>//! But this one is different
242 //! because it has another flavor</fold> 249 //! because it has another flavor</fold>
243 <fold>/* As does this 250 <fold comment>/* As does this
244 multiline comment */</fold> 251 multiline comment */</fold>
245}</fold>"#; 252}</fold>"#,
246 253 );
247 let fold_kinds = &[
248 FoldKind::Comment,
249 FoldKind::Block,
250 FoldKind::Comment,
251 FoldKind::Comment,
252 FoldKind::Comment,
253 ];
254 do_check(text, fold_kinds);
255 } 254 }
256 255
257 #[test] 256 #[test]
258 fn test_fold_imports() { 257 fn test_fold_imports() {
259 let text = r#" 258 check(
260<fold>use std::<fold>{ 259 r#"
260<fold imports>use std::<fold block>{
261 str, 261 str,
262 vec, 262 vec,
263 io as iop 263 io as iop
264}</fold>;</fold> 264}</fold>;</fold>
265 265
266fn main() <fold>{ 266fn main() <fold block>{
267}</fold>"#; 267}</fold>"#,
268 268 );
269 let folds = &[FoldKind::Imports, FoldKind::Block, FoldKind::Block];
270 do_check(text, folds);
271 } 269 }
272 270
273 #[test] 271 #[test]
274 fn test_fold_mods() { 272 fn test_fold_mods() {
275 let text = r#" 273 check(
274 r#"
276 275
277pub mod foo; 276pub mod foo;
278<fold>mod after_pub; 277<fold mods>mod after_pub;
279mod after_pub_next;</fold> 278mod after_pub_next;</fold>
280 279
281<fold>mod before_pub; 280<fold mods>mod before_pub;
282mod before_pub_next;</fold> 281mod before_pub_next;</fold>
283pub mod bar; 282pub mod bar;
284 283
@@ -286,90 +285,105 @@ mod not_folding_single;
286pub mod foobar; 285pub mod foobar;
287pub not_folding_single_next; 286pub not_folding_single_next;
288 287
289<fold>#[cfg(test)] 288<fold mods>#[cfg(test)]
290mod with_attribute; 289mod with_attribute;
291mod with_attribute_next;</fold> 290mod with_attribute_next;</fold>
292 291
293fn main() <fold>{ 292fn main() <fold block>{
294}</fold>"#; 293}</fold>"#,
295 294 );
296 let folds = &[FoldKind::Mods, FoldKind::Mods, FoldKind::Mods, FoldKind::Block];
297 do_check(text, folds);
298 } 295 }
299 296
300 #[test] 297 #[test]
301 fn test_fold_import_groups() { 298 fn test_fold_import_groups() {
302 let text = r#" 299 check(
303<fold>use std::str; 300 r#"
301<fold imports>use std::str;
304use std::vec; 302use std::vec;
305use std::io as iop;</fold> 303use std::io as iop;</fold>
306 304
307<fold>use std::mem; 305<fold imports>use std::mem;
308use std::f64;</fold> 306use std::f64;</fold>
309 307
310use std::collections::HashMap; 308use std::collections::HashMap;
311// Some random comment 309// Some random comment
312use std::collections::VecDeque; 310use std::collections::VecDeque;
313 311
314fn main() <fold>{ 312fn main() <fold block>{
315}</fold>"#; 313}</fold>"#,
316 314 );
317 let folds = &[FoldKind::Imports, FoldKind::Imports, FoldKind::Block];
318 do_check(text, folds);
319 } 315 }
320 316
321 #[test] 317 #[test]
322 fn test_fold_import_and_groups() { 318 fn test_fold_import_and_groups() {
323 let text = r#" 319 check(
324<fold>use std::str; 320 r#"
321<fold imports>use std::str;
325use std::vec; 322use std::vec;
326use std::io as iop;</fold> 323use std::io as iop;</fold>
327 324
328<fold>use std::mem; 325<fold imports>use std::mem;
329use std::f64;</fold> 326use std::f64;</fold>
330 327
331<fold>use std::collections::<fold>{ 328<fold imports>use std::collections::<fold block>{
332 HashMap, 329 HashMap,
333 VecDeque, 330 VecDeque,
334}</fold>;</fold> 331}</fold>;</fold>
335// Some random comment 332// Some random comment
336 333
337fn main() <fold>{ 334fn main() <fold block>{
338}</fold>"#; 335}</fold>"#,
339 336 );
340 let folds = &[
341 FoldKind::Imports,
342 FoldKind::Imports,
343 FoldKind::Imports,
344 FoldKind::Block,
345 FoldKind::Block,
346 ];
347 do_check(text, folds);
348 } 337 }
349 338
350 #[test] 339 #[test]
351 fn test_folds_macros() { 340 fn test_folds_macros() {
352 let text = r#" 341 check(
353macro_rules! foo <fold>{ 342 r#"
343macro_rules! foo <fold block>{
354 ($($tt:tt)*) => { $($tt)* } 344 ($($tt:tt)*) => { $($tt)* }
355}</fold> 345}</fold>
356"#; 346"#,
357 347 );
358 let folds = &[FoldKind::Block];
359 do_check(text, folds);
360 } 348 }
361 349
362 #[test] 350 #[test]
363 fn test_fold_match_arms() { 351 fn test_fold_match_arms() {
364 let text = r#" 352 check(
365fn main() <fold>{ 353 r#"
366 match 0 <fold>{ 354fn main() <fold block>{
355 match 0 <fold block>{
367 0 => 0, 356 0 => 0,
368 _ => 1, 357 _ => 1,
369 }</fold> 358 }</fold>
370}</fold>"#; 359}</fold>
360"#,
361 );
362 }
363
364 #[test]
365 fn fold_big_calls() {
366 check(
367 r#"
368fn main() <fold block>{
369 frobnicate<fold arglist>(
370 1,
371 2,
372 3,
373 )</fold>
374}</fold>
375"#,
376 )
377 }
371 378
372 let folds = &[FoldKind::Block, FoldKind::Block]; 379 #[test]
373 do_check(text, folds); 380 fn fold_record_literals() {
381 check(
382 r#"
383const _: S = S <fold block>{
384
385}</fold>;
386"#,
387 )
374 } 388 }
375} 389}