Make some of ruff_python_semantic pub(crate) (#5093)

This commit is contained in:
Charlie Marsh 2023-06-14 13:49:37 -04:00 committed by GitHub
parent 916f0889f8
commit c992cfa76e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 22 deletions

View file

@ -115,7 +115,7 @@ impl<'a> Definitions<'a> {
/// ///
/// Members are assumed to be pushed in traversal order, such that parents are pushed before /// Members are assumed to be pushed in traversal order, such that parents are pushed before
/// their children. /// their children.
pub fn push_member(&mut self, member: Member<'a>) -> DefinitionId { pub(crate) fn push_member(&mut self, member: Member<'a>) -> DefinitionId {
self.0.push(Definition::Member(member)) self.0.push(Definition::Member(member))
} }

View file

@ -49,11 +49,11 @@ impl<'a> Globals<'a> {
builder.finish() builder.finish()
} }
pub fn get(&self, name: &str) -> Option<&TextRange> { pub(crate) fn get(&self, name: &str) -> Option<&TextRange> {
self.0.get(name) self.0.get(name)
} }
pub fn iter(&self) -> impl Iterator<Item = (&&'a str, &TextRange)> + '_ { pub(crate) fn iter(&self) -> impl Iterator<Item = (&&'a str, &TextRange)> + '_ {
self.0.iter() self.0.iter()
} }
} }

View file

@ -37,7 +37,7 @@ impl<'a> Nodes<'a> {
/// Inserts a new node into the node tree and returns its unique id. /// Inserts a new node into the node tree and returns its unique id.
/// ///
/// Panics if a node with the same pointer already exists. /// Panics if a node with the same pointer already exists.
pub fn insert(&mut self, stmt: &'a Stmt, parent: Option<NodeId>) -> NodeId { pub(crate) fn insert(&mut self, stmt: &'a Stmt, parent: Option<NodeId>) -> NodeId {
let next_id = self.nodes.next_index(); let next_id = self.nodes.next_index();
if let Some(existing_id) = self.node_to_id.insert(RefEquality(stmt), next_id) { if let Some(existing_id) = self.node_to_id.insert(RefEquality(stmt), next_id) {
panic!("Node already exists with id {existing_id:?}"); panic!("Node already exists with id {existing_id:?}");
@ -61,23 +61,23 @@ impl<'a> Nodes<'a> {
self.nodes[node_id].parent self.nodes[node_id].parent
} }
/// Return the depth of the node.
#[inline]
pub fn depth(&self, node_id: NodeId) -> u32 {
self.nodes[node_id].depth
}
/// Returns an iterator over all [`NodeId`] ancestors, starting from the given [`NodeId`].
pub fn ancestor_ids(&self, node_id: NodeId) -> impl Iterator<Item = NodeId> + '_ {
std::iter::successors(Some(node_id), |&node_id| self.nodes[node_id].parent)
}
/// Return the parent of the given node. /// Return the parent of the given node.
pub fn parent(&self, node: &'a Stmt) -> Option<&'a Stmt> { pub fn parent(&self, node: &'a Stmt) -> Option<&'a Stmt> {
let node_id = self.node_to_id.get(&RefEquality(node))?; let node_id = self.node_to_id.get(&RefEquality(node))?;
let parent_id = self.nodes[*node_id].parent?; let parent_id = self.nodes[*node_id].parent?;
Some(self[parent_id]) Some(self[parent_id])
} }
/// Return the depth of the node.
#[inline]
pub(crate) fn depth(&self, node_id: NodeId) -> u32 {
self.nodes[node_id].depth
}
/// Returns an iterator over all [`NodeId`] ancestors, starting from the given [`NodeId`].
pub(crate) fn ancestor_ids(&self, node_id: NodeId) -> impl Iterator<Item = NodeId> + '_ {
std::iter::successors(Some(node_id), |&node_id| self.nodes[node_id].parent)
}
} }
impl<'a> Index<NodeId> for Nodes<'a> { impl<'a> Index<NodeId> for Nodes<'a> {

View file

@ -36,11 +36,11 @@ pub struct ReferenceId;
/// The references of a program indexed by [`ReferenceId`]. /// The references of a program indexed by [`ReferenceId`].
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct References(IndexVec<ReferenceId, Reference>); pub(crate) struct References(IndexVec<ReferenceId, Reference>);
impl References { impl References {
/// Pushes a new [`Reference`] and returns its [`ReferenceId`]. /// Pushes a new [`Reference`] and returns its [`ReferenceId`].
pub fn push( pub(crate) fn push(
&mut self, &mut self,
scope_id: ScopeId, scope_id: ScopeId,
range: TextRange, range: TextRange,

View file

@ -124,12 +124,12 @@ impl<'a> Scope<'a> {
} }
/// Set the globals pointer for this scope. /// Set the globals pointer for this scope.
pub fn set_globals_id(&mut self, globals: GlobalsId) { pub(crate) fn set_globals_id(&mut self, globals: GlobalsId) {
self.globals_id = Some(globals); self.globals_id = Some(globals);
} }
/// Returns the globals pointer for this scope. /// Returns the globals pointer for this scope.
pub fn globals_id(&self) -> Option<GlobalsId> { pub(crate) fn globals_id(&self) -> Option<GlobalsId> {
self.globals_id self.globals_id
} }
@ -196,17 +196,17 @@ pub struct Scopes<'a>(IndexVec<ScopeId, Scope<'a>>);
impl<'a> Scopes<'a> { impl<'a> Scopes<'a> {
/// Returns a reference to the global scope /// Returns a reference to the global scope
pub fn global(&self) -> &Scope<'a> { pub(crate) fn global(&self) -> &Scope<'a> {
&self[ScopeId::global()] &self[ScopeId::global()]
} }
/// Returns a mutable reference to the global scope /// Returns a mutable reference to the global scope
pub fn global_mut(&mut self) -> &mut Scope<'a> { pub(crate) fn global_mut(&mut self) -> &mut Scope<'a> {
&mut self[ScopeId::global()] &mut self[ScopeId::global()]
} }
/// Pushes a new scope and returns its unique id /// Pushes a new scope and returns its unique id
pub fn push_scope(&mut self, kind: ScopeKind<'a>, parent: ScopeId) -> ScopeId { pub(crate) fn push_scope(&mut self, kind: ScopeKind<'a>, parent: ScopeId) -> ScopeId {
let next_id = ScopeId::new(self.0.len()); let next_id = ScopeId::new(self.0.len());
self.0.push(Scope::local(kind, parent)); self.0.push(Scope::local(kind, parent));
next_id next_id