From eb8254c4a67a8be8aff3671535a86f814781268e Mon Sep 17 00:00:00 2001 From: Dimitri Sabadie Date: Tue, 24 Sep 2024 18:00:55 +0200 Subject: [PATCH] Implement From for Movement. That allows to refactor a bit of code; we have the following pattern in many different places: if cx.editor.mode == Mode::Select { Movement::Extend } else { Movement::Move } The From impl captures that pattern, so that we can just do cx.editor.mode.into(). --- helix-term/src/commands.rs | 68 ++++---------------------------- helix-term/src/commands/typed.rs | 10 +---- helix-view/src/document.rs | 11 ++++++ 3 files changed, 19 insertions(+), 70 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index d16fd7764..c1747733a 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -785,15 +785,7 @@ fn goto_line_end_impl(view: &mut View, doc: &mut Document, movement: Movement) { fn goto_line_end(cx: &mut Context) { let (view, doc) = current!(cx.editor); - goto_line_end_impl( - view, - doc, - if cx.editor.mode == Mode::Select { - Movement::Extend - } else { - Movement::Move - }, - ) + goto_line_end_impl(view, doc, cx.editor.mode.into()) } fn extend_to_line_end(cx: &mut Context) { @@ -815,15 +807,7 @@ fn goto_line_end_newline_impl(view: &mut View, doc: &mut Document, movement: Mov fn goto_line_end_newline(cx: &mut Context) { let (view, doc) = current!(cx.editor); - goto_line_end_newline_impl( - view, - doc, - if cx.editor.mode == Mode::Select { - Movement::Extend - } else { - Movement::Move - }, - ) + goto_line_end_newline_impl(view, doc, cx.editor.mode.into()) } fn extend_to_line_end_newline(cx: &mut Context) { @@ -846,15 +830,7 @@ fn goto_line_start_impl(view: &mut View, doc: &mut Document, movement: Movement) fn goto_line_start(cx: &mut Context) { let (view, doc) = current!(cx.editor); - goto_line_start_impl( - view, - doc, - if cx.editor.mode == Mode::Select { - Movement::Extend - } else { - Movement::Move - }, - ) + goto_line_start_impl(view, doc, cx.editor.mode.into()) } fn goto_next_buffer(cx: &mut Context) { @@ -944,16 +920,7 @@ fn kill_to_line_end(cx: &mut Context) { fn goto_first_nonwhitespace(cx: &mut Context) { let (view, doc) = current!(cx.editor); - - goto_first_nonwhitespace_impl( - view, - doc, - if cx.editor.mode == Mode::Select { - Movement::Extend - } else { - Movement::Move - }, - ) + goto_first_nonwhitespace_impl(view, doc, cx.editor.mode.into()) } fn extend_to_first_nonwhitespace(cx: &mut Context) { @@ -1224,14 +1191,7 @@ fn goto_next_paragraph(cx: &mut Context) { } fn goto_file_start(cx: &mut Context) { - goto_file_start_impl( - cx, - if cx.editor.mode == Mode::Select { - Movement::Extend - } else { - Movement::Move - }, - ); + goto_file_start_impl(cx, cx.editor.mode.into()); } fn extend_file_start(cx: &mut Context) { @@ -1254,14 +1214,7 @@ fn goto_file_start_impl(cx: &mut Context, movement: Movement) { } fn goto_file_end(cx: &mut Context) { - goto_file_end_impl( - cx, - if cx.editor.mode == Mode::Select { - Movement::Extend - } else { - Movement::Move - }, - ); + goto_file_end_impl(cx, cx.editor.mode.into()); } fn extend_file_end(cx: &mut Context) { @@ -3557,14 +3510,7 @@ fn push_jump(view: &mut View, doc: &Document) { } fn goto_line(cx: &mut Context) { - goto_line_impl( - cx, - if cx.editor.mode == Mode::Select { - Movement::Extend - } else { - Movement::Move - }, - ); + goto_line_impl(cx, cx.editor.mode.into()); } fn goto_line_impl(cx: &mut Context, movement: Movement) { diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 6703fe2ca..9fa1ea5ef 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1814,15 +1814,7 @@ fn update_goto_line_number_preview( let scrolloff = cx.editor.config().scrolloff; let line = args[0].parse::()?; - goto_line_without_jumplist( - cx.editor, - NonZeroUsize::new(line), - if cx.editor.mode == Mode::Select { - Movement::Extend - } else { - Movement::Move - }, - ); + goto_line_without_jumplist(cx.editor, NonZeroUsize::new(line), cx.editor.mode.into()); let (view, doc) = current!(cx.editor); view.ensure_cursor_in_view(doc, scrolloff); diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 91ec27874..2c8f4cb4f 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -7,6 +7,7 @@ use helix_core::auto_pairs::AutoPairs; use helix_core::chars::char_is_word; use helix_core::doc_formatter::TextFormat; use helix_core::encoding::Encoding; +use helix_core::movement::Movement; use helix_core::syntax::{Highlight, LanguageServerFeature}; use helix_core::text_annotations::{InlineAnnotation, Overlay}; use helix_lsp::util::lsp_pos_to_pos; @@ -102,6 +103,16 @@ impl Serialize for Mode { serializer.collect_str(self) } } + +impl From for Movement { + fn from(mode: Mode) -> Self { + match mode { + Mode::Select => Movement::Extend, + _ => Movement::Move, + } + } +} + /// A snapshot of the text of a document that we want to write out to disk #[derive(Debug, Clone)] pub struct DocumentSavedEvent {