From 7ad3c3c1fdd8e9aea1eaa406b8c9ed13ba42b76f Mon Sep 17 00:00:00 2001 From: woojiq Date: Tue, 29 Aug 2023 22:58:56 +0300 Subject: [PATCH] fix: change line-ending char when finding ret * Takes into account direction and an inclusive flag to select the first or last char of the line-ending string. * Adds tests to ensure `T` and `F` work with `\n` and `\r\n` line-endings. --- helix-term/src/commands.rs | 10 ++++++++- helix-term/tests/test/movement.rs | 36 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 1f88079eb27d2..1eb7aaf12d2ac 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1272,7 +1272,15 @@ fn find_char(cx: &mut Context, direction: Direction, inclusive: bool, extend: bo // usually mix line endings. But we should fix it eventually // anyway. { - doc!(cx.editor).line_ending.as_str().chars().next().unwrap() + // Handle multichar endings like CRLF correctly + let mut line_ending = doc!(cx.editor).line_ending.as_str().chars(); + match (direction, inclusive) { + (Direction::Forward, true) => line_ending.next_back(), + (Direction::Forward, false) => line_ending.next(), + (Direction::Backward, true) => line_ending.next(), + (Direction::Backward, false) => line_ending.next_back(), + } + .unwrap() } KeyEvent { diff --git a/helix-term/tests/test/movement.rs b/helix-term/tests/test/movement.rs index 9a48cdbcb9fb4..e78e8d8ba8266 100644 --- a/helix-term/tests/test/movement.rs +++ b/helix-term/tests/test/movement.rs @@ -513,3 +513,39 @@ async fn select_mode_tree_sitter_prev_function_goes_backwards_to_object() -> any Ok(()) } + +#[tokio::test(flavor = "multi_thread")] +async fn previous_occurrence_of_char() -> anyhow::Result<()> { + use helix_term::config::Config; + use helix_view::editor::LineEndingConfig; + + let mut config_crlf = Config::default(); + config_crlf.editor.default_line_ending = LineEndingConfig::Crlf; + let mut config_lf = Config::default(); + config_lf.editor.default_line_ending = LineEndingConfig::LF; + + // Move till previous char occurrence should properly handle when line-ending is Crlf + test_with_config( + AppBuilder::new().with_config(config_crlf.clone()), + ("text\r\nnext #[l|]#ine", "T", "text\r\n#[|next l]#ine"), + ) + .await?; + test_with_config( + AppBuilder::new().with_config(config_lf.clone()), + ("text\nnext #[l|]#ine", "T", "text\n#[|next l]#ine"), + ) + .await?; + + test_with_config( + AppBuilder::new().with_config(config_crlf), + ("text\r\nnext #[l|]#ine", "F", "text#[|\r\nnext l]#ine"), + ) + .await?; + test_with_config( + AppBuilder::new().with_config(config_lf), + ("text\nnext #[l|]#ine", "F", "text#[|\nnext l]#ine"), + ) + .await?; + + Ok(()) +}