Skip to content

Commit 821a488

Browse files
cli: Use --wait to prefer focused window (#41051)
Introduce a new `prefer_focused_window` field to the `workspace::OpenOptions` struct that, when provided, will make it so that Zed opens the provided path in the currently focused window. This will now automatically be set to true when the `--wait` flag is used with the CLI. Closes #40551 Release Notes: - Improved the `--wait` flag in Zed's CLI so as to always open the provided file in the currently focused window --------- Co-authored-by: Conrad Irwin <[email protected]>
1 parent 7f17d4b commit 821a488

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

crates/workspace/src/workspace.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7323,6 +7323,7 @@ pub struct OpenOptions {
73237323
pub visible: Option<OpenVisible>,
73247324
pub focus: Option<bool>,
73257325
pub open_new_workspace: Option<bool>,
7326+
pub prefer_focused_window: bool,
73267327
pub replace_window: Option<WindowHandle<Workspace>>,
73277328
pub env: Option<HashMap<String, String>>,
73287329
}
@@ -7379,7 +7380,7 @@ pub fn open_paths(
73797380
})?;
73807381

73817382
if open_options.open_new_workspace.is_none()
7382-
&& existing.is_none()
7383+
&& (existing.is_none() || open_options.prefer_focused_window)
73837384
&& all_metadatas.iter().all(|file| !file.is_dir)
73847385
{
73857386
cx.update(|cx| {

crates/zed/src/zed.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5086,4 +5086,63 @@ mod tests {
50865086
"BUG FOUND: Project settings were overwritten when opening via command - original custom content was lost"
50875087
);
50885088
}
5089+
5090+
#[gpui::test]
5091+
async fn test_prefer_focused_window(cx: &mut gpui::TestAppContext) {
5092+
let app_state = init_test(cx);
5093+
let paths = [PathBuf::from(path!("/dir/document.txt"))];
5094+
5095+
app_state
5096+
.fs
5097+
.as_fake()
5098+
.insert_tree(
5099+
path!("/dir"),
5100+
json!({
5101+
"document.txt": "Some of the documentation's content."
5102+
}),
5103+
)
5104+
.await;
5105+
5106+
let project_a = Project::test(app_state.fs.clone(), [path!("/dir").as_ref()], cx).await;
5107+
let window_a =
5108+
cx.add_window(|window, cx| Workspace::test_new(project_a.clone(), window, cx));
5109+
5110+
let project_b = Project::test(app_state.fs.clone(), [path!("/dir").as_ref()], cx).await;
5111+
let window_b =
5112+
cx.add_window(|window, cx| Workspace::test_new(project_b.clone(), window, cx));
5113+
5114+
let project_c = Project::test(app_state.fs.clone(), [path!("/dir").as_ref()], cx).await;
5115+
let window_c =
5116+
cx.add_window(|window, cx| Workspace::test_new(project_c.clone(), window, cx));
5117+
5118+
for window in [window_a, window_b, window_c] {
5119+
let _ = cx.update_window(*window, |_, window, _| {
5120+
window.activate_window();
5121+
});
5122+
5123+
cx.update(|cx| {
5124+
let open_options = OpenOptions {
5125+
prefer_focused_window: true,
5126+
..Default::default()
5127+
};
5128+
5129+
workspace::open_paths(&paths, app_state.clone(), open_options, cx)
5130+
})
5131+
.await
5132+
.unwrap();
5133+
5134+
cx.update_window(*window, |_, window, _| assert!(window.is_window_active()))
5135+
.unwrap();
5136+
5137+
let _ = window.read_with(cx, |workspace, cx| {
5138+
let pane = workspace.active_pane().read(cx);
5139+
let project_path = pane.active_item().unwrap().project_path(cx).unwrap();
5140+
5141+
assert_eq!(
5142+
project_path.path.as_ref().as_std_path().to_str().unwrap(),
5143+
path!("document.txt")
5144+
)
5145+
});
5146+
}
5147+
}
50895148
}

crates/zed/src/zed/open_listener.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ async fn open_local_workspace(
531531
workspace::OpenOptions {
532532
open_new_workspace: effective_open_new_workspace,
533533
replace_window,
534+
prefer_focused_window: wait,
534535
env: env.cloned(),
535536
..Default::default()
536537
},

0 commit comments

Comments
 (0)