Merge branch 'master' into buffer-per-inode

Nathan Sobo created

Change summary

zed/src/editor/buffer/selection.rs     |  32 
zed/src/editor/buffer_view.rs          | 888 +++++++++++++++++++++++++--
zed/src/editor/display_map/fold_map.rs | 193 ++++-
zed/src/editor/display_map/mod.rs      |  26 
zed/src/editor/mod.rs                  |   9 
zed/src/editor/movement.rs             |  95 ++
6 files changed, 1,117 insertions(+), 126 deletions(-)

Detailed changes

zed/src/editor/buffer/selection.rs 🔗

@@ -1,7 +1,7 @@
 use crate::{
     editor::{
         buffer::{Anchor, Buffer, Point, ToPoint},
-        display_map::DisplayMap,
+        display_map::{Bias, DisplayMap},
         DisplayPoint,
     },
     time,
@@ -72,4 +72,34 @@ impl Selection {
             start..end
         }
     }
+
+    pub fn buffer_rows_for_display_rows(
+        &self,
+        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 mut display_end = self.end.to_display_point(map, ctx).unwrap();
+        if display_end.row() != map.max_point(ctx).row()
+            && display_start.row() != display_end.row()
+            && display_end.column() == 0
+        {
+            *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();
+
+        (
+            buffer_start.row..buffer_end.row + 1,
+            display_start.row()..display_end.row() + 1,
+        )
+    }
 }

zed/src/editor/buffer_view.rs 🔗

@@ -19,6 +19,7 @@ use std::{
     cmp::{self, Ordering},
     fmt::Write,
     iter::FromIterator,
+    mem,
     ops::Range,
     path::Path,
     sync::Arc,
@@ -30,7 +31,34 @@ const CURSOR_BLINK_INTERVAL: Duration = Duration::from_millis(500);
 pub fn init(app: &mut MutableAppContext) {
     app.add_bindings(vec![
         Binding::new("backspace", "buffer:backspace", Some("BufferView")),
+        Binding::new("ctrl-h", "buffer:backspace", Some("BufferView")),
+        Binding::new("delete", "buffer:delete", Some("BufferView")),
+        Binding::new("ctrl-d", "buffer:delete", Some("BufferView")),
         Binding::new("enter", "buffer:newline", Some("BufferView")),
+        Binding::new("ctrl-shift-K", "buffer:delete_line", Some("BufferView")),
+        Binding::new(
+            "alt-backspace",
+            "buffer:delete_to_previous_word_boundary",
+            Some("BufferView"),
+        ),
+        Binding::new(
+            "alt-delete",
+            "buffer:delete_to_next_word_boundary",
+            Some("BufferView"),
+        ),
+        Binding::new(
+            "cmd-backspace",
+            "buffer:delete_to_beginning_of_line",
+            Some("BufferView"),
+        ),
+        Binding::new(
+            "cmd-delete",
+            "buffer:delete_to_end_of_line",
+            Some("BufferView"),
+        ),
+        Binding::new("cmd-shift-D", "buffer:duplicate_line", Some("BufferView")),
+        Binding::new("ctrl-cmd-up", "buffer:move_line_up", Some("BufferView")),
+        Binding::new("ctrl-cmd-down", "buffer:move_line_down", Some("BufferView")),
         Binding::new("cmd-x", "buffer:cut", Some("BufferView")),
         Binding::new("cmd-c", "buffer:copy", Some("BufferView")),
         Binding::new("cmd-v", "buffer:paste", Some("BufferView")),
@@ -40,10 +68,77 @@ pub fn init(app: &mut MutableAppContext) {
         Binding::new("down", "buffer:move_down", Some("BufferView")),
         Binding::new("left", "buffer:move_left", Some("BufferView")),
         Binding::new("right", "buffer:move_right", Some("BufferView")),
+        Binding::new(
+            "alt-left",
+            "buffer:move_to_previous_word_boundary",
+            Some("BufferView"),
+        ),
+        Binding::new(
+            "alt-right",
+            "buffer:move_to_next_word_boundary",
+            Some("BufferView"),
+        ),
+        Binding::new(
+            "cmd-left",
+            "buffer:move_to_beginning_of_line",
+            Some("BufferView"),
+        ),
+        Binding::new(
+            "ctrl-a",
+            "buffer:move_to_beginning_of_line",
+            Some("BufferView"),
+        ),
+        Binding::new(
+            "cmd-right",
+            "buffer:move_to_end_of_line",
+            Some("BufferView"),
+        ),
+        Binding::new("ctrl-e", "buffer:move_to_end_of_line", Some("BufferView")),
+        Binding::new("cmd-up", "buffer:move_to_beginning", Some("BufferView")),
+        Binding::new("cmd-down", "buffer:move_to_end", Some("BufferView")),
         Binding::new("shift-up", "buffer:select_up", Some("BufferView")),
         Binding::new("shift-down", "buffer:select_down", Some("BufferView")),
         Binding::new("shift-left", "buffer:select_left", Some("BufferView")),
         Binding::new("shift-right", "buffer:select_right", Some("BufferView")),
+        Binding::new(
+            "alt-shift-left",
+            "buffer:select_to_previous_word_boundary",
+            Some("BufferView"),
+        ),
+        Binding::new(
+            "alt-shift-right",
+            "buffer:select_to_next_word_boundary",
+            Some("BufferView"),
+        ),
+        Binding::new(
+            "cmd-shift-left",
+            "buffer:select_to_beginning_of_line",
+            Some("BufferView"),
+        )
+        .with_arg(true),
+        Binding::new(
+            "ctrl-shift-A",
+            "buffer:select_to_beginning_of_line",
+            Some("BufferView"),
+        )
+        .with_arg(true),
+        Binding::new(
+            "cmd-shift-right",
+            "buffer:select_to_end_of_line",
+            Some("BufferView"),
+        ),
+        Binding::new(
+            "ctrl-shift-E",
+            "buffer:select_to_end_of_line",
+            Some("BufferView"),
+        ),
+        Binding::new(
+            "cmd-shift-up",
+            "buffer:select_to_beginning",
+            Some("BufferView"),
+        ),
+        Binding::new("cmd-shift-down", "buffer:select_to_end", Some("BufferView")),
+        Binding::new("cmd-a", "buffer:select_all", Some("BufferView")),
         Binding::new("pageup", "buffer:page_up", Some("BufferView")),
         Binding::new("pagedown", "buffer:page_down", Some("BufferView")),
         Binding::new("alt-cmd-[", "buffer:fold", Some("BufferView")),
@@ -60,6 +155,27 @@ pub fn init(app: &mut MutableAppContext) {
     app.add_action("buffer:insert", BufferView::insert);
     app.add_action("buffer:newline", BufferView::newline);
     app.add_action("buffer:backspace", BufferView::backspace);
+    app.add_action("buffer:delete", BufferView::delete);
+    app.add_action("buffer:delete_line", BufferView::delete_line);
+    app.add_action(
+        "buffer:delete_to_previous_word_boundary",
+        BufferView::delete_to_previous_word_boundary,
+    );
+    app.add_action(
+        "buffer:delete_to_next_word_boundary",
+        BufferView::delete_to_next_word_boundary,
+    );
+    app.add_action(
+        "buffer:delete_to_beginning_of_line",
+        BufferView::delete_to_beginning_of_line,
+    );
+    app.add_action(
+        "buffer:delete_to_end_of_line",
+        BufferView::delete_to_end_of_line,
+    );
+    app.add_action("buffer:duplicate_line", BufferView::duplicate_line);
+    app.add_action("buffer:move_line_up", BufferView::move_line_up);
+    app.add_action("buffer:move_line_down", BufferView::move_line_down);
     app.add_action("buffer:cut", BufferView::cut);
     app.add_action("buffer:copy", BufferView::copy);
     app.add_action("buffer:paste", BufferView::paste);
@@ -69,10 +185,50 @@ pub fn init(app: &mut MutableAppContext) {
     app.add_action("buffer:move_down", BufferView::move_down);
     app.add_action("buffer:move_left", BufferView::move_left);
     app.add_action("buffer:move_right", BufferView::move_right);
+    app.add_action(
+        "buffer:move_to_previous_word_boundary",
+        BufferView::move_to_previous_word_boundary,
+    );
+    app.add_action(
+        "buffer:move_to_next_word_boundary",
+        BufferView::move_to_next_word_boundary,
+    );
+    app.add_action(
+        "buffer:move_to_beginning_of_line",
+        BufferView::move_to_beginning_of_line,
+    );
+    app.add_action(
+        "buffer:move_to_end_of_line",
+        BufferView::move_to_end_of_line,
+    );
+    app.add_action("buffer:move_to_beginning", BufferView::move_to_beginning);
+    app.add_action("buffer:move_to_end", BufferView::move_to_end);
     app.add_action("buffer:select_up", BufferView::select_up);
     app.add_action("buffer:select_down", BufferView::select_down);
     app.add_action("buffer:select_left", BufferView::select_left);
     app.add_action("buffer:select_right", BufferView::select_right);
+    app.add_action(
+        "buffer:select_to_previous_word_boundary",
+        BufferView::select_to_previous_word_boundary,
+    );
+    app.add_action(
+        "buffer:select_to_next_word_boundary",
+        BufferView::select_to_next_word_boundary,
+    );
+    app.add_action(
+        "buffer:select_to_beginning_of_line",
+        BufferView::select_to_beginning_of_line,
+    );
+    app.add_action(
+        "buffer:select_to_end_of_line",
+        BufferView::select_to_end_of_line,
+    );
+    app.add_action(
+        "buffer:select_to_beginning",
+        BufferView::select_to_beginning,
+    );
+    app.add_action("buffer:select_to_end", BufferView::select_to_end);
+    app.add_action("buffer:select_all", BufferView::select_all);
     app.add_action("buffer:page_up", BufferView::page_up);
     app.add_action("buffer:page_down", BufferView::page_down);
     app.add_action("buffer:fold", BufferView::fold);
@@ -374,23 +530,30 @@ impl BufferView {
         self.pending_selection.is_some()
     }
 
-    #[cfg(test)]
-    fn select_ranges<'a, T>(&mut self, ranges: T, ctx: &mut ViewContext<Self>) -> Result<()>
+    fn select_ranges<I, T>(&mut self, ranges: I, autoscroll: bool, ctx: &mut ViewContext<Self>)
     where
-        T: IntoIterator<Item = &'a Range<usize>>,
+        I: IntoIterator<Item = Range<T>>,
+        T: ToOffset,
     {
         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 reversed = if start > end {
+                mem::swap(&mut start, &mut end);
+                true
+            } else {
+                false
+            };
             selections.push(Selection {
-                start: buffer.anchor_before(range.start)?,
-                end: buffer.anchor_before(range.end)?,
-                reversed: false,
+                start: buffer.anchor_before(start).unwrap(),
+                end: buffer.anchor_before(end).unwrap(),
+                reversed,
                 goal_column: None,
             });
         }
-        self.update_selections(selections, false, ctx);
-        Ok(())
+        self.update_selections(selections, autoscroll, ctx);
     }
 
     #[cfg(test)]
@@ -401,10 +564,19 @@ impl BufferView {
         let map = self.display_map.read(ctx);
         let mut selections = Vec::new();
         for range in ranges {
+            let mut start = range.start;
+            let mut end = range.end;
+            let reversed = if start > end {
+                mem::swap(&mut start, &mut end);
+                true
+            } else {
+                false
+            };
+
             selections.push(Selection {
-                start: map.anchor_before(range.start, Bias::Left, ctx.as_ref())?,
-                end: map.anchor_before(range.end, Bias::Left, ctx.as_ref())?,
-                reversed: false,
+                start: map.anchor_before(start, Bias::Left, ctx.as_ref())?,
+                end: map.anchor_before(end, Bias::Left, ctx.as_ref())?,
+                reversed,
                 goal_column: None,
             });
         }
@@ -471,7 +643,8 @@ impl BufferView {
             let buffer = self.buffer.read(ctx);
             let map = self.display_map.read(ctx);
             for selection in &mut selections {
-                if selection.range(buffer).is_empty() {
+                let range = selection.range(buffer);
+                if range.start == range.end {
                     let head = selection
                         .head()
                         .to_display_point(map, ctx.as_ref())
@@ -494,6 +667,377 @@ impl BufferView {
         self.end_transaction(ctx);
     }
 
+    pub fn delete(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        self.start_transaction(ctx);
+        let mut selections = self.selections(ctx.as_ref()).to_vec();
+        {
+            let buffer = self.buffer.read(ctx);
+            let map = self.display_map.read(ctx);
+            for selection in &mut selections {
+                let range = selection.range(buffer);
+                if range.start == range.end {
+                    let head = selection
+                        .head()
+                        .to_display_point(map, ctx.as_ref())
+                        .unwrap();
+                    let cursor = map
+                        .anchor_before(
+                            movement::right(map, head, ctx.as_ref()).unwrap(),
+                            Bias::Right,
+                            ctx.as_ref(),
+                        )
+                        .unwrap();
+                    selection.set_head(&buffer, cursor);
+                    selection.goal_column = None;
+                }
+            }
+        }
+
+        self.update_selections(selections, true, ctx);
+        self.insert(&String::new(), ctx);
+        self.end_transaction(ctx);
+    }
+
+    pub fn delete_line(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        self.start_transaction(ctx);
+
+        let app = ctx.as_ref();
+        let map = self.display_map.read(app);
+        let buffer = self.buffer.read(app);
+
+        let mut new_cursors = Vec::new();
+        let mut edit_ranges = Vec::new();
+
+        let mut selections = self.selections(app).iter().peekable();
+        while let Some(selection) = selections.next() {
+            let (mut rows, _) = selection.buffer_rows_for_display_rows(map, app);
+            let goal_display_column = selection
+                .head()
+                .to_display_point(map, app)
+                .unwrap()
+                .column();
+
+            // Accumulate contiguous regions of rows that we want to delete.
+            while let Some(next_selection) = selections.peek() {
+                let (next_rows, _) = next_selection.buffer_rows_for_display_rows(map, app);
+                if next_rows.start <= rows.end {
+                    rows.end = next_rows.end;
+                    selections.next().unwrap();
+                } else {
+                    break;
+                }
+            }
+
+            let mut edit_start = Point::new(rows.start, 0).to_offset(buffer).unwrap();
+            let edit_end;
+            let cursor_buffer_row;
+            if let Ok(end_offset) = Point::new(rows.end, 0).to_offset(buffer) {
+                // 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;
+                cursor_buffer_row = rows.end;
+            } else {
+                // If there isn't a line after the range, delete the \n from the line before the
+                // start of the row range and position the cursor there.
+                edit_start = edit_start.saturating_sub(1);
+                edit_end = buffer.len();
+                cursor_buffer_row = rows.start.saturating_sub(1);
+            }
+
+            let mut cursor = Point::new(cursor_buffer_row, 0)
+                .to_display_point(map, app)
+                .unwrap();
+            *cursor.column_mut() = cmp::min(
+                goal_display_column,
+                map.line_len(cursor.row(), app).unwrap(),
+            );
+
+            new_cursors.push(cursor.to_buffer_point(map, Bias::Left, app).unwrap());
+            edit_ranges.push(edit_start..edit_end);
+        }
+
+        new_cursors.sort_unstable();
+        let new_selections = new_cursors
+            .into_iter()
+            .map(|cursor| buffer.anchor_before(cursor).unwrap())
+            .map(|anchor| Selection {
+                start: anchor.clone(),
+                end: anchor,
+                reversed: false,
+                goal_column: None,
+            })
+            .collect();
+        self.buffer
+            .update(ctx, |buffer, ctx| buffer.edit(edit_ranges, "", Some(ctx)))
+            .unwrap();
+        self.update_selections(new_selections, true, ctx);
+        self.end_transaction(ctx);
+    }
+
+    pub fn duplicate_line(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        self.start_transaction(ctx);
+
+        let mut selections = self.selections(ctx.as_ref()).to_vec();
+        {
+            // Temporarily bias selections right to allow newly duplicate lines to push them down
+            // 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();
+            }
+        }
+        self.update_selections(selections.clone(), false, ctx);
+
+        let app = ctx.as_ref();
+        let buffer = self.buffer.read(ctx);
+        let map = self.display_map.read(ctx);
+
+        let mut edits = Vec::new();
+        let mut selections_iter = selections.iter_mut().peekable();
+        while let Some(selection) = selections_iter.next() {
+            // Avoid duplicating the same lines twice.
+            let (mut rows, _) = selection.buffer_rows_for_display_rows(map, app);
+            while let Some(next_selection) = selections_iter.peek() {
+                let (next_rows, _) = next_selection.buffer_rows_for_display_rows(map, app);
+                if next_rows.start <= rows.end - 1 {
+                    rows.end = next_rows.end;
+                    selections_iter.next().unwrap();
+                } else {
+                    break;
+                }
+            }
+
+            // 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 text = buffer
+                .text_for_range(start..end)
+                .unwrap()
+                .chain(Some('\n'))
+                .collect::<String>();
+            edits.push((start, text));
+        }
+
+        self.buffer.update(ctx, |buffer, ctx| {
+            for (offset, text) in edits.into_iter().rev() {
+                buffer.edit(Some(offset..offset), text, Some(ctx)).unwrap();
+            }
+        });
+
+        // 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();
+        }
+        self.update_selections(selections, true, ctx);
+
+        self.end_transaction(ctx);
+    }
+
+    pub fn move_line_up(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        self.start_transaction(ctx);
+
+        let app = ctx.as_ref();
+        let buffer = self.buffer.read(ctx);
+        let map = self.display_map.read(ctx);
+
+        let mut edits = Vec::new();
+        let mut new_selection_ranges = Vec::new();
+        let mut old_folds = Vec::new();
+        let mut new_folds = Vec::new();
+
+        let mut selections = self.selections(app).iter().peekable();
+        let mut contiguous_selections = Vec::new();
+        while let Some(selection) = selections.next() {
+            // Accumulate contiguous regions of rows that we want to move.
+            contiguous_selections.push(selection.range(buffer));
+            let (mut buffer_rows, mut display_rows) =
+                selection.buffer_rows_for_display_rows(map, app);
+            while let Some(next_selection) = selections.peek() {
+                let (next_buffer_rows, next_display_rows) =
+                    next_selection.buffer_rows_for_display_rows(map, app);
+                if next_buffer_rows.start <= buffer_rows.end {
+                    buffer_rows.end = next_buffer_rows.end;
+                    display_rows.end = next_display_rows.end;
+                    contiguous_selections.push(next_selection.range(buffer));
+                    selections.next().unwrap();
+                } else {
+                    break;
+                }
+            }
+
+            // Cut the text from the selected rows and paste it at the start of the previous line.
+            if display_rows.start != 0 {
+                let selection_row_start =
+                    Point::new(buffer_rows.start, 0).to_offset(buffer).unwrap();
+                let selection_row_end = Point::new(
+                    buffer_rows.end - 1,
+                    buffer.line_len(buffer_rows.end - 1).unwrap(),
+                )
+                .to_offset(buffer)
+                .unwrap();
+
+                let prev_row_display_start = DisplayPoint::new(display_rows.start - 1, 0);
+                let prev_row_start = prev_row_display_start
+                    .to_buffer_offset(map, Bias::Left, app)
+                    .unwrap();
+
+                let mut text = String::new();
+                text.extend(
+                    buffer
+                        .text_for_range(selection_row_start..selection_row_end)
+                        .unwrap(),
+                );
+                text.push('\n');
+                edits.push((prev_row_start..prev_row_start, text));
+                edits.push((selection_row_start - 1..selection_row_end, String::new()));
+
+                let row_delta = buffer_rows.start
+                    - prev_row_display_start
+                        .to_buffer_point(map, Bias::Left, app)
+                        .unwrap()
+                        .row;
+
+                // Move selections up.
+                for range in &mut contiguous_selections {
+                    range.start.row -= row_delta;
+                    range.end.row -= row_delta;
+                }
+
+                // Move folds up.
+                old_folds.push(selection_row_start..selection_row_end);
+                for fold in map
+                    .folds_in_range(selection_row_start..selection_row_end, app)
+                    .unwrap()
+                {
+                    let mut start = fold.start.to_point(buffer).unwrap();
+                    let mut end = fold.end.to_point(buffer).unwrap();
+                    start.row -= row_delta;
+                    end.row -= row_delta;
+                    new_folds.push(start..end);
+                }
+            }
+
+            new_selection_ranges.extend(contiguous_selections.drain(..));
+        }
+
+        self.unfold_ranges(old_folds, ctx);
+        self.buffer.update(ctx, |buffer, ctx| {
+            for (range, text) in edits.into_iter().rev() {
+                buffer.edit(Some(range), text, Some(ctx)).unwrap();
+            }
+        });
+        self.fold_ranges(new_folds, ctx);
+        self.select_ranges(new_selection_ranges, true, ctx);
+
+        self.end_transaction(ctx);
+    }
+
+    pub fn move_line_down(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        self.start_transaction(ctx);
+
+        let app = ctx.as_ref();
+        let buffer = self.buffer.read(ctx);
+        let map = self.display_map.read(ctx);
+
+        let mut edits = Vec::new();
+        let mut new_selection_ranges = Vec::new();
+        let mut old_folds = Vec::new();
+        let mut new_folds = Vec::new();
+
+        let mut selections = self.selections(app).iter().peekable();
+        let mut contiguous_selections = Vec::new();
+        while let Some(selection) = selections.next() {
+            // Accumulate contiguous regions of rows that we want to move.
+            contiguous_selections.push(selection.range(buffer));
+            let (mut buffer_rows, mut display_rows) =
+                selection.buffer_rows_for_display_rows(map, app);
+            while let Some(next_selection) = selections.peek() {
+                let (next_buffer_rows, next_display_rows) =
+                    next_selection.buffer_rows_for_display_rows(map, app);
+                if next_buffer_rows.start <= buffer_rows.end {
+                    buffer_rows.end = next_buffer_rows.end;
+                    display_rows.end = next_display_rows.end;
+                    contiguous_selections.push(next_selection.range(buffer));
+                    selections.next().unwrap();
+                } else {
+                    break;
+                }
+            }
+
+            // Cut the text from the selected rows and paste it at the end of the next line.
+            if display_rows.end <= map.max_point(app).row() {
+                let selection_row_start =
+                    Point::new(buffer_rows.start, 0).to_offset(buffer).unwrap();
+                let selection_row_end = Point::new(
+                    buffer_rows.end - 1,
+                    buffer.line_len(buffer_rows.end - 1).unwrap(),
+                )
+                .to_offset(buffer)
+                .unwrap();
+
+                let next_row_display_end = DisplayPoint::new(
+                    display_rows.end,
+                    map.line_len(display_rows.end, app).unwrap(),
+                );
+                let next_row_end = next_row_display_end
+                    .to_buffer_offset(map, Bias::Left, app)
+                    .unwrap();
+
+                let mut text = String::new();
+                text.push('\n');
+                text.extend(
+                    buffer
+                        .text_for_range(selection_row_start..selection_row_end)
+                        .unwrap(),
+                );
+                edits.push((selection_row_start..selection_row_end + 1, String::new()));
+                edits.push((next_row_end..next_row_end, text));
+
+                let row_delta = next_row_display_end
+                    .to_buffer_point(map, Bias::Right, app)
+                    .unwrap()
+                    .row
+                    - buffer_rows.end
+                    + 1;
+
+                // Move selections down.
+                for range in &mut contiguous_selections {
+                    range.start.row += row_delta;
+                    range.end.row += row_delta;
+                }
+
+                // Move folds down.
+                old_folds.push(selection_row_start..selection_row_end);
+                for fold in map
+                    .folds_in_range(selection_row_start..selection_row_end, app)
+                    .unwrap()
+                {
+                    let mut start = fold.start.to_point(buffer).unwrap();
+                    let mut end = fold.end.to_point(buffer).unwrap();
+                    start.row += row_delta;
+                    end.row += row_delta;
+                    new_folds.push(start..end);
+                }
+            }
+
+            new_selection_ranges.extend(contiguous_selections.drain(..));
+        }
+
+        self.unfold_ranges(old_folds, ctx);
+        self.buffer.update(ctx, |buffer, ctx| {
+            for (range, text) in edits.into_iter().rev() {
+                buffer.edit(Some(range), text, Some(ctx)).unwrap();
+            }
+        });
+        self.fold_ranges(new_folds, ctx);
+        self.select_ranges(new_selection_ranges, true, ctx);
+
+        self.end_transaction(ctx);
+    }
+
     pub fn cut(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
         self.start_transaction(ctx);
         let mut text = String::new();
@@ -754,27 +1298,23 @@ impl BufferView {
     }
 
     pub fn select_up(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
-        if self.single_line {
-            ctx.propagate_action();
-        } else {
-            let mut selections = self.selections(ctx.as_ref()).to_vec();
-            {
-                let app = ctx.as_ref();
-                let buffer = self.buffer.read(app);
-                let map = self.display_map.read(app);
-                for selection in &mut selections {
-                    let head = selection.head().to_display_point(map, app).unwrap();
-                    let (head, goal_column) =
-                        movement::up(map, head, selection.goal_column, app).unwrap();
-                    selection.set_head(&buffer, map.anchor_before(head, Bias::Left, app).unwrap());
-                    selection.goal_column = goal_column;
-                }
-            }
-            self.update_selections(selections, true, ctx);
-        }
-    }
-
-    pub fn move_down(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        let mut selections = self.selections(ctx.as_ref()).to_vec();
+        {
+            let app = ctx.as_ref();
+            let buffer = self.buffer.read(app);
+            let map = self.display_map.read(app);
+            for selection in &mut selections {
+                let head = selection.head().to_display_point(map, app).unwrap();
+                let (head, goal_column) =
+                    movement::up(map, head, selection.goal_column, app).unwrap();
+                selection.set_head(&buffer, map.anchor_before(head, Bias::Left, app).unwrap());
+                selection.goal_column = goal_column;
+            }
+        }
+        self.update_selections(selections, true, ctx);
+    }
+
+    pub fn move_down(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
         if self.single_line {
             ctx.propagate_action();
         } else {
@@ -803,24 +1343,238 @@ impl BufferView {
     }
 
     pub fn select_down(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
-        if self.single_line {
-            ctx.propagate_action();
-        } else {
-            let mut selections = self.selections(ctx.as_ref()).to_vec();
-            {
-                let app = ctx.as_ref();
-                let buffer = self.buffer.read(app);
-                let map = self.display_map.read(app);
-                for selection in &mut selections {
-                    let head = selection.head().to_display_point(map, app).unwrap();
-                    let (head, goal_column) =
-                        movement::down(map, head, selection.goal_column, app).unwrap();
-                    selection.set_head(&buffer, map.anchor_before(head, Bias::Right, app).unwrap());
-                    selection.goal_column = goal_column;
-                }
+        let mut selections = self.selections(ctx.as_ref()).to_vec();
+        {
+            let app = ctx.as_ref();
+            let buffer = self.buffer.read(app);
+            let map = self.display_map.read(app);
+            for selection in &mut selections {
+                let head = selection.head().to_display_point(map, app).unwrap();
+                let (head, goal_column) =
+                    movement::down(map, head, selection.goal_column, app).unwrap();
+                selection.set_head(&buffer, map.anchor_before(head, Bias::Right, app).unwrap());
+                selection.goal_column = goal_column;
+            }
+        }
+        self.update_selections(selections, true, ctx);
+    }
+
+    pub fn move_to_previous_word_boundary(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        let app = ctx.as_ref();
+        let mut selections = self.selections(app).to_vec();
+        {
+            let map = self.display_map.read(app);
+            for selection in &mut selections {
+                let head = selection.head().to_display_point(map, app).unwrap();
+                let new_head = movement::prev_word_boundary(map, head, app).unwrap();
+                let anchor = map.anchor_before(new_head, Bias::Left, app).unwrap();
+                selection.start = anchor.clone();
+                selection.end = anchor;
+                selection.reversed = false;
+                selection.goal_column = None;
+            }
+        }
+        self.update_selections(selections, true, ctx);
+    }
+
+    pub fn select_to_previous_word_boundary(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        let app = ctx.as_ref();
+        let mut selections = self.selections(app).to_vec();
+        {
+            let buffer = self.buffer.read(ctx);
+            let map = self.display_map.read(app);
+            for selection in &mut selections {
+                let head = selection.head().to_display_point(map, app).unwrap();
+                let new_head = movement::prev_word_boundary(map, head, app).unwrap();
+                let anchor = map.anchor_before(new_head, Bias::Left, app).unwrap();
+                selection.set_head(buffer, anchor);
+                selection.goal_column = None;
+            }
+        }
+        self.update_selections(selections, true, ctx);
+    }
+
+    pub fn delete_to_previous_word_boundary(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        self.start_transaction(ctx);
+        self.select_to_previous_word_boundary(&(), ctx);
+        self.backspace(&(), ctx);
+        self.end_transaction(ctx);
+    }
+
+    pub fn move_to_next_word_boundary(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        let app = ctx.as_ref();
+        let mut selections = self.selections(app).to_vec();
+        {
+            let map = self.display_map.read(app);
+            for selection in &mut selections {
+                let head = selection.head().to_display_point(map, app).unwrap();
+                let new_head = movement::next_word_boundary(map, head, app).unwrap();
+                let anchor = map.anchor_before(new_head, Bias::Left, app).unwrap();
+                selection.start = anchor.clone();
+                selection.end = anchor;
+                selection.reversed = false;
+                selection.goal_column = None;
+            }
+        }
+        self.update_selections(selections, true, ctx);
+    }
+
+    pub fn select_to_next_word_boundary(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        let app = ctx.as_ref();
+        let mut selections = self.selections(app).to_vec();
+        {
+            let buffer = self.buffer.read(ctx);
+            let map = self.display_map.read(app);
+            for selection in &mut selections {
+                let head = selection.head().to_display_point(map, app).unwrap();
+                let new_head = movement::next_word_boundary(map, head, app).unwrap();
+                let anchor = map.anchor_before(new_head, Bias::Left, app).unwrap();
+                selection.set_head(buffer, anchor);
+                selection.goal_column = None;
+            }
+        }
+        self.update_selections(selections, true, ctx);
+    }
+
+    pub fn delete_to_next_word_boundary(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        self.start_transaction(ctx);
+        self.select_to_next_word_boundary(&(), ctx);
+        self.delete(&(), ctx);
+        self.end_transaction(ctx);
+    }
+
+    pub fn move_to_beginning_of_line(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        let app = ctx.as_ref();
+        let mut selections = self.selections(app).to_vec();
+        {
+            let map = self.display_map.read(app);
+            for selection in &mut selections {
+                let head = selection.head().to_display_point(map, app).unwrap();
+                let new_head = movement::line_beginning(map, head, true, app).unwrap();
+                let anchor = map.anchor_before(new_head, Bias::Left, app).unwrap();
+                selection.start = anchor.clone();
+                selection.end = anchor;
+                selection.reversed = false;
+                selection.goal_column = None;
+            }
+        }
+        self.update_selections(selections, true, ctx);
+    }
+
+    pub fn select_to_beginning_of_line(
+        &mut self,
+        toggle_indent: &bool,
+        ctx: &mut ViewContext<Self>,
+    ) {
+        let app = ctx.as_ref();
+        let mut selections = self.selections(app).to_vec();
+        {
+            let buffer = self.buffer.read(ctx);
+            let map = self.display_map.read(app);
+            for selection in &mut selections {
+                let head = selection.head().to_display_point(map, app).unwrap();
+                let new_head = movement::line_beginning(map, head, *toggle_indent, app).unwrap();
+                let anchor = map.anchor_before(new_head, Bias::Left, app).unwrap();
+                selection.set_head(buffer, anchor);
+                selection.goal_column = None;
+            }
+        }
+        self.update_selections(selections, true, ctx);
+    }
+
+    pub fn delete_to_beginning_of_line(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        self.start_transaction(ctx);
+        self.select_to_beginning_of_line(&false, ctx);
+        self.backspace(&(), ctx);
+        self.end_transaction(ctx);
+    }
+
+    pub fn move_to_end_of_line(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        let app = ctx.as_ref();
+        let mut selections = self.selections(app).to_vec();
+        {
+            let map = self.display_map.read(app);
+            for selection in &mut selections {
+                let head = selection.head().to_display_point(map, app).unwrap();
+                let new_head = movement::line_end(map, head, app).unwrap();
+                let anchor = map.anchor_before(new_head, Bias::Left, app).unwrap();
+                selection.start = anchor.clone();
+                selection.end = anchor;
+                selection.reversed = false;
+                selection.goal_column = None;
+            }
+        }
+        self.update_selections(selections, true, ctx);
+    }
+
+    pub fn select_to_end_of_line(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        let app = ctx.as_ref();
+        let mut selections = self.selections(app).to_vec();
+        {
+            let buffer = self.buffer.read(ctx);
+            let map = self.display_map.read(app);
+            for selection in &mut selections {
+                let head = selection.head().to_display_point(map, app).unwrap();
+                let new_head = movement::line_end(map, head, app).unwrap();
+                let anchor = map.anchor_before(new_head, Bias::Left, app).unwrap();
+                selection.set_head(buffer, anchor);
+                selection.goal_column = None;
             }
-            self.update_selections(selections, true, ctx);
         }
+        self.update_selections(selections, true, ctx);
+    }
+
+    pub fn delete_to_end_of_line(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        self.start_transaction(ctx);
+        self.select_to_end_of_line(&(), ctx);
+        self.delete(&(), ctx);
+        self.end_transaction(ctx);
+    }
+
+    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 selection = Selection {
+            start: cursor.clone(),
+            end: cursor,
+            reversed: false,
+            goal_column: None,
+        };
+        self.update_selections(vec![selection], true, ctx);
+    }
+
+    pub fn select_to_beginning(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        let mut selection = self.selections(ctx.as_ref()).last().unwrap().clone();
+        selection.set_head(self.buffer.read(ctx), Anchor::Start);
+        self.update_selections(vec![selection], true, ctx);
+    }
+
+    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 selection = Selection {
+            start: cursor.clone(),
+            end: cursor,
+            reversed: false,
+            goal_column: None,
+        };
+        self.update_selections(vec![selection], true, ctx);
+    }
+
+    pub fn select_to_end(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        let mut selection = self.selections(ctx.as_ref()).last().unwrap().clone();
+        selection.set_head(self.buffer.read(ctx), Anchor::End);
+        self.update_selections(vec![selection], true, ctx);
+    }
+
+    pub fn select_all(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
+        let selection = Selection {
+            start: Anchor::Start,
+            end: Anchor::End,
+            reversed: false,
+            goal_column: None,
+        };
+        self.update_selections(vec![selection], false, ctx);
     }
 
     pub fn selections_in_range<'a>(
@@ -944,15 +1698,19 @@ impl BufferView {
         let app = ctx.as_ref();
         let map = self.display_map.read(app);
         for selection in self.selections(app) {
-            let (start, end) = selection.display_range(map, app).sorted();
-            let buffer_start_row = start.to_buffer_point(map, Bias::Left, app).unwrap().row;
+            let range = selection.display_range(map, app).sorted();
+            let buffer_start_row = range
+                .start
+                .to_buffer_point(map, Bias::Left, app)
+                .unwrap()
+                .row;
 
-            for row in (0..=end.row()).rev() {
+            for row in (0..=range.end.row()).rev() {
                 if self.is_line_foldable(row, app) && !map.is_line_folded(row) {
                     let fold_range = self.foldable_range_for_line(row, app).unwrap();
                     if fold_range.end.row >= buffer_start_row {
                         fold_ranges.push(fold_range);
-                        if row <= start.row() {
+                        if row <= range.start.row() {
                             break;
                         }
                     }
@@ -960,12 +1718,7 @@ impl BufferView {
             }
         }
 
-        if !fold_ranges.is_empty() {
-            self.display_map.update(ctx, |map, ctx| {
-                map.fold(fold_ranges, ctx).unwrap();
-            });
-            *self.autoscroll_requested.lock() = true;
-        }
+        self.fold_ranges(fold_ranges, ctx);
     }
 
     pub fn unfold(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
@@ -978,32 +1731,29 @@ impl BufferView {
             .selections(app)
             .iter()
             .map(|s| {
-                let (start, end) = s.display_range(map, app).sorted();
-                let mut start = start.to_buffer_point(map, Bias::Left, app).unwrap();
-                let mut end = end.to_buffer_point(map, Bias::Left, app).unwrap();
+                let range = s.display_range(map, app).sorted();
+                let mut start = range.start.to_buffer_point(map, Bias::Left, app).unwrap();
+                let mut end = range.end.to_buffer_point(map, Bias::Left, app).unwrap();
                 start.column = 0;
                 end.column = buffer.line_len(end.row).unwrap();
                 start..end
             })
             .collect::<Vec<_>>();
-
-        self.display_map.update(ctx, |map, ctx| {
-            map.unfold(ranges, ctx).unwrap();
-        });
-        *self.autoscroll_requested.lock() = true;
+        self.unfold_ranges(ranges, ctx);
     }
 
     fn is_line_foldable(&self, display_row: u32, app: &AppContext) -> bool {
+        let map = self.display_map.read(app);
         let max_point = self.max_point(app);
         if display_row >= max_point.row() {
             false
         } else {
-            let (start_indent, is_blank) = self.line_indent(display_row, app).unwrap();
+            let (start_indent, is_blank) = map.line_indent(display_row, app).unwrap();
             if is_blank {
                 false
             } else {
                 for display_row in display_row + 1..=max_point.row() {
-                    let (indent, is_blank) = self.line_indent(display_row, app).unwrap();
+                    let (indent, is_blank) = map.line_indent(display_row, app).unwrap();
                     if !is_blank {
                         return indent > start_indent;
                     }

zed/src/editor/display_map/fold_map.rs 🔗

@@ -89,6 +89,33 @@ impl FoldMap {
         DisplayPoint(self.transforms.summary().display.rightmost_point)
     }
 
+    pub fn folds_in_range<T>(&self, range: Range<T>, app: &AppContext) -> Result<&[Range<Anchor>]>
+    where
+        T: ToOffset,
+    {
+        let buffer = self.buffer.read(app);
+        let range = buffer.anchor_before(range.start)?..buffer.anchor_before(range.end)?;
+        let mut start_ix = find_insertion_index(&self.folds, |probe| probe.cmp(&range, buffer))?;
+        let mut end_ix = start_ix;
+
+        for fold in self.folds[..start_ix].iter().rev() {
+            if fold.end.cmp(&range.start, buffer)? == Ordering::Greater {
+                start_ix -= 1;
+            } else {
+                break;
+            }
+        }
+        for fold in &self.folds[end_ix..] {
+            if range.end.cmp(&fold.start, buffer)? == Ordering::Greater {
+                end_ix += 1;
+            } else {
+                break;
+            }
+        }
+
+        Ok(&self.folds[start_ix..end_ix])
+    }
+
     pub fn fold<T: ToOffset>(
         &mut self,
         ranges: impl IntoIterator<Item = Range<T>>,
@@ -169,6 +196,13 @@ impl FoldMap {
         false
     }
 
+    pub fn to_buffer_offset(&self, point: DisplayPoint, app: &AppContext) -> Result<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(app))
+    }
+
     pub fn to_display_offset(
         &self,
         point: DisplayPoint,
@@ -235,7 +269,7 @@ impl FoldMap {
                     let next_edit = edits.next().unwrap();
                     delta += next_edit.delta();
 
-                    if next_edit.old_range.end > edit.old_range.end {
+                    if next_edit.old_range.end >= edit.old_range.end {
                         edit.old_range.end = next_edit.old_range.end;
                         cursor.seek(&edit.old_range.end, SeekBias::Right);
                         cursor.next();
@@ -415,7 +449,7 @@ impl<'a> Iterator for Chars<'a> {
             return Some(c);
         }
 
-        if self.offset == self.cursor.end().display.chars {
+        while self.offset == self.cursor.end().display.chars && self.cursor.item().is_some() {
             self.cursor.next();
         }
 
@@ -519,6 +553,50 @@ mod tests {
         });
     }
 
+    #[test]
+    fn test_adjacent_folds() {
+        App::test((), |app| {
+            let buffer = app.add_model(|_| Buffer::new(0, "abcdefghijkl"));
+
+            {
+                let mut map = FoldMap::new(buffer.clone(), app.as_ref());
+
+                map.fold(vec![5..8], app.as_ref()).unwrap();
+                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.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.check_invariants(app.as_ref());
+                assert_eq!(map.text(app.as_ref()), "…b…kl");
+            }
+
+            {
+                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.check_invariants(app.as_ref());
+                assert_eq!(map.text(app.as_ref()), "…fghijkl");
+
+                // Edit within one of the folds.
+                let edits = buffer.update(app, |buffer, ctx| {
+                    let version = buffer.version();
+                    buffer.edit(vec![0..1], "12345", Some(ctx)).unwrap();
+                    buffer.edits_since(version).collect::<Vec<_>>()
+                });
+                map.apply_edits(edits.as_slice(), app.as_ref()).unwrap();
+                map.check_invariants(app.as_ref());
+                assert_eq!(map.text(app.as_ref()), "12345…fghijkl");
+            }
+        });
+    }
+
     #[test]
     fn test_overlapping_folds() {
         App::test((), |app| {
@@ -577,6 +655,9 @@ mod tests {
         let iterations = env::var("ITERATIONS")
             .map(|i| i.parse().expect("invalid `ITERATIONS` variable"))
             .unwrap_or(100);
+        let operations = env::var("OPERATIONS")
+            .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
+            .unwrap_or(10);
         let seed_range = if let Ok(seed) = env::var("SEED") {
             let seed = seed.parse().expect("invalid `SEED` variable");
             seed..seed + 1
@@ -596,66 +677,70 @@ mod tests {
                 });
                 let mut map = FoldMap::new(buffer.clone(), app.as_ref());
 
-                {
-                    let buffer = buffer.read(app);
-
-                    let fold_count = rng.gen_range(0..10);
-                    let mut fold_ranges: Vec<Range<usize>> = Vec::new();
-                    for _ in 0..fold_count {
-                        let end = rng.gen_range(0..buffer.len() + 1);
-                        let start = rng.gen_range(0..end + 1);
-                        fold_ranges.push(start..end);
+                for _ in 0..operations {
+                    log::info!("text: {:?}", buffer.read(app).text());
+                    {
+                        let buffer = buffer.read(app);
+
+                        let fold_count = rng.gen_range(0..=2);
+                        let mut fold_ranges: Vec<Range<usize>> = Vec::new();
+                        for _ in 0..fold_count {
+                            let end = rng.gen_range(0..buffer.len() + 1);
+                            let start = rng.gen_range(0..end + 1);
+                            fold_ranges.push(start..end);
+                        }
+                        log::info!("folding {:?}", fold_ranges);
+                        map.fold(fold_ranges.clone(), app.as_ref()).unwrap();
+                        map.check_invariants(app.as_ref());
+
+                        let mut expected_text = buffer.text();
+                        for fold_range in map.merged_fold_ranges(app.as_ref()).into_iter().rev() {
+                            expected_text.replace_range(fold_range.start..fold_range.end, "…");
+                        }
+                        assert_eq!(map.text(app.as_ref()), expected_text);
+
+                        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());
+                            assert!(map.is_line_folded(display_point.row()));
+                        }
                     }
 
-                    map.fold(fold_ranges, app.as_ref()).unwrap();
+                    let edits = buffer.update(app, |buffer, ctx| {
+                        let start_version = buffer.version.clone();
+                        let edit_count = rng.gen_range(0..=2);
+                        buffer.randomly_edit(&mut rng, edit_count, Some(ctx));
+                        buffer.edits_since(start_version).collect::<Vec<_>>()
+                    });
+                    log::info!("editing {:?}", edits);
+                    map.apply_edits(&edits, app.as_ref()).unwrap();
+                    map.check_invariants(app.as_ref());
 
+                    let buffer = map.buffer.read(app);
                     let mut expected_text = buffer.text();
+                    let mut expected_buffer_rows = Vec::new();
+                    let mut next_row = buffer.max_point().row;
                     for fold_range in map.merged_fold_ranges(app.as_ref()).into_iter().rev() {
+                        let fold_start = buffer.point_for_offset(fold_range.start).unwrap();
+                        let fold_end = buffer.point_for_offset(fold_range.end).unwrap();
+                        expected_buffer_rows.extend((fold_end.row + 1..=next_row).rev());
+                        next_row = fold_start.row;
+
                         expected_text.replace_range(fold_range.start..fold_range.end, "…");
                     }
+                    expected_buffer_rows.extend((0..=next_row).rev());
+                    expected_buffer_rows.reverse();
 
                     assert_eq!(map.text(app.as_ref()), expected_text);
 
-                    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());
-                        assert!(map.is_line_folded(display_point.row()));
+                    for (idx, buffer_row) in expected_buffer_rows.iter().enumerate() {
+                        let display_row = map.to_display_point(Point::new(*buffer_row, 0)).row();
+                        assert_eq!(
+                            map.buffer_rows(display_row).unwrap().collect::<Vec<_>>(),
+                            expected_buffer_rows[idx..],
+                        );
                     }
                 }
-
-                let edits = buffer.update(app, |buffer, ctx| {
-                    let start_version = buffer.version.clone();
-                    let edit_count = rng.gen_range(1..10);
-                    buffer.randomly_edit(&mut rng, edit_count, Some(ctx));
-                    buffer.edits_since(start_version).collect::<Vec<_>>()
-                });
-
-                map.apply_edits(&edits, app.as_ref()).unwrap();
-
-                let buffer = map.buffer.read(app);
-                let mut expected_text = buffer.text();
-                let mut expected_buffer_rows = Vec::new();
-                let mut next_row = buffer.max_point().row;
-                for fold_range in map.merged_fold_ranges(app.as_ref()).into_iter().rev() {
-                    let fold_start = buffer.point_for_offset(fold_range.start).unwrap();
-                    let fold_end = buffer.point_for_offset(fold_range.end).unwrap();
-                    expected_buffer_rows.extend((fold_end.row + 1..=next_row).rev());
-                    next_row = fold_start.row;
-
-                    expected_text.replace_range(fold_range.start..fold_range.end, "…");
-                }
-                expected_buffer_rows.extend((0..=next_row).rev());
-                expected_buffer_rows.reverse();
-
-                assert_eq!(map.text(app.as_ref()), expected_text);
-
-                for (idx, buffer_row) in expected_buffer_rows.iter().enumerate() {
-                    let display_row = map.to_display_point(Point::new(*buffer_row, 0)).row();
-                    assert_eq!(
-                        map.buffer_rows(display_row).unwrap().collect::<Vec<_>>(),
-                        expected_buffer_rows[idx..],
-                    );
-                }
             });
         }
     }
@@ -721,5 +806,13 @@ mod tests {
             }
             merged_ranges
         }
+
+        fn check_invariants(&self, app: &AppContext) {
+            assert_eq!(
+                self.transforms.summary().buffer.chars,
+                self.buffer.read(app).len(),
+                "transform tree does not match buffer's length"
+            );
+        }
     }
 }

zed/src/editor/display_map/mod.rs 🔗

@@ -34,6 +34,13 @@ impl DisplayMap {
         }
     }
 
+    pub fn folds_in_range<T>(&self, range: Range<T>, app: &AppContext) -> Result<&[Range<Anchor>]>
+    where
+        T: ToOffset,
+    {
+        self.fold_map.folds_in_range(range, app)
+    }
+
     pub fn fold<T: ToOffset>(
         &mut self,
         ranges: impl IntoIterator<Item = Range<T>>,
@@ -67,6 +74,20 @@ impl DisplayMap {
         Ok(chars.take_while(|c| *c != '\n').collect())
     }
 
+    pub fn line_indent(&self, display_row: u32, app: &AppContext) -> Result<(u32, bool)> {
+        let mut indent = 0;
+        let mut is_blank = true;
+        for c in self.chars_at(DisplayPoint::new(display_row, 0), app)? {
+            if c == ' ' {
+                indent += 1;
+            } else {
+                is_blank = c == '\n';
+                break;
+            }
+        }
+        Ok((indent, is_blank))
+    }
+
     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) = point.collapse_tabs(self, Bias::Left, app)?;
@@ -165,6 +186,11 @@ impl DisplayPoint {
             .to_buffer_point(self.collapse_tabs(map, bias, app)?.0))
     }
 
+    pub fn to_buffer_offset(self, map: &DisplayMap, bias: Bias, app: &AppContext) -> Result<usize> {
+        map.fold_map
+            .to_buffer_offset(self.collapse_tabs(map, bias, app)?.0, app)
+    }
+
     fn expand_tabs(mut self, map: &DisplayMap, app: &AppContext) -> Result<Self> {
         let chars = map
             .fold_map

zed/src/editor/mod.rs 🔗

@@ -12,14 +12,11 @@ use display_map::*;
 use std::{cmp, ops::Range};
 
 trait RangeExt<T> {
-    fn sorted(&self) -> (T, T);
+    fn sorted(&self) -> Range<T>;
 }
 
 impl<T: Ord + Clone> RangeExt<T> for Range<T> {
-    fn sorted(&self) -> (T, T) {
-        (
-            cmp::min(&self.start, &self.end).clone(),
-            cmp::max(&self.start, &self.end).clone(),
-        )
+    fn sorted(&self) -> Self {
+        cmp::min(&self.start, &self.end).clone()..cmp::max(&self.start, &self.end).clone()
     }
 }

zed/src/editor/movement.rs 🔗

@@ -58,3 +58,98 @@ pub fn down(
 
     Ok((point, goal_column))
 }
+
+pub fn line_beginning(
+    map: &DisplayMap,
+    point: DisplayPoint,
+    toggle_indent: bool,
+    app: &AppContext,
+) -> Result<DisplayPoint> {
+    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 {
+        Ok(DisplayPoint::new(point.row(), 0))
+    }
+}
+
+pub fn line_end(map: &DisplayMap, point: DisplayPoint, app: &AppContext) -> Result<DisplayPoint> {
+    Ok(DisplayPoint::new(
+        point.row(),
+        map.line_len(point.row(), app)?,
+    ))
+}
+
+pub fn prev_word_boundary(
+    map: &DisplayMap,
+    point: DisplayPoint,
+    app: &AppContext,
+) -> Result<DisplayPoint> {
+    if point.column() == 0 {
+        if point.row() == 0 {
+            Ok(DisplayPoint::new(0, 0))
+        } else {
+            let row = point.row() - 1;
+            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.chars_at(boundary, app)? {
+            if column >= point.column() {
+                break;
+            }
+
+            if prev_c.is_none() || char_kind(prev_c.unwrap()) != char_kind(c) {
+                *boundary.column_mut() = column;
+            }
+
+            prev_c = Some(c);
+            column += 1;
+        }
+        Ok(boundary)
+    }
+}
+
+pub fn next_word_boundary(
+    map: &DisplayMap,
+    mut point: DisplayPoint,
+    app: &AppContext,
+) -> Result<DisplayPoint> {
+    let mut prev_c = None;
+    for c in map.chars_at(point, app)? {
+        if prev_c.is_some() && (c == '\n' || char_kind(prev_c.unwrap()) != char_kind(c)) {
+            break;
+        }
+
+        if c == '\n' {
+            *point.row_mut() += 1;
+            *point.column_mut() = 0;
+        } else {
+            *point.column_mut() += 1;
+        }
+        prev_c = Some(c);
+    }
+    Ok(point)
+}
+
+#[derive(Copy, Clone, Eq, PartialEq)]
+enum CharKind {
+    Newline,
+    Whitespace,
+    Punctuation,
+    Word,
+}
+
+fn char_kind(c: char) -> CharKind {
+    if c == '\n' {
+        CharKind::Newline
+    } else if c.is_whitespace() {
+        CharKind::Whitespace
+    } else if c.is_alphanumeric() || c == '_' {
+        CharKind::Word
+    } else {
+        CharKind::Punctuation
+    }
+}