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