@@ -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;
}