From 2aadc97126f644d5dea3f852d7b1ee4855b45db7 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 6 Sep 2021 13:08:19 +0200 Subject: [PATCH] Simplify how we determine if enter should insert a newline or propagate --- zed/src/editor.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/zed/src/editor.rs b/zed/src/editor.rs index 7d13628f8a..6176b402c5 100644 --- a/zed/src/editor.rs +++ b/zed/src/editor.rs @@ -46,7 +46,7 @@ const MAX_LINE_LEN: usize = 1024; action!(Cancel); action!(Backspace); action!(Delete); -action!(Newline, bool); +action!(Newline); action!(Insert, String); action!(DeleteLine); action!(DeleteToPreviousWordBoundary); @@ -105,8 +105,8 @@ pub fn init(cx: &mut MutableAppContext) { Binding::new("ctrl-h", Backspace, Some("Editor")), Binding::new("delete", Delete, Some("Editor")), Binding::new("ctrl-d", Delete, Some("Editor")), - Binding::new("enter", Newline(false), Some("Editor")), - Binding::new("alt-enter", Newline(true), Some("Editor")), + Binding::new("enter", Newline, Some("Editor && mode == full")), + Binding::new("alt-enter", Newline, Some("Editor && mode == auto_height")), Binding::new("tab", Insert("\t".into()), Some("Editor")), Binding::new("ctrl-shift-K", DeleteLine, Some("Editor")), Binding::new( @@ -752,18 +752,8 @@ impl Editor { self.end_transaction(cx); } - fn newline(&mut self, Newline(insert_newline): &Newline, cx: &mut ViewContext) { - match self.mode { - EditorMode::SingleLine => cx.propagate_action(), - EditorMode::AutoHeight { .. } => { - if *insert_newline { - self.insert(&Insert("\n".into()), cx); - } else { - cx.propagate_action(); - } - } - EditorMode::Full => self.insert(&Insert("\n".into()), cx), - } + fn newline(&mut self, _: &Newline, cx: &mut ViewContext) { + self.insert(&Insert("\n".into()), cx); } pub fn backspace(&mut self, _: &Backspace, cx: &mut ViewContext) { @@ -2610,6 +2600,17 @@ impl View for Editor { cx.emit(Event::Blurred); cx.notify(); } + + fn keymap_context(&self, _: &AppContext) -> gpui::keymap::Context { + let mut cx = Self::default_keymap_context(); + let mode = match self.mode { + EditorMode::SingleLine => "single_line", + EditorMode::AutoHeight { .. } => "auto_height", + EditorMode::Full => "full", + }; + cx.map.insert("mode".into(), mode.into()); + cx + } } impl workspace::Item for Buffer {