From 018bf3d7b1cdc070aa1a0926df0840f1cf9a03b9 Mon Sep 17 00:00:00 2001 From: Alexander Andreev Date: Wed, 3 Jul 2019 17:37:56 +0300 Subject: Added resolve submodules with raw name #1211 --- crates/ra_hir/src/nameres/collector.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index ef4d1ed70..e11198e02 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -643,6 +643,7 @@ fn resolve_submodule( let mod_name = path.file_stem().unwrap_or("unknown"); let is_dir_owner = is_root || mod_name == "mod"; + let name = resolve_mod_name(name.to_string()); let file_mod = dir_path.join(format!("{}.rs", name)); let dir_mod = dir_path.join(format!("{}/mod.rs", name)); let file_dir_mod = dir_path.join(format!("{}/{}.rs", mod_name, name)); @@ -662,6 +663,14 @@ fn resolve_submodule( } } +fn resolve_mod_name(name: String) -> String { + if name.starts_with("r#") { + name.replace("r#", "") + } else { + name + } +} + #[cfg(test)] mod tests { use ra_db::SourceDatabase; -- cgit v1.2.3 From 02e9e46ed6443f053d2c22335ff57f98d5f64f05 Mon Sep 17 00:00:00 2001 From: Alexander Andreev Date: Wed, 3 Jul 2019 22:01:41 +0300 Subject: Move resolve raw name in name.rs Added test for check module resolution with raw name --- crates/ra_hir/src/name.rs | 15 +++++++++++++-- crates/ra_hir/src/nameres/collector.rs | 9 --------- crates/ra_hir/src/nameres/tests.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 11 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs index ba17958eb..0bb7cae99 100644 --- a/crates/ra_hir/src/name.rs +++ b/crates/ra_hir/src/name.rs @@ -98,13 +98,15 @@ pub(crate) trait AsName { impl AsName for ast::NameRef { fn as_name(&self) -> Name { - Name::new(self.text().clone()) + let name = resolve_name(self.text().clone()); + Name::new(name) } } impl AsName for ast::Name { fn as_name(&self) -> Name { - Name::new(self.text().clone()) + let name = resolve_name(self.text().clone()); + Name::new(name) } } @@ -184,3 +186,12 @@ impl AsName for KnownName { Name::new(s.into()) } } + +fn resolve_name(text: SmolStr) -> SmolStr { + let raw_start = "r#"; + if text.as_str().starts_with(raw_start) { + SmolStr::new(&text[raw_start.len()..]) + } else { + text + } +} diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index e11198e02..ef4d1ed70 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -643,7 +643,6 @@ fn resolve_submodule( let mod_name = path.file_stem().unwrap_or("unknown"); let is_dir_owner = is_root || mod_name == "mod"; - let name = resolve_mod_name(name.to_string()); let file_mod = dir_path.join(format!("{}.rs", name)); let dir_mod = dir_path.join(format!("{}/mod.rs", name)); let file_dir_mod = dir_path.join(format!("{}/{}.rs", mod_name, name)); @@ -663,14 +662,6 @@ fn resolve_submodule( } } -fn resolve_mod_name(name: String) -> String { - if name.starts_with("r#") { - name.replace("r#", "") - } else { - name - } -} - #[cfg(test)] mod tests { use ra_db::SourceDatabase; diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index adac814d9..78332ce7c 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -338,6 +338,33 @@ fn module_resolution_works_for_non_standard_filenames() { "###); } +#[test] +fn module_resolution_works_for_raw_modules() { + let map = def_map_with_crate_graph( + " + //- /library.rs + mod r#async; + use self::r#async::Bar; + + //- /async.rs + pub struct Bar; + ", + crate_graph! { + "library": ("/library.rs", []), + }, + ); + + assert_snapshot_matches!(map, @r###" + ⋮crate + ⋮Bar: t v + ⋮async: t + ⋮ + ⋮crate::async + ⋮Bar: t v + "###); + +} + #[test] fn name_res_works_for_broken_modules() { covers!(name_res_works_for_broken_modules); -- cgit v1.2.3 From 6263aa13d0eed983827d14fa39bf6490eb27e7a5 Mon Sep 17 00:00:00 2001 From: Alexander Andreev Date: Wed, 3 Jul 2019 22:17:43 +0300 Subject: Fixed request changes --- crates/ra_hir/src/name.rs | 8 ++++---- crates/ra_hir/src/nameres/tests.rs | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs index 0bb7cae99..06a2dbc76 100644 --- a/crates/ra_hir/src/name.rs +++ b/crates/ra_hir/src/name.rs @@ -98,14 +98,14 @@ pub(crate) trait AsName { impl AsName for ast::NameRef { fn as_name(&self) -> Name { - let name = resolve_name(self.text().clone()); + let name = resolve_name(self.text()); Name::new(name) } } impl AsName for ast::Name { fn as_name(&self) -> Name { - let name = resolve_name(self.text().clone()); + let name = resolve_name(self.text()); Name::new(name) } } @@ -187,11 +187,11 @@ impl AsName for KnownName { } } -fn resolve_name(text: SmolStr) -> SmolStr { +fn resolve_name(text: &SmolStr) -> SmolStr { let raw_start = "r#"; if text.as_str().starts_with(raw_start) { SmolStr::new(&text[raw_start.len()..]) } else { - text + text.clone() } } diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 78332ce7c..07b533e29 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -340,7 +340,7 @@ fn module_resolution_works_for_non_standard_filenames() { #[test] fn module_resolution_works_for_raw_modules() { - let map = def_map_with_crate_graph( + let map = def_map_with_crate_graph( " //- /library.rs mod r#async; @@ -362,7 +362,6 @@ fn module_resolution_works_for_raw_modules() { ⋮crate::async ⋮Bar: t v "###); - } #[test] -- cgit v1.2.3