Improve names and documentation on scope API (#5095)

## Summary

Just minor improvements to improve consistency of method names and
availability.
This commit is contained in:
Charlie Marsh 2023-06-14 14:28:55 -04:00 committed by GitHub
parent 86ff1febea
commit 65dbfd2556
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 7 deletions

View file

@ -90,24 +90,41 @@ impl<'a> Scope<'a> {
self.bindings.contains_key(name)
}
/// Returns the ids of all bindings defined in this scope.
/// Returns the IDs of all bindings defined in this scope.
pub fn binding_ids(&self) -> impl Iterator<Item = BindingId> + '_ {
self.bindings.values().copied()
}
/// Returns a tuple of the name and id of all bindings defined in this scope.
/// Returns a tuple of the name and ID of all bindings defined in this scope.
pub fn bindings(&self) -> impl Iterator<Item = (&str, BindingId)> + '_ {
self.bindings.iter().map(|(&name, &id)| (name, id))
}
/// Returns an iterator over all [bindings](BindingId) bound to the given name, including
/// Like [`Scope::get`], but returns all bindings with the given name, including
/// those that were shadowed by later bindings.
pub fn bindings_for_name(&self, name: &str) -> impl Iterator<Item = BindingId> + '_ {
pub fn get_all(&self, name: &str) -> impl Iterator<Item = BindingId> + '_ {
std::iter::successors(self.bindings.get(name).copied(), |id| {
self.shadowed_bindings.get(id).copied()
})
}
/// Like [`Scope::binding_ids`], but returns all bindings that were added to the scope,
/// including those that were shadowed by later bindings.
pub fn all_binding_ids(&self) -> impl Iterator<Item = BindingId> + '_ {
self.bindings.values().copied().flat_map(|id| {
std::iter::successors(Some(id), |id| self.shadowed_bindings.get(id).copied())
})
}
/// Like [`Scope::bindings`], but returns all bindings added to the scope, including those that
/// were shadowed by later bindings.
pub fn all_bindings(&self) -> impl Iterator<Item = (&str, BindingId)> + '_ {
self.bindings.iter().flat_map(|(&name, &id)| {
std::iter::successors(Some(id), |id| self.shadowed_bindings.get(id).copied())
.map(move |id| (name, id))
})
}
/// Adds a reference to a star import (e.g., `from sys import *`) to this scope.
pub fn add_star_import(&mut self, import: StarImportation<'a>) {
self.star_imports.push(import);