From 4a2b7e48209ab61ae7be187cfdc765eaaf5dc11d Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 14 Feb 2023 15:16:06 +0100 Subject: [PATCH 1/2] Use `alt-z` to toggle soft wrap in active editor When there isn't a default soft-wrapping for the active editor, we will soft wrap at the editor width. This is consistent with Visual Studio Code. --- assets/keymaps/default.json | 1 + crates/editor/src/editor.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/assets/keymaps/default.json b/assets/keymaps/default.json index 17168f679f7cc940cc3dc04925f36963939ee174..1d324fe582fb020896399c9195536d4302ab4286 100644 --- a/assets/keymaps/default.json +++ b/assets/keymaps/default.json @@ -164,6 +164,7 @@ "bindings": { "enter": "editor::Newline", "cmd-enter": "editor::NewlineBelow", + "alt-z": "editor::ToggleSoftWrap", "cmd-f": [ "buffer_search::Deploy", { diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 8ab02db130b9f7664f4551f61477d64f8bd7ed55..db357f2b83916d9c47c962e7264b4ee7ef5efc79 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -236,6 +236,7 @@ actions!( RestartLanguageServer, Hover, Format, + ToggleSoftWrap ] ); @@ -346,6 +347,7 @@ pub fn init(cx: &mut MutableAppContext) { cx.add_action(Editor::toggle_code_actions); cx.add_action(Editor::open_excerpts); cx.add_action(Editor::jump); + cx.add_action(Editor::toggle_soft_wrap); cx.add_async_action(Editor::format); cx.add_action(Editor::restart_language_server); cx.add_action(Editor::show_character_palette); @@ -5812,6 +5814,19 @@ impl Editor { .update(cx, |map, cx| map.set_wrap_width(width, cx)) } + pub fn toggle_soft_wrap(&mut self, _: &ToggleSoftWrap, cx: &mut ViewContext) { + if self.soft_wrap_mode_override.is_some() { + self.soft_wrap_mode_override.take(); + } else { + let soft_wrap = match self.soft_wrap_mode(cx) { + SoftWrap::None => settings::SoftWrap::EditorWidth, + SoftWrap::EditorWidth | SoftWrap::Column(_) => settings::SoftWrap::None, + }; + self.soft_wrap_mode_override = Some(soft_wrap); + } + cx.notify(); + } + pub fn highlight_rows(&mut self, rows: Option>) { self.highlighted_rows = rows; } From 1012cea4aff235ba465cc94f54753b0f7d1f57bd Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 14 Feb 2023 15:22:00 +0100 Subject: [PATCH 2/2] Soft wrap at editor width if it's narrower than preferred line length --- crates/editor/src/element.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index bce63ca0cf796ef0e9537ff7f40f241f196c5331..9d8922fab5f42f3c9effc71c7e4125967a2bbec1 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -1534,15 +1534,14 @@ impl Element for EditorElement { let snapshot = self.update_view(cx.app, |view, cx| { view.set_visible_line_count(size.y() / line_height); + let editor_width = text_width - gutter_margin - overscroll.x() - em_width; let wrap_width = match view.soft_wrap_mode(cx) { - SoftWrap::None => Some((MAX_LINE_LEN / 2) as f32 * em_advance), - SoftWrap::EditorWidth => { - Some(text_width - gutter_margin - overscroll.x() - em_width) - } - SoftWrap::Column(column) => Some(column as f32 * em_advance), + SoftWrap::None => (MAX_LINE_LEN / 2) as f32 * em_advance, + SoftWrap::EditorWidth => editor_width, + SoftWrap::Column(column) => editor_width.min(column as f32 * em_advance), }; - if view.set_wrap_width(wrap_width, cx) { + if view.set_wrap_width(Some(wrap_width), cx) { view.snapshot(cx) } else { snapshot