From 529a42e78fd2a004f1859d35e51800f9fbc04a29 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 17 May 2021 15:06:45 +0200 Subject: [PATCH 01/18] Introduce `Rope::chunks_in_range` --- zed/src/editor/buffer/rope.rs | 50 +++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/zed/src/editor/buffer/rope.rs b/zed/src/editor/buffer/rope.rs index 9924120cc0c1ea2fe72914bbbdbee1a45c36d6d7..bd9d90b0bd86eb04273a3d77bae5cc436d23eb3c 100644 --- a/zed/src/editor/buffer/rope.rs +++ b/zed/src/editor/buffer/rope.rs @@ -4,7 +4,7 @@ use crate::util::byte_range_for_char_range; use anyhow::{anyhow, Result}; use arrayvec::ArrayString; use smallvec::SmallVec; -use std::{cmp, iter::Skip, str}; +use std::{cmp, iter::Skip, ops::Range, str}; #[cfg(test)] const CHUNK_BASE: usize = 6; @@ -118,7 +118,11 @@ impl Rope { } pub fn chunks<'a>(&'a self) -> impl Iterator { - self.chunks.cursor::<(), ()>().map(|c| c.0.as_str()) + self.chunks_in_range(0..self.len()) + } + + pub fn chunks_in_range<'a>(&'a self, range: Range) -> impl Iterator { + ChunksIter::new(self, range) } pub fn to_point(&self, offset: usize) -> Result { @@ -242,6 +246,37 @@ impl<'a> Cursor<'a> { } } +pub struct ChunksIter<'a> { + chunks: sum_tree::Cursor<'a, Chunk, usize, usize>, + range: Range, +} + +impl<'a> ChunksIter<'a> { + pub fn new(rope: &'a Rope, range: Range) -> Self { + let mut chunks = rope.chunks.cursor(); + chunks.seek(&range.start, SeekBias::Right, &()); + Self { chunks, range } + } +} + +impl<'a> Iterator for ChunksIter<'a> { + type Item = &'a str; + + fn next(&mut self) -> Option { + if *self.chunks.start() >= self.range.end { + None + } else if let Some(chunk) = self.chunks.item() { + let start = self.range.start.saturating_sub(*self.chunks.start()); + let end = self.range.end - self.chunks.start(); + let byte_range = byte_range_for_char_range(&chunk.0, start..end); + self.chunks.next(); + Some(&chunk.0[byte_range]) + } else { + None + } + } +} + #[derive(Clone, Debug, Default)] struct Chunk(ArrayString<[u8; 2 * CHUNK_BASE]>); @@ -509,10 +544,15 @@ mod tests { log::info!("text: {:?}", expected); for _ in 0..5 { - let ix = rng.gen_range(0..=expected.chars().count()); + let end_ix = rng.gen_range(0..=expected.chars().count()); + let start_ix = rng.gen_range(0..=end_ix); assert_eq!( - actual.chars_at(ix).collect::(), - expected.chars().skip(ix).collect::() + actual.chunks_in_range(start_ix..end_ix).collect::(), + expected + .chars() + .skip(start_ix) + .take(end_ix - start_ix) + .collect::() ); } From f7691fc00c8e724b1f8b926eef33bd3a0a82f71e Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 17 May 2021 18:01:08 +0200 Subject: [PATCH 02/18] WIP: Switch to byte-oriented indexing Co-Authored-By: Nathan Sobo --- zed/src/editor/buffer/rope.rs | 135 ++++++++++++++++++++-------------- zed/src/util.rs | 15 ---- 2 files changed, 80 insertions(+), 70 deletions(-) diff --git a/zed/src/editor/buffer/rope.rs b/zed/src/editor/buffer/rope.rs index bd9d90b0bd86eb04273a3d77bae5cc436d23eb3c..0b7ff6411b5382f5d71e57e50fd3f2023d35c4c7 100644 --- a/zed/src/editor/buffer/rope.rs +++ b/zed/src/editor/buffer/rope.rs @@ -1,6 +1,5 @@ use super::Point; use crate::sum_tree::{self, SeekBias, SumTree}; -use crate::util::byte_range_for_char_range; use anyhow::{anyhow, Result}; use arrayvec::ArrayString; use smallvec::SmallVec; @@ -125,31 +124,54 @@ impl Rope { ChunksIter::new(self, range) } - pub fn to_point(&self, offset: usize) -> Result { - if offset <= self.summary().chars { - let mut cursor = self.chunks.cursor::(); - cursor.seek(&offset, SeekBias::Left, &()); - let overshoot = offset - cursor.start().chars; - Ok(cursor.start().lines - + cursor - .item() - .map_or(Point::zero(), |chunk| chunk.to_point(overshoot))) + pub fn to_point(&self, offset: usize) -> Point { + assert!(offset <= self.summary().bytes); + let mut cursor = self.chunks.cursor::(); + cursor.seek(&offset, SeekBias::Left, &()); + let overshoot = offset - cursor.start().bytes; + cursor.start().lines + + cursor + .item() + .map_or(Point::zero(), |chunk| chunk.to_point(overshoot)) + } + + pub fn to_offset(&self, point: Point) -> usize { + assert!(point <= self.summary().lines); + let mut cursor = self.chunks.cursor::(); + cursor.seek(&point, SeekBias::Left, &()); + let overshoot = point - cursor.start().lines; + cursor.start().bytes + cursor.item().map_or(0, |chunk| chunk.to_offset(overshoot)) + } + + pub fn next_char_boundary(&self, mut offset: usize) -> usize { + assert!(offset <= self.summary().bytes); + let mut cursor = self.chunks.cursor::(); + cursor.seek(&offset, SeekBias::Left, &()); + if let Some(chunk) = cursor.item() { + let ix = offset - cursor.start(); + while !chunk.0.is_char_boundary(ix) { + ix += 1; + offset += 1; + } + offset } else { - Err(anyhow!("offset out of bounds")) + offset } } - pub fn to_offset(&self, point: Point) -> Result { - if point <= self.summary().lines { - let mut cursor = self.chunks.cursor::(); - cursor.seek(&point, SeekBias::Left, &()); - let overshoot = point - cursor.start().lines; - Ok(cursor.start().chars - + cursor - .item() - .map_or(Ok(0), |chunk| chunk.to_offset(overshoot))?) + pub fn prev_char_boundary(&self, offset: usize) -> usize { + assert!(offset <= self.summary().bytes); + let mut cursor = self.chunks.cursor::(); + cursor.seek(&offset, SeekBias::Left, &()); + if let Some(chunk) = cursor.item() { + let ix = offset - cursor.start(); + while !chunk.0.is_char_boundary(ix) { + ix -= 1; + offset -= 1; + } + offset } else { - Err(anyhow!("offset out of bounds")) + offset } } } @@ -193,8 +215,7 @@ impl<'a> Cursor<'a> { if let Some(start_chunk) = self.chunks.item() { let start_ix = self.offset - self.chunks.start(); let end_ix = cmp::min(end_offset, self.chunks.end()) - self.chunks.start(); - let byte_range = byte_range_for_char_range(start_chunk.0, start_ix..end_ix); - slice.push(&start_chunk.0[byte_range]); + slice.push(&start_chunk.0[start_ix..end_ix]); } if end_offset > self.chunks.end() { @@ -204,8 +225,7 @@ impl<'a> Cursor<'a> { }); if let Some(end_chunk) = self.chunks.item() { let end_ix = end_offset - self.chunks.start(); - let byte_range = byte_range_for_char_range(end_chunk.0, 0..end_ix); - slice.push(&end_chunk.0[byte_range]); + slice.push(&end_chunk.0[..end_ix]); } } @@ -220,8 +240,7 @@ impl<'a> Cursor<'a> { if let Some(start_chunk) = self.chunks.item() { let start_ix = self.offset - self.chunks.start(); let end_ix = cmp::min(end_offset, self.chunks.end()) - self.chunks.start(); - let byte_range = byte_range_for_char_range(start_chunk.0, start_ix..end_ix); - summary = TextSummary::from(&start_chunk.0[byte_range]); + summary = TextSummary::from(&start_chunk.0[start_ix..end_ix]); } if end_offset > self.chunks.end() { @@ -229,8 +248,7 @@ impl<'a> Cursor<'a> { summary += &self.chunks.summary(&end_offset, SeekBias::Right, &()); if let Some(end_chunk) = self.chunks.item() { let end_ix = end_offset - self.chunks.start(); - let byte_range = byte_range_for_char_range(end_chunk.0, 0..end_ix); - summary += TextSummary::from(&end_chunk.0[byte_range]); + summary += TextSummary::from(&end_chunk.0[..end_ix]); } } @@ -268,9 +286,8 @@ impl<'a> Iterator for ChunksIter<'a> { } else if let Some(chunk) = self.chunks.item() { let start = self.range.start.saturating_sub(*self.chunks.start()); let end = self.range.end - self.chunks.start(); - let byte_range = byte_range_for_char_range(&chunk.0, start..end); self.chunks.next(); - Some(&chunk.0[byte_range]) + Some(&chunk.0[start..end]) } else { None } @@ -293,14 +310,14 @@ impl Chunk { point.row += 1; point.column = 0; } else { - point.column += 1; + point.column += ch.len_utf8() as u32; } - offset += 1; + offset += ch.len_utf8(); } point } - fn to_offset(&self, target: Point) -> Result { + fn to_offset(&self, target: Point) -> usize { let mut offset = 0; let mut point = Point::new(0, 0); for ch in self.0.chars() { @@ -312,16 +329,13 @@ impl Chunk { point.row += 1; point.column = 0; } else { - point.column += 1; + point.column += ch.len_utf8() as u32; } - offset += 1; + offset += ch.len_utf8(); } - if point == target { - Ok(offset) - } else { - Err(anyhow!("point out of bounds")) - } + assert_eq!(point, target); + offset } } @@ -335,7 +349,6 @@ impl sum_tree::Item for Chunk { #[derive(Clone, Debug, Default, Eq, PartialEq)] pub struct TextSummary { - pub chars: usize, pub bytes: usize, pub lines: Point, pub first_line_len: u32, @@ -345,18 +358,15 @@ pub struct TextSummary { impl<'a> From<&'a str> for TextSummary { fn from(text: &'a str) -> Self { let mut chars = 0; - let mut bytes = 0; let mut lines = Point::new(0, 0); let mut first_line_len = 0; let mut rightmost_point = Point::new(0, 0); for c in text.chars() { - chars += 1; - bytes += c.len_utf8(); if c == '\n' { lines.row += 1; lines.column = 0; } else { - lines.column += 1; + lines.column += c.len_utf8() as u32; if lines.row == 0 { first_line_len = lines.column; } @@ -367,8 +377,7 @@ impl<'a> From<&'a str> for TextSummary { } TextSummary { - chars, - bytes, + bytes: text.len(), lines, first_line_len, rightmost_point, @@ -398,7 +407,6 @@ impl<'a> std::ops::AddAssign<&'a Self> for TextSummary { self.first_line_len += other.first_line_len; } - self.chars += other.chars; self.bytes += other.bytes; self.lines += &other.lines; } @@ -418,7 +426,7 @@ impl<'a> sum_tree::Dimension<'a, TextSummary> for TextSummary { impl<'a> sum_tree::Dimension<'a, TextSummary> for usize { fn add_summary(&mut self, summary: &'a TextSummary) { - *self += summary.chars; + *self += summary.bytes; } } @@ -439,6 +447,7 @@ impl<'a> Chars<'a> { cursor.seek(&start, SeekBias::Left, &()); let chars = if let Some(chunk) = cursor.item() { let ix = start - cursor.start(); + assert_char_boundary(&chunk.0, ix); cursor.next(); chunk.0.chars().skip(ix) } else { @@ -484,6 +493,23 @@ fn find_split_ix(text: &str) -> usize { ix } +#[inline(always)] +fn assert_char_boundary(s: &str, ix: usize) { + if !s.is_char_boundary(ix) { + let mut char_start = ix; + while !s.is_char_boundary(char_start) { + char_start -= 1; + } + + let ch = s[char_start..].chars().next().unwrap(); + let char_range = char_start..char_start + ch.len_utf8(); + panic!( + "byte index {} is not a char boundary; it is inside {:?} (bytes {:?}) of `{}`", + ix, ch, char_range, s + ); + } +} + #[cfg(test)] mod tests { use crate::util::RandomCharIter; @@ -521,8 +547,8 @@ mod tests { let mut expected = String::new(); let mut actual = Rope::new(); for _ in 0..operations { - let end_ix = rng.gen_range(0..=expected.chars().count()); - let start_ix = rng.gen_range(0..=end_ix); + let end_ix = actual.next_char_boundary(rng.gen_range(0..=expected.len())); + let start_ix = actual.prev_char_boundary(rng.gen_range(0..=end_ix)); let len = rng.gen_range(0..=64); let new_text: String = RandomCharIter::new(&mut rng).take(len).collect(); @@ -559,8 +585,8 @@ mod tests { let mut point = Point::new(0, 0); let mut offset = 0; for ch in expected.chars() { - assert_eq!(actual.to_point(offset).unwrap(), point); - assert_eq!(actual.to_offset(point).unwrap(), offset); + assert_eq!(actual.to_point(offset), point); + assert_eq!(actual.to_offset(point), offset); if ch == '\n' { assert!(actual .to_offset(Point::new(point.row, point.column + 1)) @@ -581,7 +607,6 @@ mod tests { for _ in 0..5 { let end_ix = rng.gen_range(0..=expected.chars().count()); let start_ix = rng.gen_range(0..=end_ix); - let byte_range = byte_range_for_char_range(&expected, start_ix..end_ix); assert_eq!( actual.cursor(start_ix).summary(end_ix), TextSummary::from(&expected[byte_range]) diff --git a/zed/src/util.rs b/zed/src/util.rs index c5e968243f7fd16a4faf1327994328aadf50e6e3..18227dc2d228bf968d233c996cf7a0381e3e5ffa 100644 --- a/zed/src/util.rs +++ b/zed/src/util.rs @@ -1,21 +1,6 @@ use rand::prelude::*; use std::{cmp::Ordering, ops::Range}; -pub fn byte_range_for_char_range(text: impl AsRef, char_range: Range) -> Range { - let text = text.as_ref(); - let mut result = text.len()..text.len(); - for (i, (offset, _)) in text.char_indices().enumerate() { - if i == char_range.start { - result.start = offset; - } - if i == char_range.end { - result.end = offset; - break; - } - } - result -} - pub fn post_inc(value: &mut usize) -> usize { let prev = *value; *value += 1; From 72b98ad688c79cd39d516d6441f01bcdc11573df Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 17 May 2021 13:21:49 -0700 Subject: [PATCH 03/18] Get buffer tests passing after switching to byte coordinates --- zed/src/editor/buffer/anchor.rs | 8 +- zed/src/editor/buffer/mod.rs | 405 +++++++++++-------------- zed/src/editor/buffer/rope.rs | 93 ++---- zed/src/editor/buffer/selection.rs | 4 +- zed/src/editor/buffer_view.rs | 121 ++++---- zed/src/editor/display_map/fold_map.rs | 79 +++-- zed/src/editor/display_map/mod.rs | 17 +- zed/src/util.rs | 3 +- 8 files changed, 308 insertions(+), 422 deletions(-) diff --git a/zed/src/editor/buffer/anchor.rs b/zed/src/editor/buffer/anchor.rs index 60106daba5af22db74067985cff1da335238f31d..3b2687f96daf63e6149d3400f4034fe8b8e94359 100644 --- a/zed/src/editor/buffer/anchor.rs +++ b/zed/src/editor/buffer/anchor.rs @@ -70,24 +70,24 @@ impl Anchor { }) } - pub fn bias_left(&self, buffer: &Buffer) -> Result { + pub fn bias_left(&self, buffer: &Buffer) -> Anchor { match self { Anchor::Start | Anchor::Middle { bias: AnchorBias::Left, .. - } => Ok(self.clone()), + } => self.clone(), _ => buffer.anchor_before(self), } } - pub fn bias_right(&self, buffer: &Buffer) -> Result { + pub fn bias_right(&self, buffer: &Buffer) -> Anchor { match self { Anchor::End | Anchor::Middle { bias: AnchorBias::Right, .. - } => Ok(self.clone()), + } => self.clone(), _ => buffer.anchor_after(self), } } diff --git a/zed/src/editor/buffer/mod.rs b/zed/src/editor/buffer/mod.rs index 8a57201ccff3d5c549f5e9caafd60606806614be..4113359f9de82314bb055ce4be4a7ed1fa08e191 100644 --- a/zed/src/editor/buffer/mod.rs +++ b/zed/src/editor/buffer/mod.rs @@ -14,13 +14,11 @@ use crate::{ operation_queue::{self, OperationQueue}, sum_tree::{self, FilterCursor, SeekBias, SumTree}, time::{self, ReplicaId}, - util::RandomCharIter, worktree::FileHandle, }; use anyhow::{anyhow, Result}; use gpui::{AppContext, Entity, ModelContext, Task}; use lazy_static::lazy_static; -use rand::prelude::*; use std::{ cmp, hash::BuildHasher, @@ -607,15 +605,14 @@ impl Buffer { self.fragments.extent::() } - pub fn line_len(&self, row: u32) -> Result { - let row_start_offset = Point::new(row, 0).to_offset(self)?; + pub fn line_len(&self, row: u32) -> u32 { + let row_start_offset = Point::new(row, 0).to_offset(self); let row_end_offset = if row >= self.max_point().row { self.len() } else { - Point::new(row + 1, 0).to_offset(self)? - 1 + Point::new(row + 1, 0).to_offset(self) - 1 }; - - Ok((row_end_offset - row_start_offset) as u32) + (row_end_offset - row_start_offset) as u32 } pub fn rightmost_point(&self) -> Point { @@ -630,33 +627,32 @@ impl Buffer { self.visible_text.max_point() } - pub fn line(&self, row: u32) -> Result { - Ok(self - .chars_at(Point::new(row, 0))? + pub fn line(&self, row: u32) -> String { + self.chars_at(Point::new(row, 0)) .take_while(|c| *c != '\n') - .collect()) + .collect() } pub fn text(&self) -> String { - self.chars().collect() + self.text_for_range(0..self.len()).collect() } pub fn text_for_range<'a, T: ToOffset>( &'a self, range: Range, - ) -> Result> { - let start = range.start.to_offset(self)?; - let end = range.end.to_offset(self)?; - Ok(self.chars_at(start)?.take(end - start)) + ) -> impl 'a + Iterator { + let start = range.start.to_offset(self); + let end = range.end.to_offset(self); + self.visible_text.chunks_in_range(start..end) } pub fn chars(&self) -> rope::Chars { - self.chars_at(0).unwrap() + self.chars_at(0) } - pub fn chars_at(&self, position: T) -> Result { - let offset = position.to_offset(self)?; - Ok(self.visible_text.chars_at(offset)) + pub fn chars_at(&self, position: T) -> rope::Chars { + let offset = position.to_offset(self); + self.visible_text.chars_at(offset) } pub fn selections_changed_since(&self, since: SelectionsVersion) -> bool { @@ -763,8 +759,8 @@ impl Buffer { let old_ranges = old_ranges .into_iter() - .map(|range| Ok(range.start.to_offset(self)?..range.end.to_offset(self)?)) - .collect::>>>()?; + .map(|range| range.start.to_offset(self)..range.end.to_offset(self)) + .collect::>>(); let has_new_text = new_text.is_some(); let ops = self.splice_fragments( @@ -802,50 +798,6 @@ impl Buffer { } } - pub fn simulate_typing(&mut self, rng: &mut T) { - let end = rng.gen_range(0..self.len() + 1); - let start = rng.gen_range(0..end + 1); - let mut range = start..end; - - let new_text_len = rng.gen_range(0..100); - let new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect(); - - for char in new_text.chars() { - self.edit(Some(range.clone()), char.to_string().as_str(), None) - .unwrap(); - range = range.end + 1..range.end + 1; - } - } - - pub fn randomly_edit( - &mut self, - rng: &mut T, - old_range_count: usize, - ctx: Option<&mut ModelContext>, - ) -> (Vec>, String, Vec) - where - T: Rng, - { - let mut old_ranges: Vec> = Vec::new(); - for _ in 0..old_range_count { - let last_end = old_ranges.last().map_or(0, |last_range| last_range.end + 1); - if last_end > self.len() { - break; - } - let end = rng.gen_range(last_end..self.len() + 1); - let start = rng.gen_range(last_end..end + 1); - old_ranges.push(start..end); - } - let new_text_len = rng.gen_range(0..10); - let new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect(); - - let operations = self - .edit(old_ranges.iter().cloned(), new_text.as_str(), ctx) - .unwrap(); - - (old_ranges, new_text, operations) - } - pub fn add_selection_set( &mut self, selections: impl Into>, @@ -1777,8 +1729,7 @@ impl Buffer { .unwrap_or(&FragmentId::max_value()), ); - // TODO: extent could be expressed in bytes, which would save a linear scan. - let range_in_insertion = 0..text.chars().count(); + let range_in_insertion = 0..text.len(); let mut split_tree = SumTree::new(); split_tree.push( InsertionSplit { @@ -1801,33 +1752,31 @@ impl Buffer { ) } - pub fn anchor_before(&self, position: T) -> Result { + pub fn anchor_before(&self, position: T) -> Anchor { self.anchor_at(position, AnchorBias::Left) } - pub fn anchor_after(&self, position: T) -> Result { + pub fn anchor_after(&self, position: T) -> Anchor { self.anchor_at(position, AnchorBias::Right) } - pub fn anchor_at(&self, position: T, bias: AnchorBias) -> Result { - let offset = position.to_offset(self)?; + pub fn anchor_at(&self, position: T, bias: AnchorBias) -> Anchor { + let offset = position.to_offset(self); let max_offset = self.len(); - if offset > max_offset { - return Err(anyhow!("offset is out of range")); - } + assert!(offset <= max_offset, "offset is out of range"); let seek_bias; match bias { AnchorBias::Left => { if offset == 0 { - return Ok(Anchor::Start); + return Anchor::Start; } else { seek_bias = SeekBias::Left; } } AnchorBias::Right => { if offset == max_offset { - return Ok(Anchor::End); + return Anchor::End; } else { seek_bias = SeekBias::Right; } @@ -1844,7 +1793,7 @@ impl Buffer { offset: offset_in_insertion, bias, }; - Ok(anchor) + anchor } fn fragment_id_for_anchor(&self, anchor: &Anchor) -> Result<&FragmentId> { @@ -1876,10 +1825,10 @@ impl Buffer { } } - fn summary_for_anchor(&self, anchor: &Anchor) -> Result { + fn summary_for_anchor(&self, anchor: &Anchor) -> TextSummary { match anchor { - Anchor::Start => Ok(TextSummary::default()), - Anchor::End => Ok(self.text_summary()), + Anchor::Start => TextSummary::default(), + Anchor::End => self.text_summary(), Anchor::Middle { insertion_id, offset, @@ -1893,24 +1842,20 @@ impl Buffer { let splits = self .insertion_splits .get(&insertion_id) - .ok_or_else(|| anyhow!("split does not exist for insertion id"))?; + .expect("split does not exist for insertion id"); let mut splits_cursor = splits.cursor::(); splits_cursor.seek(offset, seek_bias, &()); - let split = splits_cursor - .item() - .ok_or_else(|| anyhow!("split offset is out of range"))?; + let split = splits_cursor.item().expect("split offset is out of range"); let mut fragments_cursor = self.fragments.cursor::(); fragments_cursor.seek(&FragmentIdRef::new(&split.fragment_id), SeekBias::Left, &()); - let fragment = fragments_cursor - .item() - .ok_or_else(|| anyhow!("fragment id does not exist"))?; + let fragment = fragments_cursor.item().expect("fragment id does not exist"); let mut ix = *fragments_cursor.start(); if fragment.visible { ix += offset - fragment.range_in_insertion.start; } - Ok(self.text_summary_for_range(0..ix)) + self.text_summary_for_range(0..ix) } } } @@ -1922,6 +1867,14 @@ impl Buffer { Err(anyhow!("offset out of bounds")) } } + + pub fn next_char_boundary(&self, offset: usize) -> usize { + self.visible_text.next_char_boundary(offset) + } + + pub fn prev_char_boundary(&self, offset: usize) -> usize { + self.visible_text.prev_char_boundary(offset) + } } impl Clone for Buffer { @@ -2352,45 +2305,45 @@ impl operation_queue::Operation for Operation { } pub trait ToOffset { - fn to_offset(&self, buffer: &Buffer) -> Result; + fn to_offset(&self, buffer: &Buffer) -> usize; } impl ToOffset for Point { - fn to_offset(&self, buffer: &Buffer) -> Result { + fn to_offset(&self, buffer: &Buffer) -> usize { buffer.visible_text.to_offset(*self) } } impl ToOffset for usize { - fn to_offset(&self, _: &Buffer) -> Result { - Ok(*self) + fn to_offset(&self, _: &Buffer) -> usize { + *self } } impl ToOffset for Anchor { - fn to_offset(&self, buffer: &Buffer) -> Result { - Ok(buffer.summary_for_anchor(self)?.chars) + fn to_offset(&self, buffer: &Buffer) -> usize { + buffer.summary_for_anchor(self).bytes } } impl<'a> ToOffset for &'a Anchor { - fn to_offset(&self, buffer: &Buffer) -> Result { - Ok(buffer.summary_for_anchor(self)?.chars) + fn to_offset(&self, buffer: &Buffer) -> usize { + buffer.summary_for_anchor(self).bytes } } pub trait ToPoint { - fn to_point(&self, buffer: &Buffer) -> Result; + fn to_point(&self, buffer: &Buffer) -> Point; } impl ToPoint for Anchor { - fn to_point(&self, buffer: &Buffer) -> Result { - Ok(buffer.summary_for_anchor(self)?.lines) + fn to_point(&self, buffer: &Buffer) -> Point { + buffer.summary_for_anchor(self).lines } } impl ToPoint for usize { - fn to_point(&self, buffer: &Buffer) -> Result { + fn to_point(&self, buffer: &Buffer) -> Point { buffer.visible_text.to_point(*self) } } @@ -2400,13 +2353,15 @@ mod tests { use super::*; use crate::{ test::temp_tree, + util::RandomCharIter, worktree::{Worktree, WorktreeHandle}, }; - use cmp::Ordering; use gpui::App; + use rand::prelude::*; use serde_json::json; use std::{ cell::RefCell, + cmp::Ordering, collections::BTreeMap, fs, rc::Rc, @@ -2506,12 +2461,7 @@ mod tests { for _i in 0..10 { let (old_ranges, new_text, _) = buffer.randomly_mutate(rng, None); for old_range in old_ranges.iter().rev() { - reference_string = reference_string - .chars() - .take(old_range.start) - .chain(new_text.chars()) - .chain(reference_string.chars().skip(old_range.end)) - .collect(); + reference_string.replace_range(old_range.clone(), &new_text); } assert_eq!(buffer.text(), reference_string); @@ -2525,7 +2475,7 @@ mod tests { for (len, rows) in &line_lengths { for row in rows { - assert_eq!(buffer.line_len(*row).unwrap(), *len); + assert_eq!(buffer.line_len(*row), *len); } } @@ -2537,22 +2487,14 @@ mod tests { } for _ in 0..5 { - let end = rng.gen_range(0..buffer.len() + 1); - let start = rng.gen_range(0..end + 1); - - let line_lengths = line_lengths_in_range(&buffer, start..end); + let range = buffer.random_byte_range(0, rng); + let line_lengths = line_lengths_in_range(&buffer, range.clone()); let (longest_column, longest_rows) = line_lengths.iter().next_back().unwrap(); - let range_sum = buffer.text_summary_for_range(start..end); + let range_sum = buffer.text_summary_for_range(range.clone()); assert_eq!(range_sum.rightmost_point.column, *longest_column); assert!(longest_rows.contains(&range_sum.rightmost_point.row)); - let range_text = buffer - .text() - .chars() - .skip(start) - .take(end - start) - .collect::(); - assert_eq!(range_sum.chars, range_text.chars().count()); + let range_text = &buffer.text()[range]; assert_eq!(range_sum.bytes, range_text.len()); } @@ -2571,7 +2513,7 @@ mod tests { let old_len = old_range.end - old_range.start; let new_len = new_range.end - new_range.start; let old_start = (old_range.start as isize + delta) as usize; - let new_text: String = buffer.text_for_range(new_range).unwrap().collect(); + let new_text: String = buffer.text_for_range(new_range).collect(); old_buffer .edit(Some(old_start..old_start + old_len), new_text, None) .unwrap(); @@ -2595,13 +2537,12 @@ mod tests { buffer.edit(vec![18..18], "\npqrs\n", None).unwrap(); buffer.edit(vec![18..21], "\nPQ", None).unwrap(); - assert_eq!(buffer.line_len(0).unwrap(), 4); - assert_eq!(buffer.line_len(1).unwrap(), 3); - assert_eq!(buffer.line_len(2).unwrap(), 5); - assert_eq!(buffer.line_len(3).unwrap(), 3); - assert_eq!(buffer.line_len(4).unwrap(), 4); - assert_eq!(buffer.line_len(5).unwrap(), 0); - assert!(buffer.line_len(6).is_err()); + assert_eq!(buffer.line_len(0), 4); + assert_eq!(buffer.line_len(1), 3); + assert_eq!(buffer.line_len(2), 5); + assert_eq!(buffer.line_len(3), 3); + assert_eq!(buffer.line_len(4), 4); + assert_eq!(buffer.line_len(5), 0); buffer }); } @@ -2632,7 +2573,6 @@ mod tests { assert_eq!( buffer.text_summary_for_range(1..3), TextSummary { - chars: 2, bytes: 2, lines: Point::new(1, 0), first_line_len: 1, @@ -2642,7 +2582,6 @@ mod tests { assert_eq!( buffer.text_summary_for_range(1..12), TextSummary { - chars: 11, bytes: 11, lines: Point::new(3, 0), first_line_len: 1, @@ -2652,7 +2591,6 @@ mod tests { assert_eq!( buffer.text_summary_for_range(0..20), TextSummary { - chars: 20, bytes: 20, lines: Point::new(4, 1), first_line_len: 2, @@ -2662,7 +2600,6 @@ mod tests { assert_eq!( buffer.text_summary_for_range(0..22), TextSummary { - chars: 22, bytes: 22, lines: Point::new(4, 3), first_line_len: 2, @@ -2672,7 +2609,6 @@ mod tests { assert_eq!( buffer.text_summary_for_range(7..22), TextSummary { - chars: 15, bytes: 15, lines: Point::new(2, 3), first_line_len: 4, @@ -2692,19 +2628,19 @@ mod tests { buffer.edit(vec![18..18], "\npqrs", None).unwrap(); buffer.edit(vec![18..21], "\nPQ", None).unwrap(); - let chars = buffer.chars_at(Point::new(0, 0)).unwrap(); + let chars = buffer.chars_at(Point::new(0, 0)); assert_eq!(chars.collect::(), "abcd\nefgh\nijkl\nmno\nPQrs"); - let chars = buffer.chars_at(Point::new(1, 0)).unwrap(); + let chars = buffer.chars_at(Point::new(1, 0)); assert_eq!(chars.collect::(), "efgh\nijkl\nmno\nPQrs"); - let chars = buffer.chars_at(Point::new(2, 0)).unwrap(); + let chars = buffer.chars_at(Point::new(2, 0)); assert_eq!(chars.collect::(), "ijkl\nmno\nPQrs"); - let chars = buffer.chars_at(Point::new(3, 0)).unwrap(); + let chars = buffer.chars_at(Point::new(3, 0)); assert_eq!(chars.collect::(), "mno\nPQrs"); - let chars = buffer.chars_at(Point::new(4, 0)).unwrap(); + let chars = buffer.chars_at(Point::new(4, 0)); assert_eq!(chars.collect::(), "PQrs"); // Regression test: @@ -2712,7 +2648,7 @@ mod tests { buffer.edit(vec![0..0], "[workspace]\nmembers = [\n \"xray_core\",\n \"xray_server\",\n \"xray_cli\",\n \"xray_wasm\",\n]\n", None).unwrap(); buffer.edit(vec![60..60], "\n", None).unwrap(); - let chars = buffer.chars_at(Point::new(6, 0)).unwrap(); + let chars = buffer.chars_at(Point::new(6, 0)); assert_eq!(chars.collect::(), " \"xray_wasm\",\n]\n"); buffer @@ -2744,103 +2680,79 @@ mod tests { ctx.add_model(|ctx| { let mut buffer = Buffer::new(0, "", ctx); buffer.edit(vec![0..0], "abc", None).unwrap(); - let left_anchor = buffer.anchor_before(2).unwrap(); - let right_anchor = buffer.anchor_after(2).unwrap(); + let left_anchor = buffer.anchor_before(2); + let right_anchor = buffer.anchor_after(2); buffer.edit(vec![1..1], "def\n", None).unwrap(); assert_eq!(buffer.text(), "adef\nbc"); - assert_eq!(left_anchor.to_offset(&buffer).unwrap(), 6); - assert_eq!(right_anchor.to_offset(&buffer).unwrap(), 6); - assert_eq!( - left_anchor.to_point(&buffer).unwrap(), - Point { row: 1, column: 1 } - ); - assert_eq!( - right_anchor.to_point(&buffer).unwrap(), - Point { row: 1, column: 1 } - ); + assert_eq!(left_anchor.to_offset(&buffer), 6); + assert_eq!(right_anchor.to_offset(&buffer), 6); + assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 }); + assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 1 }); buffer.edit(vec![2..3], "", None).unwrap(); assert_eq!(buffer.text(), "adf\nbc"); - assert_eq!(left_anchor.to_offset(&buffer).unwrap(), 5); - assert_eq!(right_anchor.to_offset(&buffer).unwrap(), 5); - assert_eq!( - left_anchor.to_point(&buffer).unwrap(), - Point { row: 1, column: 1 } - ); - assert_eq!( - right_anchor.to_point(&buffer).unwrap(), - Point { row: 1, column: 1 } - ); + assert_eq!(left_anchor.to_offset(&buffer), 5); + assert_eq!(right_anchor.to_offset(&buffer), 5); + assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 }); + assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 1 }); buffer.edit(vec![5..5], "ghi\n", None).unwrap(); assert_eq!(buffer.text(), "adf\nbghi\nc"); - assert_eq!(left_anchor.to_offset(&buffer).unwrap(), 5); - assert_eq!(right_anchor.to_offset(&buffer).unwrap(), 9); - assert_eq!( - left_anchor.to_point(&buffer).unwrap(), - Point { row: 1, column: 1 } - ); - assert_eq!( - right_anchor.to_point(&buffer).unwrap(), - Point { row: 2, column: 0 } - ); + assert_eq!(left_anchor.to_offset(&buffer), 5); + assert_eq!(right_anchor.to_offset(&buffer), 9); + assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 }); + assert_eq!(right_anchor.to_point(&buffer), Point { row: 2, column: 0 }); buffer.edit(vec![7..9], "", None).unwrap(); assert_eq!(buffer.text(), "adf\nbghc"); - assert_eq!(left_anchor.to_offset(&buffer).unwrap(), 5); - assert_eq!(right_anchor.to_offset(&buffer).unwrap(), 7); - assert_eq!( - left_anchor.to_point(&buffer).unwrap(), - Point { row: 1, column: 1 }, - ); - assert_eq!( - right_anchor.to_point(&buffer).unwrap(), - Point { row: 1, column: 3 } - ); + assert_eq!(left_anchor.to_offset(&buffer), 5); + assert_eq!(right_anchor.to_offset(&buffer), 7); + assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 },); + assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 3 }); // Ensure anchoring to a point is equivalent to anchoring to an offset. assert_eq!( - buffer.anchor_before(Point { row: 0, column: 0 }).unwrap(), - buffer.anchor_before(0).unwrap() + buffer.anchor_before(Point { row: 0, column: 0 }), + buffer.anchor_before(0) ); assert_eq!( - buffer.anchor_before(Point { row: 0, column: 1 }).unwrap(), - buffer.anchor_before(1).unwrap() + buffer.anchor_before(Point { row: 0, column: 1 }), + buffer.anchor_before(1) ); assert_eq!( - buffer.anchor_before(Point { row: 0, column: 2 }).unwrap(), - buffer.anchor_before(2).unwrap() + buffer.anchor_before(Point { row: 0, column: 2 }), + buffer.anchor_before(2) ); assert_eq!( - buffer.anchor_before(Point { row: 0, column: 3 }).unwrap(), - buffer.anchor_before(3).unwrap() + buffer.anchor_before(Point { row: 0, column: 3 }), + buffer.anchor_before(3) ); assert_eq!( - buffer.anchor_before(Point { row: 1, column: 0 }).unwrap(), - buffer.anchor_before(4).unwrap() + buffer.anchor_before(Point { row: 1, column: 0 }), + buffer.anchor_before(4) ); assert_eq!( - buffer.anchor_before(Point { row: 1, column: 1 }).unwrap(), - buffer.anchor_before(5).unwrap() + buffer.anchor_before(Point { row: 1, column: 1 }), + buffer.anchor_before(5) ); assert_eq!( - buffer.anchor_before(Point { row: 1, column: 2 }).unwrap(), - buffer.anchor_before(6).unwrap() + buffer.anchor_before(Point { row: 1, column: 2 }), + buffer.anchor_before(6) ); assert_eq!( - buffer.anchor_before(Point { row: 1, column: 3 }).unwrap(), - buffer.anchor_before(7).unwrap() + buffer.anchor_before(Point { row: 1, column: 3 }), + buffer.anchor_before(7) ); assert_eq!( - buffer.anchor_before(Point { row: 1, column: 4 }).unwrap(), - buffer.anchor_before(8).unwrap() + buffer.anchor_before(Point { row: 1, column: 4 }), + buffer.anchor_before(8) ); // Comparison between anchors. - let anchor_at_offset_0 = buffer.anchor_before(0).unwrap(); - let anchor_at_offset_1 = buffer.anchor_before(1).unwrap(); - let anchor_at_offset_2 = buffer.anchor_before(2).unwrap(); + let anchor_at_offset_0 = buffer.anchor_before(0); + let anchor_at_offset_1 = buffer.anchor_before(1); + let anchor_at_offset_2 = buffer.anchor_before(2); assert_eq!( anchor_at_offset_0 @@ -2906,24 +2818,24 @@ mod tests { fn test_anchors_at_start_and_end(ctx: &mut gpui::MutableAppContext) { ctx.add_model(|ctx| { let mut buffer = Buffer::new(0, "", ctx); - let before_start_anchor = buffer.anchor_before(0).unwrap(); - let after_end_anchor = buffer.anchor_after(0).unwrap(); + let before_start_anchor = buffer.anchor_before(0); + let after_end_anchor = buffer.anchor_after(0); buffer.edit(vec![0..0], "abc", None).unwrap(); assert_eq!(buffer.text(), "abc"); - assert_eq!(before_start_anchor.to_offset(&buffer).unwrap(), 0); - assert_eq!(after_end_anchor.to_offset(&buffer).unwrap(), 3); + assert_eq!(before_start_anchor.to_offset(&buffer), 0); + assert_eq!(after_end_anchor.to_offset(&buffer), 3); - let after_start_anchor = buffer.anchor_after(0).unwrap(); - let before_end_anchor = buffer.anchor_before(3).unwrap(); + let after_start_anchor = buffer.anchor_after(0); + let before_end_anchor = buffer.anchor_before(3); buffer.edit(vec![3..3], "def", None).unwrap(); buffer.edit(vec![0..0], "ghi", None).unwrap(); assert_eq!(buffer.text(), "ghiabcdef"); - assert_eq!(before_start_anchor.to_offset(&buffer).unwrap(), 0); - assert_eq!(after_start_anchor.to_offset(&buffer).unwrap(), 3); - assert_eq!(before_end_anchor.to_offset(&buffer).unwrap(), 6); - assert_eq!(after_end_anchor.to_offset(&buffer).unwrap(), 9); + assert_eq!(before_start_anchor.to_offset(&buffer), 0); + assert_eq!(after_start_anchor.to_offset(&buffer), 3); + assert_eq!(before_end_anchor.to_offset(&buffer), 6); + assert_eq!(after_end_anchor.to_offset(&buffer), 9); buffer }); } @@ -3062,9 +2974,7 @@ mod tests { buffer.add_selection_set( (0..3) .map(|row| { - let anchor = buffer - .anchor_at(Point::new(row, 0), AnchorBias::Right) - .unwrap(); + let anchor = buffer.anchor_at(Point::new(row, 0), AnchorBias::Right); Selection { id: row as usize, start: anchor.clone(), @@ -3104,7 +3014,7 @@ mod tests { .iter() .map(|selection| { assert_eq!(selection.start, selection.end); - selection.start.to_point(&buffer).unwrap() + selection.start.to_point(&buffer) }) .collect::>(); assert_eq!( @@ -3324,6 +3234,39 @@ mod tests { } impl Buffer { + fn random_byte_range(&mut self, start_offset: usize, rng: &mut impl Rng) -> Range { + let end = self.next_char_boundary(rng.gen_range(start_offset..=self.len())); + let start = self.prev_char_boundary(rng.gen_range(start_offset..=end)); + start..end + } + + pub fn randomly_edit( + &mut self, + rng: &mut T, + old_range_count: usize, + ctx: Option<&mut ModelContext>, + ) -> (Vec>, String, Vec) + where + T: Rng, + { + let mut old_ranges: Vec> = Vec::new(); + for _ in 0..old_range_count { + let last_end = old_ranges.last().map_or(0, |last_range| last_range.end + 1); + if last_end > self.len() { + break; + } + old_ranges.push(self.random_byte_range(last_end, rng)); + } + let new_text_len = rng.gen_range(0..10); + let new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect(); + + let operations = self + .edit(old_ranges.iter().cloned(), new_text.as_str(), ctx) + .unwrap(); + + (old_ranges, new_text, operations) + } + pub fn randomly_mutate( &mut self, rng: &mut T, @@ -3349,9 +3292,7 @@ mod tests { } else { let mut ranges = Vec::new(); for _ in 0..5 { - let start = rng.gen_range(0..self.len() + 1); - let end = rng.gen_range(0..self.len() + 1); - ranges.push(start..end); + ranges.push(self.random_byte_range(0, rng)); } let new_selections = self.selections_from_ranges(ranges).unwrap(); @@ -3391,16 +3332,16 @@ mod tests { if range.start > range.end { selections.push(Selection { id: NEXT_SELECTION_ID.fetch_add(1, atomic::Ordering::SeqCst), - start: self.anchor_before(range.end)?, - end: self.anchor_before(range.start)?, + start: self.anchor_before(range.end), + end: self.anchor_before(range.start), reversed: true, goal: SelectionGoal::None, }); } else { selections.push(Selection { id: NEXT_SELECTION_ID.fetch_add(1, atomic::Ordering::SeqCst), - start: self.anchor_after(range.start)?, - end: self.anchor_before(range.end)?, + start: self.anchor_after(range.start), + end: self.anchor_before(range.end), reversed: false, goal: SelectionGoal::None, }); @@ -3414,8 +3355,8 @@ mod tests { .selections(set_id)? .iter() .map(move |selection| { - let start = selection.start.to_offset(self).unwrap(); - let end = selection.end.to_offset(self).unwrap(); + let start = selection.start.to_offset(self); + let end = selection.end.to_offset(self); if selection.reversed { end..start } else { @@ -3452,17 +3393,9 @@ mod tests { fn line_lengths_in_range(buffer: &Buffer, range: Range) -> BTreeMap> { let mut lengths = BTreeMap::new(); - for (row, line) in buffer - .text() - .chars() - .skip(range.start) - .take(range.len()) - .collect::() - .lines() - .enumerate() - { + for (row, line) in buffer.text()[range.start..range.end].lines().enumerate() { lengths - .entry(line.chars().count() as u32) + .entry(line.len() as u32) .or_insert(HashSet::default()) .insert(row as u32); } diff --git a/zed/src/editor/buffer/rope.rs b/zed/src/editor/buffer/rope.rs index 0b7ff6411b5382f5d71e57e50fd3f2023d35c4c7..3128aedbd31a2b71d801646e89c5cabebe282d32 100644 --- a/zed/src/editor/buffer/rope.rs +++ b/zed/src/editor/buffer/rope.rs @@ -1,6 +1,5 @@ use super::Point; use crate::sum_tree::{self, SeekBias, SumTree}; -use anyhow::{anyhow, Result}; use arrayvec::ArrayString; use smallvec::SmallVec; use std::{cmp, iter::Skip, ops::Range, str}; @@ -148,31 +147,27 @@ impl Rope { let mut cursor = self.chunks.cursor::(); cursor.seek(&offset, SeekBias::Left, &()); if let Some(chunk) = cursor.item() { - let ix = offset - cursor.start(); + let mut ix = offset - cursor.start(); while !chunk.0.is_char_boundary(ix) { ix += 1; offset += 1; } - offset - } else { - offset } + offset } - pub fn prev_char_boundary(&self, offset: usize) -> usize { + pub fn prev_char_boundary(&self, mut offset: usize) -> usize { assert!(offset <= self.summary().bytes); let mut cursor = self.chunks.cursor::(); cursor.seek(&offset, SeekBias::Left, &()); if let Some(chunk) = cursor.item() { - let ix = offset - cursor.start(); + let mut ix = offset - cursor.start(); while !chunk.0.is_char_boundary(ix) { ix -= 1; offset -= 1; } - offset - } else { - offset } + offset } } @@ -281,16 +276,15 @@ impl<'a> Iterator for ChunksIter<'a> { type Item = &'a str; fn next(&mut self) -> Option { - if *self.chunks.start() >= self.range.end { - None - } else if let Some(chunk) = self.chunks.item() { - let start = self.range.start.saturating_sub(*self.chunks.start()); - let end = self.range.end - self.chunks.start(); - self.chunks.next(); - Some(&chunk.0[start..end]) - } else { - None + if let Some(chunk) = self.chunks.item() { + if self.range.end > *self.chunks.start() { + let start = self.range.start.saturating_sub(*self.chunks.start()); + let end = self.range.end - self.chunks.start(); + self.chunks.next(); + return Some(&chunk.0[start..chunk.0.len().min(end)]); + } } + None } } @@ -357,22 +351,19 @@ pub struct TextSummary { impl<'a> From<&'a str> for TextSummary { fn from(text: &'a str) -> Self { - let mut chars = 0; let mut lines = Point::new(0, 0); let mut first_line_len = 0; let mut rightmost_point = Point::new(0, 0); - for c in text.chars() { - if c == '\n' { + for (i, line) in text.split('\n').enumerate() { + if i > 0 { lines.row += 1; - lines.column = 0; - } else { - lines.column += c.len_utf8() as u32; - if lines.row == 0 { - first_line_len = lines.column; - } - if lines.column > rightmost_point.column { - rightmost_point = lines; - } + } + lines.column = line.len() as u32; + if i == 0 { + first_line_len = lines.column; + } + if lines.column > rightmost_point.column { + rightmost_point = lines; } } @@ -560,56 +551,38 @@ mod tests { new_actual.append(cursor.suffix()); actual = new_actual; - let mut new_expected = String::new(); - new_expected.extend(expected.chars().take(start_ix)); - new_expected.push_str(&new_text); - new_expected.extend(expected.chars().skip(end_ix)); - expected = new_expected; + expected.replace_range(start_ix..end_ix, &new_text); assert_eq!(actual.text(), expected); log::info!("text: {:?}", expected); for _ in 0..5 { - let end_ix = rng.gen_range(0..=expected.chars().count()); - let start_ix = rng.gen_range(0..=end_ix); + let end_ix = actual.next_char_boundary(rng.gen_range(0..=expected.len())); + let start_ix = actual.prev_char_boundary(rng.gen_range(0..=end_ix)); assert_eq!( actual.chunks_in_range(start_ix..end_ix).collect::(), - expected - .chars() - .skip(start_ix) - .take(end_ix - start_ix) - .collect::() + &expected[start_ix..end_ix] ); } let mut point = Point::new(0, 0); - let mut offset = 0; - for ch in expected.chars() { - assert_eq!(actual.to_point(offset), point); - assert_eq!(actual.to_offset(point), offset); + for (ix, ch) in expected.char_indices().chain(Some((expected.len(), '\0'))) { + assert_eq!(actual.to_point(ix), point, "to_point({})", ix); + assert_eq!(actual.to_offset(point), ix, "to_offset({:?})", point); if ch == '\n' { - assert!(actual - .to_offset(Point::new(point.row, point.column + 1)) - .is_err()); - point.row += 1; point.column = 0 } else { - point.column += 1; + point.column += ch.len_utf8() as u32; } - offset += 1; } - assert_eq!(actual.to_point(offset).unwrap(), point); - assert!(actual.to_point(offset + 1).is_err()); - assert_eq!(actual.to_offset(point).unwrap(), offset); - assert!(actual.to_offset(Point::new(point.row + 1, 0)).is_err()); for _ in 0..5 { - let end_ix = rng.gen_range(0..=expected.chars().count()); - let start_ix = rng.gen_range(0..=end_ix); + let end_ix = actual.next_char_boundary(rng.gen_range(0..=expected.len())); + let start_ix = actual.prev_char_boundary(rng.gen_range(0..=end_ix)); assert_eq!( actual.cursor(start_ix).summary(end_ix), - TextSummary::from(&expected[byte_range]) + TextSummary::from(&expected[start_ix..end_ix]) ); } } diff --git a/zed/src/editor/buffer/selection.rs b/zed/src/editor/buffer/selection.rs index bff91a83e922a712889a62dbf5a6c80184be821c..95ad181beac7bf3988f2c632822c8697f26ea4f7 100644 --- a/zed/src/editor/buffer/selection.rs +++ b/zed/src/editor/buffer/selection.rs @@ -62,8 +62,8 @@ impl Selection { } pub fn range(&self, buffer: &Buffer) -> Range { - let start = self.start.to_point(buffer).unwrap(); - let end = self.end.to_point(buffer).unwrap(); + let start = self.start.to_point(buffer); + let end = self.end.to_point(buffer); if self.reversed { end..start } else { diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 3319e81f41fdfb4bd9145d8f17bec366d99e7353..ebf10a4f4a4036f5d8d104a9fc12466b8d0b88e8 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -337,8 +337,8 @@ impl BufferView { buffer.add_selection_set( vec![Selection { id: post_inc(&mut next_selection_id), - start: buffer.anchor_before(0).unwrap(), - end: buffer.anchor_before(0).unwrap(), + start: buffer.anchor_before(0), + end: buffer.anchor_before(0), reversed: false, goal: SelectionGoal::None, }], @@ -588,8 +588,8 @@ impl BufferView { let buffer = self.buffer.read(ctx); let mut selections = Vec::new(); for range in ranges { - let mut start = range.start.to_offset(buffer).unwrap(); - let mut end = range.end.to_offset(buffer).unwrap(); + let mut start = range.start.to_offset(buffer); + let mut end = range.end.to_offset(buffer); let reversed = if start > end { mem::swap(&mut start, &mut end); true @@ -598,8 +598,8 @@ impl BufferView { }; selections.push(Selection { id: post_inc(&mut self.next_selection_id), - start: buffer.anchor_before(start).unwrap(), - end: buffer.anchor_before(end).unwrap(), + start: buffer.anchor_before(start), + end: buffer.anchor_before(end), reversed, goal: SelectionGoal::None, }); @@ -644,8 +644,8 @@ impl BufferView { { let buffer = self.buffer.read(ctx); for selection in self.selections(ctx.as_ref()) { - let start = selection.start.to_offset(buffer).unwrap(); - let end = selection.end.to_offset(buffer).unwrap(); + let start = selection.start.to_offset(buffer); + let end = selection.end.to_offset(buffer); old_selections.push((selection.id, start..end)); } } @@ -664,9 +664,7 @@ impl BufferView { .map(|(id, range)| { let start = range.start as isize; let end = range.end as isize; - let anchor = buffer - .anchor_before((start + delta + char_count) as usize) - .unwrap(); + let anchor = buffer.anchor_before((start + delta + char_count) as usize); let deleted_count = end - start; delta += char_count - deleted_count; Selection { @@ -785,13 +783,13 @@ impl BufferView { } } - let mut edit_start = Point::new(rows.start, 0).to_offset(buffer).unwrap(); + let mut edit_start = Point::new(rows.start, 0).to_offset(buffer); let edit_end; let cursor_buffer_row; - if let Ok(end_offset) = Point::new(rows.end, 0).to_offset(buffer) { + if buffer.max_point().row >= rows.end { // If there's a line after the range, delete the \n from the end of the row range // and position the cursor on the next line. - edit_end = end_offset; + edit_end = Point::new(rows.end, 0).to_offset(buffer); cursor_buffer_row = rows.end; } else { // If there isn't a line after the range, delete the \n from the line before the @@ -822,7 +820,7 @@ impl BufferView { let new_selections = new_cursors .into_iter() .map(|(id, cursor)| { - let anchor = buffer.anchor_before(cursor).unwrap(); + let anchor = buffer.anchor_before(cursor); Selection { id, start: anchor.clone(), @@ -848,8 +846,8 @@ impl BufferView { // when the selections are at the beginning of a line. let buffer = self.buffer.read(ctx); for selection in &mut selections { - selection.start = selection.start.bias_right(buffer).unwrap(); - selection.end = selection.end.bias_right(buffer).unwrap(); + selection.start = selection.start.bias_right(buffer); + selection.end = selection.end.bias_right(buffer); } } self.update_selections(selections.clone(), false, ctx); @@ -876,11 +874,10 @@ impl BufferView { // Copy the text from the selected row region and splice it at the start of the region. let start = Point::new(rows.start, 0); - let end = Point::new(rows.end - 1, buffer.line_len(rows.end - 1).unwrap()); + let end = Point::new(rows.end - 1, buffer.line_len(rows.end - 1)); let text = buffer .text_for_range(start..end) - .unwrap() - .chain(Some('\n')) + .chain(Some("\n")) .collect::(); edits.push((start, text)); } @@ -894,8 +891,8 @@ impl BufferView { // Restore bias on selections. let buffer = self.buffer.read(ctx); for selection in &mut selections { - selection.start = selection.start.bias_left(buffer).unwrap(); - selection.end = selection.end.bias_left(buffer).unwrap(); + selection.start = selection.start.bias_left(buffer); + selection.end = selection.end.bias_left(buffer); } self.update_selections(selections, true, ctx); @@ -935,13 +932,9 @@ impl BufferView { // Cut the text from the selected rows and paste it at the start of the previous line. if display_rows.start != 0 { - let start = Point::new(buffer_rows.start, 0).to_offset(buffer).unwrap(); - let end = Point::new( - buffer_rows.end - 1, - buffer.line_len(buffer_rows.end - 1).unwrap(), - ) - .to_offset(buffer) - .unwrap(); + let start = Point::new(buffer_rows.start, 0).to_offset(buffer); + let end = Point::new(buffer_rows.end - 1, buffer.line_len(buffer_rows.end - 1)) + .to_offset(buffer); let prev_row_display_start = DisplayPoint::new(display_rows.start - 1, 0); let prev_row_start = prev_row_display_start @@ -949,7 +942,7 @@ impl BufferView { .unwrap(); let mut text = String::new(); - text.extend(buffer.text_for_range(start..end).unwrap()); + text.extend(buffer.text_for_range(start..end)); text.push('\n'); edits.push((prev_row_start..prev_row_start, text)); edits.push((start - 1..end, String::new())); @@ -969,8 +962,8 @@ impl BufferView { // Move folds up. old_folds.push(start..end); for fold in self.display_map.folds_in_range(start..end, app).unwrap() { - let mut start = fold.start.to_point(buffer).unwrap(); - let mut end = fold.end.to_point(buffer).unwrap(); + let mut start = fold.start.to_point(buffer); + let mut end = fold.end.to_point(buffer); start.row -= row_delta; end.row -= row_delta; new_folds.push(start..end); @@ -1025,13 +1018,9 @@ impl BufferView { // Cut the text from the selected rows and paste it at the end of the next line. if display_rows.end <= self.display_map.max_point(app).row() { - let start = Point::new(buffer_rows.start, 0).to_offset(buffer).unwrap(); - let end = Point::new( - buffer_rows.end - 1, - buffer.line_len(buffer_rows.end - 1).unwrap(), - ) - .to_offset(buffer) - .unwrap(); + let start = Point::new(buffer_rows.start, 0).to_offset(buffer); + let end = Point::new(buffer_rows.end - 1, buffer.line_len(buffer_rows.end - 1)) + .to_offset(buffer); let next_row_display_end = DisplayPoint::new( display_rows.end, @@ -1043,7 +1032,7 @@ impl BufferView { let mut text = String::new(); text.push('\n'); - text.extend(buffer.text_for_range(start..end).unwrap()); + text.extend(buffer.text_for_range(start..end)); edits.push((start..end + 1, String::new())); edits.push((next_row_end..next_row_end, text)); @@ -1063,8 +1052,8 @@ impl BufferView { // Move folds down. old_folds.push(start..end); for fold in self.display_map.folds_in_range(start..end, app).unwrap() { - let mut start = fold.start.to_point(buffer).unwrap(); - let mut end = fold.end.to_point(buffer).unwrap(); + let mut start = fold.start.to_point(buffer); + let mut end = fold.end.to_point(buffer); start.row += row_delta; end.row += row_delta; new_folds.push(start..end); @@ -1095,19 +1084,19 @@ impl BufferView { let buffer = self.buffer.read(ctx); let max_point = buffer.max_point(); for selection in &mut selections { - let mut start = selection.start.to_point(buffer).expect("invalid start"); - let mut end = selection.end.to_point(buffer).expect("invalid end"); + let mut start = selection.start.to_point(buffer); + let mut end = selection.end.to_point(buffer); let is_entire_line = start == end; if is_entire_line { start = Point::new(start.row, 0); end = cmp::min(max_point, Point::new(start.row + 1, 0)); - selection.start = buffer.anchor_before(start).unwrap(); - selection.end = buffer.anchor_before(end).unwrap(); + selection.start = buffer.anchor_before(start); + selection.end = buffer.anchor_before(end); } let mut len = 0; - for ch in buffer.text_for_range(start..end).unwrap() { - text.push(ch); - len += 1; + for chunk in buffer.text_for_range(start..end) { + text.push_str(chunk); + len += chunk.len(); } clipboard_selections.push(ClipboardSelection { len, @@ -1130,17 +1119,17 @@ impl BufferView { let selections = self.selections(ctx.as_ref()); let mut clipboard_selections = Vec::with_capacity(selections.len()); for selection in selections { - let mut start = selection.start.to_point(buffer).expect("invalid start"); - let mut end = selection.end.to_point(buffer).expect("invalid end"); + let mut start = selection.start.to_point(buffer); + let mut end = selection.end.to_point(buffer); let is_entire_line = start == end; if is_entire_line { start = Point::new(start.row, 0); end = cmp::min(max_point, Point::new(start.row + 1, 0)); } let mut len = 0; - for ch in buffer.text_for_range(start..end).unwrap() { - text.push(ch); - len += 1; + for chunk in buffer.text_for_range(start..end) { + text.push_str(chunk); + len += chunk.len(); } clipboard_selections.push(ClipboardSelection { len, @@ -1176,14 +1165,14 @@ impl BufferView { String::from_iter(clipboard_chars.by_ref().take(clipboard_selection.len)); self.buffer.update(ctx, |buffer, ctx| { - let selection_start = selection.start.to_point(buffer).unwrap(); - let selection_end = selection.end.to_point(buffer).unwrap(); + let selection_start = selection.start.to_point(buffer); + let selection_end = selection.end.to_point(buffer); // If the corresponding selection was empty when this slice of the // clipboard text was written, then the entire line containing the // selection was copied. If this selection is also currently empty, // then paste the line before the current line of the buffer. - let new_selection_start = selection.end.bias_right(buffer).unwrap(); + let new_selection_start = selection.end.bias_right(buffer); if selection_start == selection_end && clipboard_selection.is_entire_line { let line_start = Point::new(selection_start.row, 0); buffer @@ -1195,7 +1184,7 @@ impl BufferView { .unwrap(); }; - let new_selection_start = new_selection_start.bias_left(buffer).unwrap(); + let new_selection_start = new_selection_start.bias_left(buffer); new_selections.push(Selection { id: selection.id, start: new_selection_start.clone(), @@ -1678,7 +1667,7 @@ impl BufferView { pub fn move_to_beginning(&mut self, _: &(), ctx: &mut ViewContext) { let buffer = self.buffer.read(ctx); - let cursor = buffer.anchor_before(Point::new(0, 0)).unwrap(); + let cursor = buffer.anchor_before(Point::new(0, 0)); let selection = Selection { id: post_inc(&mut self.next_selection_id), start: cursor.clone(), @@ -1697,7 +1686,7 @@ impl BufferView { pub fn move_to_end(&mut self, _: &(), ctx: &mut ViewContext) { let buffer = self.buffer.read(ctx); - let cursor = buffer.anchor_before(buffer.max_point()).unwrap(); + let cursor = buffer.anchor_before(buffer.max_point()); let selection = Selection { id: post_inc(&mut self.next_selection_id), start: cursor.clone(), @@ -1732,10 +1721,8 @@ impl BufferView { let max_point = buffer.max_point(); for selection in &mut selections { let (rows, _) = selection.buffer_rows_for_display_rows(true, &self.display_map, app); - selection.start = buffer.anchor_before(Point::new(rows.start, 0)).unwrap(); - selection.end = buffer - .anchor_before(cmp::min(max_point, Point::new(rows.end, 0))) - .unwrap(); + selection.start = buffer.anchor_before(Point::new(rows.start, 0)); + selection.end = buffer.anchor_before(cmp::min(max_point, Point::new(rows.end, 0))); selection.reversed = false; } self.update_selections(selections, true, ctx); @@ -1761,9 +1748,7 @@ impl BufferView { }); } for row in range.start.row + 1..range.end.row { - let cursor = buffer - .anchor_before(Point::new(row, buffer.line_len(row).unwrap())) - .unwrap(); + let cursor = buffer.anchor_before(Point::new(row, buffer.line_len(row))); new_selections.push(Selection { id: post_inc(&mut self.next_selection_id), start: cursor.clone(), @@ -2085,7 +2070,7 @@ impl BufferView { .to_buffer_point(&self.display_map, Bias::Left, app) .unwrap(); start.column = 0; - end.column = buffer.line_len(end.row).unwrap(); + end.column = buffer.line_len(end.row); start..end }) .collect::>(); diff --git a/zed/src/editor/display_map/fold_map.rs b/zed/src/editor/display_map/fold_map.rs index 89eb7b062e1e2ef8ec06df13bf303d8ec7d9b116..417ad04f319517e9015a7cc601efcf059abf4888 100644 --- a/zed/src/editor/display_map/fold_map.rs +++ b/zed/src/editor/display_map/fold_map.rs @@ -52,7 +52,7 @@ impl FoldMap { } pub fn len(&self, ctx: &AppContext) -> usize { - self.sync(ctx).summary().display.chars + self.sync(ctx).summary().display.bytes } pub fn line_len(&self, row: u32, ctx: &AppContext) -> Result { @@ -98,10 +98,9 @@ impl FoldMap { let mut folds = Vec::new(); let buffer = self.buffer.read(ctx); for range in ranges.into_iter() { - let range = range.start.to_offset(buffer)?..range.end.to_offset(buffer)?; + let range = range.start.to_offset(buffer)..range.end.to_offset(buffer); if range.start != range.end { - let fold = - Fold(buffer.anchor_after(range.start)?..buffer.anchor_before(range.end)?); + let fold = Fold(buffer.anchor_after(range.start)..buffer.anchor_before(range.end)); folds.push(fold); edits.push(Edit { old_range: range.clone(), @@ -147,8 +146,7 @@ impl FoldMap { // Remove intersecting folds and add their ranges to edits that are passed to apply_edits. let mut folds_cursor = self.intersecting_folds(range, ctx)?; while let Some(fold) = folds_cursor.item() { - let offset_range = - fold.0.start.to_offset(buffer).unwrap()..fold.0.end.to_offset(buffer).unwrap(); + let offset_range = fold.0.start.to_offset(buffer)..fold.0.end.to_offset(buffer); edits.push(Edit { old_range: offset_range.clone(), new_range: offset_range, @@ -190,8 +188,8 @@ impl FoldMap { T: ToOffset, { let buffer = self.buffer.read(ctx); - let start = buffer.anchor_before(range.start.to_offset(buffer)?)?; - let end = buffer.anchor_after(range.end.to_offset(buffer)?)?; + let start = buffer.anchor_before(range.start.to_offset(buffer)); + let end = buffer.anchor_after(range.end.to_offset(buffer)); Ok(self.folds.filter::<_, usize>(move |summary| { start.cmp(&summary.max_end, buffer).unwrap() == Ordering::Less && end.cmp(&summary.min_start, buffer).unwrap() == Ordering::Greater @@ -215,7 +213,7 @@ impl FoldMap { false } - pub fn to_buffer_offset(&self, point: DisplayPoint, ctx: &AppContext) -> Result { + pub fn to_buffer_offset(&self, point: DisplayPoint, ctx: &AppContext) -> usize { let transforms = self.sync(ctx); let mut cursor = transforms.cursor::(); cursor.seek(&point, SeekBias::Right, &()); @@ -305,11 +303,11 @@ impl FoldMap { edit.new_range.end = ((edit.new_range.start + edit.old_extent()) as isize + delta) as usize; - let anchor = buffer.anchor_before(edit.new_range.start).unwrap(); + let anchor = buffer.anchor_before(edit.new_range.start); let mut folds_cursor = self.folds.cursor::<_, ()>(); folds_cursor.seek(&Fold(anchor..Anchor::End), SeekBias::Left, buffer); let mut folds = folds_cursor - .map(|f| f.0.start.to_offset(buffer).unwrap()..f.0.end.to_offset(buffer).unwrap()) + .map(|f| f.0.start.to_offset(buffer)..f.0.end.to_offset(buffer)) .peekable(); while folds @@ -319,7 +317,7 @@ impl FoldMap { let mut fold = folds.next().unwrap(); let sum = new_transforms.summary(); - assert!(fold.start >= sum.buffer.chars); + assert!(fold.start >= sum.buffer.bytes); while folds .peek() @@ -331,8 +329,8 @@ impl FoldMap { } } - if fold.start > sum.buffer.chars { - let text_summary = buffer.text_summary_for_range(sum.buffer.chars..fold.start); + if fold.start > sum.buffer.bytes { + let text_summary = buffer.text_summary_for_range(sum.buffer.bytes..fold.start); new_transforms.push( Transform { summary: TransformSummary { @@ -350,7 +348,6 @@ impl FoldMap { Transform { summary: TransformSummary { display: TextSummary { - chars: 1, bytes: '…'.len_utf8(), lines: Point::new(0, 1), first_line_len: 1, @@ -366,9 +363,9 @@ impl FoldMap { } let sum = new_transforms.summary(); - if sum.buffer.chars < edit.new_range.end { + if sum.buffer.bytes < edit.new_range.end { let text_summary = - buffer.text_summary_for_range(sum.buffer.chars..edit.new_range.end); + buffer.text_summary_for_range(sum.buffer.bytes..edit.new_range.end); new_transforms.push( Transform { summary: TransformSummary { @@ -439,15 +436,15 @@ impl FoldMapSnapshot { let mut cursor = self.transforms.cursor::(); cursor.seek(&point, SeekBias::Right, &()); let overshoot = point.0 - cursor.start().display.lines; - let mut offset = cursor.start().display.chars; + let mut offset = cursor.start().display.bytes; if !overshoot.is_zero() { let transform = cursor .item() .ok_or_else(|| anyhow!("display point {:?} is out of range", point))?; assert!(transform.display_text.is_none()); let end_buffer_offset = - (cursor.start().buffer.lines + overshoot).to_offset(self.buffer.read(ctx))?; - offset += end_buffer_offset - cursor.start().buffer.chars; + (cursor.start().buffer.lines + overshoot).to_offset(self.buffer.read(ctx)); + offset += end_buffer_offset - cursor.start().buffer.bytes; } Ok(DisplayOffset(offset)) } @@ -620,7 +617,7 @@ impl<'a> Iterator for Chars<'a> { return Some(c); } - while self.offset == self.cursor.end().display.chars && self.cursor.item().is_some() { + while self.offset == self.cursor.end().display.bytes && self.cursor.item().is_some() { self.cursor.next(); } @@ -629,11 +626,10 @@ impl<'a> Iterator for Chars<'a> { self.offset += 1; Some(c) } else { - let overshoot = self.offset - self.cursor.start().display.chars; - let buffer_start = self.cursor.start().buffer.chars + overshoot; - let char_count = self.cursor.end().buffer.chars - buffer_start; - self.buffer_chars = - Some(self.buffer.chars_at(buffer_start).unwrap().take(char_count)); + let overshoot = self.offset - self.cursor.start().display.bytes; + let buffer_start = self.cursor.start().buffer.bytes + overshoot; + let char_count = self.cursor.end().buffer.bytes - buffer_start; + self.buffer_chars = Some(self.buffer.chars_at(buffer_start).take(char_count)); self.next() } }) @@ -651,7 +647,7 @@ pub struct DisplayOffset(usize); impl<'a> sum_tree::Dimension<'a, TransformSummary> for DisplayOffset { fn add_summary(&mut self, summary: &'a TransformSummary) { - self.0 += &summary.display.chars; + self.0 += &summary.display.bytes; } } @@ -663,7 +659,7 @@ impl<'a> sum_tree::Dimension<'a, TransformSummary> for Point { impl<'a> sum_tree::Dimension<'a, TransformSummary> for usize { fn add_summary(&mut self, summary: &'a TransformSummary) { - *self += &summary.buffer.chars; + *self += &summary.buffer.bytes; } } @@ -816,7 +812,7 @@ mod tests { let fold_ranges = map .folds_in_range(Point::new(1, 0)..Point::new(1, 3), app.as_ref()) .unwrap() - .map(|fold| fold.start.to_point(buffer).unwrap()..fold.end.to_point(buffer).unwrap()) + .map(|fold| fold.start.to_point(buffer)..fold.end.to_point(buffer)) .collect::>(); assert_eq!( fold_ranges, @@ -830,7 +826,7 @@ mod tests { #[gpui::test] fn test_random_folds(app: &mut gpui::MutableAppContext) { use crate::editor::ToPoint; - use crate::util::{byte_range_for_char_range, RandomCharIter}; + use crate::util::RandomCharIter; use rand::prelude::*; use std::env; @@ -905,10 +901,7 @@ mod tests { expected_buffer_rows.extend((fold_end.row + 1..=next_row).rev()); next_row = fold_start.row; - expected_text.replace_range( - byte_range_for_char_range(&expected_text, fold_range.start..fold_range.end), - "…", - ); + expected_text.replace_range(fold_range.start..fold_range.end, "…"); } expected_buffer_rows.extend((0..=next_row).rev()); expected_buffer_rows.reverse(); @@ -925,13 +918,13 @@ mod tests { let mut display_offset = DisplayOffset(0); for c in expected_text.chars() { let buffer_point = map.to_buffer_point(display_point, app.as_ref()); - let buffer_offset = buffer_point.to_offset(buffer).unwrap(); + let buffer_offset = buffer_point.to_offset(buffer); assert_eq!( map.to_display_point(buffer_point, app.as_ref()), display_point ); assert_eq!( - map.to_buffer_offset(display_point, app.as_ref()).unwrap(), + map.to_buffer_offset(display_point, app.as_ref()), buffer_offset ); assert_eq!( @@ -988,8 +981,8 @@ mod tests { } for fold_range in map.merged_fold_ranges(app.as_ref()) { - let display_point = map - .to_display_point(fold_range.start.to_point(buffer).unwrap(), app.as_ref()); + let display_point = + map.to_display_point(fold_range.start.to_point(buffer), app.as_ref()); assert!(map.is_line_folded(display_point.row(), app.as_ref())); } @@ -1001,8 +994,8 @@ mod tests { .items() .into_iter() .filter(|fold| { - let start = buffer.anchor_before(start).unwrap(); - let end = buffer.anchor_after(end).unwrap(); + let start = buffer.anchor_before(start); + let end = buffer.anchor_after(end); start.cmp(&fold.0.end, buffer).unwrap() == Ordering::Less && end.cmp(&fold.0.start, buffer).unwrap() == Ordering::Greater }) @@ -1069,9 +1062,7 @@ mod tests { folds.sort_by(|a, b| a.0.cmp(&b.0, buffer).unwrap()); let mut fold_ranges = folds .iter() - .map(|fold| { - fold.0.start.to_offset(buffer).unwrap()..fold.0.end.to_offset(buffer).unwrap() - }) + .map(|fold| fold.0.start.to_offset(buffer)..fold.0.end.to_offset(buffer)) .peekable(); let mut merged_ranges = Vec::new(); @@ -1097,7 +1088,7 @@ mod tests { let transforms = self.sync(ctx); let buffer = self.buffer.read(ctx); assert_eq!( - transforms.summary().buffer.chars, + transforms.summary().buffer.bytes, buffer.len(), "transform tree does not match buffer's length" ); diff --git a/zed/src/editor/display_map/mod.rs b/zed/src/editor/display_map/mod.rs index 7ee540bd2e094522f9f2fd417ed7a2b68f5a9a4f..dbc0a3005c4c342cf7dd3ca7da6f55af1f02a5f0 100644 --- a/zed/src/editor/display_map/mod.rs +++ b/zed/src/editor/display_map/mod.rs @@ -118,9 +118,10 @@ impl DisplayMap { bias: Bias, app: &AppContext, ) -> Result { - self.buffer + Ok(self + .buffer .read(app) - .anchor_before(point.to_buffer_point(self, bias, app)?) + .anchor_before(point.to_buffer_point(self, bias, app)?)) } pub fn anchor_after( @@ -129,9 +130,10 @@ impl DisplayMap { bias: Bias, app: &AppContext, ) -> Result { - self.buffer + Ok(self + .buffer .read(app) - .anchor_after(point.to_buffer_point(self, bias, app)?) + .anchor_after(point.to_buffer_point(self, bias, app)?)) } } @@ -222,8 +224,9 @@ impl DisplayPoint { } pub fn to_buffer_offset(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> Result { - map.fold_map - .to_buffer_offset(self.collapse_tabs(&map, bias, ctx)?.0, ctx) + Ok(map + .fold_map + .to_buffer_offset(self.collapse_tabs(&map, bias, ctx)?.0, ctx)) } fn expand_tabs(self, map: &DisplayMap, ctx: &AppContext) -> Result { @@ -253,7 +256,7 @@ impl Point { impl Anchor { pub fn to_display_point(&self, map: &DisplayMap, app: &AppContext) -> Result { - self.to_point(map.buffer.read(app))? + self.to_point(map.buffer.read(app)) .to_display_point(map, app) } } diff --git a/zed/src/util.rs b/zed/src/util.rs index 18227dc2d228bf968d233c996cf7a0381e3e5ffa..6015ce7e0a4acc4a9929cd62bbda8db2e833f649 100644 --- a/zed/src/util.rs +++ b/zed/src/util.rs @@ -1,5 +1,5 @@ use rand::prelude::*; -use std::{cmp::Ordering, ops::Range}; +use std::cmp::Ordering; pub fn post_inc(value: &mut usize) -> usize { let prev = *value; @@ -33,6 +33,7 @@ where pub struct RandomCharIter(T); impl RandomCharIter { + #[cfg(test)] pub fn new(rng: T) -> Self { Self(rng) } From b3d2a70834d19acbeb939d243508c65eda520d03 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 17 May 2021 13:55:55 -0700 Subject: [PATCH 04/18] Don't return Results from position methods in FoldMap and DisplayMap --- zed/src/editor/buffer/selection.rs | 21 +- zed/src/editor/buffer_element.rs | 2 +- zed/src/editor/buffer_view.rs | 350 ++++++++----------------- zed/src/editor/display_map/fold_map.rs | 105 +++----- zed/src/editor/display_map/mod.rs | 114 ++++---- zed/src/editor/movement.rs | 18 +- 6 files changed, 207 insertions(+), 403 deletions(-) diff --git a/zed/src/editor/buffer/selection.rs b/zed/src/editor/buffer/selection.rs index 95ad181beac7bf3988f2c632822c8697f26ea4f7..016cbe59b3f5530e5fcc097bf9045923c4df0b8c 100644 --- a/zed/src/editor/buffer/selection.rs +++ b/zed/src/editor/buffer/selection.rs @@ -72,8 +72,8 @@ impl Selection { } pub fn display_range(&self, map: &DisplayMap, app: &AppContext) -> Range { - let start = self.start.to_display_point(map, app).unwrap(); - let end = self.end.to_display_point(map, app).unwrap(); + let start = self.start.to_display_point(map, app); + let end = self.end.to_display_point(map, app); if self.reversed { end..start } else { @@ -87,12 +87,11 @@ impl Selection { map: &DisplayMap, ctx: &AppContext, ) -> (Range, Range) { - let display_start = self.start.to_display_point(map, ctx).unwrap(); - let buffer_start = DisplayPoint::new(display_start.row(), 0) - .to_buffer_point(map, Bias::Left, ctx) - .unwrap(); + let display_start = self.start.to_display_point(map, ctx); + let buffer_start = + DisplayPoint::new(display_start.row(), 0).to_buffer_point(map, Bias::Left, ctx); - let mut display_end = self.end.to_display_point(map, ctx).unwrap(); + let mut display_end = self.end.to_display_point(map, ctx); if !include_end_if_at_line_start && display_end.row() != map.max_point(ctx).row() && display_start.row() != display_end.row() @@ -100,12 +99,8 @@ impl Selection { { *display_end.row_mut() -= 1; } - let buffer_end = DisplayPoint::new( - display_end.row(), - map.line_len(display_end.row(), ctx).unwrap(), - ) - .to_buffer_point(map, Bias::Left, ctx) - .unwrap(); + let buffer_end = DisplayPoint::new(display_end.row(), map.line_len(display_end.row(), ctx)) + .to_buffer_point(map, Bias::Left, ctx); ( buffer_start.row..buffer_end.row + 1, diff --git a/zed/src/editor/buffer_element.rs b/zed/src/editor/buffer_element.rs index 8ceb72c1e94a26670be8dcbdca4a8d53fb574d6d..d3f002239f1986ab5505f3d4d933d74f143d8a04 100644 --- a/zed/src/editor/buffer_element.rs +++ b/zed/src/editor/buffer_element.rs @@ -569,7 +569,7 @@ impl PaintState { let column = if x >= 0.0 { line.index_for_x(x) .map(|ix| ix as u32) - .unwrap_or(view.line_len(row, app).unwrap()) + .unwrap_or(view.line_len(row, app)) } else { 0 }; diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index ebf10a4f4a4036f5d8d104a9fc12466b8d0b88e8..ff8ea18cf086526fde99a7f8280afff8171b5e8d 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -411,7 +411,6 @@ impl BufferView { .unwrap() .head() .to_display_point(&self.display_map, app) - .unwrap() .row() as f32; let last_cursor_bottom = self .selections(app) @@ -419,7 +418,6 @@ impl BufferView { .unwrap() .head() .to_display_point(&self.display_map, app) - .unwrap() .row() as f32 + 1.0; @@ -456,13 +454,10 @@ impl BufferView { let mut target_left = std::f32::INFINITY; let mut target_right = 0.0_f32; for selection in self.selections(ctx) { - let head = selection - .head() - .to_display_point(&self.display_map, ctx) - .unwrap(); + let head = selection.head().to_display_point(&self.display_map, ctx); let start_column = head.column().saturating_sub(3); let end_column = cmp::min( - self.display_map.line_len(head.row(), ctx).unwrap(), + self.display_map.line_len(head.row(), ctx), head.column() + 3, ); target_left = target_left @@ -508,8 +503,7 @@ impl BufferView { let cursor = self .display_map - .anchor_before(position, Bias::Left, ctx.as_ref()) - .unwrap(); + .anchor_before(position, Bias::Left, ctx.as_ref()); let selection = Selection { id: post_inc(&mut self.next_selection_id), start: cursor.clone(), @@ -535,8 +529,7 @@ impl BufferView { let buffer = self.buffer.read(ctx); let cursor = self .display_map - .anchor_before(position, Bias::Left, ctx.as_ref()) - .unwrap(); + .anchor_before(position, Bias::Left, ctx.as_ref()); if let Some(selection) = self.pending_selection.as_mut() { selection.set_head(buffer, cursor); } else { @@ -627,10 +620,10 @@ impl BufferView { id: post_inc(&mut self.next_selection_id), start: self .display_map - .anchor_before(start, Bias::Left, ctx.as_ref())?, + .anchor_before(start, Bias::Left, ctx.as_ref()), end: self .display_map - .anchor_before(end, Bias::Left, ctx.as_ref())?, + .anchor_before(end, Bias::Left, ctx.as_ref()), reversed, goal: SelectionGoal::None, }); @@ -700,16 +693,12 @@ impl BufferView { if range.start == range.end { let head = selection .head() - .to_display_point(&self.display_map, ctx.as_ref()) - .unwrap(); - let cursor = self - .display_map - .anchor_before( - movement::left(&self.display_map, head, ctx.as_ref()).unwrap(), - Bias::Left, - ctx.as_ref(), - ) - .unwrap(); + .to_display_point(&self.display_map, ctx.as_ref()); + let cursor = self.display_map.anchor_before( + movement::left(&self.display_map, head, ctx.as_ref()).unwrap(), + Bias::Left, + ctx.as_ref(), + ); selection.set_head(&buffer, cursor); selection.goal = SelectionGoal::None; } @@ -731,16 +720,12 @@ impl BufferView { if range.start == range.end { let head = selection .head() - .to_display_point(&self.display_map, ctx.as_ref()) - .unwrap(); - let cursor = self - .display_map - .anchor_before( - movement::right(&self.display_map, head, ctx.as_ref()).unwrap(), - Bias::Right, - ctx.as_ref(), - ) - .unwrap(); + .to_display_point(&self.display_map, ctx.as_ref()); + let cursor = self.display_map.anchor_before( + movement::right(&self.display_map, head, ctx.as_ref()).unwrap(), + Bias::Right, + ctx.as_ref(), + ); selection.set_head(&buffer, cursor); selection.goal = SelectionGoal::None; } @@ -768,7 +753,6 @@ impl BufferView { let goal_display_column = selection .head() .to_display_point(&self.display_map, app) - .unwrap() .column(); // Accumulate contiguous regions of rows that we want to delete. @@ -799,19 +783,16 @@ impl BufferView { cursor_buffer_row = rows.start.saturating_sub(1); } - let mut cursor = Point::new(cursor_buffer_row, 0) - .to_display_point(&self.display_map, app) - .unwrap(); + let mut cursor = + Point::new(cursor_buffer_row, 0).to_display_point(&self.display_map, app); *cursor.column_mut() = cmp::min( goal_display_column, - self.display_map.line_len(cursor.row(), app).unwrap(), + self.display_map.line_len(cursor.row(), app), ); new_cursors.push(( selection.id, - cursor - .to_buffer_point(&self.display_map, Bias::Left, app) - .unwrap(), + cursor.to_buffer_point(&self.display_map, Bias::Left, app), )); edit_ranges.push(edit_start..edit_end); } @@ -937,9 +918,8 @@ impl BufferView { .to_offset(buffer); let prev_row_display_start = DisplayPoint::new(display_rows.start - 1, 0); - let prev_row_start = prev_row_display_start - .to_buffer_offset(&self.display_map, Bias::Left, app) - .unwrap(); + let prev_row_start = + prev_row_display_start.to_buffer_offset(&self.display_map, Bias::Left, app); let mut text = String::new(); text.extend(buffer.text_for_range(start..end)); @@ -950,7 +930,6 @@ impl BufferView { let row_delta = buffer_rows.start - prev_row_display_start .to_buffer_point(&self.display_map, Bias::Left, app) - .unwrap() .row; // Move selections up. @@ -961,7 +940,7 @@ impl BufferView { // Move folds up. old_folds.push(start..end); - for fold in self.display_map.folds_in_range(start..end, app).unwrap() { + for fold in self.display_map.folds_in_range(start..end, app) { let mut start = fold.start.to_point(buffer); let mut end = fold.end.to_point(buffer); start.row -= row_delta; @@ -1024,11 +1003,10 @@ impl BufferView { let next_row_display_end = DisplayPoint::new( display_rows.end, - self.display_map.line_len(display_rows.end, app).unwrap(), + self.display_map.line_len(display_rows.end, app), ); - let next_row_end = next_row_display_end - .to_buffer_offset(&self.display_map, Bias::Right, app) - .unwrap(); + let next_row_end = + next_row_display_end.to_buffer_offset(&self.display_map, Bias::Right, app); let mut text = String::new(); text.push('\n'); @@ -1038,7 +1016,6 @@ impl BufferView { let row_delta = next_row_display_end .to_buffer_point(&self.display_map, Bias::Right, app) - .unwrap() .row - buffer_rows.end + 1; @@ -1051,7 +1028,7 @@ impl BufferView { // Move folds down. old_folds.push(start..end); - for fold in self.display_map.folds_in_range(start..end, app).unwrap() { + for fold in self.display_map.folds_in_range(start..end, app) { let mut start = fold.start.to_point(buffer); let mut end = fold.end.to_point(buffer); start.row += row_delta; @@ -1217,26 +1194,17 @@ impl BufferView { let mut selections = self.selections(app).to_vec(); { for selection in &mut selections { - let start = selection - .start - .to_display_point(&self.display_map, app) - .unwrap(); - let end = selection - .end - .to_display_point(&self.display_map, app) - .unwrap(); + let start = selection.start.to_display_point(&self.display_map, app); + let end = selection.end.to_display_point(&self.display_map, app); if start != end { selection.end = selection.start.clone(); } else { - let cursor = self - .display_map - .anchor_before( - movement::left(&self.display_map, start, app).unwrap(), - Bias::Left, - app, - ) - .unwrap(); + let cursor = self.display_map.anchor_before( + movement::left(&self.display_map, start, app).unwrap(), + Bias::Left, + app, + ); selection.start = cursor.clone(); selection.end = cursor; } @@ -1254,16 +1222,12 @@ impl BufferView { for selection in &mut selections { let head = selection .head() - .to_display_point(&self.display_map, ctx.as_ref()) - .unwrap(); - let cursor = self - .display_map - .anchor_before( - movement::left(&self.display_map, head, ctx.as_ref()).unwrap(), - Bias::Left, - ctx.as_ref(), - ) - .unwrap(); + .to_display_point(&self.display_map, ctx.as_ref()); + let cursor = self.display_map.anchor_before( + movement::left(&self.display_map, head, ctx.as_ref()).unwrap(), + Bias::Left, + ctx.as_ref(), + ); selection.set_head(&buffer, cursor); selection.goal = SelectionGoal::None; } @@ -1276,26 +1240,17 @@ impl BufferView { { let app = ctx.as_ref(); for selection in &mut selections { - let start = selection - .start - .to_display_point(&self.display_map, app) - .unwrap(); - let end = selection - .end - .to_display_point(&self.display_map, app) - .unwrap(); + let start = selection.start.to_display_point(&self.display_map, app); + let end = selection.end.to_display_point(&self.display_map, app); if start != end { selection.start = selection.end.clone(); } else { - let cursor = self - .display_map - .anchor_before( - movement::right(&self.display_map, end, app).unwrap(), - Bias::Right, - app, - ) - .unwrap(); + let cursor = self.display_map.anchor_before( + movement::right(&self.display_map, end, app).unwrap(), + Bias::Right, + app, + ); selection.start = cursor.clone(); selection.end = cursor; } @@ -1314,16 +1269,12 @@ impl BufferView { for selection in &mut selections { let head = selection .head() - .to_display_point(&self.display_map, ctx.as_ref()) - .unwrap(); - let cursor = self - .display_map - .anchor_before( - movement::right(&self.display_map, head, app).unwrap(), - Bias::Right, - app, - ) - .unwrap(); + .to_display_point(&self.display_map, ctx.as_ref()); + let cursor = self.display_map.anchor_before( + movement::right(&self.display_map, head, app).unwrap(), + Bias::Right, + app, + ); selection.set_head(&buffer, cursor); selection.goal = SelectionGoal::None; } @@ -1339,24 +1290,15 @@ impl BufferView { { let app = ctx.as_ref(); for selection in &mut selections { - let start = selection - .start - .to_display_point(&self.display_map, app) - .unwrap(); - let end = selection - .end - .to_display_point(&self.display_map, app) - .unwrap(); + let start = selection.start.to_display_point(&self.display_map, app); + let end = selection.end.to_display_point(&self.display_map, app); if start != end { selection.goal = SelectionGoal::None; } let (start, goal) = movement::up(&self.display_map, start, selection.goal, app).unwrap(); - let cursor = self - .display_map - .anchor_before(start, Bias::Left, app) - .unwrap(); + let cursor = self.display_map.anchor_before(start, Bias::Left, app); selection.start = cursor.clone(); selection.end = cursor; selection.goal = goal; @@ -1373,17 +1315,12 @@ impl BufferView { let app = ctx.as_ref(); let buffer = self.buffer.read(app); for selection in &mut selections { - let head = selection - .head() - .to_display_point(&self.display_map, app) - .unwrap(); + let head = selection.head().to_display_point(&self.display_map, app); let (head, goal) = movement::up(&self.display_map, head, selection.goal, app).unwrap(); selection.set_head( &buffer, - self.display_map - .anchor_before(head, Bias::Left, app) - .unwrap(), + self.display_map.anchor_before(head, Bias::Left, app), ); selection.goal = goal; } @@ -1399,24 +1336,15 @@ impl BufferView { { let app = ctx.as_ref(); for selection in &mut selections { - let start = selection - .start - .to_display_point(&self.display_map, app) - .unwrap(); - let end = selection - .end - .to_display_point(&self.display_map, app) - .unwrap(); + let start = selection.start.to_display_point(&self.display_map, app); + let end = selection.end.to_display_point(&self.display_map, app); if start != end { selection.goal = SelectionGoal::None; } let (start, goal) = movement::down(&self.display_map, end, selection.goal, app).unwrap(); - let cursor = self - .display_map - .anchor_before(start, Bias::Right, app) - .unwrap(); + let cursor = self.display_map.anchor_before(start, Bias::Right, app); selection.start = cursor.clone(); selection.end = cursor; selection.goal = goal; @@ -1433,17 +1361,12 @@ impl BufferView { let app = ctx.as_ref(); let buffer = self.buffer.read(app); for selection in &mut selections { - let head = selection - .head() - .to_display_point(&self.display_map, app) - .unwrap(); + let head = selection.head().to_display_point(&self.display_map, app); let (head, goal) = movement::down(&self.display_map, head, selection.goal, app).unwrap(); selection.set_head( &buffer, - self.display_map - .anchor_before(head, Bias::Right, app) - .unwrap(), + self.display_map.anchor_before(head, Bias::Right, app), ); selection.goal = goal; } @@ -1456,15 +1379,9 @@ impl BufferView { let mut selections = self.selections(app).to_vec(); { for selection in &mut selections { - let head = selection - .head() - .to_display_point(&self.display_map, app) - .unwrap(); + let head = selection.head().to_display_point(&self.display_map, app); let new_head = movement::prev_word_boundary(&self.display_map, head, app).unwrap(); - let anchor = self - .display_map - .anchor_before(new_head, Bias::Left, app) - .unwrap(); + let anchor = self.display_map.anchor_before(new_head, Bias::Left, app); selection.start = anchor.clone(); selection.end = anchor; selection.reversed = false; @@ -1480,15 +1397,9 @@ impl BufferView { { let buffer = self.buffer.read(ctx); for selection in &mut selections { - let head = selection - .head() - .to_display_point(&self.display_map, app) - .unwrap(); + let head = selection.head().to_display_point(&self.display_map, app); let new_head = movement::prev_word_boundary(&self.display_map, head, app).unwrap(); - let anchor = self - .display_map - .anchor_before(new_head, Bias::Left, app) - .unwrap(); + let anchor = self.display_map.anchor_before(new_head, Bias::Left, app); selection.set_head(buffer, anchor); selection.goal = SelectionGoal::None; } @@ -1508,15 +1419,9 @@ impl BufferView { let mut selections = self.selections(app).to_vec(); { for selection in &mut selections { - let head = selection - .head() - .to_display_point(&self.display_map, app) - .unwrap(); + let head = selection.head().to_display_point(&self.display_map, app); let new_head = movement::next_word_boundary(&self.display_map, head, app).unwrap(); - let anchor = self - .display_map - .anchor_before(new_head, Bias::Left, app) - .unwrap(); + let anchor = self.display_map.anchor_before(new_head, Bias::Left, app); selection.start = anchor.clone(); selection.end = anchor; selection.reversed = false; @@ -1532,15 +1437,9 @@ impl BufferView { { let buffer = self.buffer.read(ctx); for selection in &mut selections { - let head = selection - .head() - .to_display_point(&self.display_map, app) - .unwrap(); + let head = selection.head().to_display_point(&self.display_map, app); let new_head = movement::next_word_boundary(&self.display_map, head, app).unwrap(); - let anchor = self - .display_map - .anchor_before(new_head, Bias::Left, app) - .unwrap(); + let anchor = self.display_map.anchor_before(new_head, Bias::Left, app); selection.set_head(buffer, anchor); selection.goal = SelectionGoal::None; } @@ -1560,16 +1459,10 @@ impl BufferView { let mut selections = self.selections(app).to_vec(); { for selection in &mut selections { - let head = selection - .head() - .to_display_point(&self.display_map, app) - .unwrap(); + let head = selection.head().to_display_point(&self.display_map, app); let new_head = movement::line_beginning(&self.display_map, head, true, app).unwrap(); - let anchor = self - .display_map - .anchor_before(new_head, Bias::Left, app) - .unwrap(); + let anchor = self.display_map.anchor_before(new_head, Bias::Left, app); selection.start = anchor.clone(); selection.end = anchor; selection.reversed = false; @@ -1589,16 +1482,10 @@ impl BufferView { { let buffer = self.buffer.read(ctx); for selection in &mut selections { - let head = selection - .head() - .to_display_point(&self.display_map, app) - .unwrap(); + let head = selection.head().to_display_point(&self.display_map, app); let new_head = movement::line_beginning(&self.display_map, head, *toggle_indent, app).unwrap(); - let anchor = self - .display_map - .anchor_before(new_head, Bias::Left, app) - .unwrap(); + let anchor = self.display_map.anchor_before(new_head, Bias::Left, app); selection.set_head(buffer, anchor); selection.goal = SelectionGoal::None; } @@ -1618,15 +1505,9 @@ impl BufferView { let mut selections = self.selections(app).to_vec(); { for selection in &mut selections { - let head = selection - .head() - .to_display_point(&self.display_map, app) - .unwrap(); + let head = selection.head().to_display_point(&self.display_map, app); let new_head = movement::line_end(&self.display_map, head, app).unwrap(); - let anchor = self - .display_map - .anchor_before(new_head, Bias::Left, app) - .unwrap(); + let anchor = self.display_map.anchor_before(new_head, Bias::Left, app); selection.start = anchor.clone(); selection.end = anchor; selection.reversed = false; @@ -1642,15 +1523,9 @@ impl BufferView { { let buffer = self.buffer.read(ctx); for selection in &mut selections { - let head = selection - .head() - .to_display_point(&self.display_map, app) - .unwrap(); + let head = selection.head().to_display_point(&self.display_map, app); let new_head = movement::line_end(&self.display_map, head, app).unwrap(); - let anchor = self - .display_map - .anchor_before(new_head, Bias::Left, app) - .unwrap(); + let anchor = self.display_map.anchor_before(new_head, Bias::Left, app); selection.set_head(buffer, anchor); selection.goal = SelectionGoal::None; } @@ -1878,20 +1753,14 @@ impl BufferView { ctx: &AppContext, ) -> Option { let is_empty = columns.start == columns.end; - let line_len = self.display_map.line_len(row, ctx).unwrap(); + let line_len = self.display_map.line_len(row, ctx); if columns.start < line_len || (is_empty && columns.start == line_len) { let start = DisplayPoint::new(row, columns.start); let end = DisplayPoint::new(row, cmp::min(columns.end, line_len)); Some(Selection { id: post_inc(&mut self.next_selection_id), - start: self - .display_map - .anchor_before(start, Bias::Left, ctx) - .unwrap(), - end: self - .display_map - .anchor_before(end, Bias::Left, ctx) - .unwrap(), + start: self.display_map.anchor_before(start, Bias::Left, ctx), + end: self.display_map.anchor_before(end, Bias::Left, ctx), reversed, goal: SelectionGoal::ColumnRange { start: columns.start, @@ -1908,10 +1777,7 @@ impl BufferView { range: Range, app: &'a AppContext, ) -> impl 'a + Iterator> { - let start = self - .display_map - .anchor_before(range.start, Bias::Left, app) - .unwrap(); + let start = self.display_map.anchor_before(range.start, Bias::Left, app); let start_index = self.selection_insertion_index(&start, app); let pending_selection = self.pending_selection.as_ref().and_then(|s| { let selection_range = s.display_range(&self.display_map, app); @@ -2030,14 +1896,13 @@ impl BufferView { let buffer_start_row = range .start .to_buffer_point(&self.display_map, Bias::Left, app) - .unwrap() .row; for row in (0..=range.end.row()).rev() { if self.is_line_foldable(row, app) && !self.display_map.is_line_folded(row, ctx.as_ref()) { - let fold_range = self.foldable_range_for_line(row, app).unwrap(); + let fold_range = self.foldable_range_for_line(row, app); if fold_range.end.row >= buffer_start_row { fold_ranges.push(fold_range); if row <= range.start.row() { @@ -2063,12 +1928,10 @@ impl BufferView { let range = s.display_range(&self.display_map, app).sorted(); let mut start = range .start - .to_buffer_point(&self.display_map, Bias::Left, app) - .unwrap(); + .to_buffer_point(&self.display_map, Bias::Left, app); let mut end = range .end - .to_buffer_point(&self.display_map, Bias::Left, app) - .unwrap(); + .to_buffer_point(&self.display_map, Bias::Left, app); start.column = 0; end.column = buffer.line_len(end.row); start..end @@ -2082,13 +1945,12 @@ impl BufferView { if display_row >= max_point.row() { false } else { - let (start_indent, is_blank) = self.display_map.line_indent(display_row, app).unwrap(); + let (start_indent, is_blank) = self.display_map.line_indent(display_row, app); if is_blank { false } else { for display_row in display_row + 1..=max_point.row() { - let (indent, is_blank) = - self.display_map.line_indent(display_row, app).unwrap(); + let (indent, is_blank) = self.display_map.line_indent(display_row, app); if !is_blank { return indent > start_indent; } @@ -2098,23 +1960,23 @@ impl BufferView { } } - fn foldable_range_for_line(&self, start_row: u32, app: &AppContext) -> Result> { + fn foldable_range_for_line(&self, start_row: u32, app: &AppContext) -> Range { let max_point = self.max_point(app); - let (start_indent, _) = self.display_map.line_indent(start_row, app)?; - let start = DisplayPoint::new(start_row, self.line_len(start_row, app)?); + let (start_indent, _) = self.display_map.line_indent(start_row, app); + let start = DisplayPoint::new(start_row, self.line_len(start_row, app)); let mut end = None; for row in start_row + 1..=max_point.row() { - let (indent, is_blank) = self.display_map.line_indent(row, app)?; + let (indent, is_blank) = self.display_map.line_indent(row, app); if !is_blank && indent <= start_indent { - end = Some(DisplayPoint::new(row - 1, self.line_len(row - 1, app)?)); + end = Some(DisplayPoint::new(row - 1, self.line_len(row - 1, app))); break; } } let end = end.unwrap_or(max_point); - return Ok(start.to_buffer_point(&self.display_map, Bias::Left, app)? - ..end.to_buffer_point(&self.display_map, Bias::Left, app)?); + return start.to_buffer_point(&self.display_map, Bias::Left, app) + ..end.to_buffer_point(&self.display_map, Bias::Left, app); } pub fn fold_selected_ranges(&mut self, _: &(), ctx: &mut ViewContext) { @@ -2131,7 +1993,7 @@ impl BufferView { fn fold_ranges(&mut self, ranges: Vec>, ctx: &mut ViewContext) { if !ranges.is_empty() { - self.display_map.fold(ranges, ctx.as_ref()).unwrap(); + self.display_map.fold(ranges, ctx.as_ref()); *self.autoscroll_requested.lock() = true; ctx.notify(); } @@ -2139,17 +2001,17 @@ impl BufferView { fn unfold_ranges(&mut self, ranges: Vec>, ctx: &mut ViewContext) { if !ranges.is_empty() { - self.display_map.unfold(ranges, ctx.as_ref()).unwrap(); + self.display_map.unfold(ranges, ctx.as_ref()); *self.autoscroll_requested.lock() = true; ctx.notify(); } } - pub fn line(&self, display_row: u32, ctx: &AppContext) -> Result { + pub fn line(&self, display_row: u32, ctx: &AppContext) -> String { self.display_map.line(display_row, ctx) } - pub fn line_len(&self, display_row: u32, ctx: &AppContext) -> Result { + pub fn line_len(&self, display_row: u32, ctx: &AppContext) -> u32 { self.display_map.line_len(display_row, ctx) } @@ -2244,7 +2106,7 @@ impl BufferView { for buffer_row in self .display_map .snapshot(ctx) - .buffer_rows(start_row as u32)? + .buffer_rows(start_row as u32) .take(line_count) { line_number.clear(); @@ -2281,9 +2143,7 @@ impl BufferView { let mut line_len = 0; let mut row = rows.start; let snapshot = self.display_map.snapshot(ctx); - let chars = snapshot - .chars_at(DisplayPoint::new(rows.start, 0), ctx) - .unwrap(); + let chars = snapshot.chars_at(DisplayPoint::new(rows.start, 0), ctx); for char in chars.chain(Some('\n')) { if char == '\n' { layouts.push(layout_cache.layout_str(&line, font_size, &[(0..line_len, font_id)])); @@ -2313,12 +2173,12 @@ impl BufferView { let font_id = font_cache.select_font(settings.buffer_font_family, &FontProperties::new())?; - let line = self.line(row, app)?; + let line = self.line(row, app); Ok(layout_cache.layout_str( &line, settings.buffer_font_size, - &[(0..self.line_len(row, app)? as usize, font_id)], + &[(0..self.line_len(row, app) as usize, font_id)], )) } diff --git a/zed/src/editor/display_map/fold_map.rs b/zed/src/editor/display_map/fold_map.rs index 417ad04f319517e9015a7cc601efcf059abf4888..2307aa46bff9284cb5f66d24b4c221a0395253f1 100644 --- a/zed/src/editor/display_map/fold_map.rs +++ b/zed/src/editor/display_map/fold_map.rs @@ -7,7 +7,6 @@ use crate::{ sum_tree::{self, Cursor, FilterCursor, SeekBias, SumTree}, time, }; -use anyhow::{anyhow, Result}; use gpui::{AppContext, ModelHandle}; use parking_lot::{Mutex, MutexGuard}; use std::{ @@ -55,17 +54,14 @@ impl FoldMap { self.sync(ctx).summary().display.bytes } - pub fn line_len(&self, row: u32, ctx: &AppContext) -> Result { - let line_start = self.to_display_offset(DisplayPoint::new(row, 0), ctx)?.0; + pub fn line_len(&self, row: u32, ctx: &AppContext) -> u32 { + let line_start = self.to_display_offset(DisplayPoint::new(row, 0), ctx).0; let line_end = if row >= self.max_point(ctx).row() { self.len(ctx) } else { - self.to_display_offset(DisplayPoint::new(row + 1, 0), ctx)? - .0 - - 1 + self.to_display_offset(DisplayPoint::new(row + 1, 0), ctx).0 - 1 }; - - Ok((line_end - line_start) as u32) + (line_end - line_start) as u32 } pub fn max_point(&self, ctx: &AppContext) -> DisplayPoint { @@ -80,18 +76,18 @@ impl FoldMap { &'a self, range: Range, ctx: &'a AppContext, - ) -> Result>> + ) -> impl Iterator> where T: ToOffset, { - Ok(self.intersecting_folds(range, ctx)?.map(|f| &f.0)) + self.intersecting_folds(range, ctx).map(|f| &f.0) } pub fn fold( &mut self, ranges: impl IntoIterator>, ctx: &AppContext, - ) -> Result<()> { + ) { let _ = self.sync(ctx); let mut edits = Vec::new(); @@ -128,14 +124,13 @@ impl FoldMap { new_tree }; self.apply_edits(edits, ctx); - Ok(()) } pub fn unfold( &mut self, ranges: impl IntoIterator>, ctx: &AppContext, - ) -> Result<()> { + ) { let _ = self.sync(ctx); let buffer = self.buffer.read(ctx); @@ -144,7 +139,7 @@ impl FoldMap { let mut fold_ixs_to_delete = Vec::new(); for range in ranges.into_iter() { // Remove intersecting folds and add their ranges to edits that are passed to apply_edits. - let mut folds_cursor = self.intersecting_folds(range, ctx)?; + let mut folds_cursor = self.intersecting_folds(range, ctx); while let Some(fold) = folds_cursor.item() { let offset_range = fold.0.start.to_offset(buffer)..fold.0.end.to_offset(buffer); edits.push(Edit { @@ -176,24 +171,23 @@ impl FoldMap { folds }; self.apply_edits(edits, ctx); - Ok(()) } fn intersecting_folds<'a, T>( &self, range: Range, ctx: &'a AppContext, - ) -> Result bool, Fold, usize>> + ) -> FilterCursor bool, Fold, usize> where T: ToOffset, { let buffer = self.buffer.read(ctx); let start = buffer.anchor_before(range.start.to_offset(buffer)); let end = buffer.anchor_after(range.end.to_offset(buffer)); - Ok(self.folds.filter::<_, usize>(move |summary| { + self.folds.filter::<_, usize>(move |summary| { start.cmp(&summary.max_end, buffer).unwrap() == Ordering::Less && end.cmp(&summary.min_start, buffer).unwrap() == Ordering::Greater - })) + }) } pub fn is_line_folded(&self, display_row: u32, ctx: &AppContext) -> bool { @@ -221,11 +215,7 @@ impl FoldMap { (cursor.start().buffer.lines + overshoot).to_offset(self.buffer.read(ctx)) } - pub fn to_display_offset( - &self, - point: DisplayPoint, - ctx: &AppContext, - ) -> Result { + pub fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> DisplayOffset { self.snapshot(ctx).to_display_offset(point, ctx) } @@ -405,48 +395,46 @@ pub struct FoldMapSnapshot { } impl FoldMapSnapshot { - pub fn buffer_rows(&self, start_row: u32) -> Result { + pub fn buffer_rows(&self, start_row: u32) -> BufferRows { if start_row > self.transforms.summary().display.lines.row { - return Err(anyhow!("invalid display row {}", start_row)); + panic!("invalid display row {}", start_row); } let display_point = Point::new(start_row, 0); let mut cursor = self.transforms.cursor(); cursor.seek(&DisplayPoint(display_point), SeekBias::Left, &()); - Ok(BufferRows { + BufferRows { display_point, cursor, - }) + } } - pub fn chars_at<'a>(&'a self, point: DisplayPoint, ctx: &'a AppContext) -> Result> { - let offset = self.to_display_offset(point, ctx)?; + pub fn chars_at<'a>(&'a self, point: DisplayPoint, ctx: &'a AppContext) -> Chars<'a> { + let offset = self.to_display_offset(point, ctx); let mut cursor = self.transforms.cursor(); cursor.seek(&offset, SeekBias::Right, &()); - Ok(Chars { + Chars { cursor, offset: offset.0, buffer: self.buffer.read(ctx), buffer_chars: None, - }) + } } - fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> Result { + fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> DisplayOffset { let mut cursor = self.transforms.cursor::(); cursor.seek(&point, SeekBias::Right, &()); let overshoot = point.0 - cursor.start().display.lines; let mut offset = cursor.start().display.bytes; if !overshoot.is_zero() { - let transform = cursor - .item() - .ok_or_else(|| anyhow!("display point {:?} is out of range", point))?; + let transform = cursor.item().expect("display point out of range"); assert!(transform.display_text.is_none()); let end_buffer_offset = (cursor.start().buffer.lines + overshoot).to_offset(self.buffer.read(ctx)); offset += end_buffer_offset - cursor.start().buffer.bytes; } - Ok(DisplayOffset(offset)) + DisplayOffset(offset) } } @@ -680,8 +668,7 @@ mod tests { Point::new(2, 4)..Point::new(4, 1), ], app.as_ref(), - ) - .unwrap(); + ); assert_eq!(map.text(app.as_ref()), "aa…cc…eeeee"); buffer.update(app, |buffer, ctx| { @@ -707,8 +694,7 @@ mod tests { }); assert_eq!(map.text(app.as_ref()), "123a…c123456eee"); - map.unfold(Some(Point::new(0, 4)..Point::new(0, 5)), app.as_ref()) - .unwrap(); + map.unfold(Some(Point::new(0, 4)..Point::new(0, 5)), app.as_ref()); assert_eq!(map.text(app.as_ref()), "123aaaaa\nbbbbbb\nccc123456eee"); } @@ -719,17 +705,17 @@ mod tests { { let mut map = FoldMap::new(buffer.clone(), app.as_ref()); - map.fold(vec![5..8], app.as_ref()).unwrap(); + map.fold(vec![5..8], app.as_ref()); map.check_invariants(app.as_ref()); assert_eq!(map.text(app.as_ref()), "abcde…ijkl"); // Create an fold adjacent to the start of the first fold. - map.fold(vec![0..1, 2..5], app.as_ref()).unwrap(); + map.fold(vec![0..1, 2..5], app.as_ref()); map.check_invariants(app.as_ref()); assert_eq!(map.text(app.as_ref()), "…b…ijkl"); // Create an fold adjacent to the end of the first fold. - map.fold(vec![11..11, 8..10], app.as_ref()).unwrap(); + map.fold(vec![11..11, 8..10], app.as_ref()); map.check_invariants(app.as_ref()); assert_eq!(map.text(app.as_ref()), "…b…kl"); } @@ -738,7 +724,7 @@ mod tests { let mut map = FoldMap::new(buffer.clone(), app.as_ref()); // Create two adjacent folds. - map.fold(vec![0..2, 2..5], app.as_ref()).unwrap(); + map.fold(vec![0..2, 2..5], app.as_ref()); map.check_invariants(app.as_ref()); assert_eq!(map.text(app.as_ref()), "…fghijkl"); @@ -765,8 +751,7 @@ mod tests { Point::new(3, 1)..Point::new(4, 1), ], app.as_ref(), - ) - .unwrap(); + ); assert_eq!(map.text(app.as_ref()), "aa…eeeee"); } @@ -781,8 +766,7 @@ mod tests { Point::new(3, 1)..Point::new(4, 1), ], app.as_ref(), - ) - .unwrap(); + ); assert_eq!(map.text(app.as_ref()), "aa…cccc\nd…eeeee"); buffer.update(app, |buffer, ctx| { @@ -807,11 +791,9 @@ mod tests { Point::new(3, 1)..Point::new(4, 1), ], app.as_ref(), - ) - .unwrap(); + ); let fold_ranges = map .folds_in_range(Point::new(1, 0)..Point::new(1, 3), app.as_ref()) - .unwrap() .map(|fold| fold.start.to_point(buffer)..fold.end.to_point(buffer)) .collect::>(); assert_eq!( @@ -866,7 +848,7 @@ mod tests { to_fold.push(start..end); } log::info!("folding {:?}", to_fold); - map.fold(to_fold, app.as_ref()).unwrap(); + map.fold(to_fold, app.as_ref()); } 35..=59 if !map.folds.is_empty() => { let buffer = buffer.read(app); @@ -877,7 +859,7 @@ mod tests { to_unfold.push(start..end); } log::info!("unfolding {:?}", to_unfold); - map.unfold(to_unfold, app.as_ref()).unwrap(); + map.unfold(to_unfold, app.as_ref()); } _ => { let edits = buffer.update(app, |buffer, ctx| { @@ -909,7 +891,7 @@ mod tests { assert_eq!(map.text(app.as_ref()), expected_text); for (display_row, line) in expected_text.lines().enumerate() { - let line_len = map.line_len(display_row as u32, app.as_ref()).unwrap(); + let line_len = map.line_len(display_row as u32, app.as_ref()); assert_eq!(line_len, line.chars().count() as u32); } @@ -928,7 +910,7 @@ mod tests { buffer_offset ); assert_eq!( - map.to_display_offset(display_point, app.as_ref()).unwrap(), + map.to_display_offset(display_point, app.as_ref()), display_offset ); @@ -949,14 +931,13 @@ mod tests { for _ in 0..5 { let row = rng.gen_range(0..=map.max_point(app.as_ref()).row()); - let column = rng.gen_range(0..=map.line_len(row, app.as_ref()).unwrap()); + let column = rng.gen_range(0..=map.line_len(row, app.as_ref())); let point = DisplayPoint::new(row, column); - let offset = map.to_display_offset(point, app.as_ref()).unwrap().0; + let offset = map.to_display_offset(point, app.as_ref()).0; let len = rng.gen_range(0..=map.len(app.as_ref()) - offset); assert_eq!( map.snapshot(app.as_ref()) .chars_at(point, app.as_ref()) - .unwrap() .take(len) .collect::(), expected_text @@ -974,7 +955,6 @@ mod tests { assert_eq!( map.snapshot(app.as_ref()) .buffer_rows(display_row) - .unwrap() .collect::>(), expected_buffer_rows[idx..], ); @@ -1004,7 +984,6 @@ mod tests { assert_eq!( map.folds_in_range(start..end, app.as_ref()) - .unwrap() .cloned() .collect::>(), expected_folds @@ -1027,21 +1006,18 @@ mod tests { Point::new(3, 1)..Point::new(4, 1), ], app.as_ref(), - ) - .unwrap(); + ); assert_eq!(map.text(app.as_ref()), "aa…cccc\nd…eeeee\nffffff\n"); assert_eq!( map.snapshot(app.as_ref()) .buffer_rows(0) - .unwrap() .collect::>(), vec![0, 3, 5, 6] ); assert_eq!( map.snapshot(app.as_ref()) .buffer_rows(3) - .unwrap() .collect::>(), vec![6] ); @@ -1051,7 +1027,6 @@ mod tests { fn text(&self, app: &AppContext) -> String { self.snapshot(app) .chars_at(DisplayPoint(Point::zero()), app) - .unwrap() .collect() } diff --git a/zed/src/editor/display_map/mod.rs b/zed/src/editor/display_map/mod.rs index dbc0a3005c4c342cf7dd3ca7da6f55af1f02a5f0..94b91301beff5bba34d4ef2a7e68904a9d97d4cc 100644 --- a/zed/src/editor/display_map/mod.rs +++ b/zed/src/editor/display_map/mod.rs @@ -1,7 +1,6 @@ mod fold_map; use super::{buffer, Anchor, Buffer, Edit, Point, ToOffset, ToPoint}; -use anyhow::Result; pub use fold_map::BufferRows; use fold_map::{FoldMap, FoldMapSnapshot}; use gpui::{AppContext, ModelHandle}; @@ -39,7 +38,7 @@ impl DisplayMap { &'a self, range: Range, app: &'a AppContext, - ) -> Result>> + ) -> impl Iterator> where T: ToOffset, { @@ -50,7 +49,7 @@ impl DisplayMap { &mut self, ranges: impl IntoIterator>, ctx: &AppContext, - ) -> Result<()> { + ) { self.fold_map.fold(ranges, ctx) } @@ -58,7 +57,7 @@ impl DisplayMap { &mut self, ranges: impl IntoIterator>, ctx: &AppContext, - ) -> Result<()> { + ) { self.fold_map.unfold(ranges, ctx) } @@ -69,24 +68,22 @@ impl DisplayMap { pub fn text(&self, ctx: &AppContext) -> String { self.snapshot(ctx) .chars_at(DisplayPoint::zero(), ctx) - .unwrap() .collect() } - pub fn line(&self, display_row: u32, ctx: &AppContext) -> Result { - Ok(self - .snapshot(ctx) - .chars_at(DisplayPoint::new(display_row, 0), ctx)? + pub fn line(&self, display_row: u32, ctx: &AppContext) -> String { + self.snapshot(ctx) + .chars_at(DisplayPoint::new(display_row, 0), ctx) .take_while(|c| *c != '\n') - .collect()) + .collect() } - pub fn line_indent(&self, display_row: u32, ctx: &AppContext) -> Result<(u32, bool)> { + pub fn line_indent(&self, display_row: u32, ctx: &AppContext) -> (u32, bool) { let mut indent = 0; let mut is_blank = true; for c in self .snapshot(ctx) - .chars_at(DisplayPoint::new(display_row, 0), ctx)? + .chars_at(DisplayPoint::new(display_row, 0), ctx) { if c == ' ' { indent += 1; @@ -95,45 +92,33 @@ impl DisplayMap { break; } } - Ok((indent, is_blank)) + (indent, is_blank) } - pub fn line_len(&self, row: u32, ctx: &AppContext) -> Result { - DisplayPoint::new(row, self.fold_map.line_len(row, ctx)?) + pub fn line_len(&self, row: u32, ctx: &AppContext) -> u32 { + DisplayPoint::new(row, self.fold_map.line_len(row, ctx)) .expand_tabs(self, ctx) - .map(|point| point.column()) + .column() } pub fn max_point(&self, ctx: &AppContext) -> DisplayPoint { - self.fold_map.max_point(ctx).expand_tabs(self, ctx).unwrap() + self.fold_map.max_point(ctx).expand_tabs(self, ctx) } pub fn rightmost_point(&self, ctx: &AppContext) -> DisplayPoint { self.fold_map.rightmost_point(ctx) } - pub fn anchor_before( - &self, - point: DisplayPoint, - bias: Bias, - app: &AppContext, - ) -> Result { - Ok(self - .buffer + pub fn anchor_before(&self, point: DisplayPoint, bias: Bias, app: &AppContext) -> Anchor { + self.buffer .read(app) - .anchor_before(point.to_buffer_point(self, bias, app)?)) + .anchor_before(point.to_buffer_point(self, bias, app)) } - pub fn anchor_after( - &self, - point: DisplayPoint, - bias: Bias, - app: &AppContext, - ) -> Result { - Ok(self - .buffer + pub fn anchor_after(&self, point: DisplayPoint, bias: Bias, app: &AppContext) -> Anchor { + self.buffer .read(app) - .anchor_after(point.to_buffer_point(self, bias, app)?)) + .anchor_after(point.to_buffer_point(self, bias, app)) } } @@ -143,33 +128,32 @@ pub struct DisplayMapSnapshot { } impl DisplayMapSnapshot { - pub fn buffer_rows(&self, start_row: u32) -> Result { + pub fn buffer_rows(&self, start_row: u32) -> BufferRows { self.folds_snapshot.buffer_rows(start_row) } - pub fn chars_at<'a>(&'a self, point: DisplayPoint, app: &'a AppContext) -> Result> { + pub fn chars_at<'a>(&'a self, point: DisplayPoint, app: &'a AppContext) -> Chars<'a> { let column = point.column() as usize; - let (point, to_next_stop) = self.collapse_tabs(point, Bias::Left, app)?; - let mut fold_chars = self.folds_snapshot.chars_at(point, app)?; + let (point, to_next_stop) = self.collapse_tabs(point, Bias::Left, app); + let mut fold_chars = self.folds_snapshot.chars_at(point, app); if to_next_stop > 0 { fold_chars.next(); } - - Ok(Chars { + Chars { fold_chars, column, to_next_stop, tab_size: self.tab_size, - }) + } } - fn expand_tabs(&self, mut point: DisplayPoint, ctx: &AppContext) -> Result { + fn expand_tabs(&self, mut point: DisplayPoint, ctx: &AppContext) -> DisplayPoint { let chars = self .folds_snapshot - .chars_at(DisplayPoint(Point::new(point.row(), 0)), ctx)?; + .chars_at(DisplayPoint(Point::new(point.row(), 0)), ctx); let expanded = expand_tabs(chars, point.column() as usize, self.tab_size); *point.column_mut() = expanded as u32; - Ok(point) + point } fn collapse_tabs( @@ -177,15 +161,15 @@ impl DisplayMapSnapshot { mut point: DisplayPoint, bias: Bias, ctx: &AppContext, - ) -> Result<(DisplayPoint, usize)> { + ) -> (DisplayPoint, usize) { let chars = self .folds_snapshot - .chars_at(DisplayPoint(Point::new(point.row(), 0)), ctx)?; + .chars_at(DisplayPoint(Point::new(point.row(), 0)), ctx); let expanded = point.column() as usize; let (collapsed, to_next_stop) = collapse_tabs(chars, expanded, bias, self.tab_size); *point.column_mut() = collapsed as u32; - Ok((point, to_next_stop)) + (point, to_next_stop) } } @@ -217,45 +201,38 @@ impl DisplayPoint { &mut self.0.column } - pub fn to_buffer_point(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> Result { - Ok(map - .fold_map - .to_buffer_point(self.collapse_tabs(map, bias, ctx)?.0, ctx)) + pub fn to_buffer_point(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> Point { + map.fold_map + .to_buffer_point(self.collapse_tabs(map, bias, ctx).0, ctx) } - pub fn to_buffer_offset(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> Result { - Ok(map - .fold_map - .to_buffer_offset(self.collapse_tabs(&map, bias, ctx)?.0, ctx)) + pub fn to_buffer_offset(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> usize { + map.fold_map + .to_buffer_offset(self.collapse_tabs(&map, bias, ctx).0, ctx) } - fn expand_tabs(self, map: &DisplayMap, ctx: &AppContext) -> Result { + fn expand_tabs(self, map: &DisplayMap, ctx: &AppContext) -> Self { map.snapshot(ctx).expand_tabs(self, ctx) } - fn collapse_tabs( - self, - map: &DisplayMap, - bias: Bias, - ctx: &AppContext, - ) -> Result<(Self, usize)> { + fn collapse_tabs(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> (Self, usize) { map.snapshot(ctx).collapse_tabs(self, bias, ctx) } } impl Point { - pub fn to_display_point(self, map: &DisplayMap, ctx: &AppContext) -> Result { + pub fn to_display_point(self, map: &DisplayMap, ctx: &AppContext) -> DisplayPoint { let mut display_point = map.fold_map.to_display_point(self, ctx); let snapshot = map.fold_map.snapshot(ctx); - let chars = snapshot.chars_at(DisplayPoint::new(display_point.row(), 0), ctx)?; + let chars = snapshot.chars_at(DisplayPoint::new(display_point.row(), 0), ctx); *display_point.column_mut() = expand_tabs(chars, display_point.column() as usize, map.tab_size) as u32; - Ok(display_point) + display_point } } impl Anchor { - pub fn to_display_point(&self, map: &DisplayMap, app: &AppContext) -> Result { + pub fn to_display_point(&self, map: &DisplayMap, app: &AppContext) -> DisplayPoint { self.to_point(map.buffer.read(app)) .to_display_point(map, app) } @@ -365,7 +342,6 @@ mod tests { assert_eq!( map.snapshot(app.as_ref()) .chars_at(DisplayPoint::new(1, 0), app.as_ref()) - .unwrap() .take(10) .collect::(), " b bb" @@ -373,7 +349,6 @@ mod tests { assert_eq!( map.snapshot(app.as_ref()) .chars_at(DisplayPoint::new(1, 2), app.as_ref()) - .unwrap() .take(10) .collect::(), " b bbbb" @@ -381,7 +356,6 @@ mod tests { assert_eq!( map.snapshot(app.as_ref()) .chars_at(DisplayPoint::new(1, 6), app.as_ref()) - .unwrap() .take(13) .collect::(), " bbbbb\nc c" diff --git a/zed/src/editor/movement.rs b/zed/src/editor/movement.rs index dbb1d4fafcb85b8e1d95eb84732c181fd920e998..162d593ef6a56b3aa724253fc45f2e39b045e18d 100644 --- a/zed/src/editor/movement.rs +++ b/zed/src/editor/movement.rs @@ -8,13 +8,13 @@ pub fn left(map: &DisplayMap, mut point: DisplayPoint, app: &AppContext) -> Resu *point.column_mut() -= 1; } else if point.row() > 0 { *point.row_mut() -= 1; - *point.column_mut() = map.line_len(point.row(), app)?; + *point.column_mut() = map.line_len(point.row(), app); } Ok(point) } pub fn right(map: &DisplayMap, mut point: DisplayPoint, app: &AppContext) -> Result { - let max_column = map.line_len(point.row(), app).unwrap(); + let max_column = map.line_len(point.row(), app); if point.column() < max_column { *point.column_mut() += 1; } else if point.row() < map.max_point(app).row() { @@ -37,7 +37,7 @@ pub fn up( }; if point.row() > 0 { *point.row_mut() -= 1; - *point.column_mut() = cmp::min(goal_column, map.line_len(point.row(), app)?); + *point.column_mut() = cmp::min(goal_column, map.line_len(point.row(), app)); } else { point = DisplayPoint::new(0, 0); } @@ -59,7 +59,7 @@ pub fn down( let max_point = map.max_point(app); if point.row() < max_point.row() { *point.row_mut() += 1; - *point.column_mut() = cmp::min(goal_column, map.line_len(point.row(), app)?) + *point.column_mut() = cmp::min(goal_column, map.line_len(point.row(), app)) } else { point = max_point; } @@ -73,7 +73,7 @@ pub fn line_beginning( toggle_indent: bool, app: &AppContext, ) -> Result { - let (indent, is_blank) = map.line_indent(point.row(), app)?; + let (indent, is_blank) = map.line_indent(point.row(), app); if toggle_indent && !is_blank && point.column() != indent { Ok(DisplayPoint::new(point.row(), indent)) } else { @@ -84,7 +84,7 @@ pub fn line_beginning( pub fn line_end(map: &DisplayMap, point: DisplayPoint, app: &AppContext) -> Result { Ok(DisplayPoint::new( point.row(), - map.line_len(point.row(), app)?, + map.line_len(point.row(), app), )) } @@ -98,13 +98,13 @@ pub fn prev_word_boundary( Ok(DisplayPoint::new(0, 0)) } else { let row = point.row() - 1; - Ok(DisplayPoint::new(row, map.line_len(row, app)?)) + Ok(DisplayPoint::new(row, map.line_len(row, app))) } } else { let mut boundary = DisplayPoint::new(point.row(), 0); let mut column = 0; let mut prev_c = None; - for c in map.snapshot(app).chars_at(boundary, app)? { + for c in map.snapshot(app).chars_at(boundary, app) { if column >= point.column() { break; } @@ -126,7 +126,7 @@ pub fn next_word_boundary( app: &AppContext, ) -> Result { let mut prev_c = None; - for c in map.snapshot(app).chars_at(point, app)? { + for c in map.snapshot(app).chars_at(point, app) { if prev_c.is_some() && (c == '\n' || char_kind(prev_c.unwrap()) != char_kind(c)) { break; } From a9583d0074c4a3f878294ed2fb450cafb8e79a0c Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 17 May 2021 16:38:42 -0700 Subject: [PATCH 05/18] Introduce FoldMapSnapshot::chunks_at, use it in FoldMap::text Get the FoldMap randomized tests passing. This also required adding a test-only FoldMap::prev_char_boundary method. --- zed/src/editor/buffer/mod.rs | 7 +- zed/src/editor/buffer/rope.rs | 35 +++-- zed/src/editor/display_map/fold_map.rs | 172 +++++++++++++++++++------ 3 files changed, 161 insertions(+), 53 deletions(-) diff --git a/zed/src/editor/buffer/mod.rs b/zed/src/editor/buffer/mod.rs index 4113359f9de82314bb055ce4be4a7ed1fa08e191..ce76c34133822a6a44e87dd3d5648aa351e0645a 100644 --- a/zed/src/editor/buffer/mod.rs +++ b/zed/src/editor/buffer/mod.rs @@ -5,7 +5,7 @@ mod selection; pub use anchor::*; pub use point::*; -pub use rope::{Rope, TextSummary}; +pub use rope::{ChunksIter, Rope, TextSummary}; use seahash::SeaHasher; pub use selection::*; use similar::{ChangeTag, TextDiff}; @@ -637,10 +637,7 @@ impl Buffer { self.text_for_range(0..self.len()).collect() } - pub fn text_for_range<'a, T: ToOffset>( - &'a self, - range: Range, - ) -> impl 'a + Iterator { + pub fn text_for_range<'a, T: ToOffset>(&'a self, range: Range) -> ChunksIter<'a> { let start = range.start.to_offset(self); let end = range.end.to_offset(self); self.visible_text.chunks_in_range(start..end) diff --git a/zed/src/editor/buffer/rope.rs b/zed/src/editor/buffer/rope.rs index 3128aedbd31a2b71d801646e89c5cabebe282d32..e409084e11dae751fc80130c4dad0e19d5b90d38 100644 --- a/zed/src/editor/buffer/rope.rs +++ b/zed/src/editor/buffer/rope.rs @@ -115,11 +115,11 @@ impl Rope { Chars::new(self, start) } - pub fn chunks<'a>(&'a self) -> impl Iterator { + pub fn chunks<'a>(&'a self) -> ChunksIter<'a> { self.chunks_in_range(0..self.len()) } - pub fn chunks_in_range<'a>(&'a self, range: Range) -> impl Iterator { + pub fn chunks_in_range<'a>(&'a self, range: Range) -> ChunksIter<'a> { ChunksIter::new(self, range) } @@ -270,17 +270,24 @@ impl<'a> ChunksIter<'a> { chunks.seek(&range.start, SeekBias::Right, &()); Self { chunks, range } } -} -impl<'a> Iterator for ChunksIter<'a> { - type Item = &'a str; + pub fn offset(&self) -> usize { + self.range.start.max(*self.chunks.start()) + } - fn next(&mut self) -> Option { + pub fn advance_to(&mut self, offset: usize) { + if offset >= self.chunks.end() { + self.chunks.seek_forward(&offset, SeekBias::Right, &()); + self.range.start = offset; + } + } + + pub fn peek(&self) -> Option<&'a str> { if let Some(chunk) = self.chunks.item() { - if self.range.end > *self.chunks.start() { + let offset = *self.chunks.start(); + if self.range.end > offset { let start = self.range.start.saturating_sub(*self.chunks.start()); let end = self.range.end - self.chunks.start(); - self.chunks.next(); return Some(&chunk.0[start..chunk.0.len().min(end)]); } } @@ -288,6 +295,18 @@ impl<'a> Iterator for ChunksIter<'a> { } } +impl<'a> Iterator for ChunksIter<'a> { + type Item = &'a str; + + fn next(&mut self) -> Option { + let result = self.peek(); + if result.is_some() { + self.chunks.next(); + } + result + } +} + #[derive(Clone, Debug, Default)] struct Chunk(ArrayString<[u8; 2 * CHUNK_BASE]>); diff --git a/zed/src/editor/display_map/fold_map.rs b/zed/src/editor/display_map/fold_map.rs index 2307aa46bff9284cb5f66d24b4c221a0395253f1..4b9115a52bb896fc3e7ca7bca7301fd707cfcf38 100644 --- a/zed/src/editor/display_map/fold_map.rs +++ b/zed/src/editor/display_map/fold_map.rs @@ -3,7 +3,7 @@ use super::{ Anchor, Buffer, DisplayPoint, Edit, Point, ToOffset, }; use crate::{ - editor::rope, + editor::{buffer, rope}, sum_tree::{self, Cursor, FilterCursor, SeekBias, SumTree}, time, }; @@ -208,11 +208,7 @@ impl FoldMap { } pub fn to_buffer_offset(&self, point: DisplayPoint, ctx: &AppContext) -> usize { - let transforms = self.sync(ctx); - let mut cursor = transforms.cursor::(); - cursor.seek(&point, SeekBias::Right, &()); - let overshoot = point.0 - cursor.start().display.lines; - (cursor.start().buffer.lines + overshoot).to_offset(self.buffer.read(ctx)) + self.snapshot(ctx).to_buffer_offset(point, ctx) } pub fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> DisplayOffset { @@ -238,6 +234,26 @@ impl FoldMap { )) } + #[cfg(test)] + pub fn prev_char_boundary(&self, offset: DisplayOffset, ctx: &AppContext) -> DisplayOffset { + let transforms = self.sync(ctx); + let mut cursor = transforms.cursor::(); + cursor.seek(&offset, SeekBias::Right, &()); + if let Some(transform) = cursor.item() { + if transform.display_text.is_some() { + DisplayOffset(cursor.start().display.bytes) + } else { + let overshoot = offset.0 - cursor.start().display.bytes; + let buffer_offset = cursor.start().buffer.bytes + overshoot; + let buffer_boundary_offset = + self.buffer.read(ctx).prev_char_boundary(buffer_offset); + DisplayOffset(offset.0 - (buffer_offset - buffer_boundary_offset)) + } + } else { + offset + } + } + fn sync(&self, ctx: &AppContext) -> MutexGuard> { let buffer = self.buffer.read(ctx); let mut edits = buffer.edits_since(self.last_sync.lock().clone()).peekable(); @@ -334,18 +350,20 @@ impl FoldMap { } if fold.end > fold.start { + let display_text = "…"; + let extent = Point::new(0, display_text.len() as u32); new_transforms.push( Transform { summary: TransformSummary { display: TextSummary { - bytes: '…'.len_utf8(), - lines: Point::new(0, 1), - first_line_len: 1, - rightmost_point: Point::new(0, 1), + bytes: display_text.len(), + lines: extent, + first_line_len: extent.column, + rightmost_point: extent, }, buffer: buffer.text_summary_for_range(fold.start..fold.end), }, - display_text: Some('…'), + display_text: Some(display_text), }, &(), ); @@ -410,6 +428,20 @@ impl FoldMapSnapshot { } } + pub fn chunks_at<'a>(&'a self, offset: DisplayOffset, ctx: &'a AppContext) -> Chunks<'a> { + let mut transform_cursor = self.transforms.cursor::(); + transform_cursor.seek(&offset, SeekBias::Right, &()); + let overshoot = offset.0 - transform_cursor.start().display.bytes; + let buffer_offset = transform_cursor.start().buffer.bytes + overshoot; + let buffer = self.buffer.read(ctx); + let rope_cursor = buffer.text_for_range(buffer_offset..buffer.len()); + Chunks { + transform_cursor, + buffer_offset, + buffer_chunks: rope_cursor, + } + } + pub fn chars_at<'a>(&'a self, point: DisplayPoint, ctx: &'a AppContext) -> Chars<'a> { let offset = self.to_display_offset(point, ctx); let mut cursor = self.transforms.cursor(); @@ -436,12 +468,19 @@ impl FoldMapSnapshot { } DisplayOffset(offset) } + + pub fn to_buffer_offset(&self, point: DisplayPoint, ctx: &AppContext) -> usize { + let mut cursor = self.transforms.cursor::(); + cursor.seek(&point, SeekBias::Right, &()); + let overshoot = point.0 - cursor.start().display.lines; + (cursor.start().buffer.lines + overshoot).to_offset(self.buffer.read(ctx)) + } } #[derive(Clone, Debug, Default, Eq, PartialEq)] struct Transform { summary: TransformSummary, - display_text: Option, + display_text: Option<&'static str>, } #[derive(Clone, Debug, Default, Eq, PartialEq)] @@ -601,7 +640,7 @@ impl<'a> Iterator for Chars<'a> { fn next(&mut self) -> Option { if let Some(c) = self.buffer_chars.as_mut().and_then(|chars| chars.next()) { - self.offset += 1; + self.offset += c.len_utf8(); return Some(c); } @@ -611,8 +650,8 @@ impl<'a> Iterator for Chars<'a> { self.cursor.item().and_then(|transform| { if let Some(c) = transform.display_text { - self.offset += 1; - Some(c) + self.offset += c.len(); + Some(c.chars().next().unwrap()) } else { let overshoot = self.offset - self.cursor.start().display.bytes; let buffer_start = self.cursor.start().buffer.bytes + overshoot; @@ -624,6 +663,59 @@ impl<'a> Iterator for Chars<'a> { } } +pub struct Chunks<'a> { + transform_cursor: Cursor<'a, Transform, DisplayOffset, TransformSummary>, + buffer_chunks: buffer::ChunksIter<'a>, + buffer_offset: usize, +} + +impl<'a> Iterator for Chunks<'a> { + type Item = &'a str; + + fn next(&mut self) -> Option { + let transform = if let Some(item) = self.transform_cursor.item() { + item + } else { + return None; + }; + + // If we're in a fold, then return the fold's display text and + // advance the transform and buffer cursors to the end of the fold. + if let Some(display_text) = transform.display_text { + self.buffer_offset += transform.summary.buffer.bytes; + self.buffer_chunks.advance_to(self.buffer_offset); + + while self.buffer_offset >= self.transform_cursor.end().buffer.bytes + && self.transform_cursor.item().is_some() + { + self.transform_cursor.next(); + } + + return Some(display_text); + } + + // Otherwise, take a chunk from the buffer's text. + if let Some(mut chunk) = self.buffer_chunks.peek() { + let offset_in_chunk = self.buffer_offset - self.buffer_chunks.offset(); + chunk = &chunk[offset_in_chunk..]; + + // Truncate the chunk so that it ends at the next fold. + let region_end = self.transform_cursor.end().buffer.bytes - self.buffer_offset; + if chunk.len() >= region_end { + chunk = &chunk[0..region_end]; + self.transform_cursor.next(); + } else { + self.buffer_chunks.next(); + } + + self.buffer_offset += chunk.len(); + return Some(chunk); + } + + None + } +} + impl<'a> sum_tree::Dimension<'a, TransformSummary> for DisplayPoint { fn add_summary(&mut self, summary: &'a TransformSummary) { self.0 += &summary.display.lines; @@ -843,8 +935,8 @@ mod tests { let buffer = buffer.read(app); let mut to_fold = Vec::new(); for _ in 0..rng.gen_range(1..=5) { - let end = rng.gen_range(0..=buffer.len()); - let start = rng.gen_range(0..=end); + let end = buffer.next_char_boundary(rng.gen_range(0..=buffer.len())); + let start = buffer.prev_char_boundary(rng.gen_range(0..=end)); to_fold.push(start..end); } log::info!("folding {:?}", to_fold); @@ -854,8 +946,8 @@ mod tests { let buffer = buffer.read(app); let mut to_unfold = Vec::new(); for _ in 0..rng.gen_range(1..=3) { - let end = rng.gen_range(0..=buffer.len()); - let start = rng.gen_range(0..=end); + let end = buffer.next_char_boundary(rng.gen_range(0..=buffer.len())); + let start = buffer.prev_char_boundary(rng.gen_range(0..=end)); to_unfold.push(start..end); } log::info!("unfolding {:?}", to_unfold); @@ -892,7 +984,7 @@ mod tests { for (display_row, line) in expected_text.lines().enumerate() { let line_len = map.line_len(display_row as u32, app.as_ref()); - assert_eq!(line_len, line.chars().count() as u32); + assert_eq!(line_len, line.len() as u32); } let rightmost_point = map.rightmost_point(app.as_ref()); @@ -903,24 +995,30 @@ mod tests { let buffer_offset = buffer_point.to_offset(buffer); assert_eq!( map.to_display_point(buffer_point, app.as_ref()), - display_point + display_point, + "to_display_point({:?})", + buffer_point, ); assert_eq!( map.to_buffer_offset(display_point, app.as_ref()), - buffer_offset + buffer_offset, + "to_buffer_offset({:?})", + display_point, ); assert_eq!( map.to_display_offset(display_point, app.as_ref()), - display_offset + display_offset, + "to_display_offset({:?})", + display_point, ); if c == '\n' { *display_point.row_mut() += 1; *display_point.column_mut() = 0; } else { - *display_point.column_mut() += 1; + *display_point.column_mut() += c.len_utf8() as u32; } - display_offset.0 += 1; + display_offset.0 += c.len_utf8(); if display_point.column() > rightmost_point.column() { panic!( "invalid rightmost point {:?}, found point {:?}", @@ -930,21 +1028,15 @@ mod tests { } for _ in 0..5 { - let row = rng.gen_range(0..=map.max_point(app.as_ref()).row()); - let column = rng.gen_range(0..=map.line_len(row, app.as_ref())); - let point = DisplayPoint::new(row, column); - let offset = map.to_display_offset(point, app.as_ref()).0; - let len = rng.gen_range(0..=map.len(app.as_ref()) - offset); + let offset = map.prev_char_boundary( + DisplayOffset(rng.gen_range(0..=map.len(app.as_ref()))), + app.as_ref(), + ); assert_eq!( map.snapshot(app.as_ref()) - .chars_at(point, app.as_ref()) - .take(len) + .chunks_at(offset, app.as_ref()) .collect::(), - expected_text - .chars() - .skip(offset) - .take(len) - .collect::() + &expected_text[offset.0..], ); } @@ -967,8 +1059,8 @@ mod tests { } for _ in 0..5 { - let end = rng.gen_range(0..=buffer.len()); - let start = rng.gen_range(0..=end); + let end = buffer.next_char_boundary(rng.gen_range(0..=buffer.len())); + let start = buffer.prev_char_boundary(rng.gen_range(0..=end)); let expected_folds = map .folds .items() @@ -1026,7 +1118,7 @@ mod tests { impl FoldMap { fn text(&self, app: &AppContext) -> String { self.snapshot(app) - .chars_at(DisplayPoint(Point::zero()), app) + .chunks_at(DisplayOffset(0), app) .collect() } From c62a679e12f31fa62db5468fa9ea6b26345183bb Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 17 May 2021 20:45:14 -0700 Subject: [PATCH 06/18] Use chunk-wise DisplayMap iteration when laying out lines --- zed/src/editor/buffer_view.rs | 21 ++-- zed/src/editor/display_map/fold_map.rs | 55 ++--------- zed/src/editor/display_map/mod.rs | 128 +++++++++++++++---------- zed/src/editor/movement.rs | 4 +- 4 files changed, 99 insertions(+), 109 deletions(-) diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index ff8ea18cf086526fde99a7f8280afff8171b5e8d..84f15aa44b6fae40ce2a2caba02a9d25dd80f544 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -2140,23 +2140,26 @@ impl BufferView { let mut layouts = Vec::with_capacity(rows.len()); let mut line = String::new(); - let mut line_len = 0; let mut row = rows.start; let snapshot = self.display_map.snapshot(ctx); - let chars = snapshot.chars_at(DisplayPoint::new(rows.start, 0), ctx); - for char in chars.chain(Some('\n')) { - if char == '\n' { - layouts.push(layout_cache.layout_str(&line, font_size, &[(0..line_len, font_id)])); + let chunks = snapshot.chunks_at(DisplayPoint::new(rows.start, 0), ctx); + for (chunk_row, chunk_line) in chunks + .chain(Some("\n")) + .flat_map(|chunk| chunk.split("\n").enumerate()) + { + if chunk_row > 0 { + layouts.push(layout_cache.layout_str( + &line, + font_size, + &[(0..line.chars().count(), font_id)], + )); line.clear(); - line_len = 0; row += 1; if row == rows.end { break; } - } else { - line_len += 1; - line.push(char); } + line.push_str(chunk_line); } Ok(layouts) diff --git a/zed/src/editor/display_map/fold_map.rs b/zed/src/editor/display_map/fold_map.rs index 4b9115a52bb896fc3e7ca7bca7301fd707cfcf38..0176da26dec6c640d2a6cf6d8d232ac179b3211c 100644 --- a/zed/src/editor/display_map/fold_map.rs +++ b/zed/src/editor/display_map/fold_map.rs @@ -3,7 +3,7 @@ use super::{ Anchor, Buffer, DisplayPoint, Edit, Point, ToOffset, }; use crate::{ - editor::{buffer, rope}, + editor::buffer, sum_tree::{self, Cursor, FilterCursor, SeekBias, SumTree}, time, }; @@ -11,7 +11,6 @@ use gpui::{AppContext, ModelHandle}; use parking_lot::{Mutex, MutexGuard}; use std::{ cmp::{self, Ordering}, - iter::Take, ops::Range, }; @@ -442,19 +441,16 @@ impl FoldMapSnapshot { } } - pub fn chars_at<'a>(&'a self, point: DisplayPoint, ctx: &'a AppContext) -> Chars<'a> { + pub fn chars_at<'a>( + &'a self, + point: DisplayPoint, + ctx: &'a AppContext, + ) -> impl Iterator + 'a { let offset = self.to_display_offset(point, ctx); - let mut cursor = self.transforms.cursor(); - cursor.seek(&offset, SeekBias::Right, &()); - Chars { - cursor, - offset: offset.0, - buffer: self.buffer.read(ctx), - buffer_chars: None, - } + self.chunks_at(offset, ctx).flat_map(str::chars) } - fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> DisplayOffset { + pub fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> DisplayOffset { let mut cursor = self.transforms.cursor::(); cursor.seek(&point, SeekBias::Right, &()); let overshoot = point.0 - cursor.start().display.lines; @@ -628,41 +624,6 @@ impl<'a> Iterator for BufferRows<'a> { } } -pub struct Chars<'a> { - cursor: Cursor<'a, Transform, DisplayOffset, TransformSummary>, - offset: usize, - buffer: &'a Buffer, - buffer_chars: Option>>, -} - -impl<'a> Iterator for Chars<'a> { - type Item = char; - - fn next(&mut self) -> Option { - if let Some(c) = self.buffer_chars.as_mut().and_then(|chars| chars.next()) { - self.offset += c.len_utf8(); - return Some(c); - } - - while self.offset == self.cursor.end().display.bytes && self.cursor.item().is_some() { - self.cursor.next(); - } - - self.cursor.item().and_then(|transform| { - if let Some(c) = transform.display_text { - self.offset += c.len(); - Some(c.chars().next().unwrap()) - } else { - let overshoot = self.offset - self.cursor.start().display.bytes; - let buffer_start = self.cursor.start().buffer.bytes + overshoot; - let char_count = self.cursor.end().buffer.bytes - buffer_start; - self.buffer_chars = Some(self.buffer.chars_at(buffer_start).take(char_count)); - self.next() - } - }) - } -} - pub struct Chunks<'a> { transform_cursor: Cursor<'a, Transform, DisplayOffset, TransformSummary>, buffer_chunks: buffer::ChunksIter<'a>, diff --git a/zed/src/editor/display_map/mod.rs b/zed/src/editor/display_map/mod.rs index 94b91301beff5bba34d4ef2a7e68904a9d97d4cc..a0f3c9d34a96b8565afd4d96f16e797fd4603dfa 100644 --- a/zed/src/editor/display_map/mod.rs +++ b/zed/src/editor/display_map/mod.rs @@ -67,15 +67,24 @@ impl DisplayMap { pub fn text(&self, ctx: &AppContext) -> String { self.snapshot(ctx) - .chars_at(DisplayPoint::zero(), ctx) + .chunks_at(DisplayPoint::zero(), ctx) .collect() } pub fn line(&self, display_row: u32, ctx: &AppContext) -> String { - self.snapshot(ctx) - .chars_at(DisplayPoint::new(display_row, 0), ctx) - .take_while(|c| *c != '\n') - .collect() + let mut result = String::new(); + for chunk in self + .snapshot(ctx) + .chunks_at(DisplayPoint::new(display_row, 0), ctx) + { + if let Some(ix) = chunk.find('\n') { + result.push_str(&chunk[0..ix]); + break; + } else { + result.push_str(chunk); + } + } + result } pub fn line_indent(&self, display_row: u32, ctx: &AppContext) -> (u32, bool) { @@ -83,7 +92,8 @@ impl DisplayMap { let mut is_blank = true; for c in self .snapshot(ctx) - .chars_at(DisplayPoint::new(display_row, 0), ctx) + .chunks_at(DisplayPoint::new(display_row, 0), ctx) + .flat_map(str::chars) { if c == ' ' { indent += 1; @@ -132,21 +142,28 @@ impl DisplayMapSnapshot { self.folds_snapshot.buffer_rows(start_row) } - pub fn chars_at<'a>(&'a self, point: DisplayPoint, app: &'a AppContext) -> Chars<'a> { + pub fn chunks_at<'a>(&'a self, point: DisplayPoint, app: &'a AppContext) -> Chunks<'a> { let column = point.column() as usize; - let (point, to_next_stop) = self.collapse_tabs(point, Bias::Left, app); - let mut fold_chars = self.folds_snapshot.chars_at(point, app); - if to_next_stop > 0 { - fold_chars.next(); - } - Chars { - fold_chars, + let (point, _) = self.collapse_tabs(point, Bias::Left, app); + let fold_chunks = self + .folds_snapshot + .chunks_at(self.folds_snapshot.to_display_offset(point, app), app); + Chunks { + fold_chunks, column, - to_next_stop, tab_size: self.tab_size, + chunk: "", } } + pub fn chars_at<'a>( + &'a self, + point: DisplayPoint, + app: &'a AppContext, + ) -> impl Iterator + 'a { + self.chunks_at(point, app).flat_map(str::chars) + } + fn expand_tabs(&self, mut point: DisplayPoint, ctx: &AppContext) -> DisplayPoint { let chars = self .folds_snapshot @@ -238,38 +255,50 @@ impl Anchor { } } -pub struct Chars<'a> { - fold_chars: fold_map::Chars<'a>, +pub struct Chunks<'a> { + fold_chunks: fold_map::Chunks<'a>, + chunk: &'a str, column: usize, - to_next_stop: usize, tab_size: usize, } -impl<'a> Iterator for Chars<'a> { - type Item = char; +impl<'a> Iterator for Chunks<'a> { + type Item = &'a str; fn next(&mut self) -> Option { - if self.to_next_stop > 0 { - self.to_next_stop -= 1; - self.column += 1; - Some(' ') - } else { - self.fold_chars.next().map(|c| match c { + // Handles a tab width <= 16 + const SPACES: &'static str = " "; + + if self.chunk.is_empty() { + if let Some(chunk) = self.fold_chunks.next() { + self.chunk = chunk; + } else { + return None; + } + } + + for (ix, c) in self.chunk.char_indices() { + match c { '\t' => { - self.to_next_stop = self.tab_size - self.column % self.tab_size - 1; - self.column += 1; - ' ' - } - '\n' => { - self.column = 0; - c - } - _ => { - self.column += 1; - c + if ix > 0 { + let (prefix, suffix) = self.chunk.split_at(ix); + self.chunk = suffix; + return Some(prefix); + } else { + self.chunk = &self.chunk[1..]; + let len = self.tab_size - self.column % self.tab_size; + self.column += len; + return Some(&SPACES[0..len]); + } } - }) + '\n' => self.column = 0, + _ => self.column += 1, + } } + + let result = Some(self.chunk); + self.chunk = ""; + result } } @@ -321,7 +350,7 @@ mod tests { use crate::test::*; #[gpui::test] - fn test_chars_at(app: &mut gpui::MutableAppContext) { + fn test_chunks_at(app: &mut gpui::MutableAppContext) { let text = sample_text(6, 6); let buffer = app.add_model(|ctx| Buffer::new(0, text, ctx)); let map = DisplayMap::new(buffer.clone(), 4, app.as_ref()); @@ -340,24 +369,21 @@ mod tests { .unwrap(); assert_eq!( - map.snapshot(app.as_ref()) - .chars_at(DisplayPoint::new(1, 0), app.as_ref()) - .take(10) - .collect::(), + &map.snapshot(app.as_ref()) + .chunks_at(DisplayPoint::new(1, 0), app.as_ref()) + .collect::()[0..10], " b bb" ); assert_eq!( - map.snapshot(app.as_ref()) - .chars_at(DisplayPoint::new(1, 2), app.as_ref()) - .take(10) - .collect::(), + &map.snapshot(app.as_ref()) + .chunks_at(DisplayPoint::new(1, 2), app.as_ref()) + .collect::()[0..10], " b bbbb" ); assert_eq!( - map.snapshot(app.as_ref()) - .chars_at(DisplayPoint::new(1, 6), app.as_ref()) - .take(13) - .collect::(), + &map.snapshot(app.as_ref()) + .chunks_at(DisplayPoint::new(1, 6), app.as_ref()) + .collect::()[0..13], " bbbbb\nc c" ); } diff --git a/zed/src/editor/movement.rs b/zed/src/editor/movement.rs index 162d593ef6a56b3aa724253fc45f2e39b045e18d..3ac135d08ce98ccf758b44bf9856db8f20e8f66b 100644 --- a/zed/src/editor/movement.rs +++ b/zed/src/editor/movement.rs @@ -114,7 +114,7 @@ pub fn prev_word_boundary( } prev_c = Some(c); - column += 1; + column += c.len_utf8() as u32; } Ok(boundary) } @@ -135,7 +135,7 @@ pub fn next_word_boundary( *point.row_mut() += 1; *point.column_mut() = 0; } else { - *point.column_mut() += 1; + *point.column_mut() += c.len_utf8() as u32; } prev_c = Some(c); } From 6621b9b552e5b1cc8a5cdf351336060d180794b1 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 18 May 2021 14:17:37 -0700 Subject: [PATCH 07/18] Expand tabs correctly with multibyte characters Co-Authored-By: Nathan Sobo --- zed/src/editor/buffer/mod.rs | 4 +- zed/src/editor/buffer/rope.rs | 46 +-------- zed/src/editor/buffer_view.rs | 14 +-- zed/src/editor/display_map/mod.rs | 150 +++++++++++++++++++++--------- 4 files changed, 118 insertions(+), 96 deletions(-) diff --git a/zed/src/editor/buffer/mod.rs b/zed/src/editor/buffer/mod.rs index ce76c34133822a6a44e87dd3d5648aa351e0645a..7c2ca518becdfc99b9effef482d59f90406c4d93 100644 --- a/zed/src/editor/buffer/mod.rs +++ b/zed/src/editor/buffer/mod.rs @@ -643,11 +643,11 @@ impl Buffer { self.visible_text.chunks_in_range(start..end) } - pub fn chars(&self) -> rope::Chars { + pub fn chars(&self) -> impl Iterator + '_ { self.chars_at(0) } - pub fn chars_at(&self, position: T) -> rope::Chars { + pub fn chars_at(&self, position: T) -> impl Iterator + '_ { let offset = position.to_offset(self); self.visible_text.chars_at(offset) } diff --git a/zed/src/editor/buffer/rope.rs b/zed/src/editor/buffer/rope.rs index e409084e11dae751fc80130c4dad0e19d5b90d38..15cf7ff0d782f55abc5d3a516430ad5e546d0cf0 100644 --- a/zed/src/editor/buffer/rope.rs +++ b/zed/src/editor/buffer/rope.rs @@ -2,7 +2,7 @@ use super::Point; use crate::sum_tree::{self, SeekBias, SumTree}; use arrayvec::ArrayString; use smallvec::SmallVec; -use std::{cmp, iter::Skip, ops::Range, str}; +use std::{cmp, ops::Range, str}; #[cfg(test)] const CHUNK_BASE: usize = 6; @@ -107,12 +107,12 @@ impl Rope { Cursor::new(self, offset) } - pub fn chars(&self) -> Chars { + pub fn chars(&self) -> impl Iterator + '_ { self.chars_at(0) } - pub fn chars_at(&self, start: usize) -> Chars { - Chars::new(self, start) + pub fn chars_at(&self, start: usize) -> impl Iterator + '_ { + self.chunks_in_range(start..self.len()).flat_map(str::chars) } pub fn chunks<'a>(&'a self) -> ChunksIter<'a> { @@ -446,44 +446,6 @@ impl<'a> sum_tree::Dimension<'a, TextSummary> for Point { } } -pub struct Chars<'a> { - cursor: sum_tree::Cursor<'a, Chunk, usize, usize>, - chars: Skip>, -} - -impl<'a> Chars<'a> { - pub fn new(rope: &'a Rope, start: usize) -> Self { - let mut cursor = rope.chunks.cursor::(); - cursor.seek(&start, SeekBias::Left, &()); - let chars = if let Some(chunk) = cursor.item() { - let ix = start - cursor.start(); - assert_char_boundary(&chunk.0, ix); - cursor.next(); - chunk.0.chars().skip(ix) - } else { - "".chars().skip(0) - }; - - Self { cursor, chars } - } -} - -impl<'a> Iterator for Chars<'a> { - type Item = char; - - fn next(&mut self) -> Option { - if let Some(ch) = self.chars.next() { - Some(ch) - } else if let Some(chunk) = self.cursor.item() { - self.chars = chunk.0.chars().skip(0); - self.cursor.next(); - Some(self.chars.next().unwrap()) - } else { - None - } - } -} - fn find_split_ix(text: &str) -> usize { let mut ix = text.len() / 2; while !text.is_char_boundary(ix) { diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 84f15aa44b6fae40ce2a2caba02a9d25dd80f544..c2964a41df6c5f19265676a2494717b336ab33e9 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -3207,7 +3207,7 @@ mod tests { &[ DisplayPoint::new(0, 1)..DisplayPoint::new(0, 1), DisplayPoint::new(3, 1)..DisplayPoint::new(3, 1), - DisplayPoint::new(3, 2)..DisplayPoint::new(4, 2), + DisplayPoint::new(3, 2)..DisplayPoint::new(4, 3), DisplayPoint::new(5, 0)..DisplayPoint::new(5, 2), ], ctx, @@ -3229,7 +3229,7 @@ mod tests { vec![ DisplayPoint::new(0, 1)..DisplayPoint::new(0, 1), DisplayPoint::new(2, 1)..DisplayPoint::new(2, 1), - DisplayPoint::new(2, 2)..DisplayPoint::new(3, 2), + DisplayPoint::new(2, 2)..DisplayPoint::new(3, 3), DisplayPoint::new(4, 0)..DisplayPoint::new(4, 2) ] ); @@ -3244,7 +3244,7 @@ mod tests { vec![ DisplayPoint::new(1, 1)..DisplayPoint::new(1, 1), DisplayPoint::new(3, 1)..DisplayPoint::new(3, 1), - DisplayPoint::new(3, 2)..DisplayPoint::new(4, 2), + DisplayPoint::new(3, 2)..DisplayPoint::new(4, 3), DisplayPoint::new(5, 0)..DisplayPoint::new(5, 2) ] ); @@ -3259,7 +3259,7 @@ mod tests { vec![ DisplayPoint::new(2, 1)..DisplayPoint::new(2, 1), DisplayPoint::new(3, 1)..DisplayPoint::new(3, 1), - DisplayPoint::new(3, 2)..DisplayPoint::new(4, 2), + DisplayPoint::new(3, 2)..DisplayPoint::new(4, 3), DisplayPoint::new(5, 0)..DisplayPoint::new(5, 2) ] ); @@ -3274,7 +3274,7 @@ mod tests { vec![ DisplayPoint::new(1, 1)..DisplayPoint::new(1, 1), DisplayPoint::new(2, 1)..DisplayPoint::new(2, 1), - DisplayPoint::new(2, 2)..DisplayPoint::new(3, 2), + DisplayPoint::new(2, 2)..DisplayPoint::new(3, 3), DisplayPoint::new(4, 0)..DisplayPoint::new(4, 2) ] ); @@ -3489,7 +3489,7 @@ mod tests { DisplayPoint::new(0, 0)..DisplayPoint::new(0, 1), DisplayPoint::new(0, 2)..DisplayPoint::new(0, 2), DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0), - DisplayPoint::new(4, 2)..DisplayPoint::new(4, 2), + DisplayPoint::new(4, 4)..DisplayPoint::new(4, 4), ], ctx, ) @@ -3511,7 +3511,7 @@ mod tests { DisplayPoint::new(0, 1)..DisplayPoint::new(0, 1), DisplayPoint::new(0, 2)..DisplayPoint::new(0, 2), DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0), - DisplayPoint::new(4, 2)..DisplayPoint::new(4, 2) + DisplayPoint::new(4, 4)..DisplayPoint::new(4, 4) ] ); diff --git a/zed/src/editor/display_map/mod.rs b/zed/src/editor/display_map/mod.rs index a0f3c9d34a96b8565afd4d96f16e797fd4603dfa..d1b09f64475dadd7398de8fa777daead6f387856 100644 --- a/zed/src/editor/display_map/mod.rs +++ b/zed/src/editor/display_map/mod.rs @@ -143,16 +143,17 @@ impl DisplayMapSnapshot { } pub fn chunks_at<'a>(&'a self, point: DisplayPoint, app: &'a AppContext) -> Chunks<'a> { - let column = point.column() as usize; - let (point, _) = self.collapse_tabs(point, Bias::Left, app); + let (point, expanded_char_column, to_next_stop) = + self.collapse_tabs(point, Bias::Left, app); let fold_chunks = self .folds_snapshot .chunks_at(self.folds_snapshot.to_display_offset(point, app), app); Chunks { fold_chunks, - column, + column: expanded_char_column, tab_size: self.tab_size, - chunk: "", + chunk: &SPACES[0..to_next_stop], + skip_leading_tab: to_next_stop > 0, } } @@ -178,15 +179,16 @@ impl DisplayMapSnapshot { mut point: DisplayPoint, bias: Bias, ctx: &AppContext, - ) -> (DisplayPoint, usize) { + ) -> (DisplayPoint, usize, usize) { let chars = self .folds_snapshot .chars_at(DisplayPoint(Point::new(point.row(), 0)), ctx); let expanded = point.column() as usize; - let (collapsed, to_next_stop) = collapse_tabs(chars, expanded, bias, self.tab_size); + let (collapsed, expanded_char_column, to_next_stop) = + collapse_tabs(chars, expanded, bias, self.tab_size); *point.column_mut() = collapsed as u32; - (point, to_next_stop) + (point, expanded_char_column, to_next_stop) } } @@ -220,20 +222,20 @@ impl DisplayPoint { pub fn to_buffer_point(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> Point { map.fold_map - .to_buffer_point(self.collapse_tabs(map, bias, ctx).0, ctx) + .to_buffer_point(self.collapse_tabs(map, bias, ctx), ctx) } pub fn to_buffer_offset(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> usize { map.fold_map - .to_buffer_offset(self.collapse_tabs(&map, bias, ctx).0, ctx) + .to_buffer_offset(self.collapse_tabs(&map, bias, ctx), ctx) } fn expand_tabs(self, map: &DisplayMap, ctx: &AppContext) -> Self { map.snapshot(ctx).expand_tabs(self, ctx) } - fn collapse_tabs(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> (Self, usize) { - map.snapshot(ctx).collapse_tabs(self, bias, ctx) + fn collapse_tabs(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> Self { + map.snapshot(ctx).collapse_tabs(self, bias, ctx).0 } } @@ -255,23 +257,28 @@ impl Anchor { } } +// Handles a tab width <= 16 +const SPACES: &'static str = " "; + pub struct Chunks<'a> { fold_chunks: fold_map::Chunks<'a>, chunk: &'a str, column: usize, tab_size: usize, + skip_leading_tab: bool, } impl<'a> Iterator for Chunks<'a> { type Item = &'a str; fn next(&mut self) -> Option { - // Handles a tab width <= 16 - const SPACES: &'static str = " "; - if self.chunk.is_empty() { if let Some(chunk) = self.fold_chunks.next() { self.chunk = chunk; + if self.skip_leading_tab { + self.chunk = &self.chunk[1..]; + self.skip_leading_tab = false; + } } else { return None; } @@ -303,15 +310,24 @@ impl<'a> Iterator for Chunks<'a> { } pub fn expand_tabs(chars: impl Iterator, column: usize, tab_size: usize) -> usize { - let mut expanded = 0; - for c in chars.take(column) { + let mut expanded_chars = 0; + let mut expanded_bytes = 0; + let mut collapsed_bytes = 0; + for c in chars { + if collapsed_bytes == column { + break; + } if c == '\t' { - expanded += tab_size - expanded % tab_size; + let tab_len = tab_size - expanded_chars % tab_size; + expanded_bytes += tab_len; + expanded_chars += tab_len; } else { - expanded += 1; + expanded_bytes += c.len_utf8(); + expanded_chars += 1; } + collapsed_bytes += c.len_utf8(); } - expanded + expanded_bytes } pub fn collapse_tabs( @@ -319,33 +335,39 @@ pub fn collapse_tabs( column: usize, bias: Bias, tab_size: usize, -) -> (usize, usize) { - let mut expanded = 0; - let mut collapsed = 0; +) -> (usize, usize, usize) { + let mut expanded_bytes = 0; + let mut expanded_chars = 0; + let mut collapsed_bytes = 0; while let Some(c) = chars.next() { - if expanded == column { + if expanded_bytes == column { break; } if c == '\t' { - expanded += tab_size - (expanded % tab_size); - if expanded > column { + let tab_len = tab_size - (expanded_chars % tab_size); + expanded_chars += tab_len; + expanded_bytes += tab_len; + if expanded_bytes > column { + expanded_chars -= expanded_bytes - column; return match bias { - Bias::Left => (collapsed, expanded - column), - Bias::Right => (collapsed + 1, 0), + Bias::Left => (collapsed_bytes, expanded_chars, expanded_bytes - column), + Bias::Right => (collapsed_bytes + 1, expanded_chars, 0), }; } - collapsed += 1; } else { - expanded += 1; - collapsed += 1; + expanded_chars += 1; + expanded_bytes += c.len_utf8(); } + collapsed_bytes += c.len_utf8(); } - (collapsed, 0) + (collapsed_bytes, expanded_chars, 0) } #[cfg(test)] mod tests { + use gpui::MutableAppContext; + use super::*; use crate::test::*; @@ -395,20 +417,58 @@ mod tests { assert_eq!(expand_tabs("\ta".chars(), 2, 4), 5); } - #[test] - fn test_collapse_tabs() { - assert_eq!(collapse_tabs("\t".chars(), 0, Bias::Left, 4), (0, 0)); - assert_eq!(collapse_tabs("\t".chars(), 0, Bias::Right, 4), (0, 0)); - assert_eq!(collapse_tabs("\t".chars(), 1, Bias::Left, 4), (0, 3)); - assert_eq!(collapse_tabs("\t".chars(), 1, Bias::Right, 4), (1, 0)); - assert_eq!(collapse_tabs("\t".chars(), 2, Bias::Left, 4), (0, 2)); - assert_eq!(collapse_tabs("\t".chars(), 2, Bias::Right, 4), (1, 0)); - assert_eq!(collapse_tabs("\t".chars(), 3, Bias::Left, 4), (0, 1)); - assert_eq!(collapse_tabs("\t".chars(), 3, Bias::Right, 4), (1, 0)); - assert_eq!(collapse_tabs("\t".chars(), 4, Bias::Left, 4), (1, 0)); - assert_eq!(collapse_tabs("\t".chars(), 4, Bias::Right, 4), (1, 0)); - assert_eq!(collapse_tabs("\ta".chars(), 5, Bias::Left, 4), (2, 0)); - assert_eq!(collapse_tabs("\ta".chars(), 5, Bias::Right, 4), (2, 0)); + #[gpui::test] + fn test_tabs_with_multibyte_chars(app: &mut MutableAppContext) { + let text = "✅\t\tx\nα\t\n🏀α\t\ty"; + let buffer = app.add_model(|ctx| Buffer::new(0, text, ctx)); + let ctx = app.as_ref(); + let map = DisplayMap::new(buffer.clone(), 4, ctx); + assert_eq!(map.text(ctx), "✅ x\nα \n🏀α y"); + + let point = Point::new(0, "✅\t\t".len() as u32); + let display_point = DisplayPoint::new(0, "✅ ".len() as u32); + assert_eq!(point.to_display_point(&map, ctx), display_point); + assert_eq!(display_point.to_buffer_point(&map, Bias::Left, ctx), point,); + + let point = Point::new(1, "α\t".len() as u32); + let display_point = DisplayPoint::new(1, "α ".len() as u32); + assert_eq!(point.to_display_point(&map, ctx), display_point); + assert_eq!(display_point.to_buffer_point(&map, Bias::Left, ctx), point,); + + let point = Point::new(2, "🏀α\t\t".len() as u32); + let display_point = DisplayPoint::new(2, "🏀α ".len() as u32); + assert_eq!(point.to_display_point(&map, ctx), display_point); + assert_eq!(display_point.to_buffer_point(&map, Bias::Left, ctx), point,); + + // Display points inside of expanded tabs + assert_eq!( + DisplayPoint::new(0, "✅ ".len() as u32).to_buffer_point(&map, Bias::Right, ctx), + Point::new(0, "✅\t\t".len() as u32), + ); + assert_eq!( + DisplayPoint::new(0, "✅ ".len() as u32).to_buffer_point(&map, Bias::Left, ctx), + Point::new(0, "✅\t".len() as u32), + ); + assert_eq!( + map.snapshot(ctx) + .chunks_at(DisplayPoint::new(0, "✅ ".len() as u32), ctx) + .collect::(), + " x\nα \n🏀α y" + ); + assert_eq!( + DisplayPoint::new(0, "✅ ".len() as u32).to_buffer_point(&map, Bias::Right, ctx), + Point::new(0, "✅\t".len() as u32), + ); + assert_eq!( + DisplayPoint::new(0, "✅ ".len() as u32).to_buffer_point(&map, Bias::Left, ctx), + Point::new(0, "✅".len() as u32), + ); + assert_eq!( + map.snapshot(ctx) + .chunks_at(DisplayPoint::new(0, "✅ ".len() as u32), ctx) + .collect::(), + " x\nα \n🏀α y" + ); } #[gpui::test] From f3db0dcff62e129f0ce9fb247a96780f795e18d7 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 18 May 2021 22:32:34 -0700 Subject: [PATCH 08/18] Start work on making row/columnwise movement work w/ byte columns --- zed/src/editor/buffer/rope.rs | 5 ++- zed/src/editor/buffer_view.rs | 70 ++++++++++++++++++++++++++++++- zed/src/editor/display_map/mod.rs | 34 +++++++++++++-- zed/src/editor/movement.rs | 19 +++++++-- 4 files changed, 119 insertions(+), 9 deletions(-) diff --git a/zed/src/editor/buffer/rope.rs b/zed/src/editor/buffer/rope.rs index 15cf7ff0d782f55abc5d3a516430ad5e546d0cf0..195d89386b6a3744a679f35202f2f4ebb3675c97 100644 --- a/zed/src/editor/buffer/rope.rs +++ b/zed/src/editor/buffer/rope.rs @@ -335,6 +335,9 @@ impl Chunk { let mut point = Point::new(0, 0); for ch in self.0.chars() { if point >= target { + if point > target { + panic!("point {:?} is inside of character {:?}", target, ch); + } break; } @@ -346,8 +349,6 @@ impl Chunk { } offset += ch.len_utf8(); } - - assert_eq!(point, target); offset } } diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index c2964a41df6c5f19265676a2494717b336ab33e9..23b5b08e90fa28426ecf2138bcebcdd456080092 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -2358,7 +2358,11 @@ impl workspace::ItemView for BufferView { #[cfg(test)] mod tests { use super::*; - use crate::{editor::Point, settings, test::sample_text}; + use crate::{ + editor::Point, + settings, + test::{multibyte_sample_text, sample_text}, + }; use unindent::Unindent; #[gpui::test] @@ -2715,6 +2719,70 @@ mod tests { }); } + #[gpui::test] + fn test_move_cursor_multibyte(app: &mut gpui::MutableAppContext) { + let buffer = app.add_model(|ctx| Buffer::new(0, "ⓐⓑⓒⓓⓔ\nabcde\nαβγδε\n", ctx)); + let settings = settings::channel(&app.font_cache()).unwrap().1; + let (_, view) = app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), settings, ctx)); + + assert_eq!('ⓐ'.len_utf8(), 3); + assert_eq!('α'.len_utf8(), 2); + + view.update(app, |view, ctx| { + view.fold_ranges( + vec![ + Point::new(0, 6)..Point::new(0, 12), + Point::new(1, 2)..Point::new(1, 4), + Point::new(2, 4)..Point::new(2, 8), + ], + ctx, + ); + assert_eq!(view.text(ctx.as_ref()), "ⓐⓑ…ⓔ\nab…e\nαβ…ε\n"); + + view.move_right(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[DisplayPoint::new(0, 3)..DisplayPoint::new(0, 3)] + ); + + view.move_down(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[DisplayPoint::new(1, 1)..DisplayPoint::new(1, 1)] + ); + + view.move_right(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[DisplayPoint::new(1, 2)..DisplayPoint::new(1, 2)] + ); + + view.move_down(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[DisplayPoint::new(2, 4)..DisplayPoint::new(2, 4)] + ); + + view.move_left(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[DisplayPoint::new(2, 2)..DisplayPoint::new(2, 2)] + ); + + view.move_up(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[DisplayPoint::new(1, 1)..DisplayPoint::new(1, 1)] + ); + + view.move_up(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[DisplayPoint::new(0, 3)..DisplayPoint::new(0, 3)] + ); + }); + } + #[gpui::test] fn test_beginning_end_of_line(app: &mut gpui::MutableAppContext) { let buffer = app.add_model(|ctx| Buffer::new(0, "abc\n def", ctx)); diff --git a/zed/src/editor/display_map/mod.rs b/zed/src/editor/display_map/mod.rs index d1b09f64475dadd7398de8fa777daead6f387856..919f2967494a2ac4af2cc756f329ab6ad3e4c2e7 100644 --- a/zed/src/editor/display_map/mod.rs +++ b/zed/src/editor/display_map/mod.rs @@ -92,8 +92,7 @@ impl DisplayMap { let mut is_blank = true; for c in self .snapshot(ctx) - .chunks_at(DisplayPoint::new(display_row, 0), ctx) - .flat_map(str::chars) + .chars_at(DisplayPoint::new(display_row, 0), ctx) { if c == ' ' { indent += 1; @@ -165,6 +164,32 @@ impl DisplayMapSnapshot { self.chunks_at(point, app).flat_map(str::chars) } + pub fn column_to_chars(&self, display_row: u32, target: u32, ctx: &AppContext) -> u32 { + let mut count = 0; + let mut column = 0; + for c in self.chars_at(DisplayPoint::new(display_row, 0), ctx) { + count += 1; + column += c.len_utf8() as u32; + if column >= target { + break; + } + } + count + } + + pub fn column_from_chars(&self, display_row: u32, char_count: u32, ctx: &AppContext) -> u32 { + let mut count = 0; + let mut column = 0; + for c in self.chars_at(DisplayPoint::new(display_row, 0), ctx) { + count += 1; + column += c.len_utf8() as u32; + if count >= char_count { + break; + } + } + column + } + fn expand_tabs(&self, mut point: DisplayPoint, ctx: &AppContext) -> DisplayPoint { let chars = self .folds_snapshot @@ -187,7 +212,6 @@ impl DisplayMapSnapshot { let (collapsed, expanded_char_column, to_next_stop) = collapse_tabs(chars, expanded, bias, self.tab_size); *point.column_mut() = collapsed as u32; - (point, expanded_char_column, to_next_stop) } } @@ -360,6 +384,10 @@ pub fn collapse_tabs( expanded_bytes += c.len_utf8(); } collapsed_bytes += c.len_utf8(); + + if expanded_bytes > column { + panic!("column {} is inside of character {:?}", column, c); + } } (collapsed_bytes, expanded_chars, 0) } diff --git a/zed/src/editor/movement.rs b/zed/src/editor/movement.rs index 3ac135d08ce98ccf758b44bf9856db8f20e8f66b..3e89537f41c3fe4c9b69e64748a48018a231c623 100644 --- a/zed/src/editor/movement.rs +++ b/zed/src/editor/movement.rs @@ -16,7 +16,12 @@ pub fn left(map: &DisplayMap, mut point: DisplayPoint, app: &AppContext) -> Resu pub fn right(map: &DisplayMap, mut point: DisplayPoint, app: &AppContext) -> Result { let max_column = map.line_len(point.row(), app); if point.column() < max_column { - *point.column_mut() += 1; + *point.column_mut() += map + .snapshot(app) + .chars_at(point, app) + .next() + .unwrap() + .len_utf8() as u32; } else if point.row() < map.max_point(app).row() { *point.row_mut() += 1; *point.column_mut() = 0; @@ -35,9 +40,13 @@ pub fn up( } else { point.column() }; + + let map = map.snapshot(app); + let char_column = map.column_to_chars(point.row(), goal_column, app); + if point.row() > 0 { *point.row_mut() -= 1; - *point.column_mut() = cmp::min(goal_column, map.line_len(point.row(), app)); + *point.column_mut() = map.column_from_chars(point.row(), char_column, app); } else { point = DisplayPoint::new(0, 0); } @@ -56,10 +65,14 @@ pub fn down( } else { point.column() }; + let max_point = map.max_point(app); + let map = map.snapshot(app); + let char_column = map.column_to_chars(point.row(), goal_column, app); + if point.row() < max_point.row() { *point.row_mut() += 1; - *point.column_mut() = cmp::min(goal_column, map.line_len(point.row(), app)) + *point.column_mut() = map.column_from_chars(point.row(), char_column, app); } else { point = max_point; } From 510f20411a26b15c8a748a9b57100f2a71c27aa1 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 19 May 2021 13:45:54 -0700 Subject: [PATCH 09/18] Get cursor movement working with byte columns Introduce `clip_point` and `clip_offset` methods on `Rope`, `Buffer`, and `FoldMap`. Co-Authored-By: Antonio Scandurra --- zed/src/editor/buffer/mod.rs | 13 ++-- zed/src/editor/buffer/rope.rs | 100 +++++++++++++++---------- zed/src/editor/buffer/selection.rs | 4 +- zed/src/editor/buffer_view.rs | 83 +++++++++++++++----- zed/src/editor/display_map/fold_map.rs | 96 +++++++++++++++++------- zed/src/editor/display_map/mod.rs | 73 +++++++++++------- zed/src/editor/mod.rs | 6 ++ zed/src/editor/movement.rs | 32 +++----- 8 files changed, 263 insertions(+), 144 deletions(-) diff --git a/zed/src/editor/buffer/mod.rs b/zed/src/editor/buffer/mod.rs index 7c2ca518becdfc99b9effef482d59f90406c4d93..a86182fbae89b1dfb90ad202a14ae81d0ce54bd5 100644 --- a/zed/src/editor/buffer/mod.rs +++ b/zed/src/editor/buffer/mod.rs @@ -11,6 +11,7 @@ pub use selection::*; use similar::{ChangeTag, TextDiff}; use crate::{ + editor::Bias, operation_queue::{self, OperationQueue}, sum_tree::{self, FilterCursor, SeekBias, SumTree}, time::{self, ReplicaId}, @@ -1865,12 +1866,12 @@ impl Buffer { } } - pub fn next_char_boundary(&self, offset: usize) -> usize { - self.visible_text.next_char_boundary(offset) + pub fn clip_point(&self, point: Point, bias: Bias) -> Point { + self.visible_text.clip_point(point, bias) } - pub fn prev_char_boundary(&self, offset: usize) -> usize { - self.visible_text.prev_char_boundary(offset) + pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize { + self.visible_text.clip_offset(offset, bias) } } @@ -3232,8 +3233,8 @@ mod tests { impl Buffer { fn random_byte_range(&mut self, start_offset: usize, rng: &mut impl Rng) -> Range { - let end = self.next_char_boundary(rng.gen_range(start_offset..=self.len())); - let start = self.prev_char_boundary(rng.gen_range(start_offset..=end)); + let end = self.clip_offset(rng.gen_range(start_offset..=self.len()), Bias::Right); + let start = self.clip_offset(rng.gen_range(start_offset..=end), Bias::Left); start..end } diff --git a/zed/src/editor/buffer/rope.rs b/zed/src/editor/buffer/rope.rs index 195d89386b6a3744a679f35202f2f4ebb3675c97..ddf1a92cbfb4a2b8647cf5b011e142acba60d8a4 100644 --- a/zed/src/editor/buffer/rope.rs +++ b/zed/src/editor/buffer/rope.rs @@ -1,5 +1,8 @@ use super::Point; -use crate::sum_tree::{self, SeekBias, SumTree}; +use crate::{ + editor::Bias, + sum_tree::{self, SeekBias, SumTree}, +}; use arrayvec::ArrayString; use smallvec::SmallVec; use std::{cmp, ops::Range, str}; @@ -142,32 +145,38 @@ impl Rope { cursor.start().bytes + cursor.item().map_or(0, |chunk| chunk.to_offset(overshoot)) } - pub fn next_char_boundary(&self, mut offset: usize) -> usize { - assert!(offset <= self.summary().bytes); + pub fn clip_offset(&self, mut offset: usize, bias: Bias) -> usize { let mut cursor = self.chunks.cursor::(); cursor.seek(&offset, SeekBias::Left, &()); if let Some(chunk) = cursor.item() { let mut ix = offset - cursor.start(); while !chunk.0.is_char_boundary(ix) { - ix += 1; - offset += 1; + match bias { + Bias::Left => { + ix -= 1; + offset -= 1; + } + Bias::Right => { + ix += 1; + offset += 1; + } + } } + offset + } else { + self.summary().bytes } - offset } - pub fn prev_char_boundary(&self, mut offset: usize) -> usize { - assert!(offset <= self.summary().bytes); - let mut cursor = self.chunks.cursor::(); - cursor.seek(&offset, SeekBias::Left, &()); + pub fn clip_point(&self, point: Point, bias: Bias) -> Point { + let mut cursor = self.chunks.cursor::(); + cursor.seek(&point, SeekBias::Right, &()); if let Some(chunk) = cursor.item() { - let mut ix = offset - cursor.start(); - while !chunk.0.is_char_boundary(ix) { - ix -= 1; - offset -= 1; - } + let overshoot = point - cursor.start(); + *cursor.start() + chunk.clip_point(overshoot, bias) + } else { + self.summary().lines } - offset } } @@ -351,6 +360,22 @@ impl Chunk { } offset } + + fn clip_point(&self, target: Point, bias: Bias) -> Point { + for (row, line) in self.0.split('\n').enumerate() { + if row == target.row as usize { + let mut column = target.column.min(line.len() as u32); + while !line.is_char_boundary(column as usize) { + match bias { + Bias::Left => column -= 1, + Bias::Right => column += 1, + } + } + return Point::new(row as u32, column); + } + } + unreachable!() + } } impl sum_tree::Item for Chunk { @@ -466,30 +491,13 @@ fn find_split_ix(text: &str) -> usize { ix } -#[inline(always)] -fn assert_char_boundary(s: &str, ix: usize) { - if !s.is_char_boundary(ix) { - let mut char_start = ix; - while !s.is_char_boundary(char_start) { - char_start -= 1; - } - - let ch = s[char_start..].chars().next().unwrap(); - let char_range = char_start..char_start + ch.len_utf8(); - panic!( - "byte index {} is not a char boundary; it is inside {:?} (bytes {:?}) of `{}`", - ix, ch, char_range, s - ); - } -} - #[cfg(test)] mod tests { - use crate::util::RandomCharIter; - use super::*; + use crate::util::RandomCharIter; use rand::prelude::*; use std::env; + use Bias::{Left, Right}; #[test] fn test_all_4_byte_chars() { @@ -520,8 +528,8 @@ mod tests { let mut expected = String::new(); let mut actual = Rope::new(); for _ in 0..operations { - let end_ix = actual.next_char_boundary(rng.gen_range(0..=expected.len())); - let start_ix = actual.prev_char_boundary(rng.gen_range(0..=end_ix)); + let end_ix = clip_offset(&expected, rng.gen_range(0..=expected.len()), Right); + let start_ix = clip_offset(&expected, rng.gen_range(0..=end_ix), Left); let len = rng.gen_range(0..=64); let new_text: String = RandomCharIter::new(&mut rng).take(len).collect(); @@ -539,8 +547,8 @@ mod tests { log::info!("text: {:?}", expected); for _ in 0..5 { - let end_ix = actual.next_char_boundary(rng.gen_range(0..=expected.len())); - let start_ix = actual.prev_char_boundary(rng.gen_range(0..=end_ix)); + let end_ix = clip_offset(&expected, rng.gen_range(0..=expected.len()), Right); + let start_ix = clip_offset(&expected, rng.gen_range(0..=end_ix), Left); assert_eq!( actual.chunks_in_range(start_ix..end_ix).collect::(), &expected[start_ix..end_ix] @@ -560,8 +568,8 @@ mod tests { } for _ in 0..5 { - let end_ix = actual.next_char_boundary(rng.gen_range(0..=expected.len())); - let start_ix = actual.prev_char_boundary(rng.gen_range(0..=end_ix)); + let end_ix = clip_offset(&expected, rng.gen_range(0..=expected.len()), Right); + let start_ix = clip_offset(&expected, rng.gen_range(0..=end_ix), Left); assert_eq!( actual.cursor(start_ix).summary(end_ix), TextSummary::from(&expected[start_ix..end_ix]) @@ -571,6 +579,16 @@ mod tests { } } + fn clip_offset(text: &str, mut offset: usize, bias: Bias) -> usize { + while !text.is_char_boundary(offset) { + match bias { + Bias::Left => offset -= 1, + Bias::Right => offset += 1, + } + } + offset + } + impl Rope { fn text(&self) -> String { let mut text = String::new(); diff --git a/zed/src/editor/buffer/selection.rs b/zed/src/editor/buffer/selection.rs index 016cbe59b3f5530e5fcc097bf9045923c4df0b8c..e150ce0725966f49cdba352f008506eae9ce8913 100644 --- a/zed/src/editor/buffer/selection.rs +++ b/zed/src/editor/buffer/selection.rs @@ -1,8 +1,8 @@ use crate::{ editor::{ buffer::{Anchor, Buffer, Point, ToPoint}, - display_map::{Bias, DisplayMap}, - DisplayPoint, + display_map::DisplayMap, + Bias, DisplayPoint, }, time, }; diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 23b5b08e90fa28426ecf2138bcebcdd456080092..94a98384396de900a99f688899013c88902943f6 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -650,16 +650,16 @@ impl BufferView { if let Err(error) = buffer.edit(edit_ranges, text.as_str(), Some(ctx)) { log::error!("error inserting text: {}", error); }; - let char_count = text.chars().count() as isize; + let text_len = text.len() as isize; let mut delta = 0_isize; new_selections = old_selections .into_iter() .map(|(id, range)| { let start = range.start as isize; let end = range.end as isize; - let anchor = buffer.anchor_before((start + delta + char_count) as usize); + let anchor = buffer.anchor_before((start + delta + text_len) as usize); let deleted_count = end - start; - delta += char_count - deleted_count; + delta += text_len - deleted_count; Selection { id, start: anchor.clone(), @@ -2358,11 +2358,7 @@ impl workspace::ItemView for BufferView { #[cfg(test)] mod tests { use super::*; - use crate::{ - editor::Point, - settings, - test::{multibyte_sample_text, sample_text}, - }; + use crate::{editor::Point, settings, test::sample_text}; use unindent::Unindent; #[gpui::test] @@ -2667,6 +2663,11 @@ mod tests { }); view.update(app, |view, ctx| { + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)] + ); + view.move_down(&(), ctx); assert_eq!( view.selection_ranges(ctx.as_ref()), @@ -2728,6 +2729,11 @@ mod tests { assert_eq!('ⓐ'.len_utf8(), 3); assert_eq!('α'.len_utf8(), 2); + fn empty_range(row: usize, column: usize) -> Range { + let point = DisplayPoint::new(row as u32, column as u32); + point..point + } + view.update(app, |view, ctx| { view.fold_ranges( vec![ @@ -2742,43 +2748,80 @@ mod tests { view.move_right(&(), ctx); assert_eq!( view.selection_ranges(ctx.as_ref()), - &[DisplayPoint::new(0, 3)..DisplayPoint::new(0, 3)] + &[empty_range(0, "ⓐ".len())] ); - - view.move_down(&(), ctx); + view.move_right(&(), ctx); assert_eq!( view.selection_ranges(ctx.as_ref()), - &[DisplayPoint::new(1, 1)..DisplayPoint::new(1, 1)] + &[empty_range(0, "ⓐⓑ".len())] ); - view.move_right(&(), ctx); assert_eq!( view.selection_ranges(ctx.as_ref()), - &[DisplayPoint::new(1, 2)..DisplayPoint::new(1, 2)] + &[empty_range(0, "ⓐⓑ…".len())] ); view.move_down(&(), ctx); assert_eq!( view.selection_ranges(ctx.as_ref()), - &[DisplayPoint::new(2, 4)..DisplayPoint::new(2, 4)] + &[empty_range(1, "ab…".len())] ); - view.move_left(&(), ctx); assert_eq!( view.selection_ranges(ctx.as_ref()), - &[DisplayPoint::new(2, 2)..DisplayPoint::new(2, 2)] + &[empty_range(1, "ab".len())] + ); + view.move_left(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[empty_range(1, "a".len())] ); - view.move_up(&(), ctx); + view.move_down(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[empty_range(2, "α".len())] + ); + view.move_right(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[empty_range(2, "αβ".len())] + ); + view.move_right(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[empty_range(2, "αβ…".len())] + ); + view.move_right(&(), ctx); assert_eq!( view.selection_ranges(ctx.as_ref()), - &[DisplayPoint::new(1, 1)..DisplayPoint::new(1, 1)] + &[empty_range(2, "αβ…ε".len())] ); view.move_up(&(), ctx); assert_eq!( view.selection_ranges(ctx.as_ref()), - &[DisplayPoint::new(0, 3)..DisplayPoint::new(0, 3)] + &[empty_range(1, "ab…e".len())] + ); + view.move_up(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[empty_range(0, "ⓐⓑ…ⓔ".len())] + ); + view.move_left(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[empty_range(0, "ⓐⓑ…".len())] + ); + view.move_left(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[empty_range(0, "ⓐⓑ".len())] + ); + view.move_left(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[empty_range(0, "ⓐ".len())] ); }); } diff --git a/zed/src/editor/display_map/fold_map.rs b/zed/src/editor/display_map/fold_map.rs index 0176da26dec6c640d2a6cf6d8d232ac179b3211c..3560fd187918517e2ab8fedb9174b2243cf6a6a9 100644 --- a/zed/src/editor/display_map/fold_map.rs +++ b/zed/src/editor/display_map/fold_map.rs @@ -1,6 +1,6 @@ use super::{ buffer::{AnchorRangeExt, TextSummary}, - Anchor, Buffer, DisplayPoint, Edit, Point, ToOffset, + Anchor, Bias, Buffer, DisplayPoint, Edit, Point, ToOffset, }; use crate::{ editor::buffer, @@ -233,26 +233,6 @@ impl FoldMap { )) } - #[cfg(test)] - pub fn prev_char_boundary(&self, offset: DisplayOffset, ctx: &AppContext) -> DisplayOffset { - let transforms = self.sync(ctx); - let mut cursor = transforms.cursor::(); - cursor.seek(&offset, SeekBias::Right, &()); - if let Some(transform) = cursor.item() { - if transform.display_text.is_some() { - DisplayOffset(cursor.start().display.bytes) - } else { - let overshoot = offset.0 - cursor.start().display.bytes; - let buffer_offset = cursor.start().buffer.bytes + overshoot; - let buffer_boundary_offset = - self.buffer.read(ctx).prev_char_boundary(buffer_offset); - DisplayOffset(offset.0 - (buffer_offset - buffer_boundary_offset)) - } - } else { - offset - } - } - fn sync(&self, ctx: &AppContext) -> MutexGuard> { let buffer = self.buffer.read(ctx); let mut edits = buffer.edits_since(self.last_sync.lock().clone()).peekable(); @@ -471,6 +451,64 @@ impl FoldMapSnapshot { let overshoot = point.0 - cursor.start().display.lines; (cursor.start().buffer.lines + overshoot).to_offset(self.buffer.read(ctx)) } + + #[cfg(test)] + pub fn clip_offset( + &self, + offset: DisplayOffset, + bias: Bias, + ctx: &AppContext, + ) -> DisplayOffset { + let mut cursor = self.transforms.cursor::(); + cursor.seek(&offset, SeekBias::Right, &()); + if let Some(transform) = cursor.item() { + let transform_start = cursor.start().display.bytes; + if transform.display_text.is_some() { + if offset.0 == transform_start || matches!(bias, Bias::Left) { + DisplayOffset(transform_start) + } else { + DisplayOffset(cursor.end().display.bytes) + } + } else { + let overshoot = offset.0 - transform_start; + let buffer_offset = cursor.start().buffer.bytes + overshoot; + let clipped_buffer_offset = self.buffer.read(ctx).clip_offset(buffer_offset, bias); + DisplayOffset( + (offset.0 as isize + (clipped_buffer_offset as isize - buffer_offset as isize)) + as usize, + ) + } + } else { + DisplayOffset(self.transforms.summary().display.bytes) + } + } + + pub fn clip_point(&self, point: DisplayPoint, bias: Bias, ctx: &AppContext) -> DisplayPoint { + let mut cursor = self.transforms.cursor::(); + cursor.seek(&point, SeekBias::Right, &()); + if let Some(transform) = cursor.item() { + let transform_start = cursor.start().display.lines; + if transform.display_text.is_some() { + if point.0 == transform_start || matches!(bias, Bias::Left) { + DisplayPoint(transform_start) + } else { + DisplayPoint(cursor.end().display.lines) + } + } else { + let overshoot = point.0 - transform_start; + let buffer_position = cursor.start().buffer.lines + overshoot; + let clipped_buffer_position = + self.buffer.read(ctx).clip_point(buffer_position, bias); + DisplayPoint::new( + point.row(), + ((point.column() as i32) + clipped_buffer_position.column as i32 + - buffer_position.column as i32) as u32, + ) + } + } else { + DisplayPoint(self.transforms.summary().display.lines) + } + } } #[derive(Clone, Debug, Default, Eq, PartialEq)] @@ -864,6 +902,7 @@ mod tests { use crate::util::RandomCharIter; use rand::prelude::*; use std::env; + use Bias::{Left, Right}; let iterations = env::var("ITERATIONS") .map(|i| i.parse().expect("invalid `ITERATIONS` variable")) @@ -896,8 +935,8 @@ mod tests { let buffer = buffer.read(app); let mut to_fold = Vec::new(); for _ in 0..rng.gen_range(1..=5) { - let end = buffer.next_char_boundary(rng.gen_range(0..=buffer.len())); - let start = buffer.prev_char_boundary(rng.gen_range(0..=end)); + let end = buffer.clip_offset(rng.gen_range(0..=buffer.len()), Right); + let start = buffer.clip_offset(rng.gen_range(0..=end), Left); to_fold.push(start..end); } log::info!("folding {:?}", to_fold); @@ -907,8 +946,8 @@ mod tests { let buffer = buffer.read(app); let mut to_unfold = Vec::new(); for _ in 0..rng.gen_range(1..=3) { - let end = buffer.next_char_boundary(rng.gen_range(0..=buffer.len())); - let start = buffer.prev_char_boundary(rng.gen_range(0..=end)); + let end = buffer.clip_offset(rng.gen_range(0..=buffer.len()), Right); + let start = buffer.clip_offset(rng.gen_range(0..=end), Left); to_unfold.push(start..end); } log::info!("unfolding {:?}", to_unfold); @@ -989,8 +1028,9 @@ mod tests { } for _ in 0..5 { - let offset = map.prev_char_boundary( + let offset = map.snapshot(app.as_ref()).clip_offset( DisplayOffset(rng.gen_range(0..=map.len(app.as_ref()))), + Bias::Right, app.as_ref(), ); assert_eq!( @@ -1020,8 +1060,8 @@ mod tests { } for _ in 0..5 { - let end = buffer.next_char_boundary(rng.gen_range(0..=buffer.len())); - let start = buffer.prev_char_boundary(rng.gen_range(0..=end)); + let end = buffer.clip_offset(rng.gen_range(0..=buffer.len()), Right); + let start = buffer.clip_offset(rng.gen_range(0..=end), Left); let expected_folds = map .folds .items() diff --git a/zed/src/editor/display_map/mod.rs b/zed/src/editor/display_map/mod.rs index 919f2967494a2ac4af2cc756f329ab6ad3e4c2e7..792df1c8d92d17127c42fa5bc20dd98e4db8670a 100644 --- a/zed/src/editor/display_map/mod.rs +++ b/zed/src/editor/display_map/mod.rs @@ -1,17 +1,11 @@ mod fold_map; -use super::{buffer, Anchor, Buffer, Edit, Point, ToOffset, ToPoint}; +use super::{buffer, Anchor, Bias, Buffer, Edit, Point, ToOffset, ToPoint}; pub use fold_map::BufferRows; use fold_map::{FoldMap, FoldMapSnapshot}; use gpui::{AppContext, ModelHandle}; use std::ops::Range; -#[derive(Copy, Clone)] -pub enum Bias { - Left, - Right, -} - pub struct DisplayMap { buffer: ModelHandle, fold_map: FoldMap, @@ -110,6 +104,7 @@ impl DisplayMap { .column() } + // TODO - make this delegate to the DisplayMapSnapshot pub fn max_point(&self, ctx: &AppContext) -> DisplayPoint { self.fold_map.max_point(ctx).expand_tabs(self, ctx) } @@ -168,11 +163,11 @@ impl DisplayMapSnapshot { let mut count = 0; let mut column = 0; for c in self.chars_at(DisplayPoint::new(display_row, 0), ctx) { - count += 1; - column += c.len_utf8() as u32; if column >= target { break; } + count += 1; + column += c.len_utf8() as u32; } count } @@ -181,15 +176,23 @@ impl DisplayMapSnapshot { let mut count = 0; let mut column = 0; for c in self.chars_at(DisplayPoint::new(display_row, 0), ctx) { - count += 1; - column += c.len_utf8() as u32; if count >= char_count { break; } + count += 1; + column += c.len_utf8() as u32; } column } + pub fn clip_point(&self, point: DisplayPoint, bias: Bias, ctx: &AppContext) -> DisplayPoint { + self.expand_tabs( + self.folds_snapshot + .clip_point(self.collapse_tabs(point, bias, ctx).0, bias, ctx), + ctx, + ) + } + fn expand_tabs(&self, mut point: DisplayPoint, ctx: &AppContext) -> DisplayPoint { let chars = self .folds_snapshot @@ -364,7 +367,7 @@ pub fn collapse_tabs( let mut expanded_chars = 0; let mut collapsed_bytes = 0; while let Some(c) = chars.next() { - if expanded_bytes == column { + if expanded_bytes >= column { break; } @@ -383,19 +386,19 @@ pub fn collapse_tabs( expanded_chars += 1; expanded_bytes += c.len_utf8(); } - collapsed_bytes += c.len_utf8(); - if expanded_bytes > column { - panic!("column {} is inside of character {:?}", column, c); + if expanded_bytes > column && matches!(bias, Bias::Left) { + expanded_chars -= 1; + break; } + + collapsed_bytes += c.len_utf8(); } (collapsed_bytes, expanded_chars, 0) } #[cfg(test)] mod tests { - use gpui::MutableAppContext; - use super::*; use crate::test::*; @@ -446,25 +449,25 @@ mod tests { } #[gpui::test] - fn test_tabs_with_multibyte_chars(app: &mut MutableAppContext) { - let text = "✅\t\tx\nα\t\n🏀α\t\ty"; + fn test_tabs_with_multibyte_chars(app: &mut gpui::MutableAppContext) { + let text = "✅\t\tα\nβ\t\n🏀β\t\tγ"; let buffer = app.add_model(|ctx| Buffer::new(0, text, ctx)); let ctx = app.as_ref(); let map = DisplayMap::new(buffer.clone(), 4, ctx); - assert_eq!(map.text(ctx), "✅ x\nα \n🏀α y"); + assert_eq!(map.text(ctx), "✅ α\nβ \n🏀β γ"); let point = Point::new(0, "✅\t\t".len() as u32); let display_point = DisplayPoint::new(0, "✅ ".len() as u32); assert_eq!(point.to_display_point(&map, ctx), display_point); assert_eq!(display_point.to_buffer_point(&map, Bias::Left, ctx), point,); - let point = Point::new(1, "α\t".len() as u32); - let display_point = DisplayPoint::new(1, "α ".len() as u32); + let point = Point::new(1, "β\t".len() as u32); + let display_point = DisplayPoint::new(1, "β ".len() as u32); assert_eq!(point.to_display_point(&map, ctx), display_point); assert_eq!(display_point.to_buffer_point(&map, Bias::Left, ctx), point,); - let point = Point::new(2, "🏀α\t\t".len() as u32); - let display_point = DisplayPoint::new(2, "🏀α ".len() as u32); + let point = Point::new(2, "🏀β\t\t".len() as u32); + let display_point = DisplayPoint::new(2, "🏀β ".len() as u32); assert_eq!(point.to_display_point(&map, ctx), display_point); assert_eq!(display_point.to_buffer_point(&map, Bias::Left, ctx), point,); @@ -481,7 +484,7 @@ mod tests { map.snapshot(ctx) .chunks_at(DisplayPoint::new(0, "✅ ".len() as u32), ctx) .collect::(), - " x\nα \n🏀α y" + " α\nβ \n🏀β γ" ); assert_eq!( DisplayPoint::new(0, "✅ ".len() as u32).to_buffer_point(&map, Bias::Right, ctx), @@ -495,7 +498,25 @@ mod tests { map.snapshot(ctx) .chunks_at(DisplayPoint::new(0, "✅ ".len() as u32), ctx) .collect::(), - " x\nα \n🏀α y" + " α\nβ \n🏀β γ" + ); + + // Clipping display points inside of multi-byte characters + assert_eq!( + map.snapshot(ctx).clip_point( + DisplayPoint::new(0, "✅".len() as u32 - 1), + Bias::Left, + ctx + ), + DisplayPoint::new(0, 0) + ); + assert_eq!( + map.snapshot(ctx).clip_point( + DisplayPoint::new(0, "✅".len() as u32 - 1), + Bias::Right, + ctx + ), + DisplayPoint::new(0, "✅".len() as u32) ); } diff --git a/zed/src/editor/mod.rs b/zed/src/editor/mod.rs index 0229e6ae7e08169a4c5e4f7e8a265f712b55137b..d3c892367471b6a6a2aa46eabef36020853d5f2f 100644 --- a/zed/src/editor/mod.rs +++ b/zed/src/editor/mod.rs @@ -11,6 +11,12 @@ pub use display_map::DisplayPoint; use display_map::*; use std::{cmp, ops::Range}; +#[derive(Copy, Clone)] +pub enum Bias { + Left, + Right, +} + trait RangeExt { fn sorted(&self) -> Range; } diff --git a/zed/src/editor/movement.rs b/zed/src/editor/movement.rs index 3e89537f41c3fe4c9b69e64748a48018a231c623..b40573e5e8ea0d4f91112e77967154beedf0e4ee 100644 --- a/zed/src/editor/movement.rs +++ b/zed/src/editor/movement.rs @@ -1,7 +1,6 @@ -use super::{DisplayMap, DisplayPoint, SelectionGoal}; +use super::{Bias, DisplayMap, DisplayPoint, SelectionGoal}; use anyhow::Result; use gpui::AppContext; -use std::cmp; pub fn left(map: &DisplayMap, mut point: DisplayPoint, app: &AppContext) -> Result { if point.column() > 0 { @@ -10,23 +9,18 @@ pub fn left(map: &DisplayMap, mut point: DisplayPoint, app: &AppContext) -> Resu *point.row_mut() -= 1; *point.column_mut() = map.line_len(point.row(), app); } - Ok(point) + Ok(map.snapshot(app).clip_point(point, Bias::Left, app)) } pub fn right(map: &DisplayMap, mut point: DisplayPoint, app: &AppContext) -> Result { let max_column = map.line_len(point.row(), app); if point.column() < max_column { - *point.column_mut() += map - .snapshot(app) - .chars_at(point, app) - .next() - .unwrap() - .len_utf8() as u32; + *point.column_mut() += 1; } else if point.row() < map.max_point(app).row() { *point.row_mut() += 1; *point.column_mut() = 0; } - Ok(point) + Ok(map.snapshot(app).clip_point(point, Bias::Right, app)) } pub fn up( @@ -35,18 +29,16 @@ pub fn up( goal: SelectionGoal, app: &AppContext, ) -> Result<(DisplayPoint, SelectionGoal)> { + let map = map.snapshot(app); let goal_column = if let SelectionGoal::Column(column) = goal { column } else { - point.column() + map.column_to_chars(point.row(), point.column(), app) }; - let map = map.snapshot(app); - let char_column = map.column_to_chars(point.row(), goal_column, app); - if point.row() > 0 { *point.row_mut() -= 1; - *point.column_mut() = map.column_from_chars(point.row(), char_column, app); + *point.column_mut() = map.column_from_chars(point.row(), goal_column, app); } else { point = DisplayPoint::new(0, 0); } @@ -60,19 +52,17 @@ pub fn down( goal: SelectionGoal, app: &AppContext, ) -> Result<(DisplayPoint, SelectionGoal)> { + let max_point = map.max_point(app); + let map = map.snapshot(app); let goal_column = if let SelectionGoal::Column(column) = goal { column } else { - point.column() + map.column_to_chars(point.row(), point.column(), app) }; - let max_point = map.max_point(app); - let map = map.snapshot(app); - let char_column = map.column_to_chars(point.row(), goal_column, app); - if point.row() < max_point.row() { *point.row_mut() += 1; - *point.column_mut() = map.column_from_chars(point.row(), char_column, app); + *point.column_mut() = map.column_from_chars(point.row(), goal_column, app); } else { point = max_point; } From a2c36fcdb7a6d01e47e6b513731630cb6bc926ec Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 19 May 2021 16:28:37 -0700 Subject: [PATCH 10/18] Fix bugs and add tests for move_{up,down} --- zed/src/editor/buffer_view.rs | 57 ++++++++++++++++++++++++++++--- zed/src/editor/display_map/mod.rs | 40 +++++++++++++++++++++- 2 files changed, 91 insertions(+), 6 deletions(-) diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 94a98384396de900a99f688899013c88902943f6..81f17ae9e3c49884f15970297b2a15a0c09abd24 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -2729,11 +2729,6 @@ mod tests { assert_eq!('ⓐ'.len_utf8(), 3); assert_eq!('α'.len_utf8(), 2); - fn empty_range(row: usize, column: usize) -> Range { - let point = DisplayPoint::new(row as u32, column as u32); - point..point - } - view.update(app, |view, ctx| { view.fold_ranges( vec![ @@ -2826,6 +2821,53 @@ mod tests { }); } + #[gpui::test] + fn test_move_cursor_different_line_lengths(app: &mut gpui::MutableAppContext) { + let buffer = app.add_model(|ctx| Buffer::new(0, "ⓐⓑⓒⓓⓔ\nabcd\nαβγ\nabcd\nⓐⓑⓒⓓⓔ\n", ctx)); + let settings = settings::channel(&app.font_cache()).unwrap().1; + let (_, view) = app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), settings, ctx)); + view.update(app, |view, ctx| { + view.select_display_ranges(&[empty_range(0, "ⓐⓑⓒⓓⓔ".len())], ctx) + .unwrap(); + + view.move_down(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[empty_range(1, "abcd".len())] + ); + + view.move_down(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[empty_range(2, "αβγ".len())] + ); + + view.move_down(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[empty_range(3, "abcd".len())] + ); + + view.move_down(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[empty_range(4, "ⓐⓑⓒⓓⓔ".len())] + ); + + view.move_up(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[empty_range(3, "abcd".len())] + ); + + view.move_up(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[empty_range(2, "αβγ".len())] + ); + }); + } + #[gpui::test] fn test_beginning_end_of_line(app: &mut gpui::MutableAppContext) { let buffer = app.add_model(|ctx| Buffer::new(0, "abc\n def", ctx)); @@ -3803,4 +3845,9 @@ mod tests { .collect::>() } } + + fn empty_range(row: usize, column: usize) -> Range { + let point = DisplayPoint::new(row as u32, column as u32); + point..point + } } diff --git a/zed/src/editor/display_map/mod.rs b/zed/src/editor/display_map/mod.rs index 792df1c8d92d17127c42fa5bc20dd98e4db8670a..df81e2505ad5fe5cae1cdf7abdb57dfc10367ab4 100644 --- a/zed/src/editor/display_map/mod.rs +++ b/zed/src/editor/display_map/mod.rs @@ -176,7 +176,7 @@ impl DisplayMapSnapshot { let mut count = 0; let mut column = 0; for c in self.chars_at(DisplayPoint::new(display_row, 0), ctx) { - if count >= char_count { + if c == '\n' || count >= char_count { break; } count += 1; @@ -441,6 +441,44 @@ mod tests { ); } + #[gpui::test] + fn test_clip_point(app: &mut gpui::MutableAppContext) { + use Bias::{Left, Right}; + + let text = "\n'a', 'α',\t'✋',\t'❎', '🍐'\n"; + let display_text = "\n'a', 'α', '✋', '❎', '🍐'\n"; + let buffer = app.add_model(|ctx| Buffer::new(0, text, ctx)); + let ctx = app.as_ref(); + let map = DisplayMap::new(buffer.clone(), 4, ctx); + assert_eq!(map.text(ctx), display_text); + + let map = map.snapshot(ctx); + for (input_column, bias, output_column) in vec![ + ("'a', '".len(), Left, "'a', '".len()), + ("'a', '".len() + 1, Left, "'a', '".len()), + ("'a', '".len() + 1, Right, "'a', 'α".len()), + ("'a', 'α', ".len(), Left, "'a', 'α',".len()), + ("'a', 'α', ".len(), Right, "'a', 'α', ".len()), + ("'a', 'α', '".len() + 1, Left, "'a', 'α', '".len()), + ("'a', 'α', '".len() + 1, Right, "'a', 'α', '✋".len()), + ("'a', 'α', '✋',".len(), Right, "'a', 'α', '✋',".len()), + ("'a', 'α', '✋', ".len(), Left, "'a', 'α', '✋',".len()), + ( + "'a', 'α', '✋', ".len(), + Right, + "'a', 'α', '✋', ".len(), + ), + ] { + assert_eq!( + map.clip_point(DisplayPoint::new(1, input_column as u32), bias, ctx), + DisplayPoint::new(1, output_column as u32), + "clip_point(({}, {}))", + 1, + input_column, + ); + } + } + #[test] fn test_expand_tabs() { assert_eq!(expand_tabs("\t".chars(), 0, 4), 0); From 92b938f4a80b25ad3c2e375fa4dd3e546e8dc254 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 19 May 2021 16:30:10 -0700 Subject: [PATCH 11/18] Adjust `layout_str` to use byte coordinates * Interpret ranges as byte ranges for incoming runs * Return glyphs whose indices are byte offsets --- gpui/src/elements/label.rs | 2 +- gpui/src/platform/mac/fonts.rs | 110 ++++++++++++++++++--------------- zed/src/editor/buffer_view.rs | 2 +- 3 files changed, 62 insertions(+), 52 deletions(-) diff --git a/gpui/src/elements/label.rs b/gpui/src/elements/label.rs index 2ef66bcc9d30a8d2b9f1f87455fe7788cd37d7c7..dd81601b623f98f32555d46888a2201a0ef5e997 100644 --- a/gpui/src/elements/label.rs +++ b/gpui/src/elements/label.rs @@ -73,7 +73,7 @@ impl Element for Label { .font_cache .select_font(self.family_id, &self.font_properties) .unwrap(); - let text_len = self.text.chars().count(); + let text_len = self.text.len(); let mut styles; let mut colors; if let Some(highlights) = self.highlights.as_ref() { diff --git a/gpui/src/platform/mac/fonts.rs b/gpui/src/platform/mac/fonts.rs index d806fc750c2d175af7e602c83ce52d41d264574d..50e9f3030ff3ce6d5d087551061ecb66b15610a3 100644 --- a/gpui/src/platform/mac/fonts.rs +++ b/gpui/src/platform/mac/fonts.rs @@ -189,49 +189,60 @@ impl FontSystemState { ) -> Line { let font_id_attr_name = CFString::from_static_string("zed_font_id"); + let len = text.len(); + let mut utf8_and_utf16_ixs = text.char_indices().chain(Some((len, '\0'))).map({ + let mut utf16_ix = 0; + move |(utf8_ix, c)| { + let result = (utf8_ix, utf16_ix); + utf16_ix += c.len_utf16(); + result + } + }); + + // Construct the attributed string, converting UTF8 ranges to UTF16 ranges. let mut string = CFMutableAttributedString::new(); - string.replace_str(&CFString::new(text), CFRange::init(0, 0)); - - let mut utf16_lens = text.chars().map(|c| c.len_utf16()); - let mut prev_char_ix = 0; - let mut prev_utf16_ix = 0; - - for (range, font_id) in runs { - let utf16_start = prev_utf16_ix - + utf16_lens - .by_ref() - .take(range.start - prev_char_ix) - .sum::(); - let utf16_end = utf16_start - + utf16_lens - .by_ref() - .take(range.end - range.start) - .sum::(); - prev_char_ix = range.end; - prev_utf16_ix = utf16_end; - - let cf_range = CFRange::init(utf16_start as isize, (utf16_end - utf16_start) as isize); - let font = &self.fonts[font_id.0]; - unsafe { - string.set_attribute( - cf_range, - kCTFontAttributeName, - &font.native_font().clone_with_font_size(font_size as f64), - ); - string.set_attribute( - cf_range, - font_id_attr_name.as_concrete_TypeRef(), - &CFNumber::from(font_id.0 as i64), - ); + { + let mut utf8_and_utf16_ixs = utf8_and_utf16_ixs.clone(); + string.replace_str(&CFString::new(text), CFRange::init(0, 0)); + + let mut utf8_ix = 0; + let mut utf16_ix = 0; + for (range, font_id) in runs { + while utf8_ix < range.start { + let (next_utf8_ix, next_utf16_ix) = utf8_and_utf16_ixs.next().unwrap(); + utf8_ix = next_utf8_ix; + utf16_ix = next_utf16_ix; + } + let utf16_start = utf16_ix; + while utf8_ix < range.end { + let (next_utf8_ix, next_utf16_ix) = utf8_and_utf16_ixs.next().unwrap(); + utf8_ix = next_utf8_ix; + utf16_ix = next_utf16_ix; + } + + let cf_range = + CFRange::init(utf16_start as isize, (utf16_ix - utf16_start) as isize); + let font = &self.fonts[font_id.0]; + unsafe { + string.set_attribute( + cf_range, + kCTFontAttributeName, + &font.native_font().clone_with_font_size(font_size as f64), + ); + string.set_attribute( + cf_range, + font_id_attr_name.as_concrete_TypeRef(), + &CFNumber::from(font_id.0 as i64), + ); + } } } + // Retrieve the glyphs from the shaped line, converting UTF16 offsets to UTF8 offsets. let line = CTLine::new_with_attributed_string(string.as_concrete_TypeRef()); - let mut utf16_chars = text.encode_utf16(); - let mut char_ix = 0; - let mut prev_utf16_ix = 0; - + let mut utf8_ix = 0; + let mut utf16_ix = 0; let mut runs = Vec::new(); for run in line.glyph_runs().into_iter() { let font_id = FontId( @@ -245,21 +256,22 @@ impl FontSystemState { ); let mut glyphs = Vec::new(); - for ((glyph_id, position), utf16_ix) in run + for ((glyph_id, position), glyph_utf16_ix) in run .glyphs() .iter() .zip(run.positions().iter()) .zip(run.string_indices().iter()) { - let utf16_ix = usize::try_from(*utf16_ix).unwrap(); - char_ix += - char::decode_utf16(utf16_chars.by_ref().take(utf16_ix - prev_utf16_ix)).count(); - prev_utf16_ix = utf16_ix; - + let glyph_utf16_ix = usize::try_from(*glyph_utf16_ix).unwrap(); + while utf16_ix < glyph_utf16_ix { + let (next_utf8_ix, next_utf16_ix) = utf8_and_utf16_ixs.next().unwrap(); + utf8_ix = next_utf8_ix; + utf16_ix = next_utf16_ix; + } glyphs.push(Glyph { id: *glyph_id as GlyphId, position: vec2f(position.x as f32, position.y as f32), - index: char_ix, + index: utf8_ix, }); } @@ -273,7 +285,7 @@ impl FontSystemState { descent: typographic_bounds.descent as f32, runs, font_size, - len: char_ix + 1, + len, } } } @@ -312,7 +324,7 @@ mod tests { } #[test] - fn test_char_indices() -> anyhow::Result<()> { + fn test_glyph_offsets() -> anyhow::Result<()> { let fonts = FontSystem::new(); let zapfino = fonts.load_family("Zapfino")?; let zapfino_regular = fonts.select_font(&zapfino, &Properties::new())?; @@ -326,7 +338,7 @@ mod tests { &[ (0..9, zapfino_regular), (9..22, menlo_regular), - (22..text.encode_utf16().count(), zapfino_regular), + (22..text.len(), zapfino_regular), ], ); assert_eq!( @@ -335,9 +347,7 @@ mod tests { .flat_map(|r| r.glyphs.iter()) .map(|g| g.index) .collect::>(), - vec![ - 0, 2, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 30, 31 - ] + vec![0, 2, 4, 5, 7, 8, 9, 10, 14, 15, 16, 17, 21, 22, 23, 24, 26, 27, 28, 29, 36, 37], ); Ok(()) } diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 81f17ae9e3c49884f15970297b2a15a0c09abd24..61a5afed54ea9bf1b0e5815dcb94a0547cdd1b27 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -2151,7 +2151,7 @@ impl BufferView { layouts.push(layout_cache.layout_str( &line, font_size, - &[(0..line.chars().count(), font_id)], + &[(0..line.len(), font_id)], )); line.clear(); row += 1; From 4bc1b0fa6fd1890dba4b14571403d567db5f4d99 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 19 May 2021 23:07:16 -0700 Subject: [PATCH 12/18] Convert fuzzy match positions to byte offsets --- zed/src/worktree/fuzzy.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/zed/src/worktree/fuzzy.rs b/zed/src/worktree/fuzzy.rs index 4fe81372cf4f053521138fb4243a331470b75095..3897b2a588ba4ef3a512c90c8c5f830d36b90946 100644 --- a/zed/src/worktree/fuzzy.rs +++ b/zed/src/worktree/fuzzy.rs @@ -323,9 +323,16 @@ fn score_match( let path_len = path.len() + prefix.len(); let mut cur_start = 0; + let mut byte_ix = 0; + let mut char_ix = 0; for i in 0..query.len() { - match_positions[i] = best_position_matrix[i * path_len + cur_start]; - cur_start = match_positions[i] + 1; + let match_char_ix = best_position_matrix[i * path_len + cur_start]; + while char_ix < match_char_ix { + byte_ix += path[char_ix].len_utf8(); + char_ix += 1; + } + cur_start = match_char_ix + 1; + match_positions[i] = byte_ix; } score @@ -550,6 +557,26 @@ mod tests { ); } + #[test] + fn test_match_multibyte_path_entries() { + let paths = vec!["aαbβ/cγdδ", "αβγδ/bcde", "c1️⃣2️⃣3️⃣/d4️⃣5️⃣6️⃣/e7️⃣8️⃣9️⃣/f", "/d/🆒/h"]; + assert_eq!("1️⃣".len(), 7); + assert_eq!( + match_query("bcd", false, &paths), + vec![ + ("αβγδ/bcde", vec![9, 10, 11]), + ("aαbβ/cγdδ", vec![3, 7, 10]), + ] + ); + assert_eq!( + match_query("cde", false, &paths), + vec![ + ("αβγδ/bcde", vec![10, 11, 12]), + ("c1️⃣2️⃣3️⃣/d4️⃣5️⃣6️⃣/e7️⃣8️⃣9️⃣/f", vec![0, 23, 46]), + ] + ); + } + fn match_query<'a>( query: &str, smart_case: bool, From 558ce4113013b3b7e9de473c9adb8cc71fe68546 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 19 May 2021 23:07:45 -0700 Subject: [PATCH 13/18] WIP - Adjust Label element to expect highlights as byte offsets --- gpui/src/elements/label.rs | 75 ++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 19 deletions(-) diff --git a/gpui/src/elements/label.rs b/gpui/src/elements/label.rs index dd81601b623f98f32555d46888a2201a0ef5e997..cd281d5fe6ccdf8f623a86d9d0a5e0a1d4699815 100644 --- a/gpui/src/elements/label.rs +++ b/gpui/src/elements/label.rs @@ -3,15 +3,15 @@ use serde_json::json; use crate::{ color::ColorU, font_cache::FamilyId, - fonts::Properties, + fonts::{FontId, Properties}, geometry::{ rect::RectF, vector::{vec2f, Vector2F}, }, json::{ToJson, Value}, text_layout::Line, - AfterLayoutContext, DebugContext, Element, Event, EventContext, LayoutContext, PaintContext, - SizeConstraint, + AfterLayoutContext, DebugContext, Element, Event, EventContext, FontCache, LayoutContext, + PaintContext, SizeConstraint, }; use std::{ops::Range, sync::Arc}; @@ -58,29 +58,19 @@ impl Label { }); self } -} - -impl Element for Label { - type LayoutState = LayoutState; - type PaintState = (); - fn layout( - &mut self, - constraint: SizeConstraint, - ctx: &mut LayoutContext, - ) -> (Vector2F, Self::LayoutState) { - let font_id = ctx - .font_cache - .select_font(self.family_id, &self.font_properties) - .unwrap(); + fn layout_text( + &self, + font_cache: &FontCache, + font_id: FontId, + ) -> (Vec<(Range, FontId)>, Vec<(Range, ColorU)>) { let text_len = self.text.len(); let mut styles; let mut colors; if let Some(highlights) = self.highlights.as_ref() { styles = Vec::new(); colors = Vec::new(); - let highlight_font_id = ctx - .font_cache + let highlight_font_id = font_cache .select_font(self.family_id, &highlights.font_properties) .unwrap_or(font_id); let mut pending_highlight: Option> = None; @@ -117,6 +107,24 @@ impl Element for Label { colors = vec![(0..text_len, ColorU::black())]; } + (styles, colors) + } +} + +impl Element for Label { + type LayoutState = LayoutState; + type PaintState = (); + + fn layout( + &mut self, + constraint: SizeConstraint, + ctx: &mut LayoutContext, + ) -> (Vector2F, Self::LayoutState) { + let font_id = ctx + .font_cache + .select_font(self.family_id, &self.font_properties) + .unwrap(); + let (styles, colors) = self.layout_text(&ctx.font_cache, font_id); let line = ctx.text_layout_cache .layout_str(self.text.as_str(), self.font_size, styles.as_slice()); @@ -185,3 +193,32 @@ impl ToJson for Highlights { }) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[crate::test(self)] + fn test_layout_label_with_highlights(app: &mut crate::MutableAppContext) { + let family_id = app.font_cache().load_family(&["Menlo"]).unwrap(); + let font_id = app + .font_cache() + .select_font(family_id, &Properties::new()) + .unwrap(); + + let label = Label::new(".αβγδε.ⓐⓑⓒⓓⓔ.abcde.".to_string(), family_id, 12.0).with_highlights( + ColorU::black(), + Properties::new(), + vec![ + ".α".len(), + ".αβ".len(), + ".αβγδ".len(), + ".αβγδε.ⓐ".len(), + ".αβγδε.ⓐⓑ".len(), + ], + ); + + let (styles, colors) = label.layout_text(app.font_cache().as_ref(), font_id); + assert_eq!(styles, &[]); + } +} From 608336c279605083bfa7cda1aa460a74fdd8f661 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 20 May 2021 10:50:49 +0200 Subject: [PATCH 14/18] Complete unit test for Label highlights --- gpui/src/elements/label.rs | 46 +++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/gpui/src/elements/label.rs b/gpui/src/elements/label.rs index cd281d5fe6ccdf8f623a86d9d0a5e0a1d4699815..7ec49df414f30e820335051af99ce0440f87acc3 100644 --- a/gpui/src/elements/label.rs +++ b/gpui/src/elements/label.rs @@ -196,19 +196,27 @@ impl ToJson for Highlights { #[cfg(test)] mod tests { + use font_kit::properties::Weight; + use super::*; #[crate::test(self)] fn test_layout_label_with_highlights(app: &mut crate::MutableAppContext) { - let family_id = app.font_cache().load_family(&["Menlo"]).unwrap(); - let font_id = app + let menlo = app.font_cache().load_family(&["Menlo"]).unwrap(); + let menlo_regular = app + .font_cache() + .select_font(menlo, &Properties::new()) + .unwrap(); + let menlo_bold = app .font_cache() - .select_font(family_id, &Properties::new()) + .select_font(menlo, Properties::new().weight(Weight::BOLD)) .unwrap(); + let black = ColorU::black(); + let red = ColorU::new(255, 0, 0, 255); - let label = Label::new(".αβγδε.ⓐⓑⓒⓓⓔ.abcde.".to_string(), family_id, 12.0).with_highlights( - ColorU::black(), - Properties::new(), + let label = Label::new(".αβγδε.ⓐⓑⓒⓓⓔ.abcde.".to_string(), menlo, 12.0).with_highlights( + red, + *Properties::new().weight(Weight::BOLD), vec![ ".α".len(), ".αβ".len(), @@ -218,7 +226,29 @@ mod tests { ], ); - let (styles, colors) = label.layout_text(app.font_cache().as_ref(), font_id); - assert_eq!(styles, &[]); + let (styles, colors) = label.layout_text(app.font_cache().as_ref(), menlo_regular); + assert_eq!(styles.len(), colors.len()); + + let mut spans = Vec::new(); + for ((style_range, font_id), (color_range, color)) in styles.into_iter().zip(colors) { + assert_eq!(style_range, color_range); + spans.push((style_range, font_id, color)); + } + assert_eq!( + spans, + &[ + (0..3, menlo_regular, black), + (3..4, menlo_bold, red), + (4..5, menlo_regular, black), + (5..6, menlo_bold, red), + (6..9, menlo_regular, black), + (9..10, menlo_bold, red), + (10..15, menlo_regular, black), + (15..16, menlo_bold, red), + (16..18, menlo_regular, black), + (18..19, menlo_bold, red), + (19..34, menlo_regular, black) + ] + ); } } From 96538354b30ea3f809d813fbcf83c8c664a972dc Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 20 May 2021 12:22:33 +0200 Subject: [PATCH 15/18] Express rightmost_point in terms of chars as opposed to bytes --- zed/src/editor/buffer/mod.rs | 91 +++++--------------------- zed/src/editor/buffer/rope.rs | 45 ++++++++----- zed/src/editor/display_map/fold_map.rs | 19 ++++-- 3 files changed, 57 insertions(+), 98 deletions(-) diff --git a/zed/src/editor/buffer/mod.rs b/zed/src/editor/buffer/mod.rs index a86182fbae89b1dfb90ad202a14ae81d0ce54bd5..ab91ef83894a691c0ad4d588d0c42312d96204da 100644 --- a/zed/src/editor/buffer/mod.rs +++ b/zed/src/editor/buffer/mod.rs @@ -616,14 +616,6 @@ impl Buffer { (row_end_offset - row_start_offset) as u32 } - pub fn rightmost_point(&self) -> Point { - self.visible_text.summary().rightmost_point - } - - pub fn rightmost_point_in_range(&self, range: Range) -> Point { - self.text_summary_for_range(range).rightmost_point - } - pub fn max_point(&self) -> Point { self.visible_text.max_point() } @@ -2360,7 +2352,6 @@ mod tests { use std::{ cell::RefCell, cmp::Ordering, - collections::BTreeMap, fs, rc::Rc, sync::atomic::{self, AtomicUsize}, @@ -2468,33 +2459,11 @@ mod tests { reference_string = buffer.text(); } - { - let line_lengths = line_lengths_in_range(&buffer, 0..buffer.len()); - - for (len, rows) in &line_lengths { - for row in rows { - assert_eq!(buffer.line_len(*row), *len); - } - } - - let (longest_column, longest_rows) = - line_lengths.iter().next_back().unwrap(); - let rightmost_point = buffer.rightmost_point(); - assert_eq!(rightmost_point.column, *longest_column); - assert!(longest_rows.contains(&rightmost_point.row)); - } - - for _ in 0..5 { - let range = buffer.random_byte_range(0, rng); - let line_lengths = line_lengths_in_range(&buffer, range.clone()); - let (longest_column, longest_rows) = - line_lengths.iter().next_back().unwrap(); - let range_sum = buffer.text_summary_for_range(range.clone()); - assert_eq!(range_sum.rightmost_point.column, *longest_column); - assert!(longest_rows.contains(&range_sum.rightmost_point.row)); - let range_text = &buffer.text()[range]; - assert_eq!(range_sum.bytes, range_text.len()); - } + let range = buffer.random_byte_range(0, rng); + assert_eq!( + buffer.text_summary_for_range(range.clone()), + TextSummary::from(&reference_string[range]) + ); if rng.gen_bool(0.3) { buffer_versions.push(buffer.clone()); @@ -2545,25 +2514,6 @@ mod tests { }); } - #[gpui::test] - fn test_rightmost_point(ctx: &mut gpui::MutableAppContext) { - ctx.add_model(|ctx| { - let mut buffer = Buffer::new(0, "", ctx); - assert_eq!(buffer.rightmost_point().row, 0); - buffer.edit(vec![0..0], "abcd\nefg\nhij", None).unwrap(); - assert_eq!(buffer.rightmost_point().row, 0); - buffer.edit(vec![12..12], "kl\nmno", None).unwrap(); - assert_eq!(buffer.rightmost_point().row, 2); - buffer.edit(vec![18..18], "\npqrs", None).unwrap(); - assert_eq!(buffer.rightmost_point().row, 2); - buffer.edit(vec![10..12], "", None).unwrap(); - assert_eq!(buffer.rightmost_point().row, 0); - buffer.edit(vec![24..24], "tuv", None).unwrap(); - assert_eq!(buffer.rightmost_point().row, 4); - buffer - }); - } - #[gpui::test] fn test_text_summary_for_range(ctx: &mut gpui::MutableAppContext) { ctx.add_model(|ctx| { @@ -2573,7 +2523,8 @@ mod tests { TextSummary { bytes: 2, lines: Point::new(1, 0), - first_line_len: 1, + first_line_chars: 1, + last_line_chars: 0, rightmost_point: Point::new(0, 1), } ); @@ -2582,7 +2533,8 @@ mod tests { TextSummary { bytes: 11, lines: Point::new(3, 0), - first_line_len: 1, + first_line_chars: 1, + last_line_chars: 0, rightmost_point: Point::new(2, 4), } ); @@ -2591,7 +2543,8 @@ mod tests { TextSummary { bytes: 20, lines: Point::new(4, 1), - first_line_len: 2, + first_line_chars: 2, + last_line_chars: 1, rightmost_point: Point::new(3, 6), } ); @@ -2600,7 +2553,8 @@ mod tests { TextSummary { bytes: 22, lines: Point::new(4, 3), - first_line_len: 2, + first_line_chars: 2, + last_line_chars: 3, rightmost_point: Point::new(3, 6), } ); @@ -2609,7 +2563,8 @@ mod tests { TextSummary { bytes: 15, lines: Point::new(2, 3), - first_line_len: 4, + first_line_chars: 4, + last_line_chars: 3, rightmost_point: Point::new(1, 6), } ); @@ -3388,20 +3343,4 @@ mod tests { } } } - - fn line_lengths_in_range(buffer: &Buffer, range: Range) -> BTreeMap> { - let mut lengths = BTreeMap::new(); - for (row, line) in buffer.text()[range.start..range.end].lines().enumerate() { - lengths - .entry(line.len() as u32) - .or_insert(HashSet::default()) - .insert(row as u32); - } - if lengths.is_empty() { - let mut rows = HashSet::default(); - rows.insert(0); - lengths.insert(0, rows); - } - lengths - } } diff --git a/zed/src/editor/buffer/rope.rs b/zed/src/editor/buffer/rope.rs index ddf1a92cbfb4a2b8647cf5b011e142acba60d8a4..d03afaf2a72fe57838a26a038eb1b1cf3da8870a 100644 --- a/zed/src/editor/buffer/rope.rs +++ b/zed/src/editor/buffer/rope.rs @@ -390,32 +390,41 @@ impl sum_tree::Item for Chunk { pub struct TextSummary { pub bytes: usize, pub lines: Point, - pub first_line_len: u32, + pub first_line_chars: u32, + pub last_line_chars: u32, pub rightmost_point: Point, } impl<'a> From<&'a str> for TextSummary { fn from(text: &'a str) -> Self { let mut lines = Point::new(0, 0); - let mut first_line_len = 0; + let mut first_line_chars = 0; + let mut last_line_chars = 0; let mut rightmost_point = Point::new(0, 0); - for (i, line) in text.split('\n').enumerate() { - if i > 0 { + for c in text.chars() { + if c == '\n' { lines.row += 1; + lines.column = 0; + last_line_chars = 0; + } else { + lines.column += c.len_utf8() as u32; + last_line_chars += 1; } - lines.column = line.len() as u32; - if i == 0 { - first_line_len = lines.column; + + if lines.row == 0 { + first_line_chars = last_line_chars; } - if lines.column > rightmost_point.column { - rightmost_point = lines; + + if last_line_chars > rightmost_point.column { + rightmost_point = Point::new(lines.row, last_line_chars); } } TextSummary { bytes: text.len(), lines, - first_line_len, + first_line_chars, + last_line_chars, rightmost_point, } } @@ -431,16 +440,22 @@ impl sum_tree::Summary for TextSummary { impl<'a> std::ops::AddAssign<&'a Self> for TextSummary { fn add_assign(&mut self, other: &'a Self) { - let joined_line_len = self.lines.column + other.first_line_len; - if joined_line_len > self.rightmost_point.column { - self.rightmost_point = Point::new(self.lines.row, joined_line_len); + let joined_chars = self.last_line_chars + other.first_line_chars; + if joined_chars > self.rightmost_point.column { + self.rightmost_point = Point::new(self.lines.row, joined_chars); } if other.rightmost_point.column > self.rightmost_point.column { - self.rightmost_point = self.lines + &other.rightmost_point; + self.rightmost_point = self.lines + other.rightmost_point; } if self.lines.row == 0 { - self.first_line_len += other.first_line_len; + self.first_line_chars += other.first_line_chars; + } + + if other.lines.row == 0 { + self.last_line_chars += other.first_line_chars; + } else { + self.last_line_chars = other.last_line_chars; } self.bytes += other.bytes; diff --git a/zed/src/editor/display_map/fold_map.rs b/zed/src/editor/display_map/fold_map.rs index 3560fd187918517e2ab8fedb9174b2243cf6a6a9..05b92da04f09a5bd53c08652ee63beaaecc55eb1 100644 --- a/zed/src/editor/display_map/fold_map.rs +++ b/zed/src/editor/display_map/fold_map.rs @@ -330,15 +330,17 @@ impl FoldMap { if fold.end > fold.start { let display_text = "…"; - let extent = Point::new(0, display_text.len() as u32); + let chars = display_text.chars().count() as u32; + let lines = Point::new(0, display_text.len() as u32); new_transforms.push( Transform { summary: TransformSummary { display: TextSummary { bytes: display_text.len(), - lines: extent, - first_line_len: extent.column, - rightmost_point: extent, + lines, + first_line_chars: chars, + last_line_chars: chars, + rightmost_point: Point::new(0, chars), }, buffer: buffer.text_summary_for_range(fold.start..fold.end), }, @@ -990,6 +992,7 @@ mod tests { let rightmost_point = map.rightmost_point(app.as_ref()); let mut display_point = DisplayPoint::new(0, 0); let mut display_offset = DisplayOffset(0); + let mut char_column = 0; for c in expected_text.chars() { let buffer_point = map.to_buffer_point(display_point, app.as_ref()); let buffer_offset = buffer_point.to_offset(buffer); @@ -1015,14 +1018,16 @@ mod tests { if c == '\n' { *display_point.row_mut() += 1; *display_point.column_mut() = 0; + char_column = 0; } else { *display_point.column_mut() += c.len_utf8() as u32; + char_column += 1; } display_offset.0 += c.len_utf8(); - if display_point.column() > rightmost_point.column() { + if char_column > rightmost_point.column() { panic!( - "invalid rightmost point {:?}, found point {:?}", - rightmost_point, display_point + "invalid rightmost point {:?}, found point {:?} (char column: {})", + rightmost_point, display_point, char_column ); } } From ed57ffe5fa654fe93b3b6cd1d9f59a7c1a4bb0af Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 20 May 2021 12:35:24 +0200 Subject: [PATCH 16/18] Replace `rightmost_point` with `righmost_row` --- zed/src/editor/buffer_element.rs | 2 +- zed/src/editor/buffer_view.rs | 4 ++-- zed/src/editor/display_map/fold_map.rs | 21 +++++++++++++++------ zed/src/editor/display_map/mod.rs | 4 ++-- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/zed/src/editor/buffer_element.rs b/zed/src/editor/buffer_element.rs index d3f002239f1986ab5505f3d4d933d74f143d8a04..b38d6eed2949728e995c74b9814abf41d20e2eaf 100644 --- a/zed/src/editor/buffer_element.rs +++ b/zed/src/editor/buffer_element.rs @@ -520,7 +520,7 @@ impl LayoutState { layout_cache: &TextLayoutCache, app: &AppContext, ) -> f32 { - let row = view.rightmost_point(app).row(); + let row = view.rightmost_row(app); let longest_line_width = view .layout_line(row, font_cache, layout_cache, app) .unwrap() diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 61a5afed54ea9bf1b0e5815dcb94a0547cdd1b27..52fc45f87b8f919d8ae57c530eb9ce008a43f319 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -2015,8 +2015,8 @@ impl BufferView { self.display_map.line_len(display_row, ctx) } - pub fn rightmost_point(&self, ctx: &AppContext) -> DisplayPoint { - self.display_map.rightmost_point(ctx) + pub fn rightmost_row(&self, ctx: &AppContext) -> u32 { + self.display_map.rightmost_row(ctx) } pub fn max_point(&self, ctx: &AppContext) -> DisplayPoint { diff --git a/zed/src/editor/display_map/fold_map.rs b/zed/src/editor/display_map/fold_map.rs index 05b92da04f09a5bd53c08652ee63beaaecc55eb1..121909cba71cebeb47aa5cf022bed30434434fd8 100644 --- a/zed/src/editor/display_map/fold_map.rs +++ b/zed/src/editor/display_map/fold_map.rs @@ -67,8 +67,8 @@ impl FoldMap { DisplayPoint(self.sync(ctx).summary().display.lines) } - pub fn rightmost_point(&self, ctx: &AppContext) -> DisplayPoint { - DisplayPoint(self.sync(ctx).summary().display.rightmost_point) + pub fn rightmost_row(&self, ctx: &AppContext) -> u32 { + self.sync(ctx).summary().display.rightmost_point.row } pub fn folds_in_range<'a, T>( @@ -989,7 +989,13 @@ mod tests { assert_eq!(line_len, line.len() as u32); } - let rightmost_point = map.rightmost_point(app.as_ref()); + let rightmost_row = map.rightmost_row(app.as_ref()); + let rightmost_char_column = expected_text + .split('\n') + .nth(rightmost_row as usize) + .unwrap() + .chars() + .count(); let mut display_point = DisplayPoint::new(0, 0); let mut display_offset = DisplayOffset(0); let mut char_column = 0; @@ -1024,10 +1030,13 @@ mod tests { char_column += 1; } display_offset.0 += c.len_utf8(); - if char_column > rightmost_point.column() { + if char_column > rightmost_char_column { panic!( - "invalid rightmost point {:?}, found point {:?} (char column: {})", - rightmost_point, display_point, char_column + "invalid rightmost row {:?} (chars {}), found row {:?} (chars: {})", + rightmost_row, + rightmost_char_column, + display_point.row(), + char_column ); } } diff --git a/zed/src/editor/display_map/mod.rs b/zed/src/editor/display_map/mod.rs index df81e2505ad5fe5cae1cdf7abdb57dfc10367ab4..81c27f349fb2f88fc663ec156b135774eec2360f 100644 --- a/zed/src/editor/display_map/mod.rs +++ b/zed/src/editor/display_map/mod.rs @@ -109,8 +109,8 @@ impl DisplayMap { self.fold_map.max_point(ctx).expand_tabs(self, ctx) } - pub fn rightmost_point(&self, ctx: &AppContext) -> DisplayPoint { - self.fold_map.rightmost_point(ctx) + pub fn rightmost_row(&self, ctx: &AppContext) -> u32 { + self.fold_map.rightmost_row(ctx) } pub fn anchor_before(&self, point: DisplayPoint, bias: Bias, app: &AppContext) -> Anchor { From 995b80ff2646c03bbe871a1d7b2528cc6b6ed81b Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 20 May 2021 12:43:32 +0200 Subject: [PATCH 17/18] Index into `prefix` or `path` depending on where the match was found This fixes a couple of tests that were panicking due to an out-of-bound access. --- zed/src/worktree/fuzzy.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/zed/src/worktree/fuzzy.rs b/zed/src/worktree/fuzzy.rs index 3897b2a588ba4ef3a512c90c8c5f830d36b90946..d9d79cc20cd0d5ffde33e4c04dc946d2ad0a0c16 100644 --- a/zed/src/worktree/fuzzy.rs +++ b/zed/src/worktree/fuzzy.rs @@ -321,14 +321,18 @@ fn score_match( return 0.0; } - let path_len = path.len() + prefix.len(); + let path_len = prefix.len() + path.len(); let mut cur_start = 0; let mut byte_ix = 0; let mut char_ix = 0; for i in 0..query.len() { let match_char_ix = best_position_matrix[i * path_len + cur_start]; while char_ix < match_char_ix { - byte_ix += path[char_ix].len_utf8(); + let ch = prefix + .get(char_ix) + .or_else(|| path.get(char_ix - prefix.len())) + .unwrap(); + byte_ix += ch.len_utf8(); char_ix += 1; } cur_start = match_char_ix + 1; From 6a0757ebf9535c45df121201e71656ed9fa39974 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 20 May 2021 12:52:58 +0200 Subject: [PATCH 18/18] Don't store rightmost row/char-column as a Point --- zed/src/editor/buffer/mod.rs | 15 ++++++++++----- zed/src/editor/buffer/rope.rs | 24 +++++++++++++++--------- zed/src/editor/display_map/fold_map.rs | 5 +++-- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/zed/src/editor/buffer/mod.rs b/zed/src/editor/buffer/mod.rs index ab91ef83894a691c0ad4d588d0c42312d96204da..ddf40a983293df16dee765f718753cf73563ac0f 100644 --- a/zed/src/editor/buffer/mod.rs +++ b/zed/src/editor/buffer/mod.rs @@ -2525,7 +2525,8 @@ mod tests { lines: Point::new(1, 0), first_line_chars: 1, last_line_chars: 0, - rightmost_point: Point::new(0, 1), + rightmost_row: 0, + rightmost_row_chars: 1, } ); assert_eq!( @@ -2535,7 +2536,8 @@ mod tests { lines: Point::new(3, 0), first_line_chars: 1, last_line_chars: 0, - rightmost_point: Point::new(2, 4), + rightmost_row: 2, + rightmost_row_chars: 4, } ); assert_eq!( @@ -2545,7 +2547,8 @@ mod tests { lines: Point::new(4, 1), first_line_chars: 2, last_line_chars: 1, - rightmost_point: Point::new(3, 6), + rightmost_row: 3, + rightmost_row_chars: 6, } ); assert_eq!( @@ -2555,7 +2558,8 @@ mod tests { lines: Point::new(4, 3), first_line_chars: 2, last_line_chars: 3, - rightmost_point: Point::new(3, 6), + rightmost_row: 3, + rightmost_row_chars: 6, } ); assert_eq!( @@ -2565,7 +2569,8 @@ mod tests { lines: Point::new(2, 3), first_line_chars: 4, last_line_chars: 3, - rightmost_point: Point::new(1, 6), + rightmost_row: 1, + rightmost_row_chars: 6, } ); buffer diff --git a/zed/src/editor/buffer/rope.rs b/zed/src/editor/buffer/rope.rs index d03afaf2a72fe57838a26a038eb1b1cf3da8870a..83c5ee4d5dec7707393f41d572562384529d160d 100644 --- a/zed/src/editor/buffer/rope.rs +++ b/zed/src/editor/buffer/rope.rs @@ -392,7 +392,8 @@ pub struct TextSummary { pub lines: Point, pub first_line_chars: u32, pub last_line_chars: u32, - pub rightmost_point: Point, + pub rightmost_row: u32, + pub rightmost_row_chars: u32, } impl<'a> From<&'a str> for TextSummary { @@ -400,7 +401,8 @@ impl<'a> From<&'a str> for TextSummary { let mut lines = Point::new(0, 0); let mut first_line_chars = 0; let mut last_line_chars = 0; - let mut rightmost_point = Point::new(0, 0); + let mut rightmost_row = 0; + let mut rightmost_row_chars = 0; for c in text.chars() { if c == '\n' { lines.row += 1; @@ -415,8 +417,9 @@ impl<'a> From<&'a str> for TextSummary { first_line_chars = last_line_chars; } - if last_line_chars > rightmost_point.column { - rightmost_point = Point::new(lines.row, last_line_chars); + if last_line_chars > rightmost_row_chars { + rightmost_row = lines.row; + rightmost_row_chars = last_line_chars; } } @@ -425,7 +428,8 @@ impl<'a> From<&'a str> for TextSummary { lines, first_line_chars, last_line_chars, - rightmost_point, + rightmost_row, + rightmost_row_chars, } } } @@ -441,11 +445,13 @@ impl sum_tree::Summary for TextSummary { impl<'a> std::ops::AddAssign<&'a Self> for TextSummary { fn add_assign(&mut self, other: &'a Self) { let joined_chars = self.last_line_chars + other.first_line_chars; - if joined_chars > self.rightmost_point.column { - self.rightmost_point = Point::new(self.lines.row, joined_chars); + if joined_chars > self.rightmost_row_chars { + self.rightmost_row = self.lines.row; + self.rightmost_row_chars = joined_chars; } - if other.rightmost_point.column > self.rightmost_point.column { - self.rightmost_point = self.lines + other.rightmost_point; + if other.rightmost_row_chars > self.rightmost_row_chars { + self.rightmost_row = self.lines.row + other.rightmost_row; + self.rightmost_row_chars = other.rightmost_row_chars; } if self.lines.row == 0 { diff --git a/zed/src/editor/display_map/fold_map.rs b/zed/src/editor/display_map/fold_map.rs index 121909cba71cebeb47aa5cf022bed30434434fd8..5494d61c9a51fd064c2374f80916fec005b6e1ea 100644 --- a/zed/src/editor/display_map/fold_map.rs +++ b/zed/src/editor/display_map/fold_map.rs @@ -68,7 +68,7 @@ impl FoldMap { } pub fn rightmost_row(&self, ctx: &AppContext) -> u32 { - self.sync(ctx).summary().display.rightmost_point.row + self.sync(ctx).summary().display.rightmost_row } pub fn folds_in_range<'a, T>( @@ -340,7 +340,8 @@ impl FoldMap { lines, first_line_chars: chars, last_line_chars: chars, - rightmost_point: Point::new(0, chars), + rightmost_row: 0, + rightmost_row_chars: chars, }, buffer: buffer.text_summary_for_range(fold.start..fold.end), },