1// Allow binary to be called Zed for a nice application menu when running executable directly
2#![allow(non_snake_case)]
3
4use anyhow::{anyhow, Context, Result};
5use assets::Assets;
6use backtrace::Backtrace;
7use cli::{
8 ipc::{self, IpcSender},
9 CliRequest, CliResponse, IpcHandshake, FORCE_CLI_MODE_ENV_VAR_NAME,
10};
11use client::{self, TelemetrySettings, UserStore, ZED_APP_VERSION, ZED_SECRET_CLIENT_TOKEN};
12use db::kvp::KEY_VALUE_STORE;
13use editor::{scroll::autoscroll::Autoscroll, Editor};
14use futures::{
15 channel::{mpsc, oneshot},
16 FutureExt, SinkExt, StreamExt,
17};
18use gpui::{Action, App, AppContext, AssetSource, AsyncAppContext, Task, ViewContext};
19use isahc::{config::Configurable, Request};
20use language::{LanguageRegistry, Point};
21use log::LevelFilter;
22use node_runtime::NodeRuntime;
23use parking_lot::Mutex;
24use project::Fs;
25use serde::{Deserialize, Serialize};
26use settings::{default_settings, handle_settings_file_changes, watch_config_file, SettingsStore};
27use simplelog::ConfigBuilder;
28use smol::process::Command;
29use std::{
30 collections::HashMap,
31 env,
32 ffi::OsStr,
33 fs::OpenOptions,
34 io::Write as _,
35 ops::Not,
36 os::unix::prelude::OsStrExt,
37 panic,
38 path::{Path, PathBuf},
39 str,
40 sync::{
41 atomic::{AtomicBool, Ordering},
42 Arc, Weak,
43 },
44 thread,
45 time::{Duration, SystemTime, UNIX_EPOCH},
46};
47use sum_tree::Bias;
48use terminal_view::{get_working_directory, TerminalSettings, TerminalView};
49use util::{
50 http::{self, HttpClient},
51 paths::PathLikeWithPosition,
52};
53use welcome::{show_welcome_experience, FIRST_OPEN};
54
55use fs::RealFs;
56#[cfg(debug_assertions)]
57use staff_mode::StaffMode;
58use util::{channel::RELEASE_CHANNEL, paths, ResultExt, TryFutureExt};
59use workspace::{
60 item::ItemHandle, notifications::NotifyResultExt, AppState, OpenSettings, Workspace,
61};
62use zed::{
63 self, build_window_options, handle_keymap_file_changes, initialize_workspace, languages, menus,
64};
65
66fn main() {
67 let http = http::client();
68 init_paths();
69 init_logger();
70
71 log::info!("========== starting zed ==========");
72 let mut app = gpui::App::new(Assets).unwrap();
73
74 init_panic_hook(&app);
75
76 app.background();
77
78 load_embedded_fonts(&app);
79
80 let fs = Arc::new(RealFs);
81 let user_settings_file_rx =
82 watch_config_file(app.background(), fs.clone(), paths::SETTINGS.clone());
83 let user_keymap_file_rx =
84 watch_config_file(app.background(), fs.clone(), paths::KEYMAP.clone());
85
86 let login_shell_env_loaded = if stdout_is_a_pty() {
87 Task::ready(())
88 } else {
89 app.background().spawn(async {
90 load_login_shell_environment().await.log_err();
91 })
92 };
93
94 let (cli_connections_tx, mut cli_connections_rx) = mpsc::unbounded();
95 let cli_connections_tx = Arc::new(cli_connections_tx);
96 let (open_paths_tx, mut open_paths_rx) = mpsc::unbounded();
97 let open_paths_tx = Arc::new(open_paths_tx);
98 let urls_callback_triggered = Arc::new(AtomicBool::new(false));
99
100 let callback_cli_connections_tx = Arc::clone(&cli_connections_tx);
101 let callback_open_paths_tx = Arc::clone(&open_paths_tx);
102 let callback_urls_callback_triggered = Arc::clone(&urls_callback_triggered);
103 app.on_open_urls(move |urls, _| {
104 callback_urls_callback_triggered.store(true, Ordering::Release);
105 open_urls(urls, &callback_cli_connections_tx, &callback_open_paths_tx);
106 })
107 .on_reopen(move |cx| {
108 if cx.has_global::<Weak<AppState>>() {
109 if let Some(app_state) = cx.global::<Weak<AppState>>().upgrade() {
110 workspace::open_new(&app_state, cx, |workspace, cx| {
111 Editor::new_file(workspace, &Default::default(), cx)
112 })
113 .detach();
114 }
115 }
116 });
117
118 app.run(move |cx| {
119 cx.set_global(*RELEASE_CHANNEL);
120
121 #[cfg(debug_assertions)]
122 cx.set_global(StaffMode(true));
123
124 let mut store = SettingsStore::default();
125 store
126 .set_default_settings(default_settings().as_ref(), cx)
127 .unwrap();
128 cx.set_global(store);
129 handle_settings_file_changes(user_settings_file_rx, cx);
130 handle_keymap_file_changes(user_keymap_file_rx, cx);
131
132 let client = client::Client::new(http.clone(), cx);
133 let mut languages = LanguageRegistry::new(login_shell_env_loaded);
134 languages.set_executor(cx.background().clone());
135 languages.set_language_server_download_dir(paths::LANGUAGES_DIR.clone());
136 let languages = Arc::new(languages);
137 let node_runtime = NodeRuntime::new(http.clone(), cx.background().to_owned());
138
139 languages::init(languages.clone(), node_runtime.clone());
140 let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http.clone(), cx));
141
142 cx.set_global(client.clone());
143
144 theme::init(Assets, cx);
145 context_menu::init(cx);
146 project::Project::init(&client, cx);
147 client::init(&client, cx);
148 command_palette::init(cx);
149 language::init(cx);
150 editor::init(cx);
151 go_to_line::init(cx);
152 file_finder::init(cx);
153 outline::init(cx);
154 project_symbols::init(cx);
155 project_panel::init(cx);
156 diagnostics::init(cx);
157 search::init(cx);
158 vim::init(cx);
159 terminal_view::init(cx);
160 theme_testbench::init(cx);
161 copilot::init(http.clone(), node_runtime, cx);
162 ai::init(cx);
163
164 cx.spawn(|cx| watch_themes(fs.clone(), cx)).detach();
165
166 languages.set_theme(theme::current(cx).clone());
167 cx.observe_global::<SettingsStore, _>({
168 let languages = languages.clone();
169 move |cx| languages.set_theme(theme::current(cx).clone())
170 })
171 .detach();
172
173 client.telemetry().start();
174 client.telemetry().report_mixpanel_event(
175 "start app",
176 Default::default(),
177 *settings::get::<TelemetrySettings>(cx),
178 );
179
180 let app_state = Arc::new(AppState {
181 languages,
182 client: client.clone(),
183 user_store,
184 fs,
185 build_window_options,
186 initialize_workspace,
187 background_actions,
188 });
189 cx.set_global(Arc::downgrade(&app_state));
190 auto_update::init(http.clone(), client::ZED_SERVER_URL.clone(), cx);
191
192 workspace::init(app_state.clone(), cx);
193 recent_projects::init(cx);
194
195 journal::init(app_state.clone(), cx);
196 language_selector::init(cx);
197 theme_selector::init(cx);
198 activity_indicator::init(cx);
199 lsp_log::init(cx);
200 call::init(app_state.client.clone(), app_state.user_store.clone(), cx);
201 collab_ui::init(&app_state, cx);
202 feedback::init(cx);
203 welcome::init(cx);
204 zed::init(&app_state, cx);
205
206 cx.set_menus(menus::menus());
207
208 if stdout_is_a_pty() {
209 cx.platform().activate(true);
210 let paths = collect_path_args();
211 if paths.is_empty() {
212 cx.spawn(|cx| async move { restore_or_create_workspace(&app_state, cx).await })
213 .detach()
214 } else {
215 workspace::open_paths(&paths, &app_state, None, cx).detach_and_log_err(cx);
216 }
217 } else {
218 upload_previous_panics(http.clone(), cx);
219
220 // TODO Development mode that forces the CLI mode usually runs Zed binary as is instead
221 // of an *app, hence gets no specific callbacks run. Emulate them here, if needed.
222 if std::env::var(FORCE_CLI_MODE_ENV_VAR_NAME).ok().is_some()
223 && !urls_callback_triggered.load(Ordering::Acquire)
224 {
225 open_urls(collect_url_args(), &cli_connections_tx, &open_paths_tx)
226 }
227
228 if let Ok(Some(connection)) = cli_connections_rx.try_next() {
229 cx.spawn(|cx| handle_cli_connection(connection, app_state.clone(), cx))
230 .detach();
231 } else if let Ok(Some(paths)) = open_paths_rx.try_next() {
232 cx.update(|cx| workspace::open_paths(&paths, &app_state, None, cx))
233 .detach();
234 } else {
235 cx.spawn({
236 let app_state = app_state.clone();
237 |cx| async move { restore_or_create_workspace(&app_state, cx).await }
238 })
239 .detach()
240 }
241
242 cx.spawn(|cx| {
243 let app_state = app_state.clone();
244 async move {
245 while let Some(connection) = cli_connections_rx.next().await {
246 handle_cli_connection(connection, app_state.clone(), cx.clone()).await;
247 }
248 }
249 })
250 .detach();
251
252 cx.spawn(|mut cx| {
253 let app_state = app_state.clone();
254 async move {
255 while let Some(paths) = open_paths_rx.next().await {
256 cx.update(|cx| workspace::open_paths(&paths, &app_state, None, cx))
257 .detach();
258 }
259 }
260 })
261 .detach();
262 }
263
264 cx.spawn(|cx| async move {
265 if stdout_is_a_pty() {
266 if client::IMPERSONATE_LOGIN.is_some() {
267 client.authenticate_and_connect(false, &cx).await?;
268 }
269 } else if client.has_keychain_credentials(&cx) {
270 client.authenticate_and_connect(true, &cx).await?;
271 }
272 Ok::<_, anyhow::Error>(())
273 })
274 .detach_and_log_err(cx);
275 });
276}
277
278fn open_urls(
279 urls: Vec<String>,
280 cli_connections_tx: &mpsc::UnboundedSender<(
281 mpsc::Receiver<CliRequest>,
282 IpcSender<CliResponse>,
283 )>,
284 open_paths_tx: &mpsc::UnboundedSender<Vec<PathBuf>>,
285) {
286 if let Some(server_name) = urls.first().and_then(|url| url.strip_prefix("zed-cli://")) {
287 if let Some(cli_connection) = connect_to_cli(server_name).log_err() {
288 cli_connections_tx
289 .unbounded_send(cli_connection)
290 .map_err(|_| anyhow!("no listener for cli connections"))
291 .log_err();
292 };
293 } else {
294 let paths: Vec<_> = urls
295 .iter()
296 .flat_map(|url| url.strip_prefix("file://"))
297 .map(|url| {
298 let decoded = urlencoding::decode_binary(url.as_bytes());
299 PathBuf::from(OsStr::from_bytes(decoded.as_ref()))
300 })
301 .collect();
302 open_paths_tx
303 .unbounded_send(paths)
304 .map_err(|_| anyhow!("no listener for open urls requests"))
305 .log_err();
306 }
307}
308
309async fn restore_or_create_workspace(app_state: &Arc<AppState>, mut cx: AsyncAppContext) {
310 if let Some(location) = workspace::last_opened_workspace_paths().await {
311 cx.update(|cx| workspace::open_paths(location.paths().as_ref(), app_state, None, cx))
312 .await
313 .log_err();
314 } else if matches!(KEY_VALUE_STORE.read_kvp(FIRST_OPEN), Ok(None)) {
315 cx.update(|cx| show_welcome_experience(app_state, cx));
316 } else {
317 cx.update(|cx| {
318 workspace::open_new(app_state, cx, |workspace, cx| {
319 Editor::new_file(workspace, &Default::default(), cx)
320 })
321 .detach();
322 });
323 }
324}
325
326fn init_paths() {
327 std::fs::create_dir_all(&*util::paths::CONFIG_DIR).expect("could not create config path");
328 std::fs::create_dir_all(&*util::paths::LANGUAGES_DIR).expect("could not create languages path");
329 std::fs::create_dir_all(&*util::paths::DB_DIR).expect("could not create database path");
330 std::fs::create_dir_all(&*util::paths::LOGS_DIR).expect("could not create logs path");
331}
332
333fn init_logger() {
334 if stdout_is_a_pty() {
335 env_logger::init();
336 } else {
337 let level = LevelFilter::Info;
338
339 // Prevent log file from becoming too large.
340 const KIB: u64 = 1024;
341 const MIB: u64 = 1024 * KIB;
342 const MAX_LOG_BYTES: u64 = MIB;
343 if std::fs::metadata(&*paths::LOG).map_or(false, |metadata| metadata.len() > MAX_LOG_BYTES)
344 {
345 let _ = std::fs::rename(&*paths::LOG, &*paths::OLD_LOG);
346 }
347
348 let log_file = OpenOptions::new()
349 .create(true)
350 .append(true)
351 .open(&*paths::LOG)
352 .expect("could not open logfile");
353
354 let config = ConfigBuilder::new()
355 .set_time_format_str("%Y-%m-%dT%T") //All timestamps are UTC
356 .build();
357
358 simplelog::WriteLogger::init(level, config, log_file).expect("could not initialize logger");
359 }
360}
361
362#[derive(Serialize, Deserialize)]
363struct LocationData {
364 file: String,
365 line: u32,
366}
367
368#[derive(Serialize, Deserialize)]
369struct Panic {
370 thread: String,
371 payload: String,
372 #[serde(skip_serializing_if = "Option::is_none")]
373 location_data: Option<LocationData>,
374 backtrace: Vec<String>,
375 release_channel: String,
376 os_name: String,
377 os_version: Option<String>,
378 architecture: String,
379 panicked_on: u128,
380 identifying_backtrace: Option<Vec<String>>,
381}
382
383#[derive(Serialize)]
384struct PanicRequest {
385 panic: Panic,
386 // TODO: Move to Panic struct, as app_version - requires changing zed.dev
387 version: String,
388 token: String,
389}
390
391fn init_panic_hook(app: &App) {
392 let is_pty = stdout_is_a_pty();
393 let platform = app.platform();
394
395 panic::set_hook(Box::new(move |info| {
396 let app_version = ZED_APP_VERSION
397 .or_else(|| platform.app_version().ok())
398 .map_or("dev".to_string(), |v| v.to_string());
399
400 let thread = thread::current();
401 let thread = thread.name().unwrap_or("<unnamed>");
402
403 let payload = info.payload();
404 let payload = None
405 .or_else(|| payload.downcast_ref::<&str>().map(|s| s.to_string()))
406 .or_else(|| payload.downcast_ref::<String>().map(|s| s.clone()))
407 .unwrap_or_else(|| "Box<Any>".to_string());
408
409 let backtrace = Backtrace::new();
410 let backtrace = backtrace
411 .frames()
412 .iter()
413 .filter_map(|frame| {
414 let symbol = frame.symbols().first()?;
415 let path = symbol.filename()?;
416 Some((path, symbol.lineno(), format!("{:#}", symbol.name()?)))
417 })
418 .collect::<Vec<_>>();
419
420 let this_file_path = Path::new(file!());
421
422 // Find the first frame in the backtrace for this panic hook itself. Exclude
423 // that frame and all frames before it.
424 let mut start_frame_ix = 0;
425 let mut codebase_root_path = None;
426 for (ix, (path, _, _)) in backtrace.iter().enumerate() {
427 if path.ends_with(this_file_path) {
428 start_frame_ix = ix + 1;
429 codebase_root_path = path.ancestors().nth(this_file_path.components().count());
430 break;
431 }
432 }
433
434 // Exclude any subsequent frames inside of rust's panic handling system.
435 while let Some((path, _, _)) = backtrace.get(start_frame_ix) {
436 if path.starts_with("/rustc") {
437 start_frame_ix += 1;
438 } else {
439 break;
440 }
441 }
442
443 // Build two backtraces:
444 // * one for display, which includes symbol names for all frames, and files
445 // and line numbers for symbols in this codebase
446 // * one for identification and de-duplication, which only includes symbol
447 // names for symbols in this codebase.
448 let mut display_backtrace = Vec::new();
449 let mut identifying_backtrace = Vec::new();
450 for (path, line, symbol) in &backtrace[start_frame_ix..] {
451 display_backtrace.push(symbol.clone());
452
453 if let Some(codebase_root_path) = &codebase_root_path {
454 if let Ok(suffix) = path.strip_prefix(&codebase_root_path) {
455 identifying_backtrace.push(symbol.clone());
456
457 let display_path = suffix.to_string_lossy();
458 if let Some(line) = line {
459 display_backtrace.push(format!(" {display_path}:{line}"));
460 } else {
461 display_backtrace.push(format!(" {display_path}"));
462 }
463 }
464 }
465 }
466
467 let panic_data = Panic {
468 thread: thread.into(),
469 payload: payload.into(),
470 location_data: info.location().map(|location| LocationData {
471 file: location.file().into(),
472 line: location.line(),
473 }),
474 release_channel: RELEASE_CHANNEL.dev_name().into(),
475 os_name: platform.os_name().into(),
476 os_version: platform
477 .os_version()
478 .ok()
479 .map(|os_version| os_version.to_string()),
480 architecture: env::consts::ARCH.into(),
481 panicked_on: SystemTime::now()
482 .duration_since(UNIX_EPOCH)
483 .unwrap()
484 .as_millis(),
485 backtrace: display_backtrace,
486 identifying_backtrace: identifying_backtrace
487 .is_empty()
488 .not()
489 .then_some(identifying_backtrace),
490 };
491
492 if let Some(panic_data_json) = serde_json::to_string_pretty(&panic_data).log_err() {
493 if is_pty {
494 eprintln!("{}", panic_data_json);
495 return;
496 }
497
498 let timestamp = chrono::Utc::now().format("%Y_%m_%d %H_%M_%S").to_string();
499 let panic_file_path =
500 paths::LOGS_DIR.join(format!("zed-{}-{}.panic", app_version, timestamp));
501 let panic_file = std::fs::OpenOptions::new()
502 .append(true)
503 .create(true)
504 .open(&panic_file_path)
505 .log_err();
506 if let Some(mut panic_file) = panic_file {
507 write!(&mut panic_file, "{}", panic_data_json).log_err();
508 panic_file.flush().log_err();
509 }
510 }
511 }));
512}
513
514fn upload_previous_panics(http: Arc<dyn HttpClient>, cx: &mut AppContext) {
515 let telemetry_settings = *settings::get::<TelemetrySettings>(cx);
516
517 cx.background()
518 .spawn({
519 async move {
520 let panic_report_url = format!("{}/api/panic", &*client::ZED_SERVER_URL);
521 let mut children = smol::fs::read_dir(&*paths::LOGS_DIR).await?;
522 while let Some(child) = children.next().await {
523 let child = child?;
524 let child_path = child.path();
525
526 if child_path.extension() != Some(OsStr::new("panic")) {
527 continue;
528 }
529 let filename = if let Some(filename) = child_path.file_name() {
530 filename.to_string_lossy()
531 } else {
532 continue;
533 };
534
535 let mut components = filename.split('-');
536 if components.next() != Some("zed") {
537 continue;
538 }
539 let version = if let Some(version) = components.next() {
540 version
541 } else {
542 continue;
543 };
544
545 if telemetry_settings.diagnostics {
546 let panic_data_text = smol::fs::read_to_string(&child_path)
547 .await
548 .context("error reading panic file")?;
549
550 let body = serde_json::to_string(&PanicRequest {
551 panic: serde_json::from_str(&panic_data_text)?,
552 version: version.to_string(),
553 token: ZED_SECRET_CLIENT_TOKEN.into(),
554 })
555 .unwrap();
556
557 let request = Request::post(&panic_report_url)
558 .redirect_policy(isahc::config::RedirectPolicy::Follow)
559 .header("Content-Type", "application/json")
560 .body(body.into())?;
561 let response = http.send(request).await.context("error sending panic")?;
562 if !response.status().is_success() {
563 log::error!("Error uploading panic to server: {}", response.status());
564 }
565 }
566
567 // We've done what we can, delete the file
568 std::fs::remove_file(child_path)
569 .context("error removing panic")
570 .log_err();
571 }
572 Ok::<_, anyhow::Error>(())
573 }
574 .log_err()
575 })
576 .detach();
577}
578
579async fn load_login_shell_environment() -> Result<()> {
580 let marker = "ZED_LOGIN_SHELL_START";
581 let shell = env::var("SHELL").context(
582 "SHELL environment variable is not assigned so we can't source login environment variables",
583 )?;
584 let output = Command::new(&shell)
585 .args(["-lic", &format!("echo {marker} && /usr/bin/env -0")])
586 .output()
587 .await
588 .context("failed to spawn login shell to source login environment variables")?;
589 if !output.status.success() {
590 Err(anyhow!("login shell exited with error"))?;
591 }
592
593 let stdout = String::from_utf8_lossy(&output.stdout);
594
595 if let Some(env_output_start) = stdout.find(marker) {
596 let env_output = &stdout[env_output_start + marker.len()..];
597 for line in env_output.split_terminator('\0') {
598 if let Some(separator_index) = line.find('=') {
599 let key = &line[..separator_index];
600 let value = &line[separator_index + 1..];
601 env::set_var(key, value);
602 }
603 }
604 log::info!(
605 "set environment variables from shell:{}, path:{}",
606 shell,
607 env::var("PATH").unwrap_or_default(),
608 );
609 }
610
611 Ok(())
612}
613
614fn stdout_is_a_pty() -> bool {
615 std::env::var(FORCE_CLI_MODE_ENV_VAR_NAME).ok().is_none()
616 && unsafe { libc::isatty(libc::STDOUT_FILENO as i32) != 0 }
617}
618
619fn collect_path_args() -> Vec<PathBuf> {
620 env::args()
621 .skip(1)
622 .filter_map(|arg| match std::fs::canonicalize(arg) {
623 Ok(path) => Some(path),
624 Err(error) => {
625 log::error!("error parsing path argument: {}", error);
626 None
627 }
628 })
629 .collect()
630}
631
632fn collect_url_args() -> Vec<String> {
633 env::args().skip(1).collect()
634}
635
636fn load_embedded_fonts(app: &App) {
637 let font_paths = Assets.list("fonts");
638 let embedded_fonts = Mutex::new(Vec::new());
639 smol::block_on(app.background().scoped(|scope| {
640 for font_path in &font_paths {
641 scope.spawn(async {
642 let font_path = &*font_path;
643 let font_bytes = Assets.load(font_path).unwrap().to_vec();
644 embedded_fonts.lock().push(Arc::from(font_bytes));
645 });
646 }
647 }));
648 app.platform()
649 .fonts()
650 .add_fonts(&embedded_fonts.into_inner())
651 .unwrap();
652}
653
654#[cfg(debug_assertions)]
655async fn watch_themes(fs: Arc<dyn Fs>, mut cx: AsyncAppContext) -> Option<()> {
656 let mut events = fs
657 .watch("styles/src".as_ref(), Duration::from_millis(100))
658 .await;
659 while (events.next().await).is_some() {
660 let output = Command::new("npm")
661 .current_dir("styles")
662 .args(["run", "build"])
663 .output()
664 .await
665 .log_err()?;
666 if output.status.success() {
667 cx.update(|cx| theme_selector::reload(cx))
668 } else {
669 eprintln!(
670 "build script failed {}",
671 String::from_utf8_lossy(&output.stderr)
672 );
673 }
674 }
675 Some(())
676}
677
678#[cfg(not(debug_assertions))]
679async fn watch_themes(_fs: Arc<dyn Fs>, _cx: AsyncAppContext) -> Option<()> {
680 None
681}
682
683fn connect_to_cli(
684 server_name: &str,
685) -> Result<(mpsc::Receiver<CliRequest>, IpcSender<CliResponse>)> {
686 let handshake_tx = cli::ipc::IpcSender::<IpcHandshake>::connect(server_name.to_string())
687 .context("error connecting to cli")?;
688 let (request_tx, request_rx) = ipc::channel::<CliRequest>()?;
689 let (response_tx, response_rx) = ipc::channel::<CliResponse>()?;
690
691 handshake_tx
692 .send(IpcHandshake {
693 requests: request_tx,
694 responses: response_rx,
695 })
696 .context("error sending ipc handshake")?;
697
698 let (mut async_request_tx, async_request_rx) =
699 futures::channel::mpsc::channel::<CliRequest>(16);
700 thread::spawn(move || {
701 while let Ok(cli_request) = request_rx.recv() {
702 if smol::block_on(async_request_tx.send(cli_request)).is_err() {
703 break;
704 }
705 }
706 Ok::<_, anyhow::Error>(())
707 });
708
709 Ok((async_request_rx, response_tx))
710}
711
712async fn handle_cli_connection(
713 (mut requests, responses): (mpsc::Receiver<CliRequest>, IpcSender<CliResponse>),
714 app_state: Arc<AppState>,
715 mut cx: AsyncAppContext,
716) {
717 if let Some(request) = requests.next().await {
718 match request {
719 CliRequest::Open { paths, wait } => {
720 let mut caret_positions = HashMap::new();
721
722 let paths = if paths.is_empty() {
723 workspace::last_opened_workspace_paths()
724 .await
725 .map(|location| location.paths().to_vec())
726 .unwrap_or_default()
727 } else {
728 paths
729 .into_iter()
730 .filter_map(|path_with_position_string| {
731 let path_with_position = PathLikeWithPosition::parse_str(
732 &path_with_position_string,
733 |path_str| {
734 Ok::<_, std::convert::Infallible>(
735 Path::new(path_str).to_path_buf(),
736 )
737 },
738 )
739 .expect("Infallible");
740 let path = path_with_position.path_like;
741 if let Some(row) = path_with_position.row {
742 if path.is_file() {
743 let row = row.saturating_sub(1);
744 let col =
745 path_with_position.column.unwrap_or(0).saturating_sub(1);
746 caret_positions.insert(path.clone(), Point::new(row, col));
747 }
748 }
749 Some(path)
750 })
751 .collect()
752 };
753
754 let mut errored = false;
755 match cx
756 .update(|cx| workspace::open_paths(&paths, &app_state, None, cx))
757 .await
758 {
759 Ok((workspace, items)) => {
760 let mut item_release_futures = Vec::new();
761
762 for (item, path) in items.into_iter().zip(&paths) {
763 match item {
764 Some(Ok(item)) => {
765 if let Some(point) = caret_positions.remove(path) {
766 if let Some(active_editor) = item.downcast::<Editor>() {
767 active_editor
768 .downgrade()
769 .update(&mut cx, |editor, cx| {
770 let snapshot =
771 editor.snapshot(cx).display_snapshot;
772 let point = snapshot
773 .buffer_snapshot
774 .clip_point(point, Bias::Left);
775 editor.change_selections(
776 Some(Autoscroll::center()),
777 cx,
778 |s| s.select_ranges([point..point]),
779 );
780 })
781 .log_err();
782 }
783 }
784
785 let released = oneshot::channel();
786 cx.update(|cx| {
787 item.on_release(
788 cx,
789 Box::new(move |_| {
790 let _ = released.0.send(());
791 }),
792 )
793 .detach();
794 });
795 item_release_futures.push(released.1);
796 }
797 Some(Err(err)) => {
798 responses
799 .send(CliResponse::Stderr {
800 message: format!("error opening {:?}: {}", path, err),
801 })
802 .log_err();
803 errored = true;
804 }
805 None => {}
806 }
807 }
808
809 if wait {
810 let background = cx.background();
811 let wait = async move {
812 if paths.is_empty() {
813 let (done_tx, done_rx) = oneshot::channel();
814 if let Some(workspace) = workspace.upgrade(&cx) {
815 let _subscription = cx.update(|cx| {
816 cx.observe_release(&workspace, move |_, _| {
817 let _ = done_tx.send(());
818 })
819 });
820 drop(workspace);
821 let _ = done_rx.await;
822 }
823 } else {
824 let _ =
825 futures::future::try_join_all(item_release_futures).await;
826 };
827 }
828 .fuse();
829 futures::pin_mut!(wait);
830
831 loop {
832 // Repeatedly check if CLI is still open to avoid wasting resources
833 // waiting for files or workspaces to close.
834 let mut timer = background.timer(Duration::from_secs(1)).fuse();
835 futures::select_biased! {
836 _ = wait => break,
837 _ = timer => {
838 if responses.send(CliResponse::Ping).is_err() {
839 break;
840 }
841 }
842 }
843 }
844 }
845 }
846 Err(error) => {
847 errored = true;
848 responses
849 .send(CliResponse::Stderr {
850 message: format!("error opening {:?}: {}", paths, error),
851 })
852 .log_err();
853 }
854 }
855
856 responses
857 .send(CliResponse::Exit {
858 status: i32::from(errored),
859 })
860 .log_err();
861 }
862 }
863 }
864}
865
866pub fn dock_default_item_factory(
867 workspace: &mut Workspace,
868 cx: &mut ViewContext<Workspace>,
869) -> Option<Box<dyn ItemHandle>> {
870 let strategy = settings::get::<TerminalSettings>(cx)
871 .working_directory
872 .clone();
873 let working_directory = get_working_directory(workspace, cx, strategy);
874
875 let window_id = cx.window_id();
876 let terminal = workspace
877 .project()
878 .update(cx, |project, cx| {
879 project.create_terminal(working_directory, window_id, cx)
880 })
881 .notify_err(workspace, cx)?;
882
883 let terminal_view = cx.add_view(|cx| TerminalView::new(terminal, workspace.database_id(), cx));
884
885 Some(Box::new(terminal_view))
886}
887
888pub fn background_actions() -> &'static [(&'static str, &'static dyn Action)] {
889 &[
890 ("Go to file", &file_finder::Toggle),
891 ("Open command palette", &command_palette::Toggle),
892 ("Open recent projects", &recent_projects::OpenRecent),
893 ("Change your settings", &OpenSettings),
894 ]
895}