Implement From<Mode> 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().
This commit is contained in:
Dimitri Sabadie 2024-09-24 18:00:55 +02:00
parent 617e800833
commit eb8254c4a6
No known key found for this signature in database
GPG Key ID: A94386A8A6252ECB
3 changed files with 19 additions and 70 deletions

View File

@ -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) {

View File

@ -1814,15 +1814,7 @@ fn update_goto_line_number_preview(
let scrolloff = cx.editor.config().scrolloff;
let line = args[0].parse::<usize>()?;
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);

View File

@ -7,6 +7,7 @@
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 @@ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
serializer.collect_str(self)
}
}
impl From<Mode> 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 {