Skip to content

Commit

Permalink
Use dedicated type for import groups
Browse files Browse the repository at this point in the history
  • Loading branch information
purefunctor committed Jan 31, 2025
1 parent b9ca34a commit 050c07d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
44 changes: 39 additions & 5 deletions crates/indexing/src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@ pub enum TypeItem {
Operator(DeclarationId),
}

/// Import declarations qualified with the same name.
///
/// ```purescript
/// import Indexing.Algorithm as Exported
/// import Indexing.Indexes as Exported
/// ```
#[derive(Debug, Default, PartialEq, Eq)]
pub struct ImportGroup(Vec<ImportId>);

impl AsRef<[ImportId]> for ImportGroup {
fn as_ref(&self) -> &[ImportId] {
self.0.as_ref()
}
}

impl ImportGroup {
pub(crate) fn push(&mut self, import_id: ImportId) {
self.0.push(import_id);
}
}

pub type ImportGroupId = Id<ImportGroup>;

#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Exportable<T> {
pub(crate) value: T,
Expand Down Expand Up @@ -145,7 +168,7 @@ impl<T> Exportable<T> {
/// [`SourceMap`]: crate::SourceMap
#[derive(Debug, Default)]
pub struct NominalIndex {
qualified: IndexMap<SmolStr, Exportable<Vec<ImportId>>>,
qualified: IndexMap<SmolStr, Exportable<ImportGroup>>,
expr_item: IndexMap<SmolStr, Exportable<ExprItem>>,
type_item: IndexMap<SmolStr, Exportable<TypeItem>>,
}
Expand All @@ -154,10 +177,14 @@ pub(crate) type MutableItem<'t, T> = (&'t mut Exportable<T>, Id<T>);

impl NominalIndex {
pub(crate) fn insert_qualified(&mut self, name: SmolStr, import: ImportId) {
self.qualified.entry(name).or_insert_with(|| Exportable::new(vec![])).value.push(import);
self.qualified
.entry(name)
.or_insert_with(|| Exportable::new(ImportGroup::default()))
.value
.push(import);
}

pub(crate) fn qualified_get_mut(&mut self, name: &str) -> Option<MutableItem<Vec<ImportId>>> {
pub(crate) fn qualified_get_mut(&mut self, name: &str) -> Option<MutableItem<ImportGroup>> {
let (index, _, value) = self.qualified.get_full_mut(name)?;
Some((value, Id::from_raw(index)))
}
Expand Down Expand Up @@ -207,8 +234,15 @@ pub type NominalLookupResult<'t, T> = Option<(Id<T>, &'t T, Option<ExportItemId>
pub type NominalIndexResult<'t, T> = Option<(&'t SmolStr, &'t T, Option<ExportItemId>)>;

impl NominalIndex {
pub fn lookup_qualified(&self, name: &str) -> Option<&[ImportId]> {
self.qualified.get(name).map(|v| &v.value[..])
pub fn lookup_qualified(&self, name: &str) -> NominalLookupResult<ImportGroup> {
let (id, _, Exportable { value, export_id }) = self.qualified.get_full(name)?;
Some((Id::from_raw(id), value, *export_id))
}

pub fn index_qualified(&self, id: ImportGroupId) -> NominalIndexResult<ImportGroup> {
let index = id.into();
let (key, Exportable { value, export_id }) = self.qualified.get_index(index)?;
Some((key, value, *export_id))
}

pub fn lookup_expr_item(&self, name: &str) -> NominalLookupResult<ExprItem> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ list_cons: Some((Id(3), Constructor(Id(2)), Some(Id(4))))
list_nil: Some((Id(4), Constructor(Id(3)), Some(Id(4))))
plus: Some((Id(5), Operator(Id(5)), Some(Id(5))))
plus_type: Some((Id(4), Operator(Id(6)), Some(Id(6))))
export: Some([Id(0)])
export: Some((Id(0), ImportGroup([Id(0)]), Some(Id(7))))
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ source: crates/indexing/tests/indexing.rs
expression: snapshot
snapshot_kind: text
---
prelude: Some([Id(0)])
data_list: Some([Id(1)])
halogen: Some([Id(2), Id(3)])
prelude: Some((Id(0), ImportGroup([Id(0)]), None))
data_list: Some((Id(1), ImportGroup([Id(1)]), None))
halogen: Some((Id(2), ImportGroup([Id(2), Id(3)]), None))
id: Some((Id(0), Value(ValueGroupId { signature: Some(Id(0)), equations: [Id(1)] }), None))
maybe: Some((Id(0), Data(TypeGroupId { signature: Some(Id(2)), declaration: Some(Id(3)), role: Some(Id(4)) }), None))
just: Some((Id(1), Constructor(Id(0)), None))
Expand Down

0 comments on commit 050c07d

Please sign in to comment.