From f9baced216df60122f2aae08d382931d77901ca9 Mon Sep 17 00:00:00 2001 From: Vince Mutolo Date: Mon, 2 May 2022 10:24:22 -0400 Subject: 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 --- helix-core/src/lib.rs | 1 + helix-core/src/syntax.rs | 1 + helix-core/src/wrap.rs | 7 +++++++ 3 files changed, 9 insertions(+) create mode 100644 helix-core/src/wrap.rs (limited to 'helix-core/src') diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index 02341265..a022a42a 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -27,6 +27,7 @@ pub mod syntax; pub mod test; pub mod textobject; mod transaction; +pub mod wrap; pub mod unicode { pub use unicode_general_category as category; diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 3f9e7bcf..eab3ab79 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -67,6 +67,7 @@ pub struct LanguageConfiguration { pub shebangs: Vec, // interpreter(s) associated with language pub roots: Vec, // these indicate project roots <.git, Cargo.toml> pub comment_token: Option, + pub max_line_length: Option, #[serde(default, skip_serializing, deserialize_with = "deserialize_lsp_config")] pub config: Option, diff --git a/helix-core/src/wrap.rs b/helix-core/src/wrap.rs new file mode 100644 index 00000000..eabc47d4 --- /dev/null +++ b/helix-core/src/wrap.rs @@ -0,0 +1,7 @@ +use smartstring::{LazyCompact, SmartString}; + +/// Given a slice of text, return the text re-wrapped to fit it +/// within the given width. +pub fn reflow_hard_wrap(text: &str, max_line_len: usize) -> SmartString { + textwrap::refill(text, max_line_len).into() +} -- cgit v1.2.3-70-g09d2