Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't break on hyphen with :reflow #8569

Merged
merged 4 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Its settings will be merged with the configuration directory `config.toml` and t
| `workspace-lsp-roots` | Directories relative to the workspace root that are treated as LSP roots. Should only be set in `.helix/config.toml` | `[]` |
| `default-line-ending` | The line ending to use for new documents. Can be `native`, `lf`, `crlf`, `ff`, `cr` or `nel`. `native` uses the platform's native line ending (`crlf` on Windows, otherwise `lf`). | `native` |
| `insert-final-newline` | Whether to automatically insert a trailing line-ending on write if missing | `true` |
| `no-break-on-hyphen` | Disable linebreak on hyphen when using `:reflow` | `true` |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do introduce this option it should be named so that it's clear that it's for the reflow command, like reflow-break-on-hyphen


### `[editor.statusline]` Section

Expand Down
13 changes: 11 additions & 2 deletions helix-core/src/wrap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
use smartstring::{LazyCompact, SmartString};
use textwrap::{word_splitters::WordSplitter, Options};

/// Given a slice of text, return the text re-wrapped to fit it
/// within the given width.
pub fn reflow_hard_wrap(text: &str, text_width: usize) -> SmartString<LazyCompact> {
textwrap::refill(text, text_width).into()
pub fn reflow_hard_wrap(
text: &str,
text_width: usize,
no_break_on_hyphen: bool,
) -> SmartString<LazyCompact> {
let mut options = Options::new(text_width);
if no_break_on_hyphen {
options.word_splitter = WordSplitter::NoHyphenation;
}
textwrap::refill(text, options).into()
}
4 changes: 3 additions & 1 deletion helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2078,6 +2078,7 @@ fn reflow(

let scrolloff = cx.editor.config().scrolloff;
let cfg_text_width: usize = cx.editor.config().text_width;
let cfg_no_break_on_hyphen: bool = cx.editor.config().no_break_on_hyphen;
let (view, doc) = current!(cx.editor);

// Find the text_width by checking the following sources in order:
Expand All @@ -2096,7 +2097,8 @@ fn reflow(
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, text_width);
let reflowed_text =
helix_core::wrap::reflow_hard_wrap(&fragment, text_width, cfg_no_break_on_hyphen);

(range.from(), range.to(), Some(reflowed_text))
});
Expand Down
3 changes: 3 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ pub struct Config {
pub insert_final_newline: bool,
/// Enables smart tab
pub smart_tab: Option<SmartTabConfig>,
/// Disable ":reflow" break on hyphen (default false)
pub no_break_on_hyphen: bool,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq, PartialOrd, Ord)]
Expand Down Expand Up @@ -846,6 +848,7 @@ impl Default for Config {
default_line_ending: LineEndingConfig::default(),
insert_final_newline: true,
smart_tab: Some(SmartTabConfig::default()),
no_break_on_hyphen: true,
}
}
}
Expand Down
Loading