mirror of https://github.com/astral-sh/ruff
Remove Statements#depth
This commit is contained in:
parent
9328606843
commit
965243d98e
|
|
@ -1,4 +1,3 @@
|
||||||
use std::cmp::Ordering;
|
|
||||||
use std::iter;
|
use std::iter;
|
||||||
|
|
||||||
use ruff_python_ast::{self as ast, ExceptHandler, Stmt};
|
use ruff_python_ast::{self as ast, ExceptHandler, Stmt};
|
||||||
|
|
@ -12,32 +11,19 @@ fn common_ancestor(
|
||||||
stop: Option<StatementId>,
|
stop: Option<StatementId>,
|
||||||
node_tree: &Statements,
|
node_tree: &Statements,
|
||||||
) -> Option<StatementId> {
|
) -> Option<StatementId> {
|
||||||
if stop.is_some_and(|stop| left == stop || right == stop) {
|
// Fast path: if the nodes are the same, they are their own common ancestor.
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
if left == right {
|
if left == right {
|
||||||
return Some(left);
|
return Some(left);
|
||||||
}
|
}
|
||||||
|
|
||||||
let left_depth = node_tree.depth(left);
|
// Grab all the ancestors of `right`.
|
||||||
let right_depth = node_tree.depth(right);
|
let candidates = node_tree.ancestor_ids(right).collect::<Vec<_>>();
|
||||||
|
|
||||||
match left_depth.cmp(&right_depth) {
|
// Find the first ancestor of `left` that is also an ancestor of `right`.
|
||||||
Ordering::Less => {
|
node_tree
|
||||||
let right = node_tree.parent_id(right)?;
|
.ancestor_ids(left)
|
||||||
common_ancestor(left, right, stop, node_tree)
|
.take_while(|id| stop != Some(*id))
|
||||||
}
|
.find(|id| candidates.contains(id))
|
||||||
Ordering::Equal => {
|
|
||||||
let left = node_tree.parent_id(left)?;
|
|
||||||
let right = node_tree.parent_id(right)?;
|
|
||||||
common_ancestor(left, right, stop, node_tree)
|
|
||||||
}
|
|
||||||
Ordering::Greater => {
|
|
||||||
let left = node_tree.parent_id(left)?;
|
|
||||||
common_ancestor(left, right, stop, node_tree)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the alternative branches for a given node.
|
/// Return the alternative branches for a given node.
|
||||||
|
|
|
||||||
|
|
@ -22,8 +22,6 @@ struct StatementWithParent<'a> {
|
||||||
statement: &'a Stmt,
|
statement: &'a Stmt,
|
||||||
/// The ID of the parent of this node, if any.
|
/// The ID of the parent of this node, if any.
|
||||||
parent: Option<StatementId>,
|
parent: Option<StatementId>,
|
||||||
/// The depth of this node in the tree.
|
|
||||||
depth: u32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The statements of a program indexed by [`StatementId`]
|
/// The statements of a program indexed by [`StatementId`]
|
||||||
|
|
@ -46,11 +44,8 @@ impl<'a> Statements<'a> {
|
||||||
if let Some(existing_id) = self.statement_to_id.insert(RefEquality(statement), next_id) {
|
if let Some(existing_id) = self.statement_to_id.insert(RefEquality(statement), next_id) {
|
||||||
panic!("Statements already exists with ID: {existing_id:?}");
|
panic!("Statements already exists with ID: {existing_id:?}");
|
||||||
}
|
}
|
||||||
self.statements.push(StatementWithParent {
|
self.statements
|
||||||
statement,
|
.push(StatementWithParent { statement, parent })
|
||||||
parent,
|
|
||||||
depth: parent.map_or(0, |parent| self.statements[parent].depth + 1),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the [`StatementId`] of the given statement.
|
/// Returns the [`StatementId`] of the given statement.
|
||||||
|
|
@ -65,12 +60,6 @@ impl<'a> Statements<'a> {
|
||||||
self.statements[statement_id].parent
|
self.statements[statement_id].parent
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the depth of the statement.
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn depth(&self, id: StatementId) -> u32 {
|
|
||||||
self.statements[id].depth
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns an iterator over all [`StatementId`] ancestors, starting from the given [`StatementId`].
|
/// Returns an iterator over all [`StatementId`] ancestors, starting from the given [`StatementId`].
|
||||||
pub(crate) fn ancestor_ids(&self, id: StatementId) -> impl Iterator<Item = StatementId> + '_ {
|
pub(crate) fn ancestor_ids(&self, id: StatementId) -> impl Iterator<Item = StatementId> + '_ {
|
||||||
std::iter::successors(Some(id), |&id| self.statements[id].parent)
|
std::iter::successors(Some(id), |&id| self.statements[id].parent)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue