From ee5f48d02f737fe5355af8eaf1c492624e66780a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Delafargue?= Date: Fri, 10 Feb 2023 13:09:50 +0100 Subject: [PATCH] Allow softwrapping to ignore `text-width` Softwrapping wraps by default to the viewport width or a configured `text-width` (whichever's smaller). In some cases we only want to set `text-width` to use for hard-wrapping and let longer lines flow if they have enough space. This setting allows that. --- book/src/configuration.md | 13 +++++++------ helix-view/src/document.rs | 18 ++++++++++-------- helix-view/src/editor.rs | 3 +++ 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/book/src/configuration.md b/book/src/configuration.md index 1fcd5735a80a..63d35c68ae1b 100644 --- a/book/src/configuration.md +++ b/book/src/configuration.md @@ -317,12 +317,13 @@ Currently unused Options for soft wrapping lines that exceed the view width -| Key | Description | Default | -| --- | --- | --- | -| `enable` | Whether soft wrapping is enabled. | `false` | -| `max-wrap` | Maximum free space left at the end of the line. | `20` | -| `max-indent-retain` | Maximum indentation to carry over when soft wrapping a line. | `40` | -| `wrap-indicator` | Text inserted before soft wrapped lines, highlighted with `ui.virtual.wrap` | `↪ ` | +| Key | Description | Default | +| --- | --- | --- | +| `enable` | Whether soft wrapping is enabled. | `false` | +| `max-wrap` | Maximum free space left at the end of the line. | `20` | +| `max-indent-retain` | Maximum indentation to carry over when soft wrapping a line. | `40` | +| `wrap-indicator` | Text inserted before soft wrapped lines, highlighted with `ui.virtual.wrap` | `↪ ` | +| `wrap-at-text-width` | Soft wrap at `text-width` instead of using the full viewport size. | `true` | Example: diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 02cbac63d562..eb610358a95d 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -1235,14 +1235,16 @@ impl Document { let text_width = self .language_config() .and_then(|config| config.text_width) - .or_else(|| config.text_width); - if let Some(text_width) = text_width { - // We increase max_line_len by 1 because softwrap considers the newline character - // as part of the line length while the "typical" expectation is that this is not the case. - // In particular other commands like :reflow do not count the line terminator. - // This is technically inconsistent for the last line as that line never has a line terminator - // but having the last visual line exceed the width by 1 seems like a rare edge case. - viewport_width = viewport_width.min(text_width as u16 + 1) + .or(config.text_width); + if config.soft_wrap.wrap_at_text_width { + if let Some(text_width) = text_width { + // We increase max_line_len by 1 because softwrap considers the newline character + // as part of the line length while the "typical" expectation is that this is not the case. + // In particular other commands like :reflow do not count the line terminator. + // This is technically inconsistent for the last line as that line never has a line terminator + // but having the last visual line exceed the width by 1 seems like a rare edge case. + viewport_width = viewport_width.min(text_width as u16 + 1) + } } let soft_wrap = &config.soft_wrap; let tab_width = self.tab_width() as u16; diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index baf4dba0b989..278755d97788 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -302,6 +302,8 @@ pub struct SoftWrap { /// /// Defaults to ↪ pub wrap_indicator: String, + /// Softwrap at `text_width` instead of viewport width if it is shorter + pub wrap_at_text_width: bool, } impl Default for SoftWrap { @@ -311,6 +313,7 @@ impl Default for SoftWrap { max_wrap: 20, max_indent_retain: 40, wrap_indicator: "↪ ".into(), + wrap_at_text_width: true, } } }