Make the statement vector private on SemanticModel (#6348)

## Summary

Instead, expose these as methods, now that we can use a reasonable
nomenclature on the API.
This commit is contained in:
Charlie Marsh 2023-08-07 11:02:14 -04:00 committed by GitHub
parent bae87fa016
commit 61d3977f95
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 55 additions and 25 deletions

View file

@ -185,7 +185,7 @@ impl<'a> Binding<'a> {
/// Returns the range of the binding's parent.
pub fn parent_range(&self, semantic: &SemanticModel) -> Option<TextRange> {
self.source
.map(|node_id| semantic.statements[node_id])
.map(|statement_id| semantic.statement(statement_id))
.and_then(|parent| {
if parent.is_import_from_stmt() {
Some(parent.range())

View file

@ -31,7 +31,7 @@ pub struct SemanticModel<'a> {
module_path: Option<&'a [String]>,
/// Stack of all visited statements.
pub statements: Nodes<'a, Stmt>,
statements: Nodes<'a, Stmt>,
/// The identifier of the current statement.
statement_id: Option<NodeId>,
@ -919,6 +919,29 @@ impl<'a> SemanticModel<'a> {
None
}
/// Return the [`Nodes`] vector of all statements.
pub const fn statements(&self) -> &Nodes<'a, Stmt> {
&self.statements
}
/// Return the [`NodeId`] corresponding to the given [`Stmt`].
#[inline]
pub fn statement_id(&self, statement: &Stmt) -> Option<NodeId> {
self.statements.node_id(statement)
}
/// Return the [`Stmt]` corresponding to the given [`NodeId`].
#[inline]
pub fn statement(&self, statement_id: NodeId) -> &'a Stmt {
self.statements[statement_id]
}
/// Given a [`Stmt`], return its parent, if any.
#[inline]
pub fn parent_statement(&self, statement: &'a Stmt) -> Option<&'a Stmt> {
self.statements.parent(statement)
}
/// Set the [`Globals`] for the current [`Scope`].
pub fn set_globals(&mut self, globals: Globals<'a>) {
// If any global bindings don't already exist in the global scope, add them.