Detailed changes
@@ -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();
- let text_len = self.text.chars().count();
+ fn layout_text(
+ &self,
+ font_cache: &FontCache,
+ font_id: FontId,
+ ) -> (Vec<(Range<usize>, FontId)>, Vec<(Range<usize>, 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<Range<usize>> = 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,62 @@ 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 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(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(), menlo, 12.0).with_highlights(
+ red,
+ *Properties::new().weight(Weight::BOLD),
+ vec![
+ ".α".len(),
+ ".αβ".len(),
+ ".αβγδ".len(),
+ ".αβγδε.ⓐ".len(),
+ ".αβγδε.ⓐⓑ".len(),
+ ],
+ );
+
+ 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)
+ ]
+ );
+ }
+}
@@ -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::<usize>();
- let utf16_end = utf16_start
- + utf16_lens
- .by_ref()
- .take(range.end - range.start)
- .sum::<usize>();
- 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<_>>(),
- 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(())
}
@@ -70,24 +70,24 @@ impl Anchor {
})
}
- pub fn bias_left(&self, buffer: &Buffer) -> Result<Anchor> {
+ 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<Anchor> {
+ 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),
}
}
@@ -5,22 +5,21 @@ 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};
use crate::{
+ editor::Bias,
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,56 +606,43 @@ impl Buffer {
self.fragments.extent::<usize>()
}
- pub fn line_len(&self, row: u32) -> Result<u32> {
- 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)
- }
-
- pub fn rightmost_point(&self) -> Point {
- self.visible_text.summary().rightmost_point
- }
-
- pub fn rightmost_point_in_range(&self, range: Range<usize>) -> Point {
- self.text_summary_for_range(range).rightmost_point
+ (row_end_offset - row_start_offset) as u32
}
pub fn max_point(&self) -> Point {
self.visible_text.max_point()
}
- pub fn line(&self, row: u32) -> Result<String> {
- 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<T>,
- ) -> Result<impl 'a + Iterator<Item = char>> {
- let start = range.start.to_offset(self)?;
- let end = range.end.to_offset(self)?;
- Ok(self.chars_at(start)?.take(end - start))
+ pub fn text_for_range<'a, T: ToOffset>(&'a self, range: Range<T>) -> ChunksIter<'a> {
+ 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()
+ pub fn chars(&self) -> impl Iterator<Item = char> + '_ {
+ self.chars_at(0)
}
- pub fn chars_at<T: ToOffset>(&self, position: T) -> Result<rope::Chars> {
- let offset = position.to_offset(self)?;
- Ok(self.visible_text.chars_at(offset))
+ pub fn chars_at<T: ToOffset>(&self, position: T) -> impl Iterator<Item = char> + '_ {
+ let offset = position.to_offset(self);
+ self.visible_text.chars_at(offset)
}
pub fn selections_changed_since(&self, since: SelectionsVersion) -> bool {
@@ -763,8 +749,8 @@ impl Buffer {
let old_ranges = old_ranges
.into_iter()
- .map(|range| Ok(range.start.to_offset(self)?..range.end.to_offset(self)?))
- .collect::<Result<Vec<Range<usize>>>>()?;
+ .map(|range| range.start.to_offset(self)..range.end.to_offset(self))
+ .collect::<Vec<Range<usize>>>();
let has_new_text = new_text.is_some();
let ops = self.splice_fragments(
@@ -802,50 +788,6 @@ impl Buffer {
}
}
- pub fn simulate_typing<T: Rng>(&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<T>(
- &mut self,
- rng: &mut T,
- old_range_count: usize,
- ctx: Option<&mut ModelContext<Self>>,
- ) -> (Vec<Range<usize>>, String, Vec<Operation>)
- where
- T: Rng,
- {
- let mut old_ranges: Vec<Range<usize>> = 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<Arc<[Selection]>>,
@@ -1777,8 +1719,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 +1742,31 @@ impl Buffer {
)
}
- pub fn anchor_before<T: ToOffset>(&self, position: T) -> Result<Anchor> {
+ pub fn anchor_before<T: ToOffset>(&self, position: T) -> Anchor {
self.anchor_at(position, AnchorBias::Left)
}
- pub fn anchor_after<T: ToOffset>(&self, position: T) -> Result<Anchor> {
+ pub fn anchor_after<T: ToOffset>(&self, position: T) -> Anchor {
self.anchor_at(position, AnchorBias::Right)
}
- pub fn anchor_at<T: ToOffset>(&self, position: T, bias: AnchorBias) -> Result<Anchor> {
- let offset = position.to_offset(self)?;
+ pub fn anchor_at<T: ToOffset>(&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 +1783,7 @@ impl Buffer {
offset: offset_in_insertion,
bias,
};
- Ok(anchor)
+ anchor
}
fn fragment_id_for_anchor(&self, anchor: &Anchor) -> Result<&FragmentId> {
@@ -1876,10 +1815,10 @@ impl Buffer {
}
}
- fn summary_for_anchor(&self, anchor: &Anchor) -> Result<TextSummary> {
+ 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 +1832,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::<usize, ()>();
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::<FragmentIdRef, usize>();
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 +1857,14 @@ impl Buffer {
Err(anyhow!("offset out of bounds"))
}
}
+
+ pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
+ self.visible_text.clip_point(point, bias)
+ }
+
+ pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize {
+ self.visible_text.clip_offset(offset, bias)
+ }
}
impl Clone for Buffer {
@@ -2352,45 +2295,45 @@ impl operation_queue::Operation for Operation {
}
pub trait ToOffset {
- fn to_offset(&self, buffer: &Buffer) -> Result<usize>;
+ fn to_offset(&self, buffer: &Buffer) -> usize;
}
impl ToOffset for Point {
- fn to_offset(&self, buffer: &Buffer) -> Result<usize> {
+ fn to_offset(&self, buffer: &Buffer) -> usize {
buffer.visible_text.to_offset(*self)
}
}
impl ToOffset for usize {
- fn to_offset(&self, _: &Buffer) -> Result<usize> {
- Ok(*self)
+ fn to_offset(&self, _: &Buffer) -> usize {
+ *self
}
}
impl ToOffset for Anchor {
- fn to_offset(&self, buffer: &Buffer) -> Result<usize> {
- 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<usize> {
- 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<Point>;
+ fn to_point(&self, buffer: &Buffer) -> Point;
}
impl ToPoint for Anchor {
- fn to_point(&self, buffer: &Buffer) -> Result<Point> {
- 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<Point> {
+ fn to_point(&self, buffer: &Buffer) -> Point {
buffer.visible_text.to_point(*self)
}
}
@@ -2400,14 +2343,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,
- collections::BTreeMap,
+ cmp::Ordering,
fs,
rc::Rc,
sync::atomic::{self, AtomicUsize},
@@ -2506,12 +2450,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);
@@ -2520,41 +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).unwrap(), *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 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 (longest_column, longest_rows) =
- line_lengths.iter().next_back().unwrap();
- let range_sum = buffer.text_summary_for_range(start..end);
- 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::<String>();
- assert_eq!(range_sum.chars, range_text.chars().count());
- 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());
@@ -2571,7 +2480,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,32 +2504,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());
- buffer
- });
- }
-
- #[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);
+ 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,51 +2521,56 @@ 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,
- rightmost_point: Point::new(0, 1),
+ first_line_chars: 1,
+ last_line_chars: 0,
+ rightmost_row: 0,
+ rightmost_row_chars: 1,
}
);
assert_eq!(
buffer.text_summary_for_range(1..12),
TextSummary {
- chars: 11,
bytes: 11,
lines: Point::new(3, 0),
- first_line_len: 1,
- rightmost_point: Point::new(2, 4),
+ first_line_chars: 1,
+ last_line_chars: 0,
+ rightmost_row: 2,
+ rightmost_row_chars: 4,
}
);
assert_eq!(
buffer.text_summary_for_range(0..20),
TextSummary {
- chars: 20,
bytes: 20,
lines: Point::new(4, 1),
- first_line_len: 2,
- rightmost_point: Point::new(3, 6),
+ first_line_chars: 2,
+ last_line_chars: 1,
+ rightmost_row: 3,
+ rightmost_row_chars: 6,
}
);
assert_eq!(
buffer.text_summary_for_range(0..22),
TextSummary {
- chars: 22,
bytes: 22,
lines: Point::new(4, 3),
- first_line_len: 2,
- rightmost_point: Point::new(3, 6),
+ first_line_chars: 2,
+ last_line_chars: 3,
+ rightmost_row: 3,
+ rightmost_row_chars: 6,
}
);
assert_eq!(
buffer.text_summary_for_range(7..22),
TextSummary {
- chars: 15,
bytes: 15,
lines: Point::new(2, 3),
- first_line_len: 4,
- rightmost_point: Point::new(1, 6),
+ first_line_chars: 4,
+ last_line_chars: 3,
+ rightmost_row: 1,
+ rightmost_row_chars: 6,
}
);
buffer
@@ -2692,19 +2586,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::<String>(), "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::<String>(), "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::<String>(), "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::<String>(), "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::<String>(), "PQrs");
// Regression test:
@@ -2712,7 +2606,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::<String>(), " \"xray_wasm\",\n]\n");
buffer
@@ -2744,103 +2638,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 +2776,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 +2932,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 +2972,7 @@ mod tests {
.iter()
.map(|selection| {
assert_eq!(selection.start, selection.end);
- selection.start.to_point(&buffer).unwrap()
+ selection.start.to_point(&buffer)
})
.collect::<Vec<_>>();
assert_eq!(
@@ -3324,6 +3192,39 @@ mod tests {
}
impl Buffer {
+ fn random_byte_range(&mut self, start_offset: usize, rng: &mut impl Rng) -> Range<usize> {
+ 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
+ }
+
+ pub fn randomly_edit<T>(
+ &mut self,
+ rng: &mut T,
+ old_range_count: usize,
+ ctx: Option<&mut ModelContext<Self>>,
+ ) -> (Vec<Range<usize>>, String, Vec<Operation>)
+ where
+ T: Rng,
+ {
+ let mut old_ranges: Vec<Range<usize>> = 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<T>(
&mut self,
rng: &mut T,
@@ -3349,9 +3250,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 +3290,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 +3313,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 {
@@ -3449,28 +3348,4 @@ mod tests {
}
}
}
-
- fn line_lengths_in_range(buffer: &Buffer, range: Range<usize>) -> BTreeMap<u32, HashSet<u32>> {
- let mut lengths = BTreeMap::new();
- for (row, line) in buffer
- .text()
- .chars()
- .skip(range.start)
- .take(range.len())
- .collect::<String>()
- .lines()
- .enumerate()
- {
- lengths
- .entry(line.chars().count() 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
- }
}
@@ -1,10 +1,11 @@
use super::Point;
-use crate::sum_tree::{self, SeekBias, SumTree};
-use crate::util::byte_range_for_char_range;
-use anyhow::{anyhow, Result};
+use crate::{
+ editor::Bias,
+ sum_tree::{self, SeekBias, SumTree},
+};
use arrayvec::ArrayString;
use smallvec::SmallVec;
-use std::{cmp, iter::Skip, str};
+use std::{cmp, ops::Range, str};
#[cfg(test)]
const CHUNK_BASE: usize = 6;
@@ -109,43 +110,72 @@ impl Rope {
Cursor::new(self, offset)
}
- pub fn chars(&self) -> Chars {
+ pub fn chars(&self) -> impl Iterator<Item = char> + '_ {
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<Item = char> + '_ {
+ self.chunks_in_range(start..self.len()).flat_map(str::chars)
}
- pub fn chunks<'a>(&'a self) -> impl Iterator<Item = &'a str> {
- self.chunks.cursor::<(), ()>().map(|c| c.0.as_str())
+ pub fn chunks<'a>(&'a self) -> ChunksIter<'a> {
+ self.chunks_in_range(0..self.len())
}
- pub fn to_point(&self, offset: usize) -> Result<Point> {
- if offset <= self.summary().chars {
- let mut cursor = self.chunks.cursor::<usize, TextSummary>();
- 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 chunks_in_range<'a>(&'a self, range: Range<usize>) -> ChunksIter<'a> {
+ ChunksIter::new(self, range)
+ }
+
+ pub fn to_point(&self, offset: usize) -> Point {
+ assert!(offset <= self.summary().bytes);
+ let mut cursor = self.chunks.cursor::<usize, TextSummary>();
+ 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::<Point, TextSummary>();
+ 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 clip_offset(&self, mut offset: usize, bias: Bias) -> usize {
+ let mut cursor = self.chunks.cursor::<usize, usize>();
+ cursor.seek(&offset, SeekBias::Left, &());
+ if let Some(chunk) = cursor.item() {
+ let mut ix = offset - cursor.start();
+ while !chunk.0.is_char_boundary(ix) {
+ match bias {
+ Bias::Left => {
+ ix -= 1;
+ offset -= 1;
+ }
+ Bias::Right => {
+ ix += 1;
+ offset += 1;
+ }
+ }
+ }
+ offset
} else {
- Err(anyhow!("offset out of bounds"))
+ self.summary().bytes
}
}
- pub fn to_offset(&self, point: Point) -> Result<usize> {
- if point <= self.summary().lines {
- let mut cursor = self.chunks.cursor::<Point, TextSummary>();
- 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 clip_point(&self, point: Point, bias: Bias) -> Point {
+ let mut cursor = self.chunks.cursor::<Point, Point>();
+ cursor.seek(&point, SeekBias::Right, &());
+ if let Some(chunk) = cursor.item() {
+ let overshoot = point - cursor.start();
+ *cursor.start() + chunk.clip_point(overshoot, bias)
} else {
- Err(anyhow!("offset out of bounds"))
+ self.summary().lines
}
}
}
@@ -189,8 +219,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() {
@@ -200,8 +229,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]);
}
}
@@ -216,8 +244,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() {
@@ -225,8 +252,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]);
}
}
@@ -242,6 +268,54 @@ impl<'a> Cursor<'a> {
}
}
+pub struct ChunksIter<'a> {
+ chunks: sum_tree::Cursor<'a, Chunk, usize, usize>,
+ range: Range<usize>,
+}
+
+impl<'a> ChunksIter<'a> {
+ pub fn new(rope: &'a Rope, range: Range<usize>) -> Self {
+ let mut chunks = rope.chunks.cursor();
+ chunks.seek(&range.start, SeekBias::Right, &());
+ Self { chunks, range }
+ }
+
+ pub fn offset(&self) -> usize {
+ self.range.start.max(*self.chunks.start())
+ }
+
+ 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() {
+ 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();
+ return Some(&chunk.0[start..chunk.0.len().min(end)]);
+ }
+ }
+ None
+ }
+}
+
+impl<'a> Iterator for ChunksIter<'a> {
+ type Item = &'a str;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ let result = self.peek();
+ if result.is_some() {
+ self.chunks.next();
+ }
+ result
+ }
+}
+
#[derive(Clone, Debug, Default)]
struct Chunk(ArrayString<[u8; 2 * CHUNK_BASE]>);
@@ -258,18 +332,21 @@ 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<usize> {
+ fn to_offset(&self, target: Point) -> usize {
let mut offset = 0;
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;
}
@@ -277,16 +354,27 @@ 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();
}
+ offset
+ }
- if point == target {
- Ok(offset)
- } else {
- Err(anyhow!("point out of bounds"))
+ 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!()
}
}
@@ -300,43 +388,48 @@ 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,
- pub rightmost_point: Point,
+ pub first_line_chars: u32,
+ pub last_line_chars: u32,
+ pub rightmost_row: u32,
+ pub rightmost_row_chars: u32,
}
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);
+ let mut first_line_chars = 0;
+ let mut last_line_chars = 0;
+ let mut rightmost_row = 0;
+ let mut rightmost_row_chars = 0;
for c in text.chars() {
- chars += 1;
- bytes += c.len_utf8();
if c == '\n' {
lines.row += 1;
lines.column = 0;
+ last_line_chars = 0;
} else {
- lines.column += 1;
- if lines.row == 0 {
- first_line_len = lines.column;
- }
- if lines.column > rightmost_point.column {
- rightmost_point = lines;
- }
+ lines.column += c.len_utf8() as u32;
+ last_line_chars += 1;
+ }
+
+ if lines.row == 0 {
+ first_line_chars = last_line_chars;
+ }
+
+ if last_line_chars > rightmost_row_chars {
+ rightmost_row = lines.row;
+ rightmost_row_chars = last_line_chars;
}
}
TextSummary {
- chars,
- bytes,
+ bytes: text.len(),
lines,
- first_line_len,
- rightmost_point,
+ first_line_chars,
+ last_line_chars,
+ rightmost_row,
+ rightmost_row_chars,
}
}
}
@@ -351,19 +444,26 @@ 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_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 {
- 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.chars += other.chars;
self.bytes += other.bytes;
self.lines += &other.lines;
}
@@ -383,7 +483,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;
}
}
@@ -393,43 +493,6 @@ impl<'a> sum_tree::Dimension<'a, TextSummary> for Point {
}
}
-pub struct Chars<'a> {
- cursor: sum_tree::Cursor<'a, Chunk, usize, usize>,
- chars: Skip<str::Chars<'a>>,
-}
-
-impl<'a> Chars<'a> {
- pub fn new(rope: &'a Rope, start: usize) -> Self {
- let mut cursor = rope.chunks.cursor::<usize, usize>();
- cursor.seek(&start, SeekBias::Left, &());
- let chars = if let Some(chunk) = cursor.item() {
- let ix = start - cursor.start();
- 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<Self::Item> {
- 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) {
@@ -451,11 +514,11 @@ fn find_split_ix(text: &str) -> usize {
#[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() {
@@ -486,8 +549,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 = 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();
@@ -499,58 +562,54 @@ 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 ix = rng.gen_range(0..=expected.chars().count());
+ 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.chars_at(ix).collect::<String>(),
- expected.chars().skip(ix).collect::<String>()
+ actual.chunks_in_range(start_ix..end_ix).collect::<String>(),
+ &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).unwrap(), point);
- assert_eq!(actual.to_offset(point).unwrap(), 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 byte_range = byte_range_for_char_range(&expected, start_ix..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[byte_range])
+ TextSummary::from(&expected[start_ix..end_ix])
);
}
}
}
}
+ 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();
@@ -1,8 +1,8 @@
use crate::{
editor::{
buffer::{Anchor, Buffer, Point, ToPoint},
- display_map::{Bias, DisplayMap},
- DisplayPoint,
+ display_map::DisplayMap,
+ Bias, DisplayPoint,
},
time,
};
@@ -62,8 +62,8 @@ impl Selection {
}
pub fn range(&self, buffer: &Buffer) -> Range<Point> {
- 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 {
@@ -72,8 +72,8 @@ impl Selection {
}
pub fn display_range(&self, map: &DisplayMap, app: &AppContext) -> Range<DisplayPoint> {
- 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<u32>, Range<u32>) {
- 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,
@@ -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()
@@ -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
};
@@ -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,
}],
@@ -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 {
@@ -588,8 +581,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 +591,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,
});
@@ -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,
});
@@ -644,8 +637,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));
}
}
@@ -657,18 +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)
- .unwrap();
+ 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(),
@@ -702,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;
}
@@ -733,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;
}
@@ -770,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.
@@ -785,13 +767,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
@@ -801,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);
}
@@ -822,7 +801,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 +827,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 +855,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::<String>();
edits.push((start, text));
}
@@ -894,8 +872,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,21 +913,16 @@ 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
- .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).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()));
@@ -957,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.
@@ -968,9 +940,9 @@ 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();
+ 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;
end.row -= row_delta;
new_folds.push(start..end);
@@ -1025,31 +997,25 @@ 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,
- 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');
- 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));
let row_delta = next_row_display_end
.to_buffer_point(&self.display_map, Bias::Right, app)
- .unwrap()
.row
- buffer_rows.end
+ 1;
@@ -1062,9 +1028,9 @@ 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();
+ 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;
end.row += row_delta;
new_folds.push(start..end);
@@ -1095,19 +1061,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 +1096,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 +1142,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 +1161,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(),
@@ -1228,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;
}
@@ -1265,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;
}
@@ -1287,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;
}
@@ -1325,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;
}
@@ -1350,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;
@@ -1384,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;
}
@@ -1410,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;
@@ -1444,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;
}
@@ -1467,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;
@@ -1491,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;
}
@@ -1519,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;
@@ -1543,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;
}
@@ -1571,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;
@@ -1600,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;
}
@@ -1629,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;
@@ -1653,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;
}
@@ -1678,7 +1542,7 @@ impl BufferView {
pub fn move_to_beginning(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
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 +1561,7 @@ impl BufferView {
pub fn move_to_end(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
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 +1596,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 +1623,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(),
@@ -1893,20 +1753,14 @@ impl BufferView {
ctx: &AppContext,
) -> Option<Selection> {
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,
@@ -1923,10 +1777,7 @@ impl BufferView {
range: Range<DisplayPoint>,
app: &'a AppContext,
) -> impl 'a + Iterator<Item = Range<DisplayPoint>> {
- 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);
@@ -2045,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() {
@@ -2078,14 +1928,12 @@ 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).unwrap();
+ end.column = buffer.line_len(end.row);
start..end
})
.collect::<Vec<_>>();
@@ -2097,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;
}
@@ -2113,23 +1960,23 @@ impl BufferView {
}
}
- fn foldable_range_for_line(&self, start_row: u32, app: &AppContext) -> Result<Range<Point>> {
+ fn foldable_range_for_line(&self, start_row: u32, app: &AppContext) -> Range<Point> {
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<Self>) {
@@ -2146,7 +1993,7 @@ impl BufferView {
fn fold_ranges<T: ToOffset>(&mut self, ranges: Vec<Range<T>>, ctx: &mut ViewContext<Self>) {
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();
}
@@ -2154,22 +2001,22 @@ impl BufferView {
fn unfold_ranges<T: ToOffset>(&mut self, ranges: Vec<Range<T>>, ctx: &mut ViewContext<Self>) {
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<String> {
+ 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<u32> {
+ pub fn line_len(&self, display_row: u32, ctx: &AppContext) -> u32 {
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 {
@@ -1,18 +1,16 @@
use super::{
buffer::{AnchorRangeExt, TextSummary},
- Anchor, Buffer, DisplayPoint, Edit, Point, ToOffset,
+ Anchor, Bias, Buffer, DisplayPoint, Edit, Point, ToOffset,
};
use crate::{
- editor::rope,
+ editor::buffer,
sum_tree::{self, Cursor, FilterCursor, SeekBias, SumTree},
time,
};
-use anyhow::{anyhow, Result};
use gpui::{AppContext, ModelHandle};
use parking_lot::{Mutex, MutexGuard};
use std::{
cmp::{self, Ordering},
- iter::Take,
ops::Range,
};
@@ -52,56 +50,52 @@ 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<u32> {
- 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 {
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_row
}
pub fn folds_in_range<'a, T>(
&'a self,
range: Range<T>,
ctx: &'a AppContext,
- ) -> Result<impl Iterator<Item = &'a Range<Anchor>>>
+ ) -> impl Iterator<Item = &'a Range<Anchor>>
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<T: ToOffset>(
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
ctx: &AppContext,
- ) -> Result<()> {
+ ) {
let _ = self.sync(ctx);
let mut edits = Vec::new();
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(),
@@ -129,14 +123,13 @@ impl FoldMap {
new_tree
};
self.apply_edits(edits, ctx);
- Ok(())
}
pub fn unfold<T: ToOffset>(
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
ctx: &AppContext,
- ) -> Result<()> {
+ ) {
let _ = self.sync(ctx);
let buffer = self.buffer.read(ctx);
@@ -145,10 +138,9 @@ 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).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,
@@ -178,24 +170,23 @@ impl FoldMap {
folds
};
self.apply_edits(edits, ctx);
- Ok(())
}
fn intersecting_folds<'a, T>(
&self,
range: Range<T>,
ctx: &'a AppContext,
- ) -> Result<FilterCursor<impl 'a + Fn(&FoldSummary) -> bool, Fold, usize>>
+ ) -> FilterCursor<impl 'a + Fn(&FoldSummary) -> 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| {
+ let start = buffer.anchor_before(range.start.to_offset(buffer));
+ let end = buffer.anchor_after(range.end.to_offset(buffer));
+ 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 {
@@ -215,19 +206,11 @@ impl FoldMap {
false
}
- pub fn to_buffer_offset(&self, point: DisplayPoint, ctx: &AppContext) -> Result<usize> {
- let transforms = self.sync(ctx);
- let mut cursor = transforms.cursor::<DisplayPoint, TransformSummary>();
- cursor.seek(&point, SeekBias::Right, &());
- let overshoot = point.0 - cursor.start().display.lines;
- (cursor.start().buffer.lines + overshoot).to_offset(self.buffer.read(ctx))
+ pub fn to_buffer_offset(&self, point: DisplayPoint, ctx: &AppContext) -> usize {
+ self.snapshot(ctx).to_buffer_offset(point, ctx)
}
- pub fn to_display_offset(
- &self,
- point: DisplayPoint,
- ctx: &AppContext,
- ) -> Result<DisplayOffset> {
+ pub fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> DisplayOffset {
self.snapshot(ctx).to_display_offset(point, ctx)
}
@@ -305,11 +288,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 +302,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 +314,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 {
@@ -346,19 +329,23 @@ impl FoldMap {
}
if fold.end > fold.start {
+ let display_text = "…";
+ 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 {
- chars: 1,
- bytes: '…'.len_utf8(),
- lines: Point::new(0, 1),
- first_line_len: 1,
- rightmost_point: Point::new(0, 1),
+ bytes: display_text.len(),
+ lines,
+ first_line_chars: chars,
+ last_line_chars: chars,
+ rightmost_row: 0,
+ rightmost_row_chars: chars,
},
buffer: buffer.text_summary_for_range(fold.start..fold.end),
},
- display_text: Some('…'),
+ display_text: Some(display_text),
},
&(),
);
@@ -366,9 +353,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 {
@@ -408,55 +395,129 @@ pub struct FoldMapSnapshot {
}
impl FoldMapSnapshot {
- pub fn buffer_rows(&self, start_row: u32) -> Result<BufferRows> {
+ 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<Chars<'a>> {
- let offset = self.to_display_offset(point, ctx)?;
- let mut cursor = self.transforms.cursor();
- cursor.seek(&offset, SeekBias::Right, &());
- Ok(Chars {
- cursor,
- offset: offset.0,
- buffer: self.buffer.read(ctx),
- buffer_chars: None,
- })
+ pub fn chunks_at<'a>(&'a self, offset: DisplayOffset, ctx: &'a AppContext) -> Chunks<'a> {
+ let mut transform_cursor = self.transforms.cursor::<DisplayOffset, TransformSummary>();
+ 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,
+ }
}
- fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> Result<DisplayOffset> {
+ pub fn chars_at<'a>(
+ &'a self,
+ point: DisplayPoint,
+ ctx: &'a AppContext,
+ ) -> impl Iterator<Item = char> + 'a {
+ let offset = self.to_display_offset(point, ctx);
+ self.chunks_at(offset, ctx).flat_map(str::chars)
+ }
+
+ pub fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> DisplayOffset {
let mut cursor = self.transforms.cursor::<DisplayPoint, TransformSummary>();
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))?;
+ 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.chars;
+ (cursor.start().buffer.lines + overshoot).to_offset(self.buffer.read(ctx));
+ offset += end_buffer_offset - cursor.start().buffer.bytes;
+ }
+ DisplayOffset(offset)
+ }
+
+ pub fn to_buffer_offset(&self, point: DisplayPoint, ctx: &AppContext) -> usize {
+ let mut cursor = self.transforms.cursor::<DisplayPoint, TransformSummary>();
+ cursor.seek(&point, SeekBias::Right, &());
+ 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::<DisplayOffset, TransformSummary>();
+ 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::<DisplayPoint, TransformSummary>();
+ 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)
}
- Ok(DisplayOffset(offset))
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
struct Transform {
summary: TransformSummary,
- display_text: Option<char>,
+ display_text: Option<&'static str>,
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
@@ -604,39 +665,56 @@ impl<'a> Iterator for BufferRows<'a> {
}
}
-pub struct Chars<'a> {
- cursor: Cursor<'a, Transform, DisplayOffset, TransformSummary>,
- offset: usize,
- buffer: &'a Buffer,
- buffer_chars: Option<Take<rope::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 Chars<'a> {
- type Item = char;
+impl<'a> Iterator for Chunks<'a> {
+ type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
- if let Some(c) = self.buffer_chars.as_mut().and_then(|chars| chars.next()) {
- self.offset += 1;
- return Some(c);
- }
+ let transform = if let Some(item) = self.transform_cursor.item() {
+ item
+ } else {
+ return None;
+ };
- while self.offset == self.cursor.end().display.chars && self.cursor.item().is_some() {
- self.cursor.next();
+ // 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);
}
- self.cursor.item().and_then(|transform| {
- if let Some(c) = transform.display_text {
- self.offset += 1;
- Some(c)
+ // 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 {
- 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));
- self.next()
+ self.buffer_chunks.next();
}
- })
+
+ self.buffer_offset += chunk.len();
+ return Some(chunk);
+ }
+
+ None
}
}
@@ -651,7 +729,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 +741,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;
}
}
@@ -684,8 +762,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| {
@@ -711,8 +788,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");
}
@@ -723,17 +799,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");
}
@@ -742,7 +818,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");
@@ -769,8 +845,7 @@ mod tests {
Point::new(3, 1)..Point::new(4, 1),
],
app.as_ref(),
- )
- .unwrap();
+ );
assert_eq!(map.text(app.as_ref()), "aa…eeeee");
}
@@ -785,8 +860,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| {
@@ -811,12 +885,10 @@ 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).unwrap()..fold.end.to_point(buffer).unwrap())
+ .map(|fold| fold.start.to_point(buffer)..fold.end.to_point(buffer))
.collect::<Vec<_>>();
assert_eq!(
fold_ranges,
@@ -830,9 +902,10 @@ 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;
+ use Bias::{Left, Right};
let iterations = env::var("ITERATIONS")
.map(|i| i.parse().expect("invalid `ITERATIONS` variable"))
@@ -865,23 +938,23 @@ 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.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);
- 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);
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.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);
- map.unfold(to_unfold, app.as_ref()).unwrap();
+ map.unfold(to_unfold, app.as_ref());
}
_ => {
let edits = buffer.update(app, |buffer, ctx| {
@@ -905,10 +978,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();
@@ -916,61 +986,73 @@ 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();
- assert_eq!(line_len, line.chars().count() as u32);
+ let line_len = map.line_len(display_row as u32, app.as_ref());
+ 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;
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
+ display_point,
+ "to_display_point({:?})",
+ buffer_point,
);
assert_eq!(
- map.to_buffer_offset(display_point, app.as_ref()).unwrap(),
- buffer_offset
+ map.to_buffer_offset(display_point, app.as_ref()),
+ buffer_offset,
+ "to_buffer_offset({:?})",
+ display_point,
);
assert_eq!(
- map.to_display_offset(display_point, app.as_ref()).unwrap(),
- display_offset
+ map.to_display_offset(display_point, app.as_ref()),
+ display_offset,
+ "to_display_offset({:?})",
+ display_point,
);
if c == '\n' {
*display_point.row_mut() += 1;
*display_point.column_mut() = 0;
+ char_column = 0;
} else {
- *display_point.column_mut() += 1;
+ *display_point.column_mut() += c.len_utf8() as u32;
+ char_column += 1;
}
- display_offset.0 += 1;
- if display_point.column() > rightmost_point.column() {
+ display_offset.0 += c.len_utf8();
+ if char_column > rightmost_char_column {
panic!(
- "invalid rightmost point {:?}, found point {:?}",
- rightmost_point, display_point
+ "invalid rightmost row {:?} (chars {}), found row {:?} (chars: {})",
+ rightmost_row,
+ rightmost_char_column,
+ display_point.row(),
+ char_column
);
}
}
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 point = DisplayPoint::new(row, column);
- let offset = map.to_display_offset(point, app.as_ref()).unwrap().0;
- let len = rng.gen_range(0..=map.len(app.as_ref()) - offset);
+ 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!(
map.snapshot(app.as_ref())
- .chars_at(point, app.as_ref())
- .unwrap()
- .take(len)
+ .chunks_at(offset, app.as_ref())
.collect::<String>(),
- expected_text
- .chars()
- .skip(offset)
- .take(len)
- .collect::<String>()
+ &expected_text[offset.0..],
);
}
@@ -981,28 +1063,27 @@ mod tests {
assert_eq!(
map.snapshot(app.as_ref())
.buffer_rows(display_row)
- .unwrap()
.collect::<Vec<_>>(),
expected_buffer_rows[idx..],
);
}
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()));
}
for _ in 0..5 {
- let end = rng.gen_range(0..=buffer.len());
- let start = 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()
.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
})
@@ -1011,7 +1092,6 @@ mod tests {
assert_eq!(
map.folds_in_range(start..end, app.as_ref())
- .unwrap()
.cloned()
.collect::<Vec<_>>(),
expected_folds
@@ -1034,21 +1114,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<_>>(),
vec![0, 3, 5, 6]
);
assert_eq!(
map.snapshot(app.as_ref())
.buffer_rows(3)
- .unwrap()
.collect::<Vec<_>>(),
vec![6]
);
@@ -1057,8 +1134,7 @@ mod tests {
impl FoldMap {
fn text(&self, app: &AppContext) -> String {
self.snapshot(app)
- .chars_at(DisplayPoint(Point::zero()), app)
- .unwrap()
+ .chunks_at(DisplayOffset(0), app)
.collect()
}
@@ -1069,9 +1145,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 +1171,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"
);
@@ -1,18 +1,11 @@
mod fold_map;
-use super::{buffer, Anchor, Buffer, Edit, Point, ToOffset, ToPoint};
-use anyhow::Result;
+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<Buffer>,
fold_map: FoldMap,
@@ -39,7 +32,7 @@ impl DisplayMap {
&'a self,
range: Range<T>,
app: &'a AppContext,
- ) -> Result<impl Iterator<Item = &'a Range<Anchor>>>
+ ) -> impl Iterator<Item = &'a Range<Anchor>>
where
T: ToOffset,
{
@@ -50,7 +43,7 @@ impl DisplayMap {
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
ctx: &AppContext,
- ) -> Result<()> {
+ ) {
self.fold_map.fold(ranges, ctx)
}
@@ -58,7 +51,7 @@ impl DisplayMap {
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
ctx: &AppContext,
- ) -> Result<()> {
+ ) {
self.fold_map.unfold(ranges, ctx)
}
@@ -68,25 +61,32 @@ impl DisplayMap {
pub fn text(&self, ctx: &AppContext) -> String {
self.snapshot(ctx)
- .chars_at(DisplayPoint::zero(), ctx)
- .unwrap()
+ .chunks_at(DisplayPoint::zero(), ctx)
.collect()
}
- pub fn line(&self, display_row: u32, ctx: &AppContext) -> Result<String> {
- Ok(self
+ pub fn line(&self, display_row: u32, ctx: &AppContext) -> String {
+ let mut result = String::new();
+ for chunk in self
.snapshot(ctx)
- .chars_at(DisplayPoint::new(display_row, 0), ctx)?
- .take_while(|c| *c != '\n')
- .collect())
+ .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) -> 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,43 +95,34 @@ impl DisplayMap {
break;
}
}
- Ok((indent, is_blank))
+ (indent, is_blank)
}
- pub fn line_len(&self, row: u32, ctx: &AppContext) -> Result<u32> {
- 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()
}
+ // 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).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 rightmost_row(&self, ctx: &AppContext) -> u32 {
+ self.fold_map.rightmost_row(ctx)
}
- pub fn anchor_before(
- &self,
- point: DisplayPoint,
- bias: Bias,
- app: &AppContext,
- ) -> Result<Anchor> {
+ 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<Anchor> {
+ 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))
}
}
@@ -141,33 +132,74 @@ pub struct DisplayMapSnapshot {
}
impl DisplayMapSnapshot {
- pub fn buffer_rows(&self, start_row: u32) -> Result<BufferRows> {
+ 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<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)?;
- if to_next_stop > 0 {
- fold_chars.next();
+ pub fn chunks_at<'a>(&'a self, point: DisplayPoint, app: &'a AppContext) -> Chunks<'a> {
+ 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: expanded_char_column,
+ tab_size: self.tab_size,
+ chunk: &SPACES[0..to_next_stop],
+ skip_leading_tab: to_next_stop > 0,
}
+ }
- Ok(Chars {
- fold_chars,
- column,
- to_next_stop,
- tab_size: self.tab_size,
- })
+ pub fn chars_at<'a>(
+ &'a self,
+ point: DisplayPoint,
+ app: &'a AppContext,
+ ) -> impl Iterator<Item = char> + 'a {
+ self.chunks_at(point, app).flat_map(str::chars)
}
- fn expand_tabs(&self, mut point: DisplayPoint, ctx: &AppContext) -> Result<DisplayPoint> {
+ 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) {
+ if column >= target {
+ break;
+ }
+ count += 1;
+ column += c.len_utf8() as u32;
+ }
+ 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) {
+ if c == '\n' || 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
- .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(
@@ -175,15 +207,15 @@ impl DisplayMapSnapshot {
mut point: DisplayPoint,
bias: Bias,
ctx: &AppContext,
- ) -> Result<(DisplayPoint, usize)> {
+ ) -> (DisplayPoint, usize, 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);
+ let (collapsed, expanded_char_column, to_next_stop) =
+ collapse_tabs(chars, expanded, bias, self.tab_size);
*point.column_mut() = collapsed as u32;
-
- Ok((point, to_next_stop))
+ (point, expanded_char_column, to_next_stop)
}
}
@@ -215,94 +247,114 @@ impl DisplayPoint {
&mut self.0.column
}
- pub fn to_buffer_point(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> Result<Point> {
- 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), ctx)
}
- pub fn to_buffer_offset(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> Result<usize> {
+ 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) -> Result<Self> {
+ 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)> {
- 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
}
}
impl Point {
- pub fn to_display_point(self, map: &DisplayMap, ctx: &AppContext) -> Result<DisplayPoint> {
+ 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<DisplayPoint> {
- self.to_point(map.buffer.read(app))?
+ pub fn to_display_point(&self, map: &DisplayMap, app: &AppContext) -> DisplayPoint {
+ self.to_point(map.buffer.read(app))
.to_display_point(map, app)
}
}
-pub struct Chars<'a> {
- fold_chars: fold_map::Chars<'a>,
+// Handles a tab width <= 16
+const SPACES: &'static str = " ";
+
+pub struct Chunks<'a> {
+ fold_chunks: fold_map::Chunks<'a>,
+ chunk: &'a str,
column: usize,
- to_next_stop: usize,
tab_size: usize,
+ skip_leading_tab: bool,
}
-impl<'a> Iterator for Chars<'a> {
- type Item = char;
+impl<'a> Iterator for Chunks<'a> {
+ type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
- if self.to_next_stop > 0 {
- self.to_next_stop -= 1;
- self.column += 1;
- Some(' ')
- } else {
- self.fold_chars.next().map(|c| match c {
- '\t' => {
- self.to_next_stop = self.tab_size - self.column % self.tab_size - 1;
- self.column += 1;
- ' '
- }
- '\n' => {
- self.column = 0;
- c
+ 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;
}
- _ => {
- self.column += 1;
- c
+ } else {
+ return None;
+ }
+ }
+
+ for (ix, c) in self.chunk.char_indices() {
+ match c {
+ '\t' => {
+ 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
}
}
pub fn expand_tabs(chars: impl Iterator<Item = char>, 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(
@@ -310,29 +362,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();
+ }
+
+ if expanded_bytes > column && matches!(bias, Bias::Left) {
+ expanded_chars -= 1;
+ break;
}
+
+ collapsed_bytes += c.len_utf8();
}
- (collapsed, 0)
+ (collapsed_bytes, expanded_chars, 0)
}
#[cfg(test)]
@@ -341,7 +403,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());
@@ -360,31 +422,63 @@ mod tests {
.unwrap();
assert_eq!(
- map.snapshot(app.as_ref())
- .chars_at(DisplayPoint::new(1, 0), app.as_ref())
- .unwrap()
- .take(10)
- .collect::<String>(),
+ &map.snapshot(app.as_ref())
+ .chunks_at(DisplayPoint::new(1, 0), app.as_ref())
+ .collect::<String>()[0..10],
" b bb"
);
assert_eq!(
- map.snapshot(app.as_ref())
- .chars_at(DisplayPoint::new(1, 2), app.as_ref())
- .unwrap()
- .take(10)
- .collect::<String>(),
+ &map.snapshot(app.as_ref())
+ .chunks_at(DisplayPoint::new(1, 2), app.as_ref())
+ .collect::<String>()[0..10],
" b bbbb"
);
assert_eq!(
- map.snapshot(app.as_ref())
- .chars_at(DisplayPoint::new(1, 6), app.as_ref())
- .unwrap()
- .take(13)
- .collect::<String>(),
+ &map.snapshot(app.as_ref())
+ .chunks_at(DisplayPoint::new(1, 6), app.as_ref())
+ .collect::<String>()[0..13],
" bbbbb\nc c"
);
}
+ #[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);
@@ -392,20 +486,76 @@ 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 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), "✅ α\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);
+ 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::<String>(),
+ " α\nβ \n🏀β γ"
+ );
+ 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::<String>(),
+ " α\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)
+ );
}
#[gpui::test]
@@ -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<T> {
fn sorted(&self) -> Range<T>;
}
@@ -1,27 +1,26 @@
-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<DisplayPoint> {
if point.column() > 0 {
*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)
+ Ok(map.snapshot(app).clip_point(point, Bias::Left, app))
}
pub fn right(map: &DisplayMap, mut point: DisplayPoint, app: &AppContext) -> Result<DisplayPoint> {
- 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() {
*point.row_mut() += 1;
*point.column_mut() = 0;
}
- Ok(point)
+ Ok(map.snapshot(app).clip_point(point, Bias::Right, app))
}
pub fn up(
@@ -30,14 +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)
};
+
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(), goal_column, app);
} else {
point = DisplayPoint::new(0, 0);
}
@@ -51,15 +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);
+
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(), goal_column, app);
} else {
point = max_point;
}
@@ -73,7 +76,7 @@ pub fn line_beginning(
toggle_indent: bool,
app: &AppContext,
) -> Result<DisplayPoint> {
- 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 +87,7 @@ pub fn line_beginning(
pub fn line_end(map: &DisplayMap, point: DisplayPoint, app: &AppContext) -> Result<DisplayPoint> {
Ok(DisplayPoint::new(
point.row(),
- map.line_len(point.row(), app)?,
+ map.line_len(point.row(), app),
))
}
@@ -98,13 +101,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;
}
@@ -114,7 +117,7 @@ pub fn prev_word_boundary(
}
prev_c = Some(c);
- column += 1;
+ column += c.len_utf8() as u32;
}
Ok(boundary)
}
@@ -126,7 +129,7 @@ pub fn next_word_boundary(
app: &AppContext,
) -> Result<DisplayPoint> {
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;
}
@@ -135,7 +138,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);
}
@@ -1,20 +1,5 @@
use rand::prelude::*;
-use std::{cmp::Ordering, ops::Range};
-
-pub fn byte_range_for_char_range(text: impl AsRef<str>, char_range: Range<usize>) -> Range<usize> {
- 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
-}
+use std::cmp::Ordering;
pub fn post_inc(value: &mut usize) -> usize {
let prev = *value;
@@ -48,6 +33,7 @@ where
pub struct RandomCharIter<T: Rng>(T);
impl<T: Rng> RandomCharIter<T> {
+ #[cfg(test)]
pub fn new(rng: T) -> Self {
Self(rng)
}
@@ -321,11 +321,22 @@ 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() {
- 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 {
+ 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;
+ match_positions[i] = byte_ix;
}
score
@@ -550,6 +561,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,