Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Ensure bundled files are opened as read-only (#36299)
Finn Evers6f2e7c355ec4
diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rsindex b06652b2ceb..a324ba0932c 100644--- a/crates/zed/src/zed.rs+++ b/crates/zed/src/zed.rs@@ -31,6 +31,7 @@ use gpui::{px, retain_all,};use image_viewer::ImageInfo;+use language::Capability;use language_tools::lsp_tool::{self, LspTool};use migrate::{MigrationBanner, MigrationEvent, MigrationNotification, MigrationType};use migrator::{migrate_keymap, migrate_settings};@@ -1764,7 +1765,11 @@ fn open_bundled_file(workspace.with_local_workspace(window, cx, |workspace, window, cx| {let project = workspace.project();let buffer = project.update(cx, move |project, cx| {- project.create_local_buffer(text.as_ref(), language, cx)+ let buffer = project.create_local_buffer(text.as_ref(), language, cx);+ buffer.update(cx, |buffer, cx| {+ buffer.set_capability(Capability::ReadOnly, cx);+ });+ buffer});let buffer =cx.new(|cx| MultiBuffer::singleton(buffer, cx).with_title(title.into()));@@ -4543,6 +4548,43 @@ mod tests {assert!(has_default_theme);}+ #[gpui::test]+ async fn test_bundled_files_editor(cx: &mut TestAppContext) {+ let app_state = init_test(cx);+ cx.update(init);++ let project = Project::test(app_state.fs.clone(), [], cx).await;+ let _window = cx.add_window(|window, cx| Workspace::test_new(project, window, cx));++ cx.update(|cx| {+ cx.dispatch_action(&OpenDefaultSettings);+ });+ cx.run_until_parked();++ assert_eq!(cx.read(|cx| cx.windows().len()), 1);++ let workspace = cx.windows()[0].downcast::<Workspace>().unwrap();+ let active_editor = workspace+ .update(cx, |workspace, _, cx| {+ workspace.active_item_as::<Editor>(cx)+ })+ .unwrap();+ assert!(+ active_editor.is_some(),+ "Settings action should have opened an editor with the default file contents"+ );++ let active_editor = active_editor.unwrap();+ assert!(+ active_editor.read_with(cx, |editor, cx| editor.read_only(cx)),+ "Default settings should be readonly"+ );+ assert!(+ active_editor.read_with(cx, |editor, cx| editor.buffer().read(cx).read_only()),+ "The underlying buffer should also be readonly for the shipped default settings"+ );+ }+#[gpui::test]async fn test_bundled_languages(cx: &mut TestAppContext) {env_logger::builder().is_test(true).try_init().ok();