From b5cb41c7f03d35167ec640663032f3d362c9aa05 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 9 Jun 2021 15:34:45 -0600 Subject: [PATCH 1/2] Open a new workspace on File > New if none exists --- zed/src/menus.rs | 2 +- zed/src/workspace.rs | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/zed/src/menus.rs b/zed/src/menus.rs index e9d50c78244b94cc75a62ad097471c8e8457e3cc..da053348f198868b0067830d980ff58b482cff6e 100644 --- a/zed/src/menus.rs +++ b/zed/src/menus.rs @@ -29,7 +29,7 @@ pub fn menus(state: AppState) -> Vec> { name: "New", keystroke: Some("cmd-n"), action: "workspace:new_file", - arg: None, + arg: Some(Box::new(state.clone())), }, MenuItem::Separator, MenuItem::Action { diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index 36ef65b1747689564aef5e776c5bbb70e69d95c0..19932d122c77b61c82c6b2f586e01054bad66b67 100644 --- a/zed/src/workspace.rs +++ b/zed/src/workspace.rs @@ -29,6 +29,7 @@ use std::{ pub fn init(cx: &mut MutableAppContext) { cx.add_global_action("workspace:open", open); cx.add_global_action("workspace:open_paths", open_paths); + cx.add_global_action("workspace:new_file", open_new); cx.add_global_action("app:quit", quit); cx.add_action("workspace:save", Workspace::save_active_item); cx.add_action("workspace:debug_elements", Workspace::debug_elements); @@ -98,6 +99,19 @@ fn open_paths(params: &OpenParams, cx: &mut MutableAppContext) { }); } +fn open_new(app_state: &AppState, cx: &mut MutableAppContext) { + cx.add_window(|cx| { + let mut view = Workspace::new( + 0, + app_state.settings.clone(), + app_state.language_registry.clone(), + cx, + ); + view.open_new_file(&app_state, cx); + view + }); +} + fn quit(_: &(), cx: &mut MutableAppContext) { cx.platform().quit(); } @@ -449,7 +463,7 @@ impl Workspace { } } - pub fn open_new_file(&mut self, _: &(), cx: &mut ViewContext) { + pub fn open_new_file(&mut self, _: &AppState, cx: &mut ViewContext) { let buffer = cx.add_model(|cx| Buffer::new(self.replica_id, "", cx)); let buffer_view = cx.add_view(|cx| Editor::for_buffer(buffer.clone(), self.settings.clone(), cx)); From 66c76d5469bc0ec113848b6dbfffd15ffa257cc7 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 9 Jun 2021 16:38:32 -0600 Subject: [PATCH 2/2] Test creating a new empty workspace and fix test compile errors --- gpui/src/app.rs | 6 ++++++ zed/src/workspace.rs | 28 +++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/gpui/src/app.rs b/gpui/src/app.rs index b11f5c63da91f9365f0e7aab957399458ece87c7..296eb50f10c41170beb5434ef30ca84eb00e32cc 100644 --- a/gpui/src/app.rs +++ b/gpui/src/app.rs @@ -264,6 +264,12 @@ impl TestAppContext { ); } + pub fn dispatch_global_action(&self, name: &str, arg: T) { + self.0 + .borrow_mut() + .dispatch_global_action(name, arg); + } + pub fn dispatch_keystroke( &self, window_id: usize, diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index 19932d122c77b61c82c6b2f586e01054bad66b67..04d1b0f5a13d5868cd346bfe77325432fb140fb3 100644 --- a/zed/src/workspace.rs +++ b/zed/src/workspace.rs @@ -1085,9 +1085,10 @@ mod tests { async fn test_open_and_save_new_file(mut cx: gpui::TestAppContext) { let dir = TempDir::new("test-new-file").unwrap(); let app_state = cx.read(build_app_state); + let app_state2 = app_state.clone(); let (_, workspace) = cx.add_window(|cx| { let mut workspace = - Workspace::new(0, app_state.settings, app_state.language_registry, cx); + Workspace::new(0, app_state2.settings, app_state2.language_registry, cx); workspace.add_worktree(dir.path(), cx); workspace }); @@ -1103,8 +1104,9 @@ mod tests { tree.flush_fs_events(&cx).await; // Create a new untitled buffer + let app_state2 = app_state.clone(); let editor = workspace.update(&mut cx, |workspace, cx| { - workspace.open_new_file(&(), cx); + workspace.open_new_file(&app_state2, cx); workspace .active_item(cx) .unwrap() @@ -1112,6 +1114,7 @@ mod tests { .downcast::() .unwrap() }); + editor.update(&mut cx, |editor, cx| { assert!(!editor.is_dirty(cx.as_ref())); assert_eq!(editor.title(cx.as_ref()), "untitled"); @@ -1154,7 +1157,7 @@ mod tests { // Open the same newly-created file in another pane item. The new editor should reuse // the same buffer. workspace.update(&mut cx, |workspace, cx| { - workspace.open_new_file(&(), cx); + workspace.open_new_file(&app_state, cx); workspace.split_pane(workspace.active_pane().clone(), SplitDirection::Right, cx); assert!(workspace .open_entry((tree.id(), Path::new("the-new-name").into()), cx) @@ -1173,6 +1176,25 @@ mod tests { }) } + #[gpui::test] + async fn test_new_empty_workspace(mut cx: gpui::TestAppContext) { + cx.update(init); + + let app_state = cx.read(build_app_state); + cx.dispatch_global_action("workspace:new_file", app_state); + let window_id = *cx.window_ids().first().unwrap(); + let workspace = cx.root_view::(window_id).unwrap(); + workspace.update(&mut cx, |workspace, cx| { + let editor = workspace + .active_item(cx) + .unwrap() + .to_any() + .downcast::() + .unwrap(); + assert!(editor.read(cx).text(cx.as_ref()).is_empty()); + }); + } + #[gpui::test] async fn test_pane_actions(mut cx: gpui::TestAppContext) { cx.update(|cx| pane::init(cx));