diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index a1135f7ad6a4b1153148da4013438190f7e765ab..3fb832b3ac7b2ebb6aa3f47b2dcd590d7eb081df 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -1869,6 +1869,7 @@ pub enum MultibufferSelectionMode { pub struct RewrapOptions { pub override_language_settings: bool, pub preserve_existing_whitespace: bool, + pub line_length: Option, } impl Editor { @@ -5150,6 +5151,7 @@ impl Editor { RewrapOptions { override_language_settings: true, preserve_existing_whitespace: true, + line_length: None, }, cx, ) @@ -13721,7 +13723,7 @@ impl Editor { continue; }; - let wrap_column = self.hard_wrap.unwrap_or_else(|| { + let wrap_column = options.line_length.or(self.hard_wrap).unwrap_or_else(|| { buffer .language_settings_at(Point::new(start_row, 0), cx) .preferred_line_length as usize diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rs index 3615a447d231ce741e38b677366da19ea1a93e84..1fc4813157f8e64a4e51cc570b906b3a2d456002 100644 --- a/crates/git_ui/src/git_panel.rs +++ b/crates/git_ui/src/git_panel.rs @@ -2276,6 +2276,7 @@ impl GitPanel { RewrapOptions { override_language_settings: false, preserve_existing_whitespace: true, + line_length: None, }, cx, ); diff --git a/crates/vim/src/command.rs b/crates/vim/src/command.rs index d185c1c0670212ffd683e79849415f479fa03b58..5e6337adda0325f6d7e5249da1a04c3317a3c051 100644 --- a/crates/vim/src/command.rs +++ b/crates/vim/src/command.rs @@ -1726,7 +1726,15 @@ fn generate_commands(_: &App) -> Vec { ) .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::().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, + ); } } diff --git a/crates/vim/src/rewrap.rs b/crates/vim/src/rewrap.rs index 3cb7d66116023d979d83e04a00b974fdd2a6d078..208bbfc7e6b37bb5b3ec2a8f53aaa191d79444bd 100644 --- a/crates/vim/src/rewrap.rs +++ b/crates/vim/src/rewrap.rs @@ -1,19 +1,20 @@ use crate::{Vim, motion::Motion, object::Object, state::Mode}; use collections::HashMap; use editor::{Bias, Editor, RewrapOptions, SelectionEffects, display_map::ToDisplayPoint}; -use gpui::{Context, Window, actions}; +use gpui::{Action, Context, Window}; use language::SelectionGoal; +use schemars::JsonSchema; +use serde::Deserialize; -actions!( - vim, - [ - /// Rewraps the selected text to fit within the line width. - Rewrap - ] -); +/// Rewraps the selected text to fit within the line width. +#[derive(Clone, Deserialize, JsonSchema, PartialEq, Action)] +#[action(namespace = vim)] +pub(crate) struct Rewrap { + pub line_length: Option, +} pub(crate) fn register(editor: &mut Editor, cx: &mut Context) { - Vim::action(editor, cx, |vim, _: &Rewrap, window, cx| { + Vim::action(editor, cx, |vim, action: &Rewrap, window, cx| { vim.record_current_action(cx); Vim::take_count(cx); Vim::take_forced_motion(cx); @@ -24,6 +25,7 @@ pub(crate) fn register(editor: &mut Editor, cx: &mut Context) { editor.rewrap_impl( RewrapOptions { override_language_settings: true, + line_length: action.line_length, ..Default::default() }, cx,