Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
editor: Respect `read_only` in `newline_below` (#50804)
Bennet Bo Fenner1a8005f24a4b
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rsindex 3b18c9a447d..94c7bb06eb9 100644--- a/crates/editor/src/editor.rs+++ b/crates/editor/src/editor.rs@@ -5088,6 +5088,10 @@ impl Editor {}pub fn newline(&mut self, _: &Newline, window: &mut Window, cx: &mut Context<Self>) {+ if self.read_only(cx) {+ return;+ }+self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);self.transact(window, cx, |this, window, cx| {let (edits_with_flags, selection_info): (Vec<_>, Vec<_>) = {@@ -5309,6 +5313,10 @@ impl Editor {}pub fn newline_above(&mut self, _: &NewlineAbove, window: &mut Window, cx: &mut Context<Self>) {+ if self.read_only(cx) {+ return;+ }+self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);let buffer = self.buffer.read(cx);@@ -5376,6 +5384,10 @@ impl Editor {}pub fn newline_below(&mut self, _: &NewlineBelow, window: &mut Window, cx: &mut Context<Self>) {+ if self.read_only(cx) {+ return;+ }+self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);let mut buffer_edits: HashMap<EntityId, (Entity<Buffer>, Vec<Point>)> = HashMap::default();diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rsindex 199cb0d3785..e3d5e698153 100644--- a/crates/editor/src/editor_tests.rs+++ b/crates/editor/src/editor_tests.rs@@ -3382,6 +3382,46 @@ async fn test_newline_below(cx: &mut TestAppContext) {"});}+#[gpui::test]+fn test_newline_respects_read_only(cx: &mut TestAppContext) {+ init_test(cx, |_| {});++ let editor = cx.add_window(|window, cx| {+ let buffer = MultiBuffer::build_simple("aaaa\nbbbb\n", cx);+ build_editor(buffer, window, cx)+ });++ _ = editor.update(cx, |editor, window, cx| {+ editor.set_read_only(true);+ editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {+ s.select_display_ranges([+ DisplayPoint::new(DisplayRow(0), 2)..DisplayPoint::new(DisplayRow(0), 2)+ ])+ });++ editor.newline(&Newline, window, cx);+ assert_eq!(+ editor.text(cx),+ "aaaa\nbbbb\n",+ "newline should not modify a read-only editor"+ );++ editor.newline_above(&NewlineAbove, window, cx);+ assert_eq!(+ editor.text(cx),+ "aaaa\nbbbb\n",+ "newline_above should not modify a read-only editor"+ );++ editor.newline_below(&NewlineBelow, window, cx);+ assert_eq!(+ editor.text(cx),+ "aaaa\nbbbb\n",+ "newline_below should not modify a read-only editor"+ );+ });+}+#[gpui::test]fn test_newline_below_multibuffer(cx: &mut TestAppContext) {init_test(cx, |_| {});