Compare commits

...

4 Commits

Author SHA1 Message Date
Piotr Osiewicz
f5da886564 WIP
Co-authored-by: Bennet <bennetbo@gmx.de>
2024-04-10 13:32:46 +02:00
Conrad Irwin
3a6887db53 WIP
Co-Authored-By: Bennet <bennetbo@gmx.de>
2024-04-09 12:33:35 -06:00
Piotr Osiewicz
b19ad92a1e Another WIP 2024-04-09 19:28:58 +02:00
Piotr Osiewicz
2a47df9d3b wip: coordinates 2024-04-09 13:22:11 +02:00
8 changed files with 486 additions and 248 deletions

View File

@@ -157,7 +157,7 @@ impl DisplayMap {
);
}
pub fn fold<T: ToOffset>(
pub fn fold<T: ToOffset<MultiBuffer>>(
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
cx: &mut ModelContext<Self>,
@@ -180,7 +180,7 @@ impl DisplayMap {
self.block_map.read(snapshot, edits);
}
pub fn unfold<T: ToOffset>(
pub fn unfold<T: ToOffset<MultiBuffer>>(
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
inclusive: bool,
@@ -693,7 +693,10 @@ impl DisplaySnapshot {
})
}
pub fn buffer_chars_at(&self, mut offset: usize) -> impl Iterator<Item = (char, usize)> + '_ {
pub fn buffer_chars_at(
&self,
mut offset: Offset<MultiBuffer>,
) -> impl Iterator<Item = (char, usize)> + '_ {
self.buffer_snapshot.chars_at(offset).map(move |ch| {
let ret = (ch, offset);
offset += ch.len_utf8();
@@ -703,7 +706,7 @@ impl DisplaySnapshot {
pub fn reverse_buffer_chars_at(
&self,
mut offset: usize,
mut offset: Offset<MultiBuffer>,
) -> impl Iterator<Item = (char, usize)> + '_ {
self.buffer_snapshot
.reversed_chars_at(offset)
@@ -732,7 +735,7 @@ impl DisplaySnapshot {
pub fn folds_in_range<T>(&self, range: Range<T>) -> impl Iterator<Item = &Fold>
where
T: ToOffset,
T: ToOffset<MultiBuffer>,
{
self.fold_snapshot.folds_in_range(range)
}
@@ -744,7 +747,7 @@ impl DisplaySnapshot {
self.block_snapshot.blocks_in_range(rows)
}
pub fn intersects_fold<T: ToOffset>(&self, offset: T) -> bool {
pub fn intersects_fold<T: ToOffset<MultiBuffer>>(&self, offset: T) -> bool {
self.fold_snapshot.intersects_fold(offset)
}

View File

@@ -4,7 +4,7 @@ use super::{
};
use gpui::{ElementId, HighlightStyle, Hsla};
use language::{Chunk, Edit, Point, TextSummary};
use multi_buffer::{Anchor, AnchorRangeExt, MultiBufferSnapshot, ToOffset};
use multi_buffer::{Anchor, AnchorRangeExt, MultiBuffer, MultiBufferSnapshot, ToOffset};
use std::{
any::TypeId,
cmp::{self, Ordering},
@@ -74,7 +74,7 @@ impl<'a> sum_tree::Dimension<'a, TransformSummary> for FoldPoint {
pub(crate) struct FoldMapWriter<'a>(&'a mut FoldMap);
impl<'a> FoldMapWriter<'a> {
pub(crate) fn fold<T: ToOffset>(
pub(crate) fn fold<T: ToOffset<MultiBuffer>>(
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
) -> (FoldSnapshot, Vec<FoldEdit>) {
@@ -129,7 +129,7 @@ impl<'a> FoldMapWriter<'a> {
(self.0.snapshot.clone(), edits)
}
pub(crate) fn unfold<T: ToOffset>(
pub(crate) fn unfold<T: ToOffset<MultiBuffer>>(
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
inclusive: bool,
@@ -609,7 +609,7 @@ impl FoldSnapshot {
pub fn folds_in_range<T>(&self, range: Range<T>) -> impl Iterator<Item = &Fold>
where
T: ToOffset,
T: ToOffset<MultiBuffer>,
{
let mut folds = intersecting_folds(&self.inlay_snapshot, &self.folds, range, false);
iter::from_fn(move || {
@@ -621,7 +621,7 @@ impl FoldSnapshot {
pub fn intersects_fold<T>(&self, offset: T) -> bool
where
T: ToOffset,
T: ToOffset<MultiBuffer>,
{
let buffer_offset = offset.to_offset(&self.inlay_snapshot.buffer);
let inlay_offset = self.inlay_snapshot.to_inlay_offset(buffer_offset);
@@ -741,7 +741,7 @@ fn intersecting_folds<'a, T>(
inclusive: bool,
) -> FilterCursor<'a, impl 'a + FnMut(&FoldSummary) -> bool, Fold, usize>
where
T: ToOffset,
T: ToOffset<MultiBuffer>,
{
let buffer = &inlay_snapshot.buffer;
let start = buffer.anchor_before(range.start.to_offset(buffer));

View File

@@ -1670,11 +1670,15 @@ impl Editor {
}
}
pub fn language_at<T: ToOffset>(&self, point: T, cx: &AppContext) -> Option<Arc<Language>> {
pub fn language_at<T: ToOffset<MultiBuffer>>(
&self,
point: T,
cx: &AppContext,
) -> Option<Arc<Language>> {
self.buffer.read(cx).language_at(point, cx)
}
pub fn file_at<T: ToOffset>(
pub fn file_at<T: ToOffset<MultiBuffer>>(
&self,
point: T,
cx: &AppContext,
@@ -1963,7 +1967,7 @@ impl Editor {
pub fn edit<I, S, T>(&mut self, edits: I, cx: &mut ViewContext<Self>)
where
I: IntoIterator<Item = (Range<S>, T)>,
S: ToOffset,
S: ToOffset<MultiBuffer>,
T: Into<Arc<str>>,
{
if self.read_only(cx) {
@@ -1977,7 +1981,7 @@ impl Editor {
pub fn edit_with_autoindent<I, S, T>(&mut self, edits: I, cx: &mut ViewContext<Self>)
where
I: IntoIterator<Item = (Range<S>, T)>,
S: ToOffset,
S: ToOffset<MultiBuffer>,
T: Into<Arc<str>>,
{
if self.read_only(cx) {
@@ -1996,7 +2000,7 @@ impl Editor {
cx: &mut ViewContext<Self>,
) where
I: IntoIterator<Item = (Range<S>, T)>,
S: ToOffset,
S: ToOffset<MultiBuffer>,
T: Into<Arc<str>>,
{
if self.read_only(cx) {
@@ -3067,7 +3071,7 @@ impl Editor {
/// Iterate the given selections, and for each one, find the smallest surrounding
/// autoclose region. This uses the ordering of the selections and the autoclose
/// regions to avoid repeated comparisons.
fn selections_with_autoclose_regions<'a, D: ToOffset + Clone>(
fn selections_with_autoclose_regions<'a, D: ToOffset<MultiBuffer> + Clone>(
&'a self,
selections: impl IntoIterator<Item = Selection<D>>,
buffer: &'a MultiBufferSnapshot,
@@ -3122,7 +3126,10 @@ impl Editor {
});
}
fn completion_query(buffer: &MultiBufferSnapshot, position: impl ToOffset) -> Option<String> {
fn completion_query(
buffer: &MultiBufferSnapshot,
position: impl ToOffset<MultiBuffer>,
) -> Option<String> {
let offset = position.to_offset(buffer);
let (word_range, kind) = buffer.surrounding_word(offset);
if offset > word_range.start && kind == Some(CharKind::Word) {
@@ -8610,7 +8617,7 @@ impl Editor {
self.fold_ranges(ranges, true, cx);
}
pub fn fold_ranges<T: ToOffset + Clone>(
pub fn fold_ranges<T: ToOffset<MultiBuffer> + Clone>(
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
auto_scroll: bool,
@@ -8628,7 +8635,7 @@ impl Editor {
}
}
pub fn unfold_ranges<T: ToOffset + Clone>(
pub fn unfold_ranges<T: ToOffset<MultiBuffer> + Clone>(
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
inclusive: bool,
@@ -10019,7 +10026,7 @@ impl EditorSnapshot {
})
}
pub fn language_at<T: ToOffset>(&self, position: T) -> Option<&Arc<Language>> {
pub fn language_at<T: ToOffset<MultiBuffer>>(&self, position: T) -> Option<&Arc<Language>> {
self.display_snapshot.buffer_snapshot.language_at(position)
}
@@ -10482,7 +10489,7 @@ trait SelectionExt {
-> Range<u32>;
}
impl<T: ToPoint + ToOffset> SelectionExt for Selection<T> {
impl<T: ToPoint + ToOffset<MultiBuffer>> SelectionExt for Selection<T> {
fn point_range(&self, buffer: &MultiBufferSnapshot) -> Range<Point> {
let start = self.start.to_point(buffer);
let end = self.end.to_point(buffer);
@@ -10539,7 +10546,7 @@ impl<T: ToPoint + ToOffset> SelectionExt for Selection<T> {
impl<T: InvalidationRegion> InvalidationStack<T> {
fn invalidate<S>(&mut self, selections: &[Selection<S>], buffer: &MultiBufferSnapshot)
where
S: Clone + ToOffset,
S: Clone + ToOffset<MultiBuffer>,
{
while let Some(region) = self.last() {
let all_selections_inside_invalidation_ranges =
@@ -10780,7 +10787,7 @@ trait RangeToAnchorExt {
fn to_anchors(self, snapshot: &MultiBufferSnapshot) -> Range<Anchor>;
}
impl<T: ToOffset> RangeToAnchorExt for Range<T> {
impl<T: ToOffset<MultiBuffer>> RangeToAnchorExt for Range<T> {
fn to_anchors(self, snapshot: &MultiBufferSnapshot) -> Range<Anchor> {
snapshot.anchor_after(self.start)..snapshot.anchor_before(self.end)
}

View File

@@ -5,7 +5,7 @@ use super::{Bias, DisplayPoint, DisplaySnapshot, SelectionGoal, ToDisplayPoint};
use crate::{char_kind, scroll::ScrollAnchor, CharKind, EditorStyle, ToOffset, ToPoint};
use gpui::{px, Pixels, WindowTextSystem};
use language::Point;
use multi_buffer::MultiBufferSnapshot;
use multi_buffer::{MultiBufferSnapshot, Offset};
use serde::Deserialize;
use std::{ops::Range, sync::Arc};
@@ -472,8 +472,8 @@ pub fn find_boundary_exclusive(
/// the [`DisplaySnapshot`]. The offsets are relative to the start of a buffer.
pub fn chars_after(
map: &DisplaySnapshot,
mut offset: usize,
) -> impl Iterator<Item = (char, Range<usize>)> + '_ {
mut offset: Offset<multi_buffer::MultiBuffer>,
) -> impl Iterator<Item = (char, Range<Offset<multi_buffer::MultiBuffer>>)> + '_ {
map.buffer_snapshot.chars_at(offset).map(move |ch| {
let before = offset;
offset = offset + ch.len_utf8();
@@ -486,13 +486,13 @@ pub fn chars_after(
/// the [`DisplaySnapshot`]. The offsets are relative to the start of a buffer.
pub fn chars_before(
map: &DisplaySnapshot,
mut offset: usize,
) -> impl Iterator<Item = (char, Range<usize>)> + '_ {
mut offset: Offset<multi_buffer::MultiBuffer>,
) -> impl Iterator<Item = (char, Range<Offset<multi_buffer::MultiBuffer>>)> + '_ {
map.buffer_snapshot
.reversed_chars_at(offset)
.map(move |ch| {
let after = offset;
offset = offset - ch.len_utf8();
offset = Offset::new(offset.0 - ch.len_utf8());
(ch, offset..after)
})
}

View File

@@ -490,7 +490,13 @@ impl<'a> MutableSelectionsCollection<'a> {
pub fn insert_range<T>(&mut self, range: Range<T>)
where
T: 'a + ToOffset + ToPoint + TextDimension + Ord + Sub<T, Output = T> + std::marker::Copy,
T: 'a
+ ToOffset<multi_buffer::MultiBuffer>
+ ToPoint
+ TextDimension
+ Ord
+ Sub<T, Output = T>
+ std::marker::Copy,
{
let mut selections = self.all(self.cx);
let mut start = range.start.to_offset(&self.buffer());
@@ -513,7 +519,11 @@ impl<'a> MutableSelectionsCollection<'a> {
pub fn select<T>(&mut self, mut selections: Vec<Selection<T>>)
where
T: ToOffset + ToPoint + Ord + std::marker::Copy + std::fmt::Debug,
T: ToOffset<multi_buffer::MultiBuffer>
+ ToPoint
+ Ord
+ std::marker::Copy
+ std::fmt::Debug,
{
let buffer = self.buffer.read(self.cx).snapshot(self.cx);
selections.sort_unstable_by_key(|s| s.start);
@@ -562,7 +572,7 @@ impl<'a> MutableSelectionsCollection<'a> {
pub fn select_ranges<I, T>(&mut self, ranges: I)
where
I: IntoIterator<Item = Range<T>>,
T: ToOffset,
T: ToOffset<multi_buffer::MultiBuffer>,
{
let buffer = self.buffer.read(self.cx).snapshot(self.cx);
let ranges = ranges

View File

@@ -1,3 +1,5 @@
use crate::{offset::Offset, MultiBuffer};
use super::{ExcerptId, MultiBufferSnapshot, ToOffset, ToOffsetUtf16, ToPoint};
use language::{OffsetUtf16, Point, TextDimension};
use std::{
@@ -97,9 +99,10 @@ impl Anchor {
}
}
impl ToOffset for Anchor {
fn to_offset(&self, snapshot: &MultiBufferSnapshot) -> usize {
self.summary(snapshot)
impl ToOffset<MultiBuffer> for Anchor {
fn to_offset(&self, snapshot: &MultiBufferSnapshot) -> Offset<MultiBuffer> {
let offset = self.summary(snapshot);
Offset::new(offset)
}
}
@@ -117,7 +120,7 @@ impl ToPoint for Anchor {
pub trait AnchorRangeExt {
fn cmp(&self, b: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Ordering;
fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize>;
fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<Offset<MultiBuffer>>;
fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point>;
}
@@ -129,7 +132,7 @@ impl AnchorRangeExt for Range<Anchor> {
}
}
fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize> {
fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<Offset<MultiBuffer>> {
self.start.to_offset(content)..self.end.to_offset(content)
}
@@ -137,6 +140,3 @@ impl AnchorRangeExt for Range<Anchor> {
self.start.to_point(content)..self.end.to_point(content)
}
}
#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash, Ord, PartialOrd)]
pub struct Offset(pub usize);

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,155 @@
use std::ops::{Add, AddAssign, Sub, SubAssign};
use crate::{MultiBuffer, MultiBufferSnapshot};
#[derive(Hash)]
pub struct Offset<S>(pub usize, core::marker::PhantomData<S>);
impl<S> Copy for Offset<S> {}
impl<S> Default for Offset<S> {
fn default() -> Self {
Offset::new(0)
}
}
impl Sub for Offset<MultiBuffer> {
type Output = usize;
fn sub(self, rhs: Offset<MultiBuffer>) -> Self::Output {
self.0 - rhs.0
}
}
use rand::distributions::uniform::{SampleBorrow, SampleUniform, UniformInt, UniformSampler};
use rand::Rng;
pub struct OffsetUniform<S>(UniformInt<usize>, core::marker::PhantomData<S>);
impl<S> UniformSampler for OffsetUniform<S> {
type X = Offset<S>;
fn new<B1, B2>(low: B1, high: B2) -> Self
where
B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + Sized,
{
OffsetUniform(
UniformInt::new(low.borrow().0, high.borrow().0),
Default::default(),
)
}
fn new_inclusive<B1, B2>(low: B1, high: B2) -> Self
where
B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + Sized,
{
OffsetUniform(
UniformInt::new_inclusive(low.borrow().0, high.borrow().0),
Default::default(),
)
}
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X {
Offset::new(self.0.sample(rng))
}
}
impl<S> SampleUniform for Offset<S> {
type Sampler = OffsetUniform<S>;
}
impl Sub<usize> for Offset<MultiBuffer> {
type Output = Offset<MultiBuffer>;
fn sub(self, rhs: usize) -> Self::Output {
Offset(self.0 - rhs, Default::default())
}
}
impl SubAssign<usize> for Offset<MultiBuffer> {
fn sub_assign(&mut self, rhs: usize) {
self.0 -= rhs;
}
}
impl Add<usize> for Offset<MultiBuffer> {
type Output = Offset<MultiBuffer>;
fn add(self, rhs: usize) -> Self::Output {
Offset(self.0 + rhs, Default::default())
}
}
impl AddAssign<usize> for Offset<MultiBuffer> {
fn add_assign(&mut self, rhs: usize) {
self.0 += rhs;
}
}
impl<S> PartialEq for Offset<S> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<S> Eq for Offset<S> {}
impl<S> PartialOrd for Offset<S> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<S> Ord for Offset<S> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.cmp(&other.0)
}
}
impl<S> Clone for Offset<S> {
fn clone(&self) -> Self {
Self(self.0, Default::default())
}
}
impl<S: 'static> std::fmt::Debug for Offset<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Offset<{}>({})", std::any::type_name::<S>(), self.0)
}
}
impl<S> Offset<S> {
pub fn new(val: usize) -> Self {
Self(val, Default::default())
}
pub fn zero() -> Self {
Self::new(0)
}
pub fn saturating_sub(self, rhs: Self) -> usize {
self.0.saturating_sub(rhs.0)
}
}
pub trait ToOffset<S: WithMapping> {
fn to_offset(&self, cx: &S::Mapping) -> Offset<S>;
}
// impl ToOffset<Buffer> for Offset<MultiBuffer> {
// fn to_offset(&self, cx: Self::Mapping) -> Offset<Buffer> {
// self
// }
// }
pub trait WithMapping {
type Mapping;
}
impl WithMapping for MultiBuffer {
type Mapping = MultiBufferSnapshot;
}
impl ToOffset<MultiBuffer> for Offset<MultiBuffer> {
fn to_offset(&self, _: &MultiBufferSnapshot) -> Offset<MultiBuffer> {
*self
}
}