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 {

View file

@ -256,14 +256,8 @@ impl<'a> SemanticModel<'a> {
// import pyarrow.csv
// print(pa.csv.read_csv("test.csv"))
// ```
let full_name = match &self.bindings[binding_id].kind {
BindingKind::Importation(Importation { full_name }) => *full_name,
BindingKind::SubmoduleImportation(SubmoduleImportation { full_name }) => *full_name,
BindingKind::FromImportation(FromImportation { full_name }) => full_name.as_str(),
_ => return None,
};
let has_alias = full_name
let qualified_name = self.bindings[binding_id].qualified_name()?;
let has_alias = qualified_name
.split('.')
.last()
.map(|segment| segment != symbol)
@ -272,7 +266,7 @@ impl<'a> SemanticModel<'a> {
return None;
}
self.scopes[scope_id].get(full_name)
self.scopes[scope_id].get(qualified_name)
}
/// Resolves the [`Expr`] to a fully-qualified symbol-name, if `value` resolves to an imported