1use client::{Client, ProxySettings, UserStore};
2use collections::HashMap;
3use extension::ExtensionHostProxy;
4use fs::RealFs;
5use gpui::http_client::read_proxy_from_env;
6use gpui::{App, AppContext, Entity};
7use gpui_tokio::Tokio;
8use language::LanguageRegistry;
9use language_extension::LspAccess;
10use node_runtime::{NodeBinaryOptions, NodeRuntime};
11use project::{Project, project_settings::ProjectSettings};
12use release_channel::{AppCommitSha, AppVersion};
13use reqwest_client::ReqwestClient;
14use settings::{Settings, SettingsStore};
15use std::path::PathBuf;
16use std::sync::{Arc, Mutex};
17use util::ResultExt as _;
18
19/// Headless subset of `workspace::AppState`.
20pub struct EpAppState {
21 pub languages: Arc<LanguageRegistry>,
22 pub client: Arc<Client>,
23 pub user_store: Entity<UserStore>,
24 pub fs: Arc<dyn fs::Fs>,
25 pub node_runtime: NodeRuntime,
26 pub project_cache: ProjectCache,
27}
28
29#[derive(Default)]
30pub struct ProjectCache(Mutex<HashMap<String, Entity<Project>>>);
31
32impl ProjectCache {
33 pub fn insert(&self, repository_url: String, project: Entity<Project>) {
34 self.0.lock().unwrap().insert(repository_url, project);
35 }
36
37 pub fn get(&self, repository_url: &String) -> Option<Entity<Project>> {
38 self.0.lock().unwrap().get(repository_url).cloned()
39 }
40
41 pub fn remove(&self, repository_url: &String) {
42 self.0.lock().unwrap().remove(repository_url);
43 }
44}
45
46pub fn init(cx: &mut App) -> EpAppState {
47 let app_commit_sha = option_env!("ZED_COMMIT_SHA").map(|s| AppCommitSha::new(s.to_owned()));
48
49 let app_version = AppVersion::load(
50 env!("ZED_PKG_VERSION"),
51 option_env!("ZED_BUILD_ID"),
52 app_commit_sha,
53 );
54 release_channel::init(app_version.clone(), cx);
55 gpui_tokio::init(cx);
56
57 let settings_store = SettingsStore::new(cx, &settings::default_settings());
58 cx.set_global(settings_store);
59
60 // Set User-Agent so we can download language servers from GitHub
61 let user_agent = format!(
62 "Zeta CLI/{} ({}; {})",
63 app_version,
64 std::env::consts::OS,
65 std::env::consts::ARCH
66 );
67 let proxy_str = ProxySettings::get_global(cx).proxy.to_owned();
68 let proxy_url = proxy_str
69 .as_ref()
70 .and_then(|input| input.parse().ok())
71 .or_else(read_proxy_from_env);
72 let http = {
73 let _guard = Tokio::handle(cx).enter();
74
75 ReqwestClient::proxy_and_user_agent(proxy_url, &user_agent)
76 .expect("could not start HTTP client")
77 };
78 cx.set_http_client(Arc::new(http));
79
80 let client = Client::production(cx);
81 cx.set_http_client(client.http_client());
82
83 let git_binary_path = None;
84 let fs = Arc::new(RealFs::new(
85 git_binary_path,
86 cx.background_executor().clone(),
87 ));
88
89 let mut languages = LanguageRegistry::new(cx.background_executor().clone());
90 languages.set_language_server_download_dir(paths::languages_dir().clone());
91 let languages = Arc::new(languages);
92
93 let user_store = cx.new(|cx| UserStore::new(client.clone(), cx));
94
95 extension::init(cx);
96
97 let (mut tx, rx) = watch::channel(None);
98 cx.observe_global::<SettingsStore>(move |cx| {
99 let settings = &ProjectSettings::get_global(cx).node;
100 let options = NodeBinaryOptions {
101 allow_path_lookup: !settings.ignore_system_version,
102 allow_binary_download: true,
103 use_paths: settings.path.as_ref().map(|node_path| {
104 let node_path = PathBuf::from(shellexpand::tilde(node_path).as_ref());
105 let npm_path = settings
106 .npm_path
107 .as_ref()
108 .map(|path| PathBuf::from(shellexpand::tilde(&path).as_ref()));
109 (
110 node_path.clone(),
111 npm_path.unwrap_or_else(|| {
112 let base_path = PathBuf::new();
113 node_path.parent().unwrap_or(&base_path).join("npm")
114 }),
115 )
116 }),
117 };
118 tx.send(Some(options)).log_err();
119 })
120 .detach();
121 let node_runtime = NodeRuntime::new(client.http_client(), None, rx);
122
123 let extension_host_proxy = ExtensionHostProxy::global(cx);
124
125 debug_adapter_extension::init(extension_host_proxy.clone(), cx);
126 language_extension::init(LspAccess::Noop, extension_host_proxy, languages.clone());
127 language_model::init(client.clone(), cx);
128 language_models::init(user_store.clone(), client.clone(), cx);
129 languages::init(languages.clone(), fs.clone(), node_runtime.clone(), cx);
130 prompt_store::init(cx);
131 terminal_view::init(cx);
132
133 let project_cache = ProjectCache::default();
134
135 EpAppState {
136 languages,
137 client,
138 user_store,
139 fs,
140 node_runtime,
141 project_cache,
142 }
143}