1use hir::{Semantics, crate_def_map};
2use ide_db::{
3 FileId, FilePosition, RootDatabase,
4 base_db::{Crate, RootQueryDb},
5};
6use itertools::Itertools;
7use syntax::{
8 algo::find_node_at_offset,
9 ast::{self, AstNode},
10};
11
12use crate::NavigationTarget;
13
14pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> {
26 let sema = Semantics::new(db);
27 let source_file = sema.parse_guess_edition(position.file_id);
28
29 let mut module = find_node_at_offset::<ast::Module>(source_file.syntax(), position.offset);
30
31 if let Some(m) = &module
33 && !m
34 .item_list()
35 .is_some_and(|it| it.syntax().text_range().contains_inclusive(position.offset))
36 {
37 cov_mark::hit!(test_resolve_parent_module_on_module_decl);
38 module = m.syntax().ancestors().skip(1).find_map(ast::Module::cast);
39 }
40
41 match module {
42 Some(module) => sema
43 .to_def(&module)
44 .into_iter()
45 .flat_map(|module| NavigationTarget::from_module_to_decl(db, module))
46 .collect(),
47 None => sema
48 .file_to_module_defs(position.file_id)
49 .flat_map(|module| NavigationTarget::from_module_to_decl(db, module))
50 .collect(),
51 }
52}
53
54pub(crate) fn crates_for(db: &RootDatabase, file_id: FileId) -> Vec<Crate> {
56 db.relevant_crates(file_id)
57 .iter()
58 .copied()
59 .filter(|&crate_id| {
60 crate_def_map(db, crate_id).modules_for_file(db, file_id).next().is_some()
61 })
62 .sorted()
63 .collect()
64}
65
66#[cfg(test)]
67mod tests {
68 use ide_db::FileRange;
69
70 use crate::fixture;
71
72 fn check(#[rust_analyzer::rust_fixture] ra_fixture: &str) {
73 let (analysis, position, expected) = fixture::annotations(ra_fixture);
74 let navs = analysis.parent_module(position).unwrap();
75 let navs = navs
76 .iter()
77 .map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
78 .collect::<Vec<_>>();
79 assert_eq!(expected.into_iter().map(|(fr, _)| fr).collect::<Vec<_>>(), navs);
80 }
81
82 #[test]
83 fn test_resolve_parent_module() {
84 check(
85 r#"
86//- /lib.rs
87mod foo;
88 //^^^
89
90//- /foo.rs
91$0// empty
92"#,
93 );
94 }
95
96 #[test]
97 fn test_resolve_parent_module_on_module_decl() {
98 cov_mark::check!(test_resolve_parent_module_on_module_decl);
99 check(
100 r#"
101//- /lib.rs
102mod foo;
103 //^^^
104//- /foo.rs
105mod $0bar;
106
107//- /foo/bar.rs
108// empty
109"#,
110 );
111 }
112
113 #[test]
114 fn test_resolve_parent_module_for_inline() {
115 check(
116 r#"
117//- /lib.rs
118mod foo {
119 mod bar {
120 mod baz { $0 }
121 } //^^^
122}
123"#,
124 );
125 }
126
127 #[test]
128 fn test_resolve_multi_parent_module() {
129 check(
130 r#"
131//- /main.rs
132mod foo;
133 //^^^
134#[path = "foo.rs"]
135mod bar;
136 //^^^
137//- /foo.rs
138$0
139"#,
140 );
141 }
142
143 #[test]
144 fn test_resolve_crate_root() {
145 let (analysis, file_id) = fixture::file(
146 r#"
147//- /foo.rs
148$0
149//- /main.rs
150mod foo;
151"#,
152 );
153 assert_eq!(analysis.crates_for(file_id).unwrap().len(), 1);
154 }
155
156 #[test]
157 fn test_resolve_multi_parent_crate() {
158 let (analysis, file_id) = fixture::file(
159 r#"
160//- /baz.rs
161$0
162//- /foo.rs crate:foo
163mod baz;
164//- /bar.rs crate:bar
165mod baz;
166"#,
167 );
168 assert_eq!(analysis.crates_for(file_id).unwrap().len(), 2);
169 }
170}