aboutsummaryrefslogtreecommitdiff
path: root/crates/libanalysis
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libanalysis')
-rw-r--r--crates/libanalysis/Cargo.toml3
-rw-r--r--crates/libanalysis/src/lib.rs24
-rw-r--r--crates/libanalysis/tests/tests.rs45
3 files changed, 63 insertions, 9 deletions
diff --git a/crates/libanalysis/Cargo.toml b/crates/libanalysis/Cargo.toml
index 42a5aca14..fa859ff94 100644
--- a/crates/libanalysis/Cargo.toml
+++ b/crates/libanalysis/Cargo.toml
@@ -12,3 +12,6 @@ rayon = "1.0.2"
12fst = { git = "https://github.com/matklad/fst", branch = "subsequence"} 12fst = { git = "https://github.com/matklad/fst", branch = "subsequence"}
13libsyntax2 = { path = "../libsyntax2" } 13libsyntax2 = { path = "../libsyntax2" }
14libeditor = { path = "../libeditor" } 14libeditor = { path = "../libeditor" }
15
16[dev-dependencies]
17assert_eq_text = { path = "../assert_eq_text" }
diff --git a/crates/libanalysis/src/lib.rs b/crates/libanalysis/src/lib.rs
index 19b64fece..a50a0f32f 100644
--- a/crates/libanalysis/src/lib.rs
+++ b/crates/libanalysis/src/lib.rs
@@ -163,15 +163,21 @@ impl World {
163 Some(name) => name.text(), 163 Some(name) => name.text(),
164 None => return Vec::new(), 164 None => return Vec::new(),
165 }; 165 };
166 let id = match self.resolve_relative_path(id, &PathBuf::from(format!("../{}.rs", name))) { 166 let paths = &[
167 Some(id) => id, 167 PathBuf::from(format!("../{}.rs", name)),
168 None => return Vec::new(), 168 PathBuf::from(format!("../{}/mod.rs", name)),
169 }; 169 ];
170 vec![(id, FileSymbol { 170 paths.iter()
171 name: name.clone(), 171 .filter_map(|path| self.resolve_relative_path(id, path))
172 node_range: TextRange::offset_len(0.into(), 0.into()), 172 .map(|id| {
173 kind: MODULE, 173 let symbol = FileSymbol {
174 })] 174 name: name.clone(),
175 node_range: TextRange::offset_len(0.into(), 0.into()),
176 kind: MODULE,
177 };
178 (id, symbol)
179 })
180 .collect()
175 } 181 }
176 182
177 fn resolve_relative_path(&self, id: FileId, path: &Path) -> Option<FileId> { 183 fn resolve_relative_path(&self, id: FileId, path: &Path) -> Option<FileId> {
diff --git a/crates/libanalysis/tests/tests.rs b/crates/libanalysis/tests/tests.rs
new file mode 100644
index 000000000..9ef5200af
--- /dev/null
+++ b/crates/libanalysis/tests/tests.rs
@@ -0,0 +1,45 @@
1extern crate libanalysis;
2extern crate assert_eq_text;
3
4use std::path::PathBuf;
5
6use libanalysis::{WorldState, FileId};
7use assert_eq_text::assert_eq_dbg;
8
9
10#[test]
11fn test_resolve_module() {
12 let mut world = WorldState::new();
13 world.change_file(FileId(1), Some("mod foo;".to_string()));
14 world.change_file(FileId(2), Some("".to_string()));
15
16 let snap = world.snapshot(|id, path| {
17 assert_eq!(id, FileId(1));
18 if path == PathBuf::from("../foo/mod.rs") {
19 return None;
20 }
21 assert_eq!(path, PathBuf::from("../foo.rs"));
22 Some(FileId(2))
23 });
24 let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into())
25 .unwrap();
26 assert_eq_dbg(
27 r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#,
28 &symbols,
29 );
30
31 let snap = world.snapshot(|id, path| {
32 assert_eq!(id, FileId(1));
33 if path == PathBuf::from("../foo.rs") {
34 return None;
35 }
36 assert_eq!(path, PathBuf::from("../foo/mod.rs"));
37 Some(FileId(2))
38 });
39 let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into())
40 .unwrap();
41 assert_eq_dbg(
42 r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#,
43 &symbols,
44 );
45}