Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Refactor out the node runtime crate and hook up all related imports
Mikayla Maki941da24f7375
diff --git a/Cargo.lock b/Cargo.lockindex 65d68aa3a89..7654a1ac471 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1343,6 +1343,7 @@ dependencies = ["language","log","lsp",+ "node_runtime","serde","serde_derive","settings",@@ -3931,6 +3932,23 @@ dependencies = ["memoffset 0.6.5",]+[[package]]+name = "node_runtime"+version = "0.1.0"+dependencies = [+ "anyhow",+ "async-compression",+ "async-tar",+ "futures 0.3.25",+ "gpui",+ "parking_lot 0.11.2",+ "serde",+ "serde_derive",+ "serde_json",+ "smol",+ "util",+]+[[package]]name = "nom"version = "7.1.1"@@ -8512,6 +8530,7 @@ dependencies = ["libc","log","lsp",+ "node_runtime","num_cpus","outline","parking_lot 0.11.2",diff --git a/Cargo.toml b/Cargo.tomlindex bf9214f49ee..f097b5b2c7c 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -36,6 +36,7 @@ members = ["crates/lsp","crates/media","crates/menu",+ "crates/node_runtime","crates/outline","crates/picker","crates/plugin",diff --git a/crates/copilot/Cargo.toml b/crates/copilot/Cargo.tomlindex a7582a6ffc5..74dd73df0b5 100644--- a/crates/copilot/Cargo.toml+++ b/crates/copilot/Cargo.toml@@ -14,6 +14,7 @@ language = { path = "../language" }settings = { path = "../settings" }theme = { path = "../theme" }lsp = { path = "../lsp" }+node_runtime = { path = "../node_runtime"}util = { path = "../util" }client = { path = "../client" }workspace = { path = "../workspace" }diff --git a/crates/copilot/src/copilot.rs b/crates/copilot/src/copilot.rsindex 5ad32ed3a57..d12e5995d0c 100644--- a/crates/copilot/src/copilot.rs+++ b/crates/copilot/src/copilot.rs@@ -8,6 +8,7 @@ use futures::{future::Shared, FutureExt, TryFutureExt};use gpui::{actions, AppContext, Entity, ModelContext, ModelHandle, MutableAppContext, Task};use language::{point_from_lsp, point_to_lsp, Anchor, Bias, Buffer, BufferSnapshot, ToPointUtf16};use lsp::LanguageServer;+use node_runtime::NodeRuntime;use settings::Settings;use smol::{fs, io::BufReader, stream::StreamExt};use std::{@@ -21,8 +22,8 @@ use util::{actions!(copilot, [SignIn, SignOut, NextSuggestion]);-pub fn init(client: Arc<Client>, cx: &mut MutableAppContext) {- let copilot = cx.add_model(|cx| Copilot::start(client.http_client(), cx));+pub fn init(client: Arc<Client>, node_runtime: Arc<NodeRuntime>, cx: &mut MutableAppContext) {+ let copilot = cx.add_model(|cx| Copilot::start(client.http_client(), node_runtime, cx));cx.set_global(copilot.clone());cx.add_global_action(|_: &SignIn, cx| {let copilot = Copilot::global(cx).unwrap();@@ -104,7 +105,11 @@ impl Copilot {}}- fn start(http: Arc<dyn HttpClient>, cx: &mut ModelContext<Self>) -> Self {+ fn start(+ http: Arc<dyn HttpClient>,+ node_runtime: Arc<NodeRuntime>,+ cx: &mut ModelContext<Self>,+ ) -> Self {// TODO: Don't eagerly download the LSPcx.spawn(|this, mut cx| async move {let start_language_server = async {@@ -469,7 +474,8 @@ mod tests {async fn test_smoke(cx: &mut TestAppContext) {Settings::test_async(cx);let http = http::client();- let copilot = cx.add_model(|cx| Copilot::start(http, cx));+ let node_runtime = NodeRuntime::new(http.clone(), cx.background());+ let copilot = cx.add_model(|cx| Copilot::start(http, node_runtime, cx));smol::Timer::after(std::time::Duration::from_secs(2)).await;copilot.update(cx, |copilot, cx| copilot.sign_in(cx))diff --git a/crates/node_runtime/Cargo.toml b/crates/node_runtime/Cargo.tomlnew file mode 100644index 00000000000..32ab6abbb3c--- /dev/null+++ b/crates/node_runtime/Cargo.toml@@ -0,0 +1,22 @@+[package]+name = "node_runtime"+version = "0.1.0"+edition = "2021"+publish = false++[lib]+path = "src/node_runtime.rs"+doctest = false++[dependencies]+gpui = { path = "../gpui" }+util = { path = "../util" }+async-compression = { version = "0.3", features = ["gzip", "futures-bufread"] }+async-tar = "0.4.2"+futures = "0.3"+anyhow = "1.0.38"+parking_lot = "0.11.1"+serde = { workspace = true }+serde_derive = { workspace = true }+serde_json = { workspace = true }+smol = "1.2.5"diff --git a/crates/zed/src/languages/node_runtime.rs b/crates/node_runtime/src/node_runtime.rssimilarity index 100%rename from crates/zed/src/languages/node_runtime.rsrename to crates/node_runtime/src/node_runtime.rsdiff --git a/crates/util/src/github.rs b/crates/util/src/github.rsindex 02082108e14..5170bd6f4f3 100644--- a/crates/util/src/github.rs+++ b/crates/util/src/github.rs@@ -4,6 +4,11 @@ use futures::AsyncReadExt;use serde::Deserialize;use std::sync::Arc;+pub struct GitHubLspBinaryVersion {+ pub name: String,+ pub url: String,+}+#[derive(Deserialize)]pub struct GithubRelease {pub name: String,diff --git a/crates/zed/Cargo.toml b/crates/zed/Cargo.tomlindex a9cbfbc7374..72470e216b4 100644--- a/crates/zed/Cargo.toml+++ b/crates/zed/Cargo.toml@@ -45,6 +45,7 @@ journal = { path = "../journal" }language = { path = "../language" }language_selector = { path = "../language_selector" }lsp = { path = "../lsp" }+node_runtime = { path = "../node_runtime" }outline = { path = "../outline" }plugin_runtime = { path = "../plugin_runtime" }project = { path = "../project" }diff --git a/crates/zed/src/languages.rs b/crates/zed/src/languages.rsindex 3a23afb9700..12e6c1b1f2e 100644--- a/crates/zed/src/languages.rs+++ b/crates/zed/src/languages.rs@@ -1,21 +1,17 @@use anyhow::Context;-use gpui::executor::Background;pub use language::*;use node_runtime::NodeRuntime;use rust_embed::RustEmbed;use std::{borrow::Cow, str, sync::Arc};use theme::ThemeRegistry;-use util::http::HttpClient;mod c;mod elixir;-mod github;mod go;mod html;mod json;mod language_plugin;mod lua;-mod node_runtime;mod python;mod ruby;mod rust;@@ -37,13 +33,10 @@ mod yaml;struct LanguageDir;pub fn init(- http: Arc<dyn HttpClient>,- background: Arc<Background>,languages: Arc<LanguageRegistry>,themes: Arc<ThemeRegistry>,+ node_runtime: Arc<NodeRuntime>,) {- let node_runtime = NodeRuntime::new(http, background);-for (name, grammar, lsp_adapter) in [("c",diff --git a/crates/zed/src/languages/c.rs b/crates/zed/src/languages/c.rsindex 88f5c4553b8..e142028196d 100644--- a/crates/zed/src/languages/c.rs+++ b/crates/zed/src/languages/c.rs@@ -9,7 +9,7 @@ use util::github::latest_github_release;use util::http::HttpClient;use util::ResultExt;-use super::github::GitHubLspBinaryVersion;+use util::github::GitHubLspBinaryVersion;pub struct CLspAdapter;diff --git a/crates/zed/src/languages/elixir.rs b/crates/zed/src/languages/elixir.rsindex ecd4028fe0b..a2debcdb2d5 100644--- a/crates/zed/src/languages/elixir.rs+++ b/crates/zed/src/languages/elixir.rs@@ -10,7 +10,7 @@ use util::github::latest_github_release;use util::http::HttpClient;use util::ResultExt;-use super::github::GitHubLspBinaryVersion;+use util::github::GitHubLspBinaryVersion;pub struct ElixirLspAdapter;diff --git a/crates/zed/src/languages/github.rs b/crates/zed/src/languages/github.rsdeleted file mode 100644index 9e0dd9b582a..00000000000--- a/crates/zed/src/languages/github.rs+++ /dev/null@@ -1,45 +0,0 @@-use anyhow::{Context, Result};-use serde::Deserialize;-use smol::io::AsyncReadExt;-use std::sync::Arc;-use util::http::HttpClient;--pub struct GitHubLspBinaryVersion {- pub name: String,- pub url: String,-}--#[derive(Deserialize)]-pub(crate) struct GithubRelease {- pub name: String,- pub assets: Vec<GithubReleaseAsset>,-}--#[derive(Deserialize)]-pub(crate) struct GithubReleaseAsset {- pub name: String,- pub browser_download_url: String,-}--pub(crate) async fn latest_github_release(- repo_name_with_owner: &str,- http: Arc<dyn HttpClient>,-) -> Result<GithubRelease, anyhow::Error> {- let mut response = http- .get(- &format!("https://api.github.com/repos/{repo_name_with_owner}/releases/latest"),- Default::default(),- true,- )- .await- .context("error fetching latest release")?;- let mut body = Vec::new();- response- .body_mut()- .read_to_end(&mut body)- .await- .context("error reading latest release")?;- let release: GithubRelease =- serde_json::from_slice(body.as_slice()).context("error deserializing latest release")?;- Ok(release)-}diff --git a/crates/zed/src/languages/html.rs b/crates/zed/src/languages/html.rsindex f77b264fbfa..db743f02af7 100644--- a/crates/zed/src/languages/html.rs+++ b/crates/zed/src/languages/html.rs@@ -1,4 +1,4 @@-use super::node_runtime::NodeRuntime;+use node_runtime::NodeRuntime;use anyhow::{anyhow, Context, Result};use async_trait::async_trait;use futures::StreamExt;diff --git a/crates/zed/src/languages/json.rs b/crates/zed/src/languages/json.rsindex 97c158fd1f8..4da10566852 100644--- a/crates/zed/src/languages/json.rs+++ b/crates/zed/src/languages/json.rs@@ -1,4 +1,4 @@-use super::node_runtime::NodeRuntime;+use node_runtime::NodeRuntime;use anyhow::{anyhow, Context, Result};use async_trait::async_trait;use collections::HashMap;diff --git a/crates/zed/src/languages/lua.rs b/crates/zed/src/languages/lua.rsindex f16761d8704..2a18138cb71 100644--- a/crates/zed/src/languages/lua.rs+++ b/crates/zed/src/languages/lua.rs@@ -8,7 +8,7 @@ use smol::fs;use std::{any::Any, env::consts, ffi::OsString, path::PathBuf, sync::Arc};use util::{async_iife, github::latest_github_release, http::HttpClient, ResultExt};-use super::github::GitHubLspBinaryVersion;+use util::github::GitHubLspBinaryVersion;#[derive(Copy, Clone)]pub struct LuaLspAdapter;diff --git a/crates/zed/src/languages/python.rs b/crates/zed/src/languages/python.rsindex 3a671c60f6e..6227b8336d7 100644--- a/crates/zed/src/languages/python.rs+++ b/crates/zed/src/languages/python.rs@@ -1,4 +1,4 @@-use super::node_runtime::NodeRuntime;+use node_runtime::NodeRuntime;use anyhow::{anyhow, Context, Result};use async_trait::async_trait;use futures::StreamExt;diff --git a/crates/zed/src/languages/rust.rs b/crates/zed/src/languages/rust.rsindex b95a64fa1eb..d5a67731292 100644--- a/crates/zed/src/languages/rust.rs+++ b/crates/zed/src/languages/rust.rs@@ -1,4 +1,3 @@-use super::github::{latest_github_release, GitHubLspBinaryVersion};use anyhow::{anyhow, Result};use async_compression::futures::bufread::GzipDecoder;use async_trait::async_trait;@@ -9,6 +8,7 @@ use regex::Regex;use smol::fs::{self, File};use std::{any::Any, borrow::Cow, env::consts, path::PathBuf, str, sync::Arc};use util::fs::remove_matching;+use util::github::{latest_github_release, GitHubLspBinaryVersion};use util::http::HttpClient;use util::ResultExt;diff --git a/crates/zed/src/languages/typescript.rs b/crates/zed/src/languages/typescript.rsindex d3704c84c87..69a135e6ec6 100644--- a/crates/zed/src/languages/typescript.rs+++ b/crates/zed/src/languages/typescript.rs@@ -1,8 +1,8 @@-use super::node_runtime::NodeRuntime;use anyhow::{anyhow, Context, Result};use async_trait::async_trait;use futures::StreamExt;use language::{LanguageServerBinary, LanguageServerName, LspAdapter};+use node_runtime::NodeRuntime;use serde_json::json;use smol::fs;use std::{diff --git a/crates/zed/src/languages/yaml.rs b/crates/zed/src/languages/yaml.rsindex 6028ecd1343..7339512f1a3 100644--- a/crates/zed/src/languages/yaml.rs+++ b/crates/zed/src/languages/yaml.rs@@ -1,9 +1,9 @@-use super::node_runtime::NodeRuntime;use anyhow::{anyhow, Context, Result};use async_trait::async_trait;use futures::{future::BoxFuture, FutureExt, StreamExt};use gpui::MutableAppContext;use language::{LanguageServerBinary, LanguageServerName, LspAdapter};+use node_runtime::NodeRuntime;use serde_json::Value;use settings::Settings;use smol::fs;diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rsindex c88f2e94f9a..8f7b858dfd7 100644--- a/crates/zed/src/main.rs+++ b/crates/zed/src/main.rs@@ -18,6 +18,7 @@ use gpui::{Action, App, AssetSource, AsyncAppContext, MutableAppContext, Task, Vuse isahc::{config::Configurable, Request};use language::LanguageRegistry;use log::LevelFilter;+use node_runtime::NodeRuntime;use parking_lot::Mutex;use project::Fs;use serde_json::json;@@ -136,12 +137,9 @@ fn main() {languages.set_executor(cx.background().clone());languages.set_language_server_download_dir(paths::LANGUAGES_DIR.clone());let languages = Arc::new(languages);- languages::init(- http.clone(),- cx.background().clone(),- languages.clone(),- themes.clone(),- );+ let node_runtime = NodeRuntime::new(http.clone(), cx.background().to_owned());++ languages::init(languages.clone(), themes.clone(), node_runtime.clone());let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http.clone(), cx));cx.set_global(client.clone());@@ -162,7 +160,7 @@ fn main() {terminal_view::init(cx);theme_testbench::init(cx);recent_projects::init(cx);- copilot::init(client.clone(), cx);+ copilot::init(client.clone(), node_runtime, cx);cx.spawn(|cx| watch_themes(fs.clone(), themes.clone(), cx)).detach();diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rsindex 32706cb47f6..43172d07fa2 100644--- a/crates/zed/src/zed.rs+++ b/crates/zed/src/zed.rs@@ -657,6 +657,7 @@ mod tests {executor::Deterministic, AssetSource, MutableAppContext, TestAppContext, ViewHandle,};use language::LanguageRegistry;+ use node_runtime::NodeRuntime;use project::{Project, ProjectPath};use serde_json::json;use std::{@@ -1851,12 +1852,9 @@ mod tests {languages.set_executor(cx.background().clone());let languages = Arc::new(languages);let themes = ThemeRegistry::new((), cx.font_cache().clone());- languages::init(- FakeHttpClient::with_404_response(),- cx.background().clone(),- languages.clone(),- themes,- );+ let http = FakeHttpClient::with_404_response();+ let node_runtime = NodeRuntime::new(http, cx.background().to_owned());+ languages::init(languages.clone(), themes, node_runtime);for name in languages.language_names() {languages.language_for_name(&name);}