headless.rs

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