From 1a0dbb2907af376d89b0ab8836ae4474e3125a4d Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 30 Apr 2021 09:41:38 +0200 Subject: [PATCH] Implement `select_to_{previous,next}_word_boundary` --- zed/src/editor/buffer_view.rs | 52 +++++++++++++++++++++++++++++++++++ zed/src/editor/movement.rs | 2 +- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 985dce20d2213bc8f7952db44e666acd2bbedd8a..3ba8ceb3f1cfac14b120695602253f8878a6385b 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -86,6 +86,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", @@ -173,6 +183,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, @@ -1121,6 +1139,23 @@ impl BufferView { self.update_selections(selections, true, ctx); } + pub fn select_to_previous_word_boundary(&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::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 move_to_next_word_boundary(&mut self, _: &(), ctx: &mut ViewContext) { let app = ctx.as_ref(); let mut selections = self.selections(app).to_vec(); @@ -1139,6 +1174,23 @@ impl BufferView { self.update_selections(selections, true, ctx); } + pub fn select_to_next_word_boundary(&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::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 move_to_beginning_of_line(&mut self, _: &(), ctx: &mut ViewContext) { let app = ctx.as_ref(); let mut selections = self.selections(app).to_vec(); diff --git a/zed/src/editor/movement.rs b/zed/src/editor/movement.rs index 5df463f43b2393862887722f62128bd450cfea1b..f4f0b0da4409c2105c55503f55be9aa731dbca21 100644 --- a/zed/src/editor/movement.rs +++ b/zed/src/editor/movement.rs @@ -119,7 +119,7 @@ pub fn next_word_boundary( ) -> Result { let mut prev_c = None; for c in map.chars_at(point, app)? { - if prev_c.is_some() && (c == '\n' || char_kind(prev_c.unwrap()) != char_kind(c)) { + if prev_c.is_some() && char_kind(prev_c.unwrap()) != char_kind(c) { break; }