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

@ -28,7 +28,7 @@ mod schemars_impls;
#[cfg(feature = "serde")]
mod serde_impls;
pub use crate::{range::TextRange, size::TextSize, traits::TextLen};
pub use crate::{range::TextRange, size::TextSize, traits::Ranged, traits::TextLen};
#[cfg(target_pointer_width = "16")]
compile_error!("text-size assumes usize >= u32 and does not work on 16-bit targets");

View file

@ -352,7 +352,7 @@ impl TextRange {
/// ## Examples
///
/// ```
/// use ruff_text_size::{TextRange, TextSize};
/// use ruff_text_size::{Ranged, TextRange, TextSize};
///
/// let range = TextRange::new(TextSize::from(5), TextSize::from(10));
/// assert_eq!(range.sub_start(TextSize::from(2)), TextRange::new(TextSize::from(3), TextSize::from(10)));
@ -371,7 +371,7 @@ impl TextRange {
/// ## Examples
///
/// ```
/// use ruff_text_size::{TextRange, TextSize};
/// use ruff_text_size::{Ranged, TextRange, TextSize};
///
/// let range = TextRange::new(TextSize::from(5), TextSize::from(10));
/// assert_eq!(range.add_start(TextSize::from(3)), TextRange::new(TextSize::from(8), TextSize::from(10)));
@ -391,7 +391,7 @@ impl TextRange {
/// ## Examples
///
/// ```
/// use ruff_text_size::{TextRange, TextSize};
/// use ruff_text_size::{Ranged, TextRange, TextSize};
///
/// let range = TextRange::new(TextSize::from(5), TextSize::from(10));
/// assert_eq!(range.sub_end(TextSize::from(2)), TextRange::new(TextSize::from(5), TextSize::from(8)));
@ -411,7 +411,7 @@ impl TextRange {
/// ## Examples
///
/// ```
/// use ruff_text_size::{TextRange, TextSize};
/// use ruff_text_size::{Ranged, TextRange, TextSize};
///
/// let range = TextRange::new(TextSize::from(5), TextSize::from(10));
/// assert_eq!(range.add_end(TextSize::from(2)), TextRange::new(TextSize::from(5), TextSize::from(12)));

View file

@ -1,4 +1,4 @@
use {crate::TextSize, std::convert::TryInto};
use {crate::TextRange, crate::TextSize, std::convert::TryInto};
use priv_in_pub::Sealed;
mod priv_in_pub {
@ -35,3 +35,34 @@ impl TextLen for char {
(self.len_utf8() as u32).into()
}
}
/// A ranged item in the source text.
pub trait Ranged {
/// The range of this item in the source text.
fn range(&self) -> TextRange;
/// The start offset of this item in the source text.
fn start(&self) -> TextSize {
self.range().start()
}
/// The end offset of this item in the source text.
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)
}
}