@@ -35,6 +35,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(
+ "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",
@@ -55,6 +65,16 @@ 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",
@@ -77,6 +97,16 @@ 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(
+ "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",
@@ -124,6 +154,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_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,
@@ -142,6 +180,14 @@ 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,
@@ -156,6 +202,14 @@ 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_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,
@@ -1086,6 +1140,90 @@ impl BufferView {
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();
@@ -2220,6 +2358,188 @@ mod tests {
});
}
+ #[test]
+ fn test_prev_next_word_boundary() {
+ App::test((), |app| {
+ let buffer = app
+ .add_model(|ctx| Buffer::new(0, "use std::str::{foo, bar}\n\n {baz.qux()}", 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, 11)..DisplayPoint::new(0, 11),
+ DisplayPoint::new(2, 4)..DisplayPoint::new(2, 4),
+ ],
+ ctx,
+ )
+ .unwrap();
+ });
+
+ view.update(app, |view, ctx| {
+ view.move_to_previous_word_boundary(&(), ctx)
+ });
+ assert_eq!(
+ view.read(app).selection_ranges(app.as_ref()),
+ &[
+ DisplayPoint::new(0, 9)..DisplayPoint::new(0, 9),
+ DisplayPoint::new(2, 3)..DisplayPoint::new(2, 3),
+ ]
+ );
+
+ view.update(app, |view, ctx| {
+ view.move_to_previous_word_boundary(&(), ctx)
+ });
+ assert_eq!(
+ view.read(app).selection_ranges(app.as_ref()),
+ &[
+ DisplayPoint::new(0, 7)..DisplayPoint::new(0, 7),
+ DisplayPoint::new(2, 2)..DisplayPoint::new(2, 2),
+ ]
+ );
+
+ view.update(app, |view, ctx| {
+ view.move_to_previous_word_boundary(&(), ctx)
+ });
+ assert_eq!(
+ view.read(app).selection_ranges(app.as_ref()),
+ &[
+ DisplayPoint::new(0, 4)..DisplayPoint::new(0, 4),
+ DisplayPoint::new(2, 0)..DisplayPoint::new(2, 0),
+ ]
+ );
+
+ view.update(app, |view, ctx| {
+ view.move_to_previous_word_boundary(&(), ctx)
+ });
+ assert_eq!(
+ view.read(app).selection_ranges(app.as_ref()),
+ &[
+ DisplayPoint::new(0, 3)..DisplayPoint::new(0, 3),
+ DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0),
+ ]
+ );
+
+ view.update(app, |view, ctx| {
+ view.move_to_previous_word_boundary(&(), ctx)
+ });
+ assert_eq!(
+ view.read(app).selection_ranges(app.as_ref()),
+ &[
+ DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0),
+ DisplayPoint::new(0, 24)..DisplayPoint::new(0, 24),
+ ]
+ );
+
+ view.update(app, |view, ctx| {
+ view.move_to_previous_word_boundary(&(), ctx)
+ });
+ assert_eq!(
+ view.read(app).selection_ranges(app.as_ref()),
+ &[
+ DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0),
+ DisplayPoint::new(0, 23)..DisplayPoint::new(0, 23),
+ ]
+ );
+
+ view.update(app, |view, ctx| view.move_to_next_word_boundary(&(), ctx));
+ assert_eq!(
+ view.read(app).selection_ranges(app.as_ref()),
+ &[
+ DisplayPoint::new(0, 3)..DisplayPoint::new(0, 3),
+ DisplayPoint::new(0, 24)..DisplayPoint::new(0, 24),
+ ]
+ );
+
+ view.update(app, |view, ctx| view.move_to_next_word_boundary(&(), ctx));
+ assert_eq!(
+ view.read(app).selection_ranges(app.as_ref()),
+ &[
+ DisplayPoint::new(0, 4)..DisplayPoint::new(0, 4),
+ DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0),
+ ]
+ );
+
+ view.update(app, |view, ctx| view.move_to_next_word_boundary(&(), ctx));
+ assert_eq!(
+ view.read(app).selection_ranges(app.as_ref()),
+ &[
+ DisplayPoint::new(0, 7)..DisplayPoint::new(0, 7),
+ DisplayPoint::new(2, 0)..DisplayPoint::new(2, 0),
+ ]
+ );
+
+ view.update(app, |view, ctx| view.move_to_next_word_boundary(&(), ctx));
+ assert_eq!(
+ view.read(app).selection_ranges(app.as_ref()),
+ &[
+ DisplayPoint::new(0, 9)..DisplayPoint::new(0, 9),
+ DisplayPoint::new(2, 2)..DisplayPoint::new(2, 2),
+ ]
+ );
+
+ view.update(app, |view, ctx| {
+ view.move_right(&(), ctx);
+ view.select_to_previous_word_boundary(&(), ctx);
+ });
+ assert_eq!(
+ view.read(app).selection_ranges(app.as_ref()),
+ &[
+ DisplayPoint::new(0, 10)..DisplayPoint::new(0, 9),
+ DisplayPoint::new(2, 3)..DisplayPoint::new(2, 2),
+ ]
+ );
+
+ view.update(app, |view, ctx| {
+ view.select_to_previous_word_boundary(&(), ctx)
+ });
+ assert_eq!(
+ view.read(app).selection_ranges(app.as_ref()),
+ &[
+ DisplayPoint::new(0, 10)..DisplayPoint::new(0, 7),
+ DisplayPoint::new(2, 3)..DisplayPoint::new(2, 0),
+ ]
+ );
+
+ view.update(app, |view, ctx| view.select_to_next_word_boundary(&(), ctx));
+ assert_eq!(
+ view.read(app).selection_ranges(app.as_ref()),
+ &[
+ DisplayPoint::new(0, 10)..DisplayPoint::new(0, 9),
+ DisplayPoint::new(2, 3)..DisplayPoint::new(2, 2),
+ ]
+ );
+
+ view.update(app, |view, ctx| view.delete_to_next_word_boundary(&(), ctx));
+ assert_eq!(
+ view.read(app).text(app.as_ref()),
+ "use std::s::{foo, bar}\n\n {az.qux()}"
+ );
+ assert_eq!(
+ view.read(app).selection_ranges(app.as_ref()),
+ &[
+ DisplayPoint::new(0, 10)..DisplayPoint::new(0, 10),
+ DisplayPoint::new(2, 3)..DisplayPoint::new(2, 3),
+ ]
+ );
+
+ view.update(app, |view, ctx| {
+ view.delete_to_previous_word_boundary(&(), ctx)
+ });
+ assert_eq!(
+ view.read(app).text(app.as_ref()),
+ "use std::::{foo, bar}\n\n az.qux()}"
+ );
+ assert_eq!(
+ view.read(app).selection_ranges(app.as_ref()),
+ &[
+ DisplayPoint::new(0, 9)..DisplayPoint::new(0, 9),
+ DisplayPoint::new(2, 2)..DisplayPoint::new(2, 2),
+ ]
+ );
+ });
+ }
+
#[test]
fn test_backspace() {
App::test((), |app| {