Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SymbolDef::name method #154

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading