Skip to content

Commit

Permalink
fix: change line-ending char when finding ret
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
woojiq committed Aug 30, 2023
1 parent 40d7e6c commit 7ad3c3c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
10 changes: 9 additions & 1 deletion helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 36 additions & 0 deletions helix-term/tests/test/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ret> when line-ending is Crlf
test_with_config(
AppBuilder::new().with_config(config_crlf.clone()),
("text\r\nnext #[l|]#ine", "T<ret>", "text\r\n#[|next l]#ine"),
)
.await?;
test_with_config(
AppBuilder::new().with_config(config_lf.clone()),
("text\nnext #[l|]#ine", "T<ret>", "text\n#[|next l]#ine"),
)
.await?;

test_with_config(
AppBuilder::new().with_config(config_crlf),
("text\r\nnext #[l|]#ine", "F<ret>", "text#[|\r\nnext l]#ine"),
)
.await?;
test_with_config(
AppBuilder::new().with_config(config_lf),
("text\nnext #[l|]#ine", "F<ret>", "text#[|\nnext l]#ine"),
)
.await?;

Ok(())
}

0 comments on commit 7ad3c3c

Please sign in to comment.