Implement toggle read-only editor action

Lukas Wirth created

Change summary

crates/editor/src/actions.rs |  4 +++-
crates/editor/src/editor.rs  | 15 +++++++++++++++
crates/editor/src/element.rs |  1 +
3 files changed, 19 insertions(+), 1 deletion(-)

Detailed changes

crates/editor/src/actions.rs 🔗

@@ -844,7 +844,9 @@ actions!(
         /// from the current selections.
         UnwrapSyntaxNode,
         /// Wraps selections in tag specified by language.
-        WrapSelectionsInTag
+        WrapSelectionsInTag,
+        /// Toggles read-only mode for the current buffer.
+        ToggleReadOnly,
     ]
 );
 

crates/editor/src/editor.rs 🔗

@@ -11007,6 +11007,21 @@ impl Editor {
         });
     }
 
+    pub fn toggle_read_only(&mut self, _: &ToggleReadOnly, _: &mut Window, cx: &mut Context<Self>) {
+        // todo(lw): We should not allow toggling every editor to read-write. Some we want to keep read-only, always as they might be read-only replicated etc.
+        if let Some(buffer) = self.buffer.read(cx).as_singleton() {
+            buffer.update(cx, |buffer, cx| {
+                buffer.set_capability(
+                    match buffer.capability() {
+                        Capability::ReadWrite => Capability::ReadOnly,
+                        Capability::ReadOnly => Capability::ReadWrite,
+                    },
+                    cx,
+                );
+            })
+        }
+    }
+
     pub fn reload_file(&mut self, _: &ReloadFile, window: &mut Window, cx: &mut Context<Self>) {
         let Some(project) = self.project.clone() else {
             return;

crates/editor/src/element.rs 🔗

@@ -615,6 +615,7 @@ impl EditorElement {
         register_action(editor, window, Editor::edit_log_breakpoint);
         register_action(editor, window, Editor::enable_breakpoint);
         register_action(editor, window, Editor::disable_breakpoint);
+        register_action(editor, window, Editor::toggle_read_only);
         if editor.read(cx).enable_wrap_selections_in_tag(cx) {
             register_action(editor, window, Editor::wrap_selections_in_tag);
         }