From f5981f72c256a834845aad0c2947a4a20fa84d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Fri, 23 Oct 2020 12:09:40 +0900 Subject: [PATCH] Introduce Selection::point. --- helix-core/src/selection.rs | 5 +++++ helix-view/src/commands.rs | 19 +++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index 13c820f15..9413fead8 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -179,6 +179,11 @@ impl Selection { } } + /// Constructs a selection holding a single cursor. + pub fn point(pos: usize) -> Self { + Self::single(pos, pos) + } + #[must_use] pub fn new(ranges: SmallVec<[Range; 1]>, primary_index: usize) -> Self { fn normalize(mut ranges: SmallVec<[Range; 1]>, mut primary_index: usize) -> Selection { diff --git a/helix-view/src/commands.rs b/helix-view/src/commands.rs index d31aed31b..52e09dd67 100644 --- a/helix-view/src/commands.rs +++ b/helix-view/src/commands.rs @@ -93,7 +93,7 @@ pub fn move_next_word_start(view: &mut View, count: usize) { count, ); - view.doc.set_selection(Selection::single(pos, pos)); + view.doc.set_selection(Selection::point(pos)); } pub fn move_prev_word_start(view: &mut View, count: usize) { @@ -104,7 +104,7 @@ pub fn move_prev_word_start(view: &mut View, count: usize) { count, ); - view.doc.set_selection(Selection::single(pos, pos)); + view.doc.set_selection(Selection::point(pos)); } pub fn move_next_word_end(view: &mut View, count: usize) { @@ -114,11 +114,11 @@ pub fn move_next_word_end(view: &mut View, count: usize) { count, ); - view.doc.set_selection(Selection::single(pos, pos)); + view.doc.set_selection(Selection::point(pos)); } pub fn move_file_start(view: &mut View, _count: usize) { - view.doc.set_selection(Selection::single(0, 0)); + view.doc.set_selection(Selection::point(0)); view.doc.mode = Mode::Normal; } @@ -126,8 +126,7 @@ pub fn move_file_start(view: &mut View, _count: usize) { pub fn move_file_end(view: &mut View, _count: usize) { let text = &view.doc.text(); let last_line = text.line_to_char(text.len_lines().saturating_sub(2)); - view.doc - .set_selection(Selection::single(last_line, last_line)); + view.doc.set_selection(Selection::point(last_line)); view.doc.mode = Mode::Normal; } @@ -153,7 +152,7 @@ pub fn page_up(view: &mut View, _count: usize) { if !check_cursor_in_view(view) { let text = view.doc.text(); let pos = text.line_to_char(view.last_line().saturating_sub(PADDING)); - view.doc.set_selection(Selection::single(pos, pos)); + view.doc.set_selection(Selection::point(pos)); } } @@ -163,7 +162,7 @@ pub fn page_down(view: &mut View, _count: usize) { if view.first_line < view.doc.text().len_lines() { let text = view.doc.text(); let pos = text.line_to_char(view.first_line as usize); - view.doc.set_selection(Selection::single(pos, pos)); + view.doc.set_selection(Selection::point(pos)); } } @@ -177,7 +176,7 @@ pub fn half_page_up(view: &mut View, _count: usize) { if !check_cursor_in_view(view) { let text = &view.doc.text(); let pos = text.line_to_char(view.last_line() - PADDING); - view.doc.set_selection(Selection::single(pos, pos)); + view.doc.set_selection(Selection::point(pos)); } } @@ -189,7 +188,7 @@ pub fn half_page_down(view: &mut View, _count: usize) { if !check_cursor_in_view(view) { let text = view.doc.text(); let pos = text.line_to_char(view.first_line as usize); - view.doc.set_selection(Selection::single(pos, pos)); + view.doc.set_selection(Selection::point(pos)); } } // avoid select by default by having a visual mode switch that makes movements into selects