Skip to content

Commit 6f2e7c3

Browse files
authored
Ensure bundled files are opened as read-only (#36299)
Closes #36297 While we set the editor as read-only for bundled files, we didn't do this for the underlying buffer. This PR fixes this and adds a test for the corresponding case. Release Notes: - Fixed an issue where bundled files (e.g. the default settings) could be edited in some circumstances
1 parent 864d4bc commit 6f2e7c3

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

crates/zed/src/zed.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use gpui::{
3131
px, retain_all,
3232
};
3333
use image_viewer::ImageInfo;
34+
use language::Capability;
3435
use language_tools::lsp_tool::{self, LspTool};
3536
use migrate::{MigrationBanner, MigrationEvent, MigrationNotification, MigrationType};
3637
use migrator::{migrate_keymap, migrate_settings};
@@ -1764,7 +1765,11 @@ fn open_bundled_file(
17641765
workspace.with_local_workspace(window, cx, |workspace, window, cx| {
17651766
let project = workspace.project();
17661767
let buffer = project.update(cx, move |project, cx| {
1767-
project.create_local_buffer(text.as_ref(), language, cx)
1768+
let buffer = project.create_local_buffer(text.as_ref(), language, cx);
1769+
buffer.update(cx, |buffer, cx| {
1770+
buffer.set_capability(Capability::ReadOnly, cx);
1771+
});
1772+
buffer
17681773
});
17691774
let buffer =
17701775
cx.new(|cx| MultiBuffer::singleton(buffer, cx).with_title(title.into()));
@@ -4543,6 +4548,43 @@ mod tests {
45434548
assert!(has_default_theme);
45444549
}
45454550

4551+
#[gpui::test]
4552+
async fn test_bundled_files_editor(cx: &mut TestAppContext) {
4553+
let app_state = init_test(cx);
4554+
cx.update(init);
4555+
4556+
let project = Project::test(app_state.fs.clone(), [], cx).await;
4557+
let _window = cx.add_window(|window, cx| Workspace::test_new(project, window, cx));
4558+
4559+
cx.update(|cx| {
4560+
cx.dispatch_action(&OpenDefaultSettings);
4561+
});
4562+
cx.run_until_parked();
4563+
4564+
assert_eq!(cx.read(|cx| cx.windows().len()), 1);
4565+
4566+
let workspace = cx.windows()[0].downcast::<Workspace>().unwrap();
4567+
let active_editor = workspace
4568+
.update(cx, |workspace, _, cx| {
4569+
workspace.active_item_as::<Editor>(cx)
4570+
})
4571+
.unwrap();
4572+
assert!(
4573+
active_editor.is_some(),
4574+
"Settings action should have opened an editor with the default file contents"
4575+
);
4576+
4577+
let active_editor = active_editor.unwrap();
4578+
assert!(
4579+
active_editor.read_with(cx, |editor, cx| editor.read_only(cx)),
4580+
"Default settings should be readonly"
4581+
);
4582+
assert!(
4583+
active_editor.read_with(cx, |editor, cx| editor.buffer().read(cx).read_only()),
4584+
"The underlying buffer should also be readonly for the shipped default settings"
4585+
);
4586+
}
4587+
45464588
#[gpui::test]
45474589
async fn test_bundled_languages(cx: &mut TestAppContext) {
45484590
env_logger::builder().is_test(true).try_init().ok();

0 commit comments

Comments
 (0)