Move import-name matching into methods on BindingKind (#4818)

This commit is contained in:
Charlie Marsh 2023-06-03 15:01:27 -04:00 committed by GitHub
parent 2c41c54e0c
commit 935094c2ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 96 additions and 134 deletions

View file

@ -101,6 +101,35 @@ impl<'a> Binding<'a> {
)
}
/// Returns the fully-qualified symbol name, if this symbol was imported from another module.
pub fn qualified_name(&self) -> Option<&str> {
match &self.kind {
BindingKind::Importation(Importation { full_name }) => Some(full_name),
BindingKind::FromImportation(FromImportation { full_name }) => Some(full_name),
BindingKind::SubmoduleImportation(SubmoduleImportation { full_name }) => {
Some(full_name)
}
_ => None,
}
}
/// Returns the fully-qualified name of the module from which this symbol was imported, if this
/// symbol was imported from another module.
pub fn module_name(&self) -> Option<&str> {
match &self.kind {
BindingKind::Importation(Importation { full_name })
| BindingKind::SubmoduleImportation(SubmoduleImportation { full_name }) => {
Some(full_name.split('.').next().unwrap_or(full_name))
}
BindingKind::FromImportation(FromImportation { full_name }) => Some(
full_name
.rsplit_once('.')
.map_or(full_name, |(module, _)| module),
),
_ => None,
}
}
/// Returns the appropriate visual range for highlighting this binding.
pub fn trimmed_range(&self, semantic_model: &SemanticModel, locator: &Locator) -> TextRange {
match self.kind {