aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-17 14:04:34 +0100
committerAleksey Kladov <[email protected]>2018-08-17 14:04:34 +0100
commit41570f60bf268c97223a864b8aa11a339929f55a (patch)
tree1eabe16612f975a0e6bfeede79dcfea98d2b3aa4
parent081c16c77642a5c86ed72c5fbd11deccc2edd5d5 (diff)
extend module resolve to mod.rs
-rw-r--r--crates/assert_eq_text/Cargo.toml1
-rw-r--r--crates/assert_eq_text/src/lib.rs11
-rw-r--r--crates/libanalysis/Cargo.toml3
-rw-r--r--crates/libanalysis/src/lib.rs24
-rw-r--r--crates/libanalysis/tests/tests.rs45
-rw-r--r--crates/libeditor/Cargo.toml2
-rw-r--r--crates/libeditor/tests/test.rs13
-rw-r--r--crates/libsyntax2/Cargo.toml2
8 files changed, 82 insertions, 19 deletions
diff --git a/crates/assert_eq_text/Cargo.toml b/crates/assert_eq_text/Cargo.toml
index 21858dfd3..e122bbbeb 100644
--- a/crates/assert_eq_text/Cargo.toml
+++ b/crates/assert_eq_text/Cargo.toml
@@ -5,3 +5,4 @@ authors = ["Aleksey Kladov <[email protected]>"]
5 5
6[dependencies] 6[dependencies]
7difference = "2.0.0" 7difference = "2.0.0"
8itertools = "0.7.8"
diff --git a/crates/assert_eq_text/src/lib.rs b/crates/assert_eq_text/src/lib.rs
index ed942d81a..26b9bfb38 100644
--- a/crates/assert_eq_text/src/lib.rs
+++ b/crates/assert_eq_text/src/lib.rs
@@ -1,4 +1,9 @@
1extern crate difference; 1extern crate difference;
2extern crate itertools;
3
4use std::fmt;
5use itertools::Itertools;
6
2pub use self::difference::Changeset as __Changeset; 7pub use self::difference::Changeset as __Changeset;
3 8
4#[macro_export] 9#[macro_export]
@@ -23,3 +28,9 @@ macro_rules! assert_eq_text {
23 } 28 }
24 }}; 29 }};
25} 30}
31
32pub fn assert_eq_dbg(expected: &str, actual: &impl fmt::Debug) {
33 let actual = format!("{:?}", actual);
34 let expected = expected.lines().map(|l| l.trim()).join(" ");
35 assert_eq!(expected, actual);
36}
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}
diff --git a/crates/libeditor/Cargo.toml b/crates/libeditor/Cargo.toml
index 502f532a7..7b39870cd 100644
--- a/crates/libeditor/Cargo.toml
+++ b/crates/libeditor/Cargo.toml
@@ -10,4 +10,6 @@ superslice = "0.1.0"
10 10
11libsyntax2 = { path = "../libsyntax2" } 11libsyntax2 = { path = "../libsyntax2" }
12smol_str = "0.1.0" 12smol_str = "0.1.0"
13
14[dev-dependencies]
13assert_eq_text = { path = "../assert_eq_text" } 15assert_eq_text = { path = "../assert_eq_text" }
diff --git a/crates/libeditor/tests/test.rs b/crates/libeditor/tests/test.rs
index 97919d347..df4cb65d1 100644
--- a/crates/libeditor/tests/test.rs
+++ b/crates/libeditor/tests/test.rs
@@ -6,6 +6,7 @@ extern crate assert_eq_text;
6 6
7use std::fmt; 7use std::fmt;
8use itertools::Itertools; 8use itertools::Itertools;
9use assert_eq_text::{assert_eq_dbg};
9use libeditor::{ 10use libeditor::{
10 File, TextUnit, TextRange, ActionResult, CursorPosition, 11 File, TextUnit, TextRange, ActionResult, CursorPosition,
11 highlight, runnables, extend_selection, file_structure, 12 highlight, runnables, extend_selection, file_structure,
@@ -33,7 +34,7 @@ fn main() {}
33 println!("Hello, {}!", 92); 34 println!("Hello, {}!", 92);
34"#); 35"#);
35 let hls = highlight(&file); 36 let hls = highlight(&file);
36 dbg_eq( 37 assert_eq_dbg(
37 r#"[HighlightedRange { range: [1; 11), tag: "comment" }, 38 r#"[HighlightedRange { range: [1; 11), tag: "comment" },
38 HighlightedRange { range: [12; 14), tag: "keyword" }, 39 HighlightedRange { range: [12; 14), tag: "keyword" },
39 HighlightedRange { range: [15; 19), tag: "function" }, 40 HighlightedRange { range: [15; 19), tag: "function" },
@@ -57,7 +58,7 @@ fn test_foo() {}
57fn test_foo() {} 58fn test_foo() {}
58"#); 59"#);
59 let runnables = runnables(&file); 60 let runnables = runnables(&file);
60 dbg_eq( 61 assert_eq_dbg(
61 r#"[Runnable { range: [1; 13), kind: Bin }, 62 r#"[Runnable { range: [1; 13), kind: Bin },
62 Runnable { range: [15; 39), kind: Test { name: "test_foo" } }, 63 Runnable { range: [15; 39), kind: Test { name: "test_foo" } },
63 Runnable { range: [41; 75), kind: Test { name: "test_foo" } }]"#, 64 Runnable { range: [41; 75), kind: Test { name: "test_foo" } }]"#,
@@ -86,7 +87,7 @@ impl E {}
86impl fmt::Debug for E {} 87impl fmt::Debug for E {}
87"#); 88"#);
88 let symbols = file_structure(&file); 89 let symbols = file_structure(&file);
89 dbg_eq( 90 assert_eq_dbg(
90 r#"[StructureNode { parent: None, label: "Foo", navigation_range: [8; 11), node_range: [1; 26), kind: STRUCT_DEF }, 91 r#"[StructureNode { parent: None, label: "Foo", navigation_range: [8; 11), node_range: [1; 26), kind: STRUCT_DEF },
91 StructureNode { parent: Some(0), label: "x", navigation_range: [18; 19), node_range: [18; 24), kind: NAMED_FIELD }, 92 StructureNode { parent: Some(0), label: "x", navigation_range: [18; 19), node_range: [18; 24), kind: NAMED_FIELD },
92 StructureNode { parent: None, label: "m", navigation_range: [32; 33), node_range: [28; 53), kind: MODULE }, 93 StructureNode { parent: None, label: "m", navigation_range: [32; 33), node_range: [28; 53), kind: MODULE },
@@ -147,12 +148,6 @@ fn file(text: &str) -> File {
147 File::parse(text) 148 File::parse(text)
148} 149}
149 150
150fn dbg_eq(expected: &str, actual: &impl fmt::Debug) {
151 let actual = format!("{:?}", actual);
152 let expected = expected.lines().map(|l| l.trim()).join(" ");
153 assert_eq!(expected, actual);
154}
155
156fn check_action<F: Fn(&File, TextUnit) -> Option<ActionResult>>( 151fn check_action<F: Fn(&File, TextUnit) -> Option<ActionResult>>(
157 before: &str, 152 before: &str,
158 after: &str, 153 after: &str,
diff --git a/crates/libsyntax2/Cargo.toml b/crates/libsyntax2/Cargo.toml
index 78e9e18a2..b55cdf481 100644
--- a/crates/libsyntax2/Cargo.toml
+++ b/crates/libsyntax2/Cargo.toml
@@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0"
7[dependencies] 7[dependencies]
8unicode-xid = "0.1.0" 8unicode-xid = "0.1.0"
9text_unit = "0.1.2" 9text_unit = "0.1.2"
10itertools = "0.7.5" 10itertools = "0.7.8"
11drop_bomb = "0.1.4" 11drop_bomb = "0.1.4"
12parking_lot = "0.6.0" 12parking_lot = "0.6.0"
13smol_str = "0.1.0" 13smol_str = "0.1.0"