From abb5800d20429d772b31bc50ae394c47f6662b35 Mon Sep 17 00:00:00 2001 From: 0x2CA <2478557459@qq.com> Date: Fri, 23 Aug 2024 02:51:32 +0800 Subject: [PATCH] Add bounded soft wrap (#16586) Closes #14700 #8164 Release Notes: - Added `soft_wrap` value `bounded`,EditorWidth and PreferredLineLength min value --------- Co-authored-by: Conrad Irwin --- assets/settings/default.json | 2 ++ crates/editor/src/editor.rs | 8 +++++++- crates/editor/src/element.rs | 3 ++- crates/language/src/language_settings.rs | 4 +++- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/assets/settings/default.json b/assets/settings/default.json index a972d689613653..09ca4b850e0b29 100644 --- a/assets/settings/default.json +++ b/assets/settings/default.json @@ -510,6 +510,8 @@ // "soft_wrap": "editor_width", // 4. Soft wrap lines at the preferred line length. // "soft_wrap": "preferred_line_length", + // 5. Soft wrap lines at the preferred line length or the editor width (whichever is smaller). + // "soft_wrap": "bounded", "soft_wrap": "prefer_line", // The column at which to soft-wrap lines, for buffers where soft-wrap // is enabled. diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 32c8075af5438f..1414d3c65b21ea 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -375,6 +375,7 @@ pub enum SoftWrap { PreferLine, EditorWidth, Column(u32), + Bounded(u32), } #[derive(Clone)] @@ -10491,6 +10492,8 @@ impl Editor { if settings.show_wrap_guides { if let SoftWrap::Column(soft_wrap) = self.soft_wrap_mode(cx) { wrap_guides.push((soft_wrap as usize, true)); + } else if let SoftWrap::Bounded(soft_wrap) = self.soft_wrap_mode(cx) { + wrap_guides.push((soft_wrap as usize, true)); } wrap_guides.extend(settings.wrap_guides.iter().map(|guide| (*guide, false))) } @@ -10510,6 +10513,9 @@ impl Editor { language_settings::SoftWrap::PreferredLineLength => { SoftWrap::Column(settings.preferred_line_length) } + language_settings::SoftWrap::Bounded => { + SoftWrap::Bounded(settings.preferred_line_length) + } } } @@ -10551,7 +10557,7 @@ impl Editor { } else { let soft_wrap = match self.soft_wrap_mode(cx) { SoftWrap::None | SoftWrap::PreferLine => language_settings::SoftWrap::EditorWidth, - SoftWrap::EditorWidth | SoftWrap::Column(_) => { + SoftWrap::EditorWidth | SoftWrap::Column(_) | SoftWrap::Bounded(_) => { language_settings::SoftWrap::PreferLine } }; diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 0053a7b5678d2a..c1ebdea5e2ad3c 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -4996,7 +4996,8 @@ impl Element for EditorElement { Some((MAX_LINE_LEN / 2) as f32 * em_advance) } SoftWrap::EditorWidth => Some(editor_width), - SoftWrap::Column(column) => { + SoftWrap::Column(column) => Some(column as f32 * em_advance), + SoftWrap::Bounded(column) => { Some(editor_width.min(column as f32 * em_advance)) } }; diff --git a/crates/language/src/language_settings.rs b/crates/language/src/language_settings.rs index 4a21ba8ee0a484..49d02cc847a775 100644 --- a/crates/language/src/language_settings.rs +++ b/crates/language/src/language_settings.rs @@ -379,10 +379,12 @@ pub enum SoftWrap { None, /// Prefer a single line generally, unless an overly long line is encountered. PreferLine, - /// Soft wrap lines that overflow the editor + /// Soft wrap lines that exceed the editor width EditorWidth, /// Soft wrap lines at the preferred line length PreferredLineLength, + /// Soft wrap line at the preferred line length or the editor width (whichever is smaller) + Bounded, } /// Controls the behavior of formatting files when they are saved.