diff --git a/app/gui/src/controller/searcher/component.rs b/app/gui/src/controller/searcher/component.rs index a27628a13de..fa777a98fb9 100644 --- a/app/gui/src/controller/searcher/component.rs +++ b/app/gui/src/controller/searcher/component.rs @@ -131,16 +131,8 @@ impl Component { id: entry::Id, entry: Rc, group_id: Option, + label: ImString, ) -> Self { - let label = match entry.kind { - entry::Kind::Module - if entry.defined_in.is_main_module() || entry.defined_in.is_top_element() => - format!("{}", entry.defined_in).into(), - _ => match entry.self_type.as_ref() { - Some(self_type) => format!("{}.{}", self_type.alias_name(), entry.name).into(), - None => entry.name.to_im_string(), - }, - }; let aliases = entry.aliases().map(|alias| format!("{alias} ({label})").into()).collect_vec().into(); let data = Suggestion::FromDatabase { id, entry }; diff --git a/app/gui/src/controller/searcher/component/builder.rs b/app/gui/src/controller/searcher/component/builder.rs index ed9399c9254..8baf9f17e7b 100644 --- a/app/gui/src/controller/searcher/component/builder.rs +++ b/app/gui/src/controller/searcher/component/builder.rs @@ -286,7 +286,16 @@ impl<'a> Builder<'a> { None => WhenDisplayed::in_base_mode(&entry, group_id.is_some()), }; let when_displayed = when_displayed.consider_tags(&entry); - let component = Component::new_from_database_entry(id, entry, group_id); + let label = match entry.kind { + suggestion_database::entry::Kind::Module if self.inside_module.is_none() => + format!("{}", entry.defined_in).into(), + _ => match entry.self_type.as_ref() { + Some(self_type) if self.this_type.is_none() => + format!("{}.{}", self_type.alias_name(), entry.name).into(), + _ => entry.name.to_im_string(), + }, + }; + let component = Component::new_from_database_entry(id, entry, group_id, label); if matches!(when_displayed, WhenDisplayed::Always) { self.built_list.displayed_by_default.push(component.clone()); } @@ -482,6 +491,35 @@ mod tests { ]); } + #[test] + fn building_main_module_content_list() { + let database = mock_database(); + let groups = mock_groups(); + let (module_id, _) = database + .lookup_by_qualified_name(&QualifiedName::from_text("test.Test").unwrap()) + .unwrap(); + let mut builder = Builder::new_inside_module(&database, &groups, module_id); + + builder.add_components_from_db(database.keys()); + let list = builder.build(); + + check_displayed_components(&list, vec!["TopModule1", "TopModule2"]); + check_groups(&list, vec![None, None]); + check_filterable_components(&list, vec![ + "TopModule1", + "TopModule1.fun1", + "TopModule1.fun2", + "SubModule1", + "SubModule1.fun4", + "SubModule2", + "SubModule2.fun5", + "SubModule3", + "SubModule3.fun6", + "TopModule2", + "TopModule2.fun0", + ]); + } + #[test] diff --git a/app/gui/src/model/module.rs b/app/gui/src/model/module.rs index 7d1938a8939..5d128d7231d 100644 --- a/app/gui/src/model/module.rs +++ b/app/gui/src/model/module.rs @@ -163,7 +163,7 @@ impl Path { if let [ref _src, ref dirs @ .., _] = *self.file_path.segments.as_slice() { // Path must designate a valid module and must be able to designate any valid module. // Therefore, unwraps in this method are safe. - let parent_modules = dirs.iter().map(ImString::new).collect(); + let parent_modules = dirs.iter().map(ImString::from).collect(); let name = ImString::new(self.module_name()); Id { parent_modules, name } } else { diff --git a/lib/rust/prelude/src/string.rs b/lib/rust/prelude/src/string.rs index 544e9b19080..435aa6d6e74 100644 --- a/lib/rust/prelude/src/string.rs +++ b/lib/rust/prelude/src/string.rs @@ -160,17 +160,16 @@ impl AsRef for CowString { // ================ /// Immutable string implementation with a fast clone implementation. -#[derive(Clone, CloneRef, Default, Eq, Hash, PartialEq, Ord, PartialOrd)] +#[derive(Clone, CloneRef, Eq, Hash, PartialEq, Ord, PartialOrd)] #[derive(Deserialize, Serialize)] pub struct ImString { - content: Rc, + content: Rc, } impl ImString { /// Constructor. - pub fn new(content: impl Into) -> Self { - let content = Rc::new(content.into()); - Self { content } + pub fn new(content: impl Into>) -> Self { + Self { content: content.into() } } /// Extract a string slice containing the entire string. @@ -179,6 +178,12 @@ impl ImString { } } +impl Default for ImString { + fn default() -> Self { + "".into() + } +} + impl std::fmt::Display for ImString { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(&self.content, f) @@ -204,12 +209,6 @@ impl AsRef for ImString { } } -impl AsRef for ImString { - fn as_ref(&self) -> &String { - self.content.as_ref() - } -} - impl AsRef for ImString { fn as_ref(&self) -> &str { self.content.as_ref() @@ -222,9 +221,9 @@ impl Borrow for ImString { } } -impl Borrow for ImString { - fn borrow(&self) -> &String { - &self.content +impl From> for ImString { + fn from(content: Rc) -> Self { + Self { content } } } @@ -236,13 +235,13 @@ impl From for ImString { impl From<&String> for ImString { fn from(t: &String) -> Self { - Self::new(t) + Self::new(t.as_str()) } } impl From<&&String> for ImString { fn from(t: &&String) -> Self { - Self::new(*t) + Self::new(t.as_str()) } } @@ -264,18 +263,21 @@ impl From> for ImString { } } +impl From for Rc { + fn from(t: ImString) -> Self { + t.content + } +} + impl From for String { fn from(value: ImString) -> Self { - match Rc::try_unwrap(value.content) { - Ok(str) => str, - Err(rc) => rc.deref().clone(), - } + value.as_str().into() } } impl PartialEq<&str> for ImString { fn eq(&self, other: &&str) -> bool { - self.content.as_ref().eq(other) + self.content.as_ref().eq(*other) } } @@ -287,7 +289,7 @@ impl PartialEq for ImString { impl PartialEq for &str { fn eq(&self, other: &ImString) -> bool { - self.eq(other.content.as_ref()) + self.eq(&other.content.as_ref()) } } @@ -410,12 +412,6 @@ macro_rules! im_string_newtype_without_serde { } } - impl AsRef for $name { - fn as_ref(&self) -> &String { - self.content.as_ref() - } - } - impl AsRef for $name { fn as_ref(&self) -> &str { self.content.as_ref() diff --git a/lib/rust/text/src/text.rs b/lib/rust/text/src/text.rs index 6bc616d3bdf..633ba771830 100644 --- a/lib/rust/text/src/text.rs +++ b/lib/rust/text/src/text.rs @@ -942,7 +942,7 @@ impl From for String { impl From for ImString { fn from(t: Rope) -> Self { - ImString::new(t) + ImString::new(t.to_im_string()) } }