helix: Add support for line length in reflow command (#52152)

* Add `editor::RewrapOptions::line_length` to, optionally, override the
  line length used when rewrapping text.
* Update `editor::Editor::rewrap_impl` to prefer
  `editor::RewrapOptions::line_length`, when set.
* Add a `line_length` field to the `vim::rewrap::Rewrap` action.
* Update the `:reflow` vim command with `vim::command::VimCommand::args`
  so as to be able to parse the provided argument as `usize`, ensuring
  that no effect is taken if the argument can't be parsed as such.

Release Notes:

- N/A
This commit is contained in:
Dino authored and GitHub committed 2026-03-23 11:09:54 +00:00
1 parent dd8f595b5c
commit c0da60901a
4 files changed
+67 -19

No files matched your search

+52 -9
View File
@@ -1726,7 +1726,15 @@ fn generate_commands(_: &App) -> Vec<VimCommand> {
)
.range(wrap_count),
VimCommand::new(("j", "oin"), JoinLines).range(select_range),
VimCommand::new(("reflow", ""), Rewrap).range(select_range),
VimCommand::new(("reflow", ""), Rewrap { line_length: None })
.range(select_range)
.args(|_action, args| {
args.parse::<usize>().map_or(None, |length| {
Some(Box::new(Rewrap {
line_length: Some(length),
}))
})
}),
VimCommand::new(("fo", "ld"), editor::actions::FoldSelectedRanges).range(act_on_range),
VimCommand::new(("foldo", "pen"), editor::actions::UnfoldLines)
.bang(editor::actions::UnfoldRecursive)
@@ -3550,7 +3558,7 @@ mod test {
cx.set_state(
indoc! {"
ˇ0123456789 0123456789 0123456789 0123456789
ˇ0123456789 0123456789
"},
Mode::Normal,
);
@@ -3560,8 +3568,6 @@ mod test {
cx.assert_state(
indoc! {"
0123456789
0123456789
0123456789
ˇ0123456789
"},
@@ -3570,22 +3576,59 @@ mod test {
cx.set_state(
indoc! {"
«0123456789 0123456789ˇ»
0123456789 0123456789
ˇ0123456789 0123456789
"},
Mode::VisualLine,
);
cx.simulate_keystrokes(": reflow");
cx.simulate_keystrokes("shift-v : reflow");
cx.simulate_keystrokes("enter");
cx.assert_state(
indoc! {"
ˇ0123456789
0123456789
0123456789 0123456789
ˇ0123456789
"},
Mode::Normal,
);
cx.set_state(
indoc! {"
ˇ0123 4567 0123 4567
"},
Mode::VisualLine,
);
cx.simulate_keystrokes(": reflow space 7");
cx.simulate_keystrokes("enter");
cx.assert_state(
indoc! {"
ˇ0123
4567
0123
4567
"},
Mode::Normal,
);
// Assert that, if `:reflow` is invoked with an invalid argument, it
// does not actually have any effect in the buffer's contents.
cx.set_state(
indoc! {"
ˇ0123 4567 0123 4567
"},
Mode::VisualLine,
);
cx.simulate_keystrokes(": reflow space a");
cx.simulate_keystrokes("enter");
cx.assert_state(
indoc! {"
ˇ0123 4567 0123 4567
"},
Mode::VisualLine,
);
}
}