aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src
diff options
context:
space:
mode:
authorVince Mutolo2022-05-02 14:24:22 +0000
committerGitHub2022-05-02 14:24:22 +0000
commitf9baced216df60122f2aae08d382931d77901ca9 (patch)
tree44326181702d03e18f5d9c810331c2993524296c /helix-term/src
parent567ddef388986dd2cea5c7f3eb1aa1a978c3e6d3 (diff)
add reflow command (#2128)
* add reflow command Users need to be able to hard-wrap text for many applications, including comments in code, git commit messages, plaintext documentation, etc. It often falls to the user to manually insert line breaks where appropriate in order to hard-wrap text. This commit introduces the "reflow" command (both in the TUI and core library) to automatically hard-wrap selected text to a given number of characters (defined by Unicode "extended grapheme clusters"). It handles lines with a repeated prefix, such as comments ("//") and indentation. * reflow: consider newlines to be word separators * replace custom reflow impl with textwrap crate * Sync reflow command docs with book * reflow: add default max_line_len language setting Co-authored-by: Vince Mutolo <vince@mutolo.org>
Diffstat (limited to 'helix-term/src')
-rw-r--r--helix-term/src/commands/typed.rs46
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.",