From 324a6ff7ae7b9879ec164c1b8267b882f8f17008 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 27 Apr 2021 13:23:41 +0200 Subject: [PATCH 01/15] Implement `select_all` for buffer --- zed/src/editor/buffer_view.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 77cf3bf7799520a13588b6f1aef1e7e4e5d8aaee..178e1d2ca8c7967854a8fb9ceb0c690879ddfddb 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -44,6 +44,7 @@ pub fn init(app: &mut MutableAppContext) { 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("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")), @@ -73,6 +74,7 @@ pub fn init(app: &mut MutableAppContext) { 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_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); @@ -816,6 +818,16 @@ impl BufferView { } } + pub fn select_all(&mut self, _: &(), ctx: &mut ViewContext) { + 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>( &'a self, range: Range, @@ -1844,6 +1856,20 @@ mod tests { }); } + #[test] + fn test_select_all() { + App::test((), |app| { + let buffer = app.add_model(|ctx| Buffer::new(0, "abc\nde\nfgh", ctx)); + let settings = settings::channel(&app.font_cache()).unwrap().1; + let (_, view) = app.add_window(|ctx| BufferView::for_buffer(buffer, settings, ctx)); + view.update(app, |b, ctx| b.select_all(&(), ctx)); + assert_eq!( + view.read(app).selection_ranges(app.as_ref()), + &[DisplayPoint::new(0, 0)..DisplayPoint::new(2, 3)] + ); + }); + } + impl BufferView { fn selection_ranges(&self, app: &AppContext) -> Vec> { self.selections_in_range(DisplayPoint::zero()..self.max_point(app), app) From b7c30eba3089aa58c5d8fcfb0b38e7b773ff9b57 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 27 Apr 2021 13:29:21 +0200 Subject: [PATCH 02/15] Implemente `delete` for buffer --- zed/src/editor/buffer_view.rs | 69 +++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 178e1d2ca8c7967854a8fb9ceb0c690879ddfddb..1da58748d392be2e3d6f5ca677be014a8d6d6f0f 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -30,6 +30,7 @@ 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("delete", "buffer:delete", Some("BufferView")), Binding::new("enter", "buffer:newline", Some("BufferView")), Binding::new("cmd-x", "buffer:cut", Some("BufferView")), Binding::new("cmd-c", "buffer:copy", Some("BufferView")), @@ -61,6 +62,7 @@ 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:cut", BufferView::cut); app.add_action("buffer:copy", BufferView::copy); app.add_action("buffer:paste", BufferView::paste); @@ -489,6 +491,36 @@ impl BufferView { self.end_transaction(ctx); } + pub fn delete(&mut self, _: &(), ctx: &mut ViewContext) { + 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 { + if selection.range(buffer).is_empty() { + 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 cut(&mut self, _: &(), ctx: &mut ViewContext) { self.start_transaction(ctx); let mut text = String::new(); @@ -1715,6 +1747,43 @@ mod tests { }) } + #[test] + fn test_delete() { + App::test((), |app| { + let buffer = app.add_model(|ctx| { + Buffer::new( + 0, + "one two three\nfour five six\nseven eight nine\nten\n", + ctx, + ) + }); + let settings = settings::channel(&app.font_cache()).unwrap().1; + let (_, view) = + app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), settings, ctx)); + + view.update(app, |view, ctx| { + view.select_display_ranges( + &[ + // an empty selection - the following character is deleted + DisplayPoint::new(0, 2)..DisplayPoint::new(0, 2), + // one character selected - it is deleted + DisplayPoint::new(1, 3)..DisplayPoint::new(1, 4), + // a line suffix selected - it is deleted + DisplayPoint::new(2, 6)..DisplayPoint::new(3, 0), + ], + ctx, + ) + .unwrap(); + view.delete(&(), ctx); + }); + + assert_eq!( + buffer.read(app).text(), + "on two three\nfou five six\nseven ten\n" + ); + }) + } + #[test] fn test_clipboard() { App::test((), |app| { From 45452bbb3a0bd027b999c9456070a02ed9eab8ab Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 27 Apr 2021 13:35:41 +0200 Subject: [PATCH 03/15] Implement `move_to_beginning` and `move_to_end` for buffer --- zed/src/editor/buffer_view.rs | 77 ++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 15 deletions(-) diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 1da58748d392be2e3d6f5ca677be014a8d6d6f0f..3f405f31a414a94d6d25438137de382e59a3dc94 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -41,6 +41,8 @@ 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("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")), @@ -72,6 +74,8 @@ 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_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); @@ -850,6 +854,26 @@ impl BufferView { } } + pub fn move_to_beginning(&mut self, _: &(), ctx: &mut ViewContext) { + let selection = Selection { + start: Anchor::Start, + end: Anchor::Start, + reversed: false, + goal_column: None, + }; + self.update_selections(vec![selection], true, ctx); + } + + pub fn move_to_end(&mut self, _: &(), ctx: &mut ViewContext) { + let selection = Selection { + start: Anchor::End, + end: Anchor::End, + reversed: false, + goal_column: None, + }; + self.update_selections(vec![selection], true, ctx); + } + pub fn select_all(&mut self, _: &(), ctx: &mut ViewContext) { let selection = Selection { start: Anchor::Start, @@ -1442,7 +1466,6 @@ impl workspace::ItemView for BufferView { mod tests { use super::*; use crate::{editor::Point, settings, test::sample_text}; - use anyhow::Error; use gpui::App; use unindent::Unindent; @@ -1674,7 +1697,7 @@ mod tests { } #[test] - fn test_move_cursor() -> Result<()> { + fn test_move_cursor() { App::test((), |app| { let buffer = app.add_model(|ctx| Buffer::new(0, sample_text(6, 6), ctx)); let settings = settings::channel(&app.font_cache()).unwrap().1; @@ -1682,15 +1705,17 @@ mod tests { app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), settings, ctx)); buffer.update(app, |buffer, ctx| { - buffer.edit( - vec![ - Point::new(1, 0)..Point::new(1, 0), - Point::new(1, 1)..Point::new(1, 1), - ], - "\t", - Some(ctx), - ) - })?; + buffer + .edit( + vec![ + Point::new(1, 0)..Point::new(1, 0), + Point::new(1, 1)..Point::new(1, 1), + ], + "\t", + Some(ctx), + ) + .unwrap(); + }); view.update(app, |view, ctx| { view.move_down(&(), ctx); @@ -1698,16 +1723,38 @@ mod tests { view.selection_ranges(ctx.as_ref()), &[DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0)] ); + view.move_right(&(), ctx); assert_eq!( view.selection_ranges(ctx.as_ref()), &[DisplayPoint::new(1, 4)..DisplayPoint::new(1, 4)] ); - Ok::<(), Error>(()) - })?; - Ok(()) - }) + view.move_left(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0)] + ); + + view.move_up(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)] + ); + + view.move_to_end(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[DisplayPoint::new(5, 6)..DisplayPoint::new(5, 6)] + ); + + view.move_to_beginning(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)] + ); + }); + }); } #[test] From c524cc4d61c46d0ed1cdd3cf89746b34ab338a2c Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 27 Apr 2021 13:47:12 +0200 Subject: [PATCH 04/15] Implement `select_to_beginning` and `select_to_end` for buffer --- zed/src/editor/buffer_view.rs | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 3f405f31a414a94d6d25438137de382e59a3dc94..2b044a3ed0b000a78bb2217f6b469e90d0f42bbc 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -47,6 +47,12 @@ pub fn init(app: &mut MutableAppContext) { 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( + "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")), @@ -80,6 +86,11 @@ pub fn init(app: &mut MutableAppContext) { 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_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); @@ -864,6 +875,12 @@ impl BufferView { self.update_selections(vec![selection], true, ctx); } + pub fn select_to_beginning(&mut self, _: &(), ctx: &mut ViewContext) { + 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) { let selection = Selection { start: Anchor::End, @@ -874,6 +891,12 @@ impl BufferView { self.update_selections(vec![selection], true, ctx); } + pub fn select_to_end(&mut self, _: &(), ctx: &mut ViewContext) { + 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) { let selection = Selection { start: Anchor::Start, @@ -1753,6 +1776,23 @@ mod tests { view.selection_ranges(ctx.as_ref()), &[DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)] ); + + view.select_display_ranges( + &[DisplayPoint::new(0, 1)..DisplayPoint::new(0, 2)], + ctx, + ) + .unwrap(); + view.select_to_beginning(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[DisplayPoint::new(0, 1)..DisplayPoint::new(0, 0)] + ); + + view.select_to_end(&(), ctx); + assert_eq!( + view.selection_ranges(ctx.as_ref()), + &[DisplayPoint::new(0, 1)..DisplayPoint::new(5, 6)] + ); }); }); } From 21442bd2b7366997d7a138dbcf3a05073d69e0e9 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 27 Apr 2021 13:49:02 +0200 Subject: [PATCH 05/15] Don't propagate action when selecting up/down in single-line editors --- zed/src/editor/buffer_view.rs | 56 +++++++++++++++-------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 2b044a3ed0b000a78bb2217f6b469e90d0f42bbc..87d8fd9c81b60ce41da59d5df5481ff291c3f0bd 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -796,24 +796,20 @@ impl BufferView { } pub fn select_up(&mut self, _: &(), ctx: &mut ViewContext) { - 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; - } + 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); } + self.update_selections(selections, true, ctx); } pub fn move_down(&mut self, _: &(), ctx: &mut ViewContext) { @@ -845,24 +841,20 @@ impl BufferView { } pub fn select_down(&mut self, _: &(), ctx: &mut ViewContext) { - 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); } + self.update_selections(selections, true, ctx); } pub fn move_to_beginning(&mut self, _: &(), ctx: &mut ViewContext) { From 834602ef67670887299fe19d3024b794e254d718 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 27 Apr 2021 17:49:26 +0200 Subject: [PATCH 06/15] Implement `delete_line` for buffer This still needs unit tests. --- zed/src/editor/buffer_view.rs | 70 +++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 87d8fd9c81b60ce41da59d5df5481ff291c3f0bd..0bb47c8966a303d60f86498dc0a064431ece9afa 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -32,6 +32,7 @@ pub fn init(app: &mut MutableAppContext) { Binding::new("backspace", "buffer:backspace", Some("BufferView")), Binding::new("delete", "buffer:delete", Some("BufferView")), Binding::new("enter", "buffer:newline", Some("BufferView")), + Binding::new("ctrl-shift-K", "buffer:delete_line", 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")), @@ -71,6 +72,7 @@ pub fn init(app: &mut MutableAppContext) { 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:cut", BufferView::cut); app.add_action("buffer:copy", BufferView::copy); app.add_action("buffer:paste", BufferView::paste); @@ -536,6 +538,74 @@ impl BufferView { self.end_transaction(ctx); } + pub fn delete_line(&mut self, _: &(), ctx: &mut ViewContext) { + self.start_transaction(ctx); + + let app = ctx.as_ref(); + let buffer = self.buffer.read(app); + + // Accumulate contiguous regions of rows that we want to delete. + let mut row_ranges: Vec<(u32, Range)> = Vec::new(); + for selection in self.selections(app) { + let start = selection.start.to_point(buffer).unwrap(); + let end = selection.end.to_point(buffer).unwrap(); + let goal_column = if selection.reversed { + start.column + } else { + end.column + }; + + if let Some((_, last_row_range)) = row_ranges.last_mut() { + if start.row <= last_row_range.end { + *last_row_range = last_row_range.start..start.row + 1; + } else { + row_ranges.push((goal_column, start.row..end.row + 1)); + } + } else { + row_ranges.push((goal_column, start.row..end.row + 1)); + } + } + + let mut edit_ranges = Vec::new(); + let mut new_selections = Vec::new(); + for (goal_column, range) in row_ranges { + let mut start = Point::new(range.start, 0).to_offset(buffer).unwrap(); + let end; + let mut cursor; + + if let Ok(end_offset) = Point::new(range.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. + end = end_offset; + cursor = Point::new(range.end, goal_column); + } 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. + start = start.saturating_sub(1); + end = buffer.len(); + cursor = Point::new(range.start.saturating_sub(1), goal_column); + } + // We tried to maintain the column of the original cursors but the new lines may be + // shorter, so clip the new cursor's column. + cursor.column = cmp::min(cursor.column, buffer.line_len(cursor.row).unwrap()); + let cursor = buffer.anchor_before(cursor).unwrap(); + + edit_ranges.push(start..end); + new_selections.push(Selection { + start: cursor.clone(), + end: cursor, + reversed: false, + goal_column: None, + }); + } + + self.update_selections(new_selections, true, ctx); + self.buffer + .update(ctx, |buffer, ctx| buffer.edit(edit_ranges, "", Some(ctx))) + .unwrap(); + self.end_transaction(ctx); + } + pub fn cut(&mut self, _: &(), ctx: &mut ViewContext) { self.start_transaction(ctx); let mut text = String::new(); From ccd244bac7e7b91822ed6ab79dcd6d6dd4053851 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 27 Apr 2021 19:28:00 +0200 Subject: [PATCH 07/15] WIP: start on a test for `delete_line` Discovered a bug that's highlighted by the test that causes selections to not be in the right order. --- zed/src/editor/buffer_view.rs | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 0bb47c8966a303d60f86498dc0a064431ece9afa..c7c02650f78df5c89ccb12619cc6edcdaf7fbb91 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -1933,6 +1933,47 @@ mod tests { }) } + #[test] + fn test_delete_lines() { + App::test((), |app| { + let settings = settings::channel(&app.font_cache()).unwrap().1; + let buffer = app.add_model(|ctx| Buffer::new(0, "abc\ndef\nghi\n", ctx)); + let (_, view) = app.add_window(|ctx| BufferView::for_buffer(buffer, settings, ctx)); + view.update(app, |view, ctx| { + view.select_display_ranges( + &[ + DisplayPoint::new(0, 1)..DisplayPoint::new(0, 1), + DisplayPoint::new(1, 0)..DisplayPoint::new(1, 1), + DisplayPoint::new(3, 0)..DisplayPoint::new(3, 0), + ], + ctx, + ) + .unwrap(); + view.delete_line(&(), ctx); + }); + assert_eq!(view.read(app).text(app.as_ref()), "ghi"); + assert_eq!( + view.read(app).selection_ranges(app.as_ref()), + vec![ + DisplayPoint::new(0, 1)..DisplayPoint::new(0, 1), + DisplayPoint::new(0, 3)..DisplayPoint::new(0, 3) + ] + ); + + // view.undo(&(), ctx); + // view.select_display_ranges( + // &[ + // DisplayPoint::new(0, 1)..DisplayPoint::new(0, 1), + // DisplayPoint::new(1, 0)..DisplayPoint::new(1, 1), + // DisplayPoint::new(3, 0)..DisplayPoint::new(3, 0), + // ], + // ctx, + // ) + // .unwrap(); + // }); + }); + } + #[test] fn test_clipboard() { App::test((), |app| { From cee95091272c56a9a2feda55f41876baa4476941 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 28 Apr 2021 09:53:25 +0200 Subject: [PATCH 08/15] Fix `delete_line` test --- zed/src/editor/buffer_view.rs | 82 ++++++++++++++++------------------- 1 file changed, 37 insertions(+), 45 deletions(-) diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index c7c02650f78df5c89ccb12619cc6edcdaf7fbb91..80f4533274e69e62819f5e0b58d46846392524ec 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -544,61 +544,65 @@ impl BufferView { let app = ctx.as_ref(); let buffer = self.buffer.read(app); - // Accumulate contiguous regions of rows that we want to delete. - let mut row_ranges: Vec<(u32, Range)> = Vec::new(); - for selection in self.selections(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 start = selection.start.to_point(buffer).unwrap(); - let end = selection.end.to_point(buffer).unwrap(); + let mut end = selection.end.to_point(buffer).unwrap(); let goal_column = if selection.reversed { start.column } else { end.column }; - if let Some((_, last_row_range)) = row_ranges.last_mut() { - if start.row <= last_row_range.end { - *last_row_range = last_row_range.start..start.row + 1; + // Accumulate contiguous regions of rows that we want to delete. + while let Some(next_selection) = selections.peek() { + let next_start = next_selection.start.to_point(buffer).unwrap(); + if next_start.row <= end.row + 1 { + end = next_selection.end.to_point(buffer).unwrap(); + selections.next().unwrap(); } else { - row_ranges.push((goal_column, start.row..end.row + 1)); + break; } - } else { - row_ranges.push((goal_column, start.row..end.row + 1)); } - } - let mut edit_ranges = Vec::new(); - let mut new_selections = Vec::new(); - for (goal_column, range) in row_ranges { - let mut start = Point::new(range.start, 0).to_offset(buffer).unwrap(); - let end; + let mut edit_start = Point::new(start.row, 0).to_offset(buffer).unwrap(); + let edit_end; let mut cursor; - if let Ok(end_offset) = Point::new(range.end, 0).to_offset(buffer) { + if let Ok(end_offset) = Point::new(end.row + 1, 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. - end = end_offset; - cursor = Point::new(range.end, goal_column); + edit_end = end_offset; + cursor = Point::new(end.row + 1, goal_column); } 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. - start = start.saturating_sub(1); - end = buffer.len(); - cursor = Point::new(range.start.saturating_sub(1), goal_column); + edit_start = edit_start.saturating_sub(1); + edit_end = buffer.len(); + cursor = Point::new(start.row.saturating_sub(1), goal_column); } - // We tried to maintain the column of the original cursors but the new lines may be + // We tried to maintain the column of the original cursor but the new line may be // shorter, so clip the new cursor's column. cursor.column = cmp::min(cursor.column, buffer.line_len(cursor.row).unwrap()); - let cursor = buffer.anchor_before(cursor).unwrap(); - edit_ranges.push(start..end); - new_selections.push(Selection { - start: cursor.clone(), - end: cursor, - reversed: false, - goal_column: None, - }); + new_cursors.push(cursor); + 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.update_selections(new_selections, true, ctx); self.buffer .update(ctx, |buffer, ctx| buffer.edit(edit_ranges, "", Some(ctx))) @@ -1955,22 +1959,10 @@ mod tests { assert_eq!( view.read(app).selection_ranges(app.as_ref()), vec![ - DisplayPoint::new(0, 1)..DisplayPoint::new(0, 1), - DisplayPoint::new(0, 3)..DisplayPoint::new(0, 3) + DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0), + DisplayPoint::new(0, 1)..DisplayPoint::new(0, 1) ] ); - - // view.undo(&(), ctx); - // view.select_display_ranges( - // &[ - // DisplayPoint::new(0, 1)..DisplayPoint::new(0, 1), - // DisplayPoint::new(1, 0)..DisplayPoint::new(1, 1), - // DisplayPoint::new(3, 0)..DisplayPoint::new(3, 0), - // ], - // ctx, - // ) - // .unwrap(); - // }); }); } From 618cb8ac184c1186b79ddbb5923bf881562595c4 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 28 Apr 2021 10:20:47 +0200 Subject: [PATCH 09/15] Bind also `ctrl-d` to `buffer:delete` --- zed/src/editor/buffer_view.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 80f4533274e69e62819f5e0b58d46846392524ec..622b7ba5653ad2917d33599f24cc53e31e5961ef 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -31,6 +31,7 @@ pub fn init(app: &mut MutableAppContext) { app.add_bindings(vec![ Binding::new("backspace", "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("cmd-x", "buffer:cut", Some("BufferView")), From 2a0a2ee636a0464262d66525e06465103d03d34f Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 28 Apr 2021 10:21:17 +0200 Subject: [PATCH 10/15] Fix `delete_line` for non-empty selections that end at the start of line --- zed/src/editor/buffer_view.rs | 42 +++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 622b7ba5653ad2917d33599f24cc53e31e5961ef..ba3ea337a44b689a957477362c6ae91f3f523e04 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -413,13 +413,24 @@ impl BufferView { where T: IntoIterator>, { + use std::mem; + 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, }); } @@ -569,6 +580,12 @@ impl BufferView { } } + // When the deletion straddles multiple rows but ends at the beginning of a line, avoid + // deleting that final line. + if start.row != end.row && end.column == 0 { + end.row -= 1; + } + let mut edit_start = Point::new(start.row, 0).to_offset(buffer).unwrap(); let edit_end; let mut cursor; @@ -1939,7 +1956,7 @@ mod tests { } #[test] - fn test_delete_lines() { + fn test_delete_line() { App::test((), |app| { let settings = settings::channel(&app.font_cache()).unwrap().1; let buffer = app.add_model(|ctx| Buffer::new(0, "abc\ndef\nghi\n", ctx)); @@ -1964,6 +1981,23 @@ mod tests { DisplayPoint::new(0, 1)..DisplayPoint::new(0, 1) ] ); + + let settings = settings::channel(&app.font_cache()).unwrap().1; + let buffer = app.add_model(|ctx| Buffer::new(0, "abc\ndef\nghi\n", ctx)); + let (_, view) = app.add_window(|ctx| BufferView::for_buffer(buffer, settings, ctx)); + view.update(app, |view, ctx| { + view.select_display_ranges( + &[DisplayPoint::new(2, 0)..DisplayPoint::new(0, 1)], + ctx, + ) + .unwrap(); + view.delete_line(&(), ctx); + }); + assert_eq!(view.read(app).text(app.as_ref()), "ghi\n"); + assert_eq!( + view.read(app).selection_ranges(app.as_ref()), + vec![DisplayPoint::new(0, 1)..DisplayPoint::new(0, 1)] + ); }); } From 5d28fb871fa26af01468b5432d5be5f33b14ff4f Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 28 Apr 2021 11:09:00 +0200 Subject: [PATCH 11/15] Extract a `Selection::buffer_row_range` method --- zed/src/editor/buffer/selection.rs | 25 ++++++++++++++- zed/src/editor/buffer_view.rs | 50 ++++++++++++++---------------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/zed/src/editor/buffer/selection.rs b/zed/src/editor/buffer/selection.rs index 99c71e0a4426e61ac863729324434b586ab83eb5..514d87394bd7f0e76793da0bc9176dba71bb54c5 100644 --- a/zed/src/editor/buffer/selection.rs +++ b/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,27 @@ impl Selection { start..end } } + + pub fn buffer_row_range(&self, map: &DisplayMap, ctx: &AppContext) -> Range { + let display_start = self.start.to_display_point(map, ctx).unwrap(); + let buffer_start = DisplayPoint::new(display_start.row(), 0) + .to_buffer_point(map, Bias::Left, ctx) + .unwrap(); + + let mut display_end = self.end.to_display_point(map, ctx).unwrap(); + if display_end != map.max_point(ctx) + && 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 + } } diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index ba3ea337a44b689a957477362c6ae91f3f523e04..3c0fd8be83f7cdc09412777261077f9b802c6047 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -554,6 +554,7 @@ impl BufferView { 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(); @@ -561,52 +562,49 @@ impl BufferView { let mut selections = self.selections(app).iter().peekable(); while let Some(selection) = selections.next() { - let start = selection.start.to_point(buffer).unwrap(); - let mut end = selection.end.to_point(buffer).unwrap(); - let goal_column = if selection.reversed { - start.column - } else { - end.column - }; + let mut range = selection.buffer_row_range(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_start = next_selection.start.to_point(buffer).unwrap(); - if next_start.row <= end.row + 1 { - end = next_selection.end.to_point(buffer).unwrap(); + let next_range = next_selection.buffer_row_range(map, app); + if next_range.start <= range.end { + range.end = next_range.end; selections.next().unwrap(); } else { break; } } - // When the deletion straddles multiple rows but ends at the beginning of a line, avoid - // deleting that final line. - if start.row != end.row && end.column == 0 { - end.row -= 1; - } - - let mut edit_start = Point::new(start.row, 0).to_offset(buffer).unwrap(); + let mut edit_start = Point::new(range.start, 0).to_offset(buffer).unwrap(); let edit_end; - let mut cursor; - - if let Ok(end_offset) = Point::new(end.row + 1, 0).to_offset(buffer) { + let cursor_buffer_row; + if let Ok(end_offset) = Point::new(range.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 = Point::new(end.row + 1, goal_column); + cursor_buffer_row = range.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 = Point::new(start.row.saturating_sub(1), goal_column); + cursor_buffer_row = range.start.saturating_sub(1); } - // We tried to maintain the column of the original cursor but the new line may be - // shorter, so clip the new cursor's column. - cursor.column = cmp::min(cursor.column, buffer.line_len(cursor.row).unwrap()); - new_cursors.push(cursor); + 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); } From fdefd32de079fbe8fac29368faa1c9a87a2c7ab0 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 28 Apr 2021 12:23:45 +0200 Subject: [PATCH 12/15] Implement `duplicate_line` for buffer --- zed/src/editor/buffer_view.rs | 125 ++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 3c0fd8be83f7cdc09412777261077f9b802c6047..b92fc6fea34bb32ae04d150fb9fa015318aa80e7 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -34,6 +34,7 @@ pub fn init(app: &mut MutableAppContext) { 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("cmd-shift-D", "buffer:duplicate_line", 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")), @@ -74,6 +75,7 @@ pub fn init(app: &mut MutableAppContext) { 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:duplicate_line", BufferView::duplicate_line); app.add_action("buffer:cut", BufferView::cut); app.add_action("buffer:copy", BufferView::copy); app.add_action("buffer:paste", BufferView::paste); @@ -626,6 +628,68 @@ impl BufferView { self.end_transaction(ctx); } + pub fn duplicate_line(&mut self, _: &(), ctx: &mut ViewContext) { + self.start_transaction(ctx); + + let mut selections = self.selections(ctx.as_ref()).to_vec(); + { + // Temporarily bias selections right to allow duplicate lines to push them down when + // they are at the start 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 range = selection.buffer_row_range(map, app); + while let Some(next_selection) = selections_iter.peek() { + let next_range = next_selection.buffer_row_range(map, app); + if next_range.start <= range.end - 1 { + range.end = next_range.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(range.start, 0); + let end = Point::new(range.end - 1, buffer.line_len(range.end - 1).unwrap()); + let text = buffer + .text_for_range(start..end) + .unwrap() + .chain(Some('\n')) + .collect::(); + 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_right(buffer).unwrap(); + selection.end = selection.end.bias_right(buffer).unwrap(); + } + self.update_selections(selections, true, ctx); + + self.end_transaction(ctx); + } + pub fn cut(&mut self, _: &(), ctx: &mut ViewContext) { self.start_transaction(ctx); let mut text = String::new(); @@ -1999,6 +2063,67 @@ mod tests { }); } + #[test] + fn test_duplicate_line() { + App::test((), |app| { + let settings = settings::channel(&app.font_cache()).unwrap().1; + let buffer = app.add_model(|ctx| Buffer::new(0, "abc\ndef\nghi\n", ctx)); + let (_, view) = app.add_window(|ctx| BufferView::for_buffer(buffer, settings, ctx)); + view.update(app, |view, ctx| { + view.select_display_ranges( + &[ + DisplayPoint::new(0, 0)..DisplayPoint::new(0, 1), + DisplayPoint::new(0, 2)..DisplayPoint::new(0, 2), + DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0), + DisplayPoint::new(3, 0)..DisplayPoint::new(3, 0), + ], + ctx, + ) + .unwrap(); + view.duplicate_line(&(), ctx); + }); + assert_eq!( + view.read(app).text(app.as_ref()), + "abc\nabc\ndef\ndef\nghi\n\n" + ); + assert_eq!( + view.read(app).selection_ranges(app.as_ref()), + vec![ + DisplayPoint::new(1, 0)..DisplayPoint::new(1, 1), + DisplayPoint::new(1, 2)..DisplayPoint::new(1, 2), + DisplayPoint::new(3, 0)..DisplayPoint::new(3, 0), + DisplayPoint::new(6, 0)..DisplayPoint::new(6, 0), + ] + ); + + let settings = settings::channel(&app.font_cache()).unwrap().1; + let buffer = app.add_model(|ctx| Buffer::new(0, "abc\ndef\nghi\n", ctx)); + let (_, view) = app.add_window(|ctx| BufferView::for_buffer(buffer, settings, ctx)); + view.update(app, |view, ctx| { + view.select_display_ranges( + &[ + DisplayPoint::new(0, 1)..DisplayPoint::new(1, 1), + DisplayPoint::new(1, 2)..DisplayPoint::new(2, 1), + ], + ctx, + ) + .unwrap(); + view.duplicate_line(&(), ctx); + }); + assert_eq!( + view.read(app).text(app.as_ref()), + "abc\ndef\nghi\nabc\ndef\nghi\n" + ); + assert_eq!( + view.read(app).selection_ranges(app.as_ref()), + vec![ + DisplayPoint::new(3, 1)..DisplayPoint::new(4, 1), + DisplayPoint::new(4, 2)..DisplayPoint::new(5, 1), + ] + ); + }); + } + #[test] fn test_clipboard() { App::test((), |app| { From 0aacf858ce9266e8bad052b019f198ef3b5fb862 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 29 Apr 2021 09:29:00 +0200 Subject: [PATCH 13/15] Don't rely on `Range::is_empty` to check for selection emptiness This method returns true when `start > end`, so our `backspace` and `delete` implementations were subtly wrong because they always deleted one extra character for reversed selections. --- zed/src/editor/buffer_view.rs | 28 ++++++++++++++++++---------- zed/src/editor/mod.rs | 9 +++------ 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index b92fc6fea34bb32ae04d150fb9fa015318aa80e7..1833175fd98afeca613a47b57cb865639a770062 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -499,7 +499,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()) @@ -529,7 +530,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()) @@ -1174,15 +1176,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; } } @@ -1208,9 +1214,9 @@ 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 @@ -1282,12 +1288,14 @@ impl BufferView { } pub fn fold_selected_ranges(&mut self, _: &(), ctx: &mut ViewContext) { + use super::RangeExt; + self.display_map.update(ctx, |map, ctx| { let buffer = self.buffer.read(ctx); let ranges = self .selections(ctx.as_ref()) .iter() - .map(|s| s.range(buffer)) + .map(|s| s.range(buffer).sorted()) .collect::>(); map.fold(ranges, ctx).unwrap(); }); diff --git a/zed/src/editor/mod.rs b/zed/src/editor/mod.rs index f998963376f443eaa0eb02e9569baa9303fee917..0229e6ae7e08169a4c5e4f7e8a265f712b55137b 100644 --- a/zed/src/editor/mod.rs +++ b/zed/src/editor/mod.rs @@ -12,14 +12,11 @@ use display_map::*; use std::{cmp, ops::Range}; trait RangeExt { - fn sorted(&self) -> (T, T); + fn sorted(&self) -> Range; } impl RangeExt for Range { - 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() } } From 283c734c91b96c5f1a7acac1af6aaa3df4f614cd Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 29 Apr 2021 09:52:03 +0200 Subject: [PATCH 14/15] Implement `{move_to,select_to,delete_to}_{beginning,end}_of_line` --- zed/src/editor/buffer_view.rs | 319 +++++++++++++++++++++++++++--- zed/src/editor/display_map/mod.rs | 14 ++ zed/src/editor/movement.rs | 21 ++ 3 files changed, 330 insertions(+), 24 deletions(-) diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 1833175fd98afeca613a47b57cb865639a770062..0b4e79eb652f6d762f2ef412e1ebc8b4091f7abe 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -34,6 +34,16 @@ pub fn init(app: &mut MutableAppContext) { 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( + "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("cmd-x", "buffer:cut", Some("BufferView")), Binding::new("cmd-c", "buffer:copy", Some("BufferView")), @@ -44,12 +54,50 @@ 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( + "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( + "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", @@ -75,6 +123,14 @@ pub fn init(app: &mut MutableAppContext) { 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_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:cut", BufferView::cut); app.add_action("buffer:copy", BufferView::copy); @@ -85,12 +141,28 @@ 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_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_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, @@ -1013,6 +1085,94 @@ impl BufferView { self.update_selections(selections, true, ctx); } + pub fn move_to_beginning_of_line(&mut self, _: &(), ctx: &mut ViewContext) { + 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, + ) { + 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.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) { + 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) { + 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); + } + + pub fn delete_to_end_of_line(&mut self, _: &(), ctx: &mut ViewContext) { + 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) { let selection = Selection { start: Anchor::Start, @@ -1230,16 +1390,17 @@ impl BufferView { } 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; } @@ -1249,33 +1410,15 @@ impl BufferView { } } - fn line_indent(&self, display_row: u32, app: &AppContext) -> Result<(usize, bool)> { - let mut indent = 0; - let mut is_blank = true; - for c in self - .display_map - .read(app) - .chars_at(DisplayPoint::new(display_row, 0), app)? - { - if c == ' ' { - indent += 1; - } else { - is_blank = c == '\n'; - break; - } - } - Ok((indent, is_blank)) - } - fn foldable_range_for_line(&self, start_row: u32, app: &AppContext) -> Result> { let map = self.display_map.read(app); let max_point = self.max_point(app); - let (start_indent, _) = self.line_indent(start_row, app)?; + let (start_indent, _) = map.line_indent(start_row, app)?; let start = DisplayPoint::new(start_row, self.line_len(start_row, app)?); let mut end = None; for row in start_row + 1..=max_point.row() { - let (indent, is_blank) = self.line_indent(row, app)?; + let (indent, is_blank) = map.line_indent(row, app)?; if !is_blank && indent <= start_indent { end = Some(DisplayPoint::new(row - 1, self.line_len(row - 1, app)?)); break; @@ -1951,6 +2094,134 @@ mod tests { }); } + #[test] + fn test_beginning_end_of_line() { + App::test((), |app| { + let buffer = app.add_model(|ctx| Buffer::new(0, "abc\n def", ctx)); + let settings = settings::channel(&app.font_cache()).unwrap().1; + let (_, view) = app.add_window(|ctx| BufferView::for_buffer(buffer, settings, ctx)); + view.update(app, |view, ctx| { + view.select_display_ranges( + &[ + DisplayPoint::new(0, 1)..DisplayPoint::new(0, 1), + DisplayPoint::new(1, 4)..DisplayPoint::new(1, 4), + ], + ctx, + ) + .unwrap(); + }); + + view.update(app, |view, ctx| view.move_to_beginning_of_line(&(), ctx)); + assert_eq!( + view.read(app).selection_ranges(app.as_ref()), + &[ + DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0), + DisplayPoint::new(1, 2)..DisplayPoint::new(1, 2), + ] + ); + + view.update(app, |view, ctx| view.move_to_beginning_of_line(&(), ctx)); + assert_eq!( + view.read(app).selection_ranges(app.as_ref()), + &[ + DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0), + DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0), + ] + ); + + view.update(app, |view, ctx| view.move_to_beginning_of_line(&(), ctx)); + assert_eq!( + view.read(app).selection_ranges(app.as_ref()), + &[ + DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0), + DisplayPoint::new(1, 2)..DisplayPoint::new(1, 2), + ] + ); + + view.update(app, |view, ctx| view.move_to_end_of_line(&(), ctx)); + assert_eq!( + view.read(app).selection_ranges(app.as_ref()), + &[ + DisplayPoint::new(0, 3)..DisplayPoint::new(0, 3), + DisplayPoint::new(1, 5)..DisplayPoint::new(1, 5), + ] + ); + + // Moving to the end of line again is a no-op. + view.update(app, |view, ctx| view.move_to_end_of_line(&(), ctx)); + assert_eq!( + view.read(app).selection_ranges(app.as_ref()), + &[ + DisplayPoint::new(0, 3)..DisplayPoint::new(0, 3), + DisplayPoint::new(1, 5)..DisplayPoint::new(1, 5), + ] + ); + + view.update(app, |view, ctx| { + view.move_left(&(), ctx); + view.select_to_beginning_of_line(&true, ctx); + }); + assert_eq!( + view.read(app).selection_ranges(app.as_ref()), + &[ + DisplayPoint::new(0, 2)..DisplayPoint::new(0, 0), + DisplayPoint::new(1, 4)..DisplayPoint::new(1, 2), + ] + ); + + view.update(app, |view, ctx| { + view.select_to_beginning_of_line(&true, ctx) + }); + assert_eq!( + view.read(app).selection_ranges(app.as_ref()), + &[ + DisplayPoint::new(0, 2)..DisplayPoint::new(0, 0), + DisplayPoint::new(1, 4)..DisplayPoint::new(1, 0), + ] + ); + + view.update(app, |view, ctx| { + view.select_to_beginning_of_line(&true, ctx) + }); + assert_eq!( + view.read(app).selection_ranges(app.as_ref()), + &[ + DisplayPoint::new(0, 2)..DisplayPoint::new(0, 0), + DisplayPoint::new(1, 4)..DisplayPoint::new(1, 2), + ] + ); + + view.update(app, |view, ctx| view.select_to_end_of_line(&(), ctx)); + assert_eq!( + view.read(app).selection_ranges(app.as_ref()), + &[ + DisplayPoint::new(0, 2)..DisplayPoint::new(0, 3), + DisplayPoint::new(1, 4)..DisplayPoint::new(1, 5), + ] + ); + + view.update(app, |view, ctx| view.delete_to_end_of_line(&(), ctx)); + assert_eq!(view.read(app).text(app.as_ref()), "ab\n de"); + assert_eq!( + view.read(app).selection_ranges(app.as_ref()), + &[ + DisplayPoint::new(0, 2)..DisplayPoint::new(0, 2), + DisplayPoint::new(1, 4)..DisplayPoint::new(1, 4), + ] + ); + + view.update(app, |view, ctx| view.delete_to_beginning_of_line(&(), ctx)); + assert_eq!(view.read(app).text(app.as_ref()), "\n"); + assert_eq!( + view.read(app).selection_ranges(app.as_ref()), + &[ + DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0), + DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0), + ] + ); + }); + } + #[test] fn test_backspace() { App::test((), |app| { @@ -1971,7 +2242,7 @@ mod tests { // an empty selection - the preceding character is deleted DisplayPoint::new(0, 2)..DisplayPoint::new(0, 2), // one character selected - it is deleted - DisplayPoint::new(1, 3)..DisplayPoint::new(1, 4), + DisplayPoint::new(1, 4)..DisplayPoint::new(1, 3), // a line suffix selected - it is deleted DisplayPoint::new(2, 6)..DisplayPoint::new(3, 0), ], @@ -2008,7 +2279,7 @@ mod tests { // an empty selection - the following character is deleted DisplayPoint::new(0, 2)..DisplayPoint::new(0, 2), // one character selected - it is deleted - DisplayPoint::new(1, 3)..DisplayPoint::new(1, 4), + DisplayPoint::new(1, 4)..DisplayPoint::new(1, 3), // a line suffix selected - it is deleted DisplayPoint::new(2, 6)..DisplayPoint::new(3, 0), ], diff --git a/zed/src/editor/display_map/mod.rs b/zed/src/editor/display_map/mod.rs index c44f3ca302396f95159b1540d8bf507b1cfcceb5..e8d2b6c6e5e199eb50111edb1d70cc43c29d7586 100644 --- a/zed/src/editor/display_map/mod.rs +++ b/zed/src/editor/display_map/mod.rs @@ -67,6 +67,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> { let column = point.column() as usize; let (point, to_next_stop) = point.collapse_tabs(self, Bias::Left, app)?; diff --git a/zed/src/editor/movement.rs b/zed/src/editor/movement.rs index 08e3600255524796f2b5a8d7d965919e85256385..44e981f71b0bbddbe413cfe215057cbcb66363f4 100644 --- a/zed/src/editor/movement.rs +++ b/zed/src/editor/movement.rs @@ -58,3 +58,24 @@ pub fn down( Ok((point, goal_column)) } + +pub fn line_beginning( + map: &DisplayMap, + point: DisplayPoint, + toggle_indent: bool, + app: &AppContext, +) -> Result { + 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 { + Ok(DisplayPoint::new( + point.row(), + map.line_len(point.row(), app)?, + )) +} From d19fd77f917fb29282acb0e1bd53cae30da7f0c4 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 29 Apr 2021 10:29:06 +0200 Subject: [PATCH 15/15] :lipstick: --- zed/src/editor/buffer/selection.rs | 2 +- zed/src/editor/buffer_view.rs | 32 +++++++++++++++--------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/zed/src/editor/buffer/selection.rs b/zed/src/editor/buffer/selection.rs index 514d87394bd7f0e76793da0bc9176dba71bb54c5..1d7459482c22a16407bf379ddff3017357c51566 100644 --- a/zed/src/editor/buffer/selection.rs +++ b/zed/src/editor/buffer/selection.rs @@ -73,7 +73,7 @@ impl Selection { } } - pub fn buffer_row_range(&self, map: &DisplayMap, ctx: &AppContext) -> Range { + pub fn buffer_rows_for_display_rows(&self, map: &DisplayMap, ctx: &AppContext) -> Range { let display_start = self.start.to_display_point(map, ctx).unwrap(); let buffer_start = DisplayPoint::new(display_start.row(), 0) .to_buffer_point(map, Bias::Left, ctx) diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 0b4e79eb652f6d762f2ef412e1ebc8b4091f7abe..e0d2897276490915c81ed484403bf55d8a8addb6 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -638,7 +638,7 @@ impl BufferView { let mut selections = self.selections(app).iter().peekable(); while let Some(selection) = selections.next() { - let mut range = selection.buffer_row_range(map, app); + let mut rows = selection.buffer_rows_for_display_rows(map, app); let goal_display_column = selection .head() .to_display_point(map, app) @@ -647,29 +647,29 @@ impl BufferView { // Accumulate contiguous regions of rows that we want to delete. while let Some(next_selection) = selections.peek() { - let next_range = next_selection.buffer_row_range(map, app); - if next_range.start <= range.end { - range.end = next_range.end; + 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(range.start, 0).to_offset(buffer).unwrap(); + 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(range.end, 0).to_offset(buffer) { + 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 = range.end; + 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 = range.start.saturating_sub(1); + cursor_buffer_row = rows.start.saturating_sub(1); } let mut cursor = Point::new(cursor_buffer_row, 0) @@ -707,8 +707,8 @@ impl BufferView { let mut selections = self.selections(ctx.as_ref()).to_vec(); { - // Temporarily bias selections right to allow duplicate lines to push them down when - // they are at the start of a line. + // 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(); @@ -725,11 +725,11 @@ impl BufferView { let mut selections_iter = selections.iter_mut().peekable(); while let Some(selection) = selections_iter.next() { // Avoid duplicating the same lines twice. - let mut range = selection.buffer_row_range(map, app); + let mut rows = selection.buffer_rows_for_display_rows(map, app); while let Some(next_selection) = selections_iter.peek() { - let next_range = next_selection.buffer_row_range(map, app); - if next_range.start <= range.end - 1 { - range.end = next_range.end; + 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; @@ -737,8 +737,8 @@ impl BufferView { } // Copy the text from the selected row region and splice it at the start of the region. - let start = Point::new(range.start, 0); - let end = Point::new(range.end - 1, buffer.line_len(range.end - 1).unwrap()); + 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()