Implement character index for point (#23989)

Fixes #22939
Fixes #23970
Supersedes https://github.com/zed-industries/zed/pull/23469

Release Notes:

- Fixed a bug where Zed could crash with certain input sources on macOS

---------

Co-authored-by: Louis Brunner <louis.brunner.fr@gmail.com>
Co-authored-by: ben <ben@zed.dev>
This commit is contained in:
Mikayla Maki 2025-02-04 12:15:43 -08:00 committed by GitHub
parent 7da60995cc
commit cfe0932c0a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 221 additions and 129 deletions

View file

@ -217,6 +217,19 @@ impl Point<Pixels> {
}
}
impl<T> Point<T>
where
T: Sub<T, Output = T> + Debug + Clone + Default,
{
/// Get the position of this point, relative to the given origin
pub fn relative_to(&self, origin: &Point<T>) -> Point<T> {
point(
self.x.clone() - origin.x.clone(),
self.y.clone() - origin.y.clone(),
)
}
}
impl<T, Rhs> Mul<Rhs> for Point<T>
where
T: Mul<Rhs, Output = T> + Clone + Default + Debug,
@ -376,6 +389,13 @@ pub struct Size<T: Clone + Default + Debug> {
pub height: T,
}
impl<T: Clone + Default + Debug> Size<T> {
/// Create a new Size, a synonym for [`size`]
pub fn new(width: T, height: T) -> Self {
size(width, height)
}
}
/// Constructs a new `Size<T>` with the provided width and height.
///
/// # Arguments
@ -1456,6 +1476,17 @@ where
}
}
impl<T> Bounds<T>
where
T: Add<T, Output = T> + PartialOrd + Clone + Default + Debug + Sub<T, Output = T>,
{
/// Convert a point to the coordinate space defined by this Bounds
pub fn localize(&self, point: &Point<T>) -> Option<Point<T>> {
self.contains(point)
.then(|| point.relative_to(&self.origin))
}
}
/// Checks if the bounds represent an empty area.
///
/// # Returns