diff options
Diffstat (limited to 'crates/ra_ide/src/runnables.rs')
-rw-r--r-- | crates/ra_ide/src/runnables.rs | 250 |
1 files changed, 223 insertions, 27 deletions
diff --git a/crates/ra_ide/src/runnables.rs b/crates/ra_ide/src/runnables.rs index 286d45eee..f32ce0d22 100644 --- a/crates/ra_ide/src/runnables.rs +++ b/crates/ra_ide/src/runnables.rs | |||
@@ -1,19 +1,19 @@ | |||
1 | use std::fmt; | ||
2 | |||
1 | use hir::{AsAssocItem, Attrs, HirFileId, InFile, Semantics}; | 3 | use hir::{AsAssocItem, Attrs, HirFileId, InFile, Semantics}; |
2 | use itertools::Itertools; | 4 | use itertools::Itertools; |
5 | use ra_cfg::CfgExpr; | ||
3 | use ra_ide_db::RootDatabase; | 6 | use ra_ide_db::RootDatabase; |
4 | use ra_syntax::{ | 7 | use ra_syntax::{ |
5 | ast::{self, AstNode, AttrsOwner, ModuleItemOwner, NameOwner}, | 8 | ast::{self, AstNode, AttrsOwner, DocCommentsOwner, ModuleItemOwner, NameOwner}, |
6 | match_ast, SyntaxNode, TextRange, | 9 | match_ast, SyntaxNode, |
7 | }; | 10 | }; |
8 | 11 | ||
9 | use crate::FileId; | 12 | use crate::{display::ToNav, FileId, NavigationTarget}; |
10 | use ast::DocCommentsOwner; | ||
11 | use ra_cfg::CfgExpr; | ||
12 | use std::fmt::Display; | ||
13 | 13 | ||
14 | #[derive(Debug)] | 14 | #[derive(Debug)] |
15 | pub struct Runnable { | 15 | pub struct Runnable { |
16 | pub range: TextRange, | 16 | pub nav: NavigationTarget, |
17 | pub kind: RunnableKind, | 17 | pub kind: RunnableKind, |
18 | pub cfg_exprs: Vec<CfgExpr>, | 18 | pub cfg_exprs: Vec<CfgExpr>, |
19 | } | 19 | } |
@@ -24,8 +24,8 @@ pub enum TestId { | |||
24 | Path(String), | 24 | Path(String), |
25 | } | 25 | } |
26 | 26 | ||
27 | impl Display for TestId { | 27 | impl fmt::Display for TestId { |
28 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | 28 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
29 | match self { | 29 | match self { |
30 | TestId::Name(name) => write!(f, "{}", name), | 30 | TestId::Name(name) => write!(f, "{}", name), |
31 | TestId::Path(path) => write!(f, "{}", path), | 31 | TestId::Path(path) => write!(f, "{}", path), |
@@ -131,7 +131,8 @@ fn runnable_fn( | |||
131 | let cfg_exprs = | 131 | let cfg_exprs = |
132 | attrs.by_key("cfg").tt_values().map(|subtree| ra_cfg::parse_cfg(subtree)).collect(); | 132 | attrs.by_key("cfg").tt_values().map(|subtree| ra_cfg::parse_cfg(subtree)).collect(); |
133 | 133 | ||
134 | Some(Runnable { range: fn_def.syntax().text_range(), kind, cfg_exprs }) | 134 | let nav = NavigationTarget::from_named(sema.db, InFile::new(file_id.into(), &fn_def)); |
135 | Some(Runnable { nav, kind, cfg_exprs }) | ||
135 | } | 136 | } |
136 | 137 | ||
137 | #[derive(Debug)] | 138 | #[derive(Debug)] |
@@ -183,7 +184,6 @@ fn runnable_mod( | |||
183 | if !has_test_function { | 184 | if !has_test_function { |
184 | return None; | 185 | return None; |
185 | } | 186 | } |
186 | let range = module.syntax().text_range(); | ||
187 | let module_def = sema.to_def(&module)?; | 187 | let module_def = sema.to_def(&module)?; |
188 | 188 | ||
189 | let path = module_def | 189 | let path = module_def |
@@ -197,7 +197,8 @@ fn runnable_mod( | |||
197 | let cfg_exprs = | 197 | let cfg_exprs = |
198 | attrs.by_key("cfg").tt_values().map(|subtree| ra_cfg::parse_cfg(subtree)).collect(); | 198 | attrs.by_key("cfg").tt_values().map(|subtree| ra_cfg::parse_cfg(subtree)).collect(); |
199 | 199 | ||
200 | Some(Runnable { range, kind: RunnableKind::TestMod { path }, cfg_exprs }) | 200 | let nav = module_def.to_nav(sema.db); |
201 | Some(Runnable { nav, kind: RunnableKind::TestMod { path }, cfg_exprs }) | ||
201 | } | 202 | } |
202 | 203 | ||
203 | #[cfg(test)] | 204 | #[cfg(test)] |
@@ -227,12 +228,38 @@ mod tests { | |||
227 | @r###" | 228 | @r###" |
228 | [ | 229 | [ |
229 | Runnable { | 230 | Runnable { |
230 | range: 1..21, | 231 | nav: NavigationTarget { |
232 | file_id: FileId( | ||
233 | 1, | ||
234 | ), | ||
235 | full_range: 1..21, | ||
236 | name: "main", | ||
237 | kind: FN_DEF, | ||
238 | focus_range: Some( | ||
239 | 12..16, | ||
240 | ), | ||
241 | container_name: None, | ||
242 | description: None, | ||
243 | docs: None, | ||
244 | }, | ||
231 | kind: Bin, | 245 | kind: Bin, |
232 | cfg_exprs: [], | 246 | cfg_exprs: [], |
233 | }, | 247 | }, |
234 | Runnable { | 248 | Runnable { |
235 | range: 22..46, | 249 | nav: NavigationTarget { |
250 | file_id: FileId( | ||
251 | 1, | ||
252 | ), | ||
253 | full_range: 22..46, | ||
254 | name: "test_foo", | ||
255 | kind: FN_DEF, | ||
256 | focus_range: Some( | ||
257 | 33..41, | ||
258 | ), | ||
259 | container_name: None, | ||
260 | description: None, | ||
261 | docs: None, | ||
262 | }, | ||
236 | kind: Test { | 263 | kind: Test { |
237 | test_id: Path( | 264 | test_id: Path( |
238 | "test_foo", | 265 | "test_foo", |
@@ -244,7 +271,20 @@ mod tests { | |||
244 | cfg_exprs: [], | 271 | cfg_exprs: [], |
245 | }, | 272 | }, |
246 | Runnable { | 273 | Runnable { |
247 | range: 47..81, | 274 | nav: NavigationTarget { |
275 | file_id: FileId( | ||
276 | 1, | ||
277 | ), | ||
278 | full_range: 47..81, | ||
279 | name: "test_foo", | ||
280 | kind: FN_DEF, | ||
281 | focus_range: Some( | ||
282 | 68..76, | ||
283 | ), | ||
284 | container_name: None, | ||
285 | description: None, | ||
286 | docs: None, | ||
287 | }, | ||
248 | kind: Test { | 288 | kind: Test { |
249 | test_id: Path( | 289 | test_id: Path( |
250 | "test_foo", | 290 | "test_foo", |
@@ -279,12 +319,38 @@ mod tests { | |||
279 | @r###" | 319 | @r###" |
280 | [ | 320 | [ |
281 | Runnable { | 321 | Runnable { |
282 | range: 1..21, | 322 | nav: NavigationTarget { |
323 | file_id: FileId( | ||
324 | 1, | ||
325 | ), | ||
326 | full_range: 1..21, | ||
327 | name: "main", | ||
328 | kind: FN_DEF, | ||
329 | focus_range: Some( | ||
330 | 12..16, | ||
331 | ), | ||
332 | container_name: None, | ||
333 | description: None, | ||
334 | docs: None, | ||
335 | }, | ||
283 | kind: Bin, | 336 | kind: Bin, |
284 | cfg_exprs: [], | 337 | cfg_exprs: [], |
285 | }, | 338 | }, |
286 | Runnable { | 339 | Runnable { |
287 | range: 22..64, | 340 | nav: NavigationTarget { |
341 | file_id: FileId( | ||
342 | 1, | ||
343 | ), | ||
344 | full_range: 22..64, | ||
345 | name: "foo", | ||
346 | kind: FN_DEF, | ||
347 | focus_range: Some( | ||
348 | 56..59, | ||
349 | ), | ||
350 | container_name: None, | ||
351 | description: None, | ||
352 | docs: None, | ||
353 | }, | ||
288 | kind: DocTest { | 354 | kind: DocTest { |
289 | test_id: Path( | 355 | test_id: Path( |
290 | "foo", | 356 | "foo", |
@@ -319,12 +385,38 @@ mod tests { | |||
319 | @r###" | 385 | @r###" |
320 | [ | 386 | [ |
321 | Runnable { | 387 | Runnable { |
322 | range: 1..21, | 388 | nav: NavigationTarget { |
389 | file_id: FileId( | ||
390 | 1, | ||
391 | ), | ||
392 | full_range: 1..21, | ||
393 | name: "main", | ||
394 | kind: FN_DEF, | ||
395 | focus_range: Some( | ||
396 | 12..16, | ||
397 | ), | ||
398 | container_name: None, | ||
399 | description: None, | ||
400 | docs: None, | ||
401 | }, | ||
323 | kind: Bin, | 402 | kind: Bin, |
324 | cfg_exprs: [], | 403 | cfg_exprs: [], |
325 | }, | 404 | }, |
326 | Runnable { | 405 | Runnable { |
327 | range: 51..105, | 406 | nav: NavigationTarget { |
407 | file_id: FileId( | ||
408 | 1, | ||
409 | ), | ||
410 | full_range: 51..105, | ||
411 | name: "foo", | ||
412 | kind: FN_DEF, | ||
413 | focus_range: Some( | ||
414 | 97..100, | ||
415 | ), | ||
416 | container_name: None, | ||
417 | description: None, | ||
418 | docs: None, | ||
419 | }, | ||
328 | kind: DocTest { | 420 | kind: DocTest { |
329 | test_id: Path( | 421 | test_id: Path( |
330 | "Data::foo", | 422 | "Data::foo", |
@@ -354,14 +446,40 @@ mod tests { | |||
354 | @r###" | 446 | @r###" |
355 | [ | 447 | [ |
356 | Runnable { | 448 | Runnable { |
357 | range: 1..59, | 449 | nav: NavigationTarget { |
450 | file_id: FileId( | ||
451 | 1, | ||
452 | ), | ||
453 | full_range: 1..59, | ||
454 | name: "test_mod", | ||
455 | kind: MODULE, | ||
456 | focus_range: Some( | ||
457 | 13..21, | ||
458 | ), | ||
459 | container_name: None, | ||
460 | description: None, | ||
461 | docs: None, | ||
462 | }, | ||
358 | kind: TestMod { | 463 | kind: TestMod { |
359 | path: "test_mod", | 464 | path: "test_mod", |
360 | }, | 465 | }, |
361 | cfg_exprs: [], | 466 | cfg_exprs: [], |
362 | }, | 467 | }, |
363 | Runnable { | 468 | Runnable { |
364 | range: 28..57, | 469 | nav: NavigationTarget { |
470 | file_id: FileId( | ||
471 | 1, | ||
472 | ), | ||
473 | full_range: 28..57, | ||
474 | name: "test_foo1", | ||
475 | kind: FN_DEF, | ||
476 | focus_range: Some( | ||
477 | 43..52, | ||
478 | ), | ||
479 | container_name: None, | ||
480 | description: None, | ||
481 | docs: None, | ||
482 | }, | ||
365 | kind: Test { | 483 | kind: Test { |
366 | test_id: Path( | 484 | test_id: Path( |
367 | "test_mod::test_foo1", | 485 | "test_mod::test_foo1", |
@@ -396,14 +514,40 @@ mod tests { | |||
396 | @r###" | 514 | @r###" |
397 | [ | 515 | [ |
398 | Runnable { | 516 | Runnable { |
399 | range: 23..85, | 517 | nav: NavigationTarget { |
518 | file_id: FileId( | ||
519 | 1, | ||
520 | ), | ||
521 | full_range: 23..85, | ||
522 | name: "test_mod", | ||
523 | kind: MODULE, | ||
524 | focus_range: Some( | ||
525 | 27..35, | ||
526 | ), | ||
527 | container_name: None, | ||
528 | description: None, | ||
529 | docs: None, | ||
530 | }, | ||
400 | kind: TestMod { | 531 | kind: TestMod { |
401 | path: "foo::test_mod", | 532 | path: "foo::test_mod", |
402 | }, | 533 | }, |
403 | cfg_exprs: [], | 534 | cfg_exprs: [], |
404 | }, | 535 | }, |
405 | Runnable { | 536 | Runnable { |
406 | range: 46..79, | 537 | nav: NavigationTarget { |
538 | file_id: FileId( | ||
539 | 1, | ||
540 | ), | ||
541 | full_range: 46..79, | ||
542 | name: "test_foo1", | ||
543 | kind: FN_DEF, | ||
544 | focus_range: Some( | ||
545 | 65..74, | ||
546 | ), | ||
547 | container_name: None, | ||
548 | description: None, | ||
549 | docs: None, | ||
550 | }, | ||
407 | kind: Test { | 551 | kind: Test { |
408 | test_id: Path( | 552 | test_id: Path( |
409 | "foo::test_mod::test_foo1", | 553 | "foo::test_mod::test_foo1", |
@@ -440,14 +584,40 @@ mod tests { | |||
440 | @r###" | 584 | @r###" |
441 | [ | 585 | [ |
442 | Runnable { | 586 | Runnable { |
443 | range: 41..115, | 587 | nav: NavigationTarget { |
588 | file_id: FileId( | ||
589 | 1, | ||
590 | ), | ||
591 | full_range: 41..115, | ||
592 | name: "test_mod", | ||
593 | kind: MODULE, | ||
594 | focus_range: Some( | ||
595 | 45..53, | ||
596 | ), | ||
597 | container_name: None, | ||
598 | description: None, | ||
599 | docs: None, | ||
600 | }, | ||
444 | kind: TestMod { | 601 | kind: TestMod { |
445 | path: "foo::bar::test_mod", | 602 | path: "foo::bar::test_mod", |
446 | }, | 603 | }, |
447 | cfg_exprs: [], | 604 | cfg_exprs: [], |
448 | }, | 605 | }, |
449 | Runnable { | 606 | Runnable { |
450 | range: 68..105, | 607 | nav: NavigationTarget { |
608 | file_id: FileId( | ||
609 | 1, | ||
610 | ), | ||
611 | full_range: 68..105, | ||
612 | name: "test_foo1", | ||
613 | kind: FN_DEF, | ||
614 | focus_range: Some( | ||
615 | 91..100, | ||
616 | ), | ||
617 | container_name: None, | ||
618 | description: None, | ||
619 | docs: None, | ||
620 | }, | ||
451 | kind: Test { | 621 | kind: Test { |
452 | test_id: Path( | 622 | test_id: Path( |
453 | "foo::bar::test_mod::test_foo1", | 623 | "foo::bar::test_mod::test_foo1", |
@@ -479,7 +649,20 @@ mod tests { | |||
479 | @r###" | 649 | @r###" |
480 | [ | 650 | [ |
481 | Runnable { | 651 | Runnable { |
482 | range: 1..58, | 652 | nav: NavigationTarget { |
653 | file_id: FileId( | ||
654 | 1, | ||
655 | ), | ||
656 | full_range: 1..58, | ||
657 | name: "test_foo1", | ||
658 | kind: FN_DEF, | ||
659 | focus_range: Some( | ||
660 | 44..53, | ||
661 | ), | ||
662 | container_name: None, | ||
663 | description: None, | ||
664 | docs: None, | ||
665 | }, | ||
483 | kind: Test { | 666 | kind: Test { |
484 | test_id: Path( | 667 | test_id: Path( |
485 | "test_foo1", | 668 | "test_foo1", |
@@ -516,7 +699,20 @@ mod tests { | |||
516 | @r###" | 699 | @r###" |
517 | [ | 700 | [ |
518 | Runnable { | 701 | Runnable { |
519 | range: 1..80, | 702 | nav: NavigationTarget { |
703 | file_id: FileId( | ||
704 | 1, | ||
705 | ), | ||
706 | full_range: 1..80, | ||
707 | name: "test_foo1", | ||
708 | kind: FN_DEF, | ||
709 | focus_range: Some( | ||
710 | 66..75, | ||
711 | ), | ||
712 | container_name: None, | ||
713 | description: None, | ||
714 | docs: None, | ||
715 | }, | ||
520 | kind: Test { | 716 | kind: Test { |
521 | test_id: Path( | 717 | test_id: Path( |
522 | "test_foo1", | 718 | "test_foo1", |