From 41570f60bf268c97223a864b8aa11a339929f55a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 17 Aug 2018 16:04:34 +0300 Subject: [PATCH] extend module resolve to mod.rs --- crates/assert_eq_text/Cargo.toml | 1 + crates/assert_eq_text/src/lib.rs | 11 ++++++++ crates/libanalysis/Cargo.toml | 3 +++ crates/libanalysis/src/lib.rs | 24 ++++++++++------- crates/libanalysis/tests/tests.rs | 45 +++++++++++++++++++++++++++++++ crates/libeditor/Cargo.toml | 2 ++ crates/libeditor/tests/test.rs | 13 +++------ crates/libsyntax2/Cargo.toml | 2 +- 8 files changed, 82 insertions(+), 19 deletions(-) create mode 100644 crates/libanalysis/tests/tests.rs diff --git a/crates/assert_eq_text/Cargo.toml b/crates/assert_eq_text/Cargo.toml index 21858dfd312..e122bbbebc0 100644 --- a/crates/assert_eq_text/Cargo.toml +++ b/crates/assert_eq_text/Cargo.toml @@ -5,3 +5,4 @@ authors = ["Aleksey Kladov "] [dependencies] difference = "2.0.0" +itertools = "0.7.8" diff --git a/crates/assert_eq_text/src/lib.rs b/crates/assert_eq_text/src/lib.rs index ed942d81a23..26b9bfb389c 100644 --- a/crates/assert_eq_text/src/lib.rs +++ b/crates/assert_eq_text/src/lib.rs @@ -1,4 +1,9 @@ extern crate difference; +extern crate itertools; + +use std::fmt; +use itertools::Itertools; + pub use self::difference::Changeset as __Changeset; #[macro_export] @@ -23,3 +28,9 @@ macro_rules! assert_eq_text { } }}; } + +pub fn assert_eq_dbg(expected: &str, actual: &impl fmt::Debug) { + let actual = format!("{:?}", actual); + let expected = expected.lines().map(|l| l.trim()).join(" "); + assert_eq!(expected, actual); +} diff --git a/crates/libanalysis/Cargo.toml b/crates/libanalysis/Cargo.toml index 42a5aca1489..fa859ff9437 100644 --- a/crates/libanalysis/Cargo.toml +++ b/crates/libanalysis/Cargo.toml @@ -12,3 +12,6 @@ rayon = "1.0.2" fst = { git = "https://github.com/matklad/fst", branch = "subsequence"} libsyntax2 = { path = "../libsyntax2" } libeditor = { path = "../libeditor" } + +[dev-dependencies] +assert_eq_text = { path = "../assert_eq_text" } diff --git a/crates/libanalysis/src/lib.rs b/crates/libanalysis/src/lib.rs index 19b64fece19..a50a0f32f05 100644 --- a/crates/libanalysis/src/lib.rs +++ b/crates/libanalysis/src/lib.rs @@ -163,15 +163,21 @@ fn resolve_module(&self, id: FileId, module: ast::Module<&SyntaxRoot>) -> Vec<(F Some(name) => name.text(), None => return Vec::new(), }; - let id = match self.resolve_relative_path(id, &PathBuf::from(format!("../{}.rs", name))) { - Some(id) => id, - None => return Vec::new(), - }; - vec![(id, FileSymbol { - name: name.clone(), - node_range: TextRange::offset_len(0.into(), 0.into()), - kind: MODULE, - })] + let paths = &[ + PathBuf::from(format!("../{}.rs", name)), + PathBuf::from(format!("../{}/mod.rs", name)), + ]; + paths.iter() + .filter_map(|path| self.resolve_relative_path(id, path)) + .map(|id| { + let symbol = FileSymbol { + name: name.clone(), + node_range: TextRange::offset_len(0.into(), 0.into()), + kind: MODULE, + }; + (id, symbol) + }) + .collect() } fn resolve_relative_path(&self, id: FileId, path: &Path) -> Option { diff --git a/crates/libanalysis/tests/tests.rs b/crates/libanalysis/tests/tests.rs new file mode 100644 index 00000000000..9ef5200af6d --- /dev/null +++ b/crates/libanalysis/tests/tests.rs @@ -0,0 +1,45 @@ +extern crate libanalysis; +extern crate assert_eq_text; + +use std::path::PathBuf; + +use libanalysis::{WorldState, FileId}; +use assert_eq_text::assert_eq_dbg; + + +#[test] +fn test_resolve_module() { + let mut world = WorldState::new(); + world.change_file(FileId(1), Some("mod foo;".to_string())); + world.change_file(FileId(2), Some("".to_string())); + + let snap = world.snapshot(|id, path| { + assert_eq!(id, FileId(1)); + if path == PathBuf::from("../foo/mod.rs") { + return None; + } + assert_eq!(path, PathBuf::from("../foo.rs")); + Some(FileId(2)) + }); + let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into()) + .unwrap(); + assert_eq_dbg( + r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#, + &symbols, + ); + + let snap = world.snapshot(|id, path| { + assert_eq!(id, FileId(1)); + if path == PathBuf::from("../foo.rs") { + return None; + } + assert_eq!(path, PathBuf::from("../foo/mod.rs")); + Some(FileId(2)) + }); + let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into()) + .unwrap(); + assert_eq_dbg( + r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#, + &symbols, + ); +} diff --git a/crates/libeditor/Cargo.toml b/crates/libeditor/Cargo.toml index 502f532a77d..7b39870cd83 100644 --- a/crates/libeditor/Cargo.toml +++ b/crates/libeditor/Cargo.toml @@ -10,4 +10,6 @@ superslice = "0.1.0" libsyntax2 = { path = "../libsyntax2" } smol_str = "0.1.0" + +[dev-dependencies] assert_eq_text = { path = "../assert_eq_text" } diff --git a/crates/libeditor/tests/test.rs b/crates/libeditor/tests/test.rs index 97919d347a6..df4cb65d105 100644 --- a/crates/libeditor/tests/test.rs +++ b/crates/libeditor/tests/test.rs @@ -6,6 +6,7 @@ use std::fmt; use itertools::Itertools; +use assert_eq_text::{assert_eq_dbg}; use libeditor::{ File, TextUnit, TextRange, ActionResult, CursorPosition, highlight, runnables, extend_selection, file_structure, @@ -33,7 +34,7 @@ fn main() {} println!("Hello, {}!", 92); "#); let hls = highlight(&file); - dbg_eq( + assert_eq_dbg( r#"[HighlightedRange { range: [1; 11), tag: "comment" }, HighlightedRange { range: [12; 14), tag: "keyword" }, HighlightedRange { range: [15; 19), tag: "function" }, @@ -57,7 +58,7 @@ fn test_foo() {} fn test_foo() {} "#); let runnables = runnables(&file); - dbg_eq( + assert_eq_dbg( r#"[Runnable { range: [1; 13), kind: Bin }, Runnable { range: [15; 39), kind: Test { name: "test_foo" } }, Runnable { range: [41; 75), kind: Test { name: "test_foo" } }]"#, @@ -86,7 +87,7 @@ impl E {} impl fmt::Debug for E {} "#); let symbols = file_structure(&file); - dbg_eq( + assert_eq_dbg( r#"[StructureNode { parent: None, label: "Foo", navigation_range: [8; 11), node_range: [1; 26), kind: STRUCT_DEF }, StructureNode { parent: Some(0), label: "x", navigation_range: [18; 19), node_range: [18; 24), kind: NAMED_FIELD }, StructureNode { parent: None, label: "m", navigation_range: [32; 33), node_range: [28; 53), kind: MODULE }, @@ -147,12 +148,6 @@ fn file(text: &str) -> File { File::parse(text) } -fn dbg_eq(expected: &str, actual: &impl fmt::Debug) { - let actual = format!("{:?}", actual); - let expected = expected.lines().map(|l| l.trim()).join(" "); - assert_eq!(expected, actual); -} - fn check_action Option>( before: &str, after: &str, diff --git a/crates/libsyntax2/Cargo.toml b/crates/libsyntax2/Cargo.toml index 78e9e18a2e7..b55cdf48127 100644 --- a/crates/libsyntax2/Cargo.toml +++ b/crates/libsyntax2/Cargo.toml @@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0" [dependencies] unicode-xid = "0.1.0" text_unit = "0.1.2" -itertools = "0.7.5" +itertools = "0.7.8" drop_bomb = "0.1.4" parking_lot = "0.6.0" smol_str = "0.1.0"