Move Ranged into ruff_text_size (#6919)

## Summary

The motivation here is that this enables us to implement `Ranged` in
crates that don't depend on `ruff_python_ast`.

Largely a mechanical refactor with a lot of regex, Clippy help, and
manual fixups.

## Test Plan

`cargo test`
This commit is contained in:
Charlie Marsh 2023-08-27 14:12:51 -04:00 committed by GitHub
parent 88c8bece38
commit fc89976c24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
468 changed files with 940 additions and 706 deletions

View file

@ -1,7 +1,7 @@
use ruff_text_size::TextRange;
use ruff_text_size::{Ranged, TextRange};
use crate::node::AnyNodeRef;
use crate::{self as ast, Expr, Ranged};
use crate::{self as ast, Expr};
/// Unowned pendant to [`ast::Expr`] that stores a reference instead of a owned value.
#[derive(Copy, Clone, Debug, PartialEq)]

View file

@ -4,13 +4,12 @@ use std::path::Path;
use num_traits::Zero;
use smallvec::SmallVec;
use ruff_text_size::TextRange;
use ruff_text_size::{Ranged, TextRange};
use crate::call_path::CallPath;
use crate::statement_visitor::{walk_body, walk_stmt, StatementVisitor};
use crate::{
self as ast, Arguments, Constant, ExceptHandler, Expr, MatchCase, Pattern, Ranged, Stmt,
TypeParam,
self as ast, Arguments, Constant, ExceptHandler, Expr, MatchCase, Pattern, Stmt, TypeParam,
};
/// Return `true` if the `Stmt` is a compound statement (as opposed to a simple statement).

View file

@ -10,8 +10,8 @@
//!
//! This module can be used to identify the [`TextRange`] of the `except` token.
use crate::{self as ast, Alias, ExceptHandler, Parameter, ParameterWithDefault, Ranged, Stmt};
use ruff_text_size::{TextLen, TextRange, TextSize};
use crate::{self as ast, Alias, ExceptHandler, Parameter, ParameterWithDefault, Stmt};
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
use ruff_python_trivia::{is_python_whitespace, Cursor};

View file

@ -1,6 +1,8 @@
use ruff_text_size::{TextRange, TextSize};
use std::path::Path;
pub use expression::*;
pub use nodes::*;
pub mod all;
pub mod call_path;
pub mod comparable;
@ -22,36 +24,6 @@ pub mod types;
pub mod visitor;
pub mod whitespace;
pub use expression::*;
pub use nodes::*;
pub trait Ranged {
fn range(&self) -> TextRange;
fn start(&self) -> TextSize {
self.range().start()
}
fn end(&self) -> TextSize {
self.range().end()
}
}
impl Ranged for TextRange {
fn range(&self) -> TextRange {
*self
}
}
impl<T> Ranged for &T
where
T: Ranged,
{
fn range(&self) -> TextRange {
T::range(self)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum PySourceType {

View file

@ -2,10 +2,10 @@ use crate::visitor::preorder::PreorderVisitor;
use crate::{
self as ast, Alias, Arguments, Comprehension, Decorator, ExceptHandler, Expr, Keyword,
MatchCase, Mod, Parameter, ParameterWithDefault, Parameters, Pattern, PatternArguments,
PatternKeyword, Ranged, Stmt, TypeParam, TypeParamParamSpec, TypeParamTypeVar,
TypeParamTypeVarTuple, TypeParams, WithItem,
PatternKeyword, Stmt, TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple,
TypeParams, WithItem,
};
use ruff_text_size::TextRange;
use ruff_text_size::{Ranged, TextRange};
use std::ptr::NonNull;
pub trait AstNode: Ranged {

View file

@ -1,12 +1,13 @@
#![allow(clippy::derive_partial_eq_without_eq)]
use crate::Ranged;
use num_bigint::BigInt;
use ruff_text_size::{TextRange, TextSize};
use std::fmt;
use std::fmt::Debug;
use std::ops::Deref;
use num_bigint::BigInt;
use ruff_text_size::{Ranged, TextRange, TextSize};
/// See also [mod](https://docs.python.org/3/library/ast.html#ast.mod)
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum Mod {
@ -3087,9 +3088,10 @@ impl Ranged for crate::nodes::ParameterWithDefault {
#[cfg(target_pointer_width = "64")]
mod size_assertions {
use static_assertions::assert_eq_size;
#[allow(clippy::wildcard_imports)]
use super::*;
use static_assertions::assert_eq_size;
assert_eq_size!(Stmt, [u8; 144]);
assert_eq_size!(StmtFunctionDef, [u8; 144]);

View file

@ -1,8 +1,8 @@
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{TextRange, TextSize};
use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::node::AnyNodeRef;
use crate::{ExpressionRef, Ranged};
use crate::ExpressionRef;
/// Returns the [`TextRange`] of a given expression including parentheses, if the expression is
/// parenthesized; or `None`, if the expression is not parenthesized.

View file

@ -1,6 +1,6 @@
use crate::{ElifElseClause, Expr, Ranged, Stmt, StmtIf};
use crate::{ElifElseClause, Expr, Stmt, StmtIf};
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::TextRange;
use ruff_text_size::{Ranged, TextRange};
use std::iter;
/// Return the `Range` of the first `Elif` or `Else` token in an `If` statement.

View file

@ -1,8 +1,8 @@
use ruff_python_trivia::{indentation_at_offset, is_python_whitespace, PythonWhitespace};
use ruff_source_file::{newlines::UniversalNewlineIterator, Locator};
use ruff_text_size::{TextRange, TextSize};
use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::{Ranged, Stmt};
use crate::Stmt;
/// Extract the leading indentation from a line.
#[inline]