Skip to content

Commit

Permalink
Add SymbolDef::name method (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkaput authored Jan 13, 2025
1 parent bb6550f commit 2d6ee1d
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/lang/inspect/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ impl SymbolDef {
span.end = span.end.add_width(width);
Some((file_id, span))
}

/// Gets the name of the symbol.
#[expect(unused)]
pub fn name(&self, db: &AnalysisDatabase) -> SmolStr {
match self {
SymbolDef::Item(it) => it.name(db),
SymbolDef::Variable(it) => it.name(),
SymbolDef::ExprInlineMacro(name) => name.clone(),
SymbolDef::Member(it) => it.name(db),
SymbolDef::Module(it) => it.name(db),
}
}
}

/// Information about the definition of an item (function, trait, impl, module, etc.).
Expand Down Expand Up @@ -200,6 +212,16 @@ impl ItemDef {
LookupItemId::ImplItem(item) => item.impl_def_id(defs_db).full_path(defs_db),
}
}

/// Gets the name of the item.
pub fn name(&self, db: &AnalysisDatabase) -> SmolStr {
let defs_db = db.upcast();
match self.lookup_item_id {
LookupItemId::ModuleItem(item) => item.name(defs_db),
LookupItemId::TraitItem(item) => item.name(defs_db),
LookupItemId::ImplItem(item) => item.name(defs_db),
}
}
}

/// Information about the definition of a variable (local, function parameter).
Expand Down Expand Up @@ -325,6 +347,11 @@ impl VariableDef {

format!("{prefix}{mutability}{name}: {ty}")
}

/// Gets this variable's name.
pub fn name(&self) -> SmolStr {
self.name.clone()
}
}

/// Information about a struct member.
Expand Down Expand Up @@ -354,6 +381,11 @@ impl MemberDef {
pub fn structure(&self) -> &ItemDef {
&self.structure
}

/// Gets member's name.
pub fn name(&self, db: &AnalysisDatabase) -> SmolStr {
self.member_id.name(db)
}
}

/// Information about the definition of a module.
Expand Down Expand Up @@ -402,6 +434,11 @@ impl ModuleDef {

db.get_item_documentation(doc_id)
}

/// Gets the name of the module.
pub fn name(&self, db: &AnalysisDatabase) -> SmolStr {
self.id.name(db)
}
}

/// Either [`ResolvedGenericItem`], [`ResolvedConcreteItem`] or [`MemberId`].
Expand Down

0 comments on commit 2d6ee1d

Please sign in to comment.