]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/ide/src/parent_module.rs
Rollup merge of #100228 - luqmana:suggestion-ice, r=estebank
[rust.git] / src / tools / rust-analyzer / crates / ide / src / parent_module.rs
1 use hir::Semantics;
2 use ide_db::{
3     base_db::{CrateId, FileId, FilePosition},
4     RootDatabase,
5 };
6 use itertools::Itertools;
7 use syntax::{
8     algo::find_node_at_offset,
9     ast::{self, AstNode},
10 };
11
12 use crate::NavigationTarget;
13
14 // Feature: Parent Module
15 //
16 // Navigates to the parent module of the current module.
17 //
18 // |===
19 // | Editor  | Action Name
20 //
21 // | VS Code | **rust-analyzer: Locate parent module**
22 // |===
23 //
24 // image::https://user-images.githubusercontent.com/48062697/113065580-04c21800-91b1-11eb-9a32-00086161c0bd.gif[]
25
26 /// This returns `Vec` because a module may be included from several places.
27 pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> {
28     let sema = Semantics::new(db);
29     let source_file = sema.parse(position.file_id);
30
31     let mut module = find_node_at_offset::<ast::Module>(source_file.syntax(), position.offset);
32
33     // If cursor is literally on `mod foo`, go to the grandpa.
34     if let Some(m) = &module {
35         if !m
36             .item_list()
37             .map_or(false, |it| it.syntax().text_range().contains_inclusive(position.offset))
38         {
39             cov_mark::hit!(test_resolve_parent_module_on_module_decl);
40             module = m.syntax().ancestors().skip(1).find_map(ast::Module::cast);
41         }
42     }
43
44     match module {
45         Some(module) => sema
46             .to_def(&module)
47             .into_iter()
48             .map(|module| NavigationTarget::from_module_to_decl(db, module))
49             .collect(),
50         None => sema
51             .to_module_defs(position.file_id)
52             .map(|module| NavigationTarget::from_module_to_decl(db, module))
53             .collect(),
54     }
55 }
56
57 /// Returns `Vec` for the same reason as `parent_module`
58 pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
59     let sema = Semantics::new(db);
60     sema.to_module_defs(file_id).map(|module| module.krate().into()).unique().collect()
61 }
62
63 #[cfg(test)]
64 mod tests {
65     use ide_db::base_db::FileRange;
66
67     use crate::fixture;
68
69     fn check(ra_fixture: &str) {
70         let (analysis, position, expected) = fixture::annotations(ra_fixture);
71         let navs = analysis.parent_module(position).unwrap();
72         let navs = navs
73             .iter()
74             .map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
75             .collect::<Vec<_>>();
76         assert_eq!(expected.into_iter().map(|(fr, _)| fr).collect::<Vec<_>>(), navs);
77     }
78
79     #[test]
80     fn test_resolve_parent_module() {
81         check(
82             r#"
83 //- /lib.rs
84 mod foo;
85   //^^^
86
87 //- /foo.rs
88 $0// empty
89 "#,
90         );
91     }
92
93     #[test]
94     fn test_resolve_parent_module_on_module_decl() {
95         cov_mark::check!(test_resolve_parent_module_on_module_decl);
96         check(
97             r#"
98 //- /lib.rs
99 mod foo;
100   //^^^
101 //- /foo.rs
102 mod $0bar;
103
104 //- /foo/bar.rs
105 // empty
106 "#,
107         );
108     }
109
110     #[test]
111     fn test_resolve_parent_module_for_inline() {
112         check(
113             r#"
114 //- /lib.rs
115 mod foo {
116     mod bar {
117         mod baz { $0 }
118     }     //^^^
119 }
120 "#,
121         );
122     }
123
124     #[test]
125     fn test_resolve_multi_parent_module() {
126         check(
127             r#"
128 //- /main.rs
129 mod foo;
130   //^^^
131 #[path = "foo.rs"]
132 mod bar;
133   //^^^
134 //- /foo.rs
135 $0
136 "#,
137         );
138     }
139
140     #[test]
141     fn test_resolve_crate_root() {
142         let (analysis, file_id) = fixture::file(
143             r#"
144 //- /foo.rs
145 $0
146 //- /main.rs
147 mod foo;
148 "#,
149         );
150         assert_eq!(analysis.crate_for(file_id).unwrap().len(), 1);
151     }
152
153     #[test]
154     fn test_resolve_multi_parent_crate() {
155         let (analysis, file_id) = fixture::file(
156             r#"
157 //- /baz.rs
158 $0
159 //- /foo.rs crate:foo
160 mod baz;
161 //- /bar.rs crate:bar
162 mod baz;
163 "#,
164         );
165         assert_eq!(analysis.crate_for(file_id).unwrap().len(), 2);
166     }
167 }