diff options
Diffstat (limited to 'helix-term/src/commands')
-rw-r--r-- | helix-term/src/commands/typed.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 373c7018..ec86e446 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1051,6 +1051,45 @@ fn sort_impl( Ok(()) } +fn reflow( + cx: &mut compositor::Context, + args: &[Cow<str>], + _event: PromptEvent, +) -> anyhow::Result<()> { + let (view, doc) = current!(cx.editor); + + const DEFAULT_MAX_LEN: usize = 79; + + // Find the max line length by checking the following sources in order: + // - The passed argument in `args` + // - The configured max_line_len for this language in languages.toml + // - The const default we set above + let max_line_len: usize = args + .get(0) + .map(|num| num.parse::<usize>()) + .transpose()? + .or_else(|| { + doc.language_config() + .and_then(|config| config.max_line_length) + }) + .unwrap_or(DEFAULT_MAX_LEN); + + let rope = doc.text(); + + let selection = doc.selection(view.id); + let transaction = Transaction::change_by_selection(rope, selection, |range| { + let fragment = range.fragment(rope.slice(..)); + let reflowed_text = helix_core::wrap::reflow_hard_wrap(&fragment, max_line_len); + + (range.from(), range.to(), Some(reflowed_text)) + }); + + doc.apply(&transaction, view.id); + doc.append_changes_to_history(view.id); + + Ok(()) +} + fn tree_sitter_subtree( cx: &mut compositor::Context, _args: &[Cow<str>], @@ -1571,6 +1610,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ completer: None, }, TypableCommand { + name: "reflow", + aliases: &[], + doc: "Hard-wrap the current selection of lines to a given width.", + fun: reflow, + completer: None, + }, + TypableCommand { name: "tree-sitter-subtree", aliases: &["ts-subtree"], doc: "Display tree sitter subtree under cursor, primarily for debugging queries.", |