|
| 1 | +use hir::Semantics; |
| 2 | +use ide_db::{FilePosition, RootDatabase}; |
| 3 | +use syntax::{ |
| 4 | + algo::find_node_at_offset, |
| 5 | + ast::{self, AstNode}, |
| 6 | +}; |
| 7 | + |
| 8 | +use crate::NavigationTarget; |
| 9 | + |
| 10 | +/// This returns `Vec` because a module may be included from several places. |
| 11 | +pub(crate) fn children_modules(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> { |
| 12 | + let sema = Semantics::new(db); |
| 13 | + let source_file = sema.parse_guess_edition(position.file_id); |
| 14 | + // First go to the parent module which contains the cursor |
| 15 | + let module = find_node_at_offset::<ast::Module>(source_file.syntax(), position.offset); |
| 16 | + |
| 17 | + match module { |
| 18 | + Some(module) => { |
| 19 | + // Return all the children module inside the ItemList of the parent module |
| 20 | + sema.to_def(&module) |
| 21 | + .into_iter() |
| 22 | + .flat_map(|module| module.children(db)) |
| 23 | + .map(|module| NavigationTarget::from_module_to_decl(db, module).call_site()) |
| 24 | + .collect() |
| 25 | + } |
| 26 | + None => { |
| 27 | + // Return all the children module inside the source file |
| 28 | + sema.file_to_module_defs(position.file_id) |
| 29 | + .flat_map(|module| module.children(db)) |
| 30 | + .map(|module| NavigationTarget::from_module_to_decl(db, module).call_site()) |
| 31 | + .collect() |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +#[cfg(test)] |
| 37 | +mod tests { |
| 38 | + use ide_db::FileRange; |
| 39 | + |
| 40 | + use crate::fixture; |
| 41 | + |
| 42 | + fn check_children_module(#[rust_analyzer::rust_fixture] ra_fixture: &str) { |
| 43 | + let (analysis, position, expected) = fixture::annotations(ra_fixture); |
| 44 | + let navs = analysis.children_modules(position).unwrap(); |
| 45 | + let navs = navs |
| 46 | + .iter() |
| 47 | + .map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() }) |
| 48 | + .collect::<Vec<_>>(); |
| 49 | + assert_eq!(expected.into_iter().map(|(fr, _)| fr).collect::<Vec<_>>(), navs); |
| 50 | + } |
| 51 | + |
| 52 | + #[test] |
| 53 | + fn test_resolve_children_module() { |
| 54 | + check_children_module( |
| 55 | + r#" |
| 56 | +//- /lib.rs |
| 57 | +$0 |
| 58 | +mod foo; |
| 59 | + //^^^ |
| 60 | +
|
| 61 | +//- /foo.rs |
| 62 | +// empty |
| 63 | +"#, |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn test_resolve_children_module_on_module_decl() { |
| 69 | + check_children_module( |
| 70 | + r#" |
| 71 | +//- /lib.rs |
| 72 | +mod $0foo; |
| 73 | +//- /foo.rs |
| 74 | +mod bar; |
| 75 | + //^^^ |
| 76 | +
|
| 77 | +//- /foo/bar.rs |
| 78 | +// empty |
| 79 | +"#, |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn test_resolve_children_module_for_inline() { |
| 85 | + check_children_module( |
| 86 | + r#" |
| 87 | +//- /lib.rs |
| 88 | +mod foo { |
| 89 | + mod $0bar { |
| 90 | + mod baz {} |
| 91 | + } //^^^ |
| 92 | +} |
| 93 | +"#, |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn test_resolve_multi_child_module() { |
| 99 | + check_children_module( |
| 100 | + r#" |
| 101 | +//- /main.rs |
| 102 | +$0 |
| 103 | +mod foo; |
| 104 | + //^^^ |
| 105 | +mod bar; |
| 106 | + //^^^ |
| 107 | + |
| 108 | +//- /foo.rs |
| 109 | +// empty |
| 110 | +
|
| 111 | +//- /bar.rs |
| 112 | +// empty |
| 113 | +"#, |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
0 commit comments