Download language servers on-demand

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2022-02-21 13:54:52 -08:00
parent db23a87228
commit ededfff3a8
10 changed files with 172 additions and 151 deletions

1
Cargo.lock generated
View File

@ -2620,6 +2620,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"async-trait",
"client",
"clock",
"collections",
"ctor",

View File

@ -224,6 +224,10 @@ impl Client {
self.id
}
pub fn http_client(&self) -> Arc<dyn HttpClient> {
self.http.clone()
}
#[cfg(any(test, feature = "test-support"))]
pub fn override_authenticate<F>(&mut self, authenticate: F) -> &mut Self
where

View File

@ -9,6 +9,7 @@ path = "src/language.rs"
[features]
test-support = [
"rand",
"client/test-support",
"collections/test-support",
"lsp/test-support",
"text/test-support",
@ -17,6 +18,7 @@ test-support = [
]
[dependencies]
client = { path = "../client" }
clock = { path = "../clock" }
collections = { path = "../collections" }
fuzzy = { path = "../fuzzy" }
@ -44,6 +46,7 @@ tree-sitter = "0.20"
tree-sitter-rust = { version = "0.20.0", optional = true }
[dev-dependencies]
client = { path = "../client", features = ["test-support"] }
collections = { path = "../collections", features = ["test-support"] }
gpui = { path = "../gpui", features = ["test-support"] }
lsp = { path = "../lsp", features = ["test-support"] }

View File

@ -7,12 +7,13 @@ pub mod proto;
mod tests;
use anyhow::{anyhow, Result};
use client::http::HttpClient;
use collections::HashSet;
use futures::{
future::{BoxFuture, Shared},
FutureExt,
FutureExt, TryFutureExt,
};
use gpui::{executor, AppContext, Task};
use gpui::{AppContext, Task};
use highlight_map::HighlightMap;
use lazy_static::lazy_static;
use parking_lot::Mutex;
@ -20,7 +21,6 @@ use postage::watch;
use serde::Deserialize;
use std::{
cell::RefCell,
future::Future,
ops::Range,
path::{Path, PathBuf},
str,
@ -60,7 +60,10 @@ pub trait ToLspPosition {
}
pub trait LspExt: 'static + Send + Sync {
fn server_bin_path(&self) -> BoxFuture<'static, Option<PathBuf>>;
fn fetch_latest_language_server(
&self,
http: Arc<dyn HttpClient>,
) -> BoxFuture<'static, Result<PathBuf>>;
fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams);
fn label_for_completion(
&self,
@ -116,7 +119,7 @@ pub struct Language {
pub(crate) config: LanguageConfig,
pub(crate) grammar: Option<Arc<Grammar>>,
pub(crate) lsp_ext: Option<Box<dyn LspExt>>,
lsp_binary_path: Mutex<Option<Shared<BoxFuture<'static, Option<PathBuf>>>>>,
lsp_binary_path: Mutex<Option<Shared<BoxFuture<'static, Result<PathBuf, Arc<anyhow::Error>>>>>>,
}
pub struct Grammar {
@ -144,17 +147,8 @@ impl LanguageRegistry {
}
}
pub fn add(&mut self, language: Arc<Language>, cx: &executor::Background) {
pub fn add(&mut self, language: Arc<Language>) {
self.languages.push(language.clone());
if let Some(lsp_binary_path) = language.lsp_binary_path() {
let pending_lsp_binaries_tx = self.pending_lsp_binaries_tx.clone();
cx.spawn(async move {
*pending_lsp_binaries_tx.lock().borrow_mut() += 1;
lsp_binary_path.await;
*pending_lsp_binaries_tx.lock().borrow_mut() -= 1;
})
.detach();
}
}
pub fn set_theme(&self, theme: &SyntaxTheme) {
@ -183,6 +177,71 @@ impl LanguageRegistry {
})
}
pub fn start_language_server(
&self,
language: &Arc<Language>,
root_path: Arc<Path>,
http_client: Arc<dyn HttpClient>,
cx: &AppContext,
) -> Option<Task<Result<Arc<lsp::LanguageServer>>>> {
#[cfg(any(test, feature = "test-support"))]
if let Some(config) = &language.config.language_server {
if let Some(fake_config) = &config.fake_config {
use postage::prelude::Stream;
let (server, mut fake_server) = lsp::LanguageServer::fake_with_capabilities(
fake_config.capabilities.clone(),
cx.background().clone(),
);
if let Some(initalizer) = &fake_config.initializer {
initalizer(&mut fake_server);
}
let servers_tx = fake_config.servers_tx.clone();
let mut initialized = server.capabilities();
cx.background()
.spawn(async move {
while initialized.recv().await.is_none() {}
servers_tx.unbounded_send(fake_server).ok();
})
.detach();
return Some(Task::ready(Ok(server.clone())));
}
}
let lsp_ext = language.lsp_ext.as_ref()?;
let background = cx.background().clone();
let server_binary_path = {
Some(
language
.lsp_binary_path
.lock()
.get_or_insert_with(|| {
let pending_lsp_binaries_tx = self.pending_lsp_binaries_tx.clone();
let language_server_path =
lsp_ext.fetch_latest_language_server(http_client);
async move {
*pending_lsp_binaries_tx.lock().borrow_mut() += 1;
let path = language_server_path.map_err(Arc::new).await;
*pending_lsp_binaries_tx.lock().borrow_mut() -= 1;
path
}
.boxed()
.shared()
})
.clone()
.map_err(|e| anyhow!(e)),
)
}?;
Some(cx.background().spawn(async move {
let server_binary_path = server_binary_path.await?;
let server = lsp::LanguageServer::new(&server_binary_path, &root_path, background)?;
Ok(server)
}))
}
pub fn pending_lsp_binaries(&self) -> watch::Receiver<usize> {
self.pending_lsp_binaries_rx.clone()
}
@ -260,52 +319,6 @@ impl Language {
self.config.line_comment.as_deref()
}
pub fn start_server(
&self,
root_path: Arc<Path>,
cx: &AppContext,
) -> Task<Result<Option<Arc<lsp::LanguageServer>>>> {
#[cfg(any(test, feature = "test-support"))]
if let Some(config) = &self.config.language_server {
if let Some(fake_config) = &config.fake_config {
use postage::prelude::Stream;
let (server, mut fake_server) = lsp::LanguageServer::fake_with_capabilities(
fake_config.capabilities.clone(),
cx.background().clone(),
);
if let Some(initalizer) = &fake_config.initializer {
initalizer(&mut fake_server);
}
let servers_tx = fake_config.servers_tx.clone();
let mut initialized = server.capabilities();
cx.background()
.spawn(async move {
while initialized.recv().await.is_none() {}
servers_tx.unbounded_send(fake_server).ok();
})
.detach();
return Task::ready(Ok(Some(server.clone())));
}
}
let background = cx.background().clone();
let server_binary_path = self
.lsp_binary_path()
.ok_or_else(|| anyhow!("cannot locate or download language server"));
cx.background().spawn(async move {
if let Some(server_binary_path) = server_binary_path?.await {
let server = lsp::LanguageServer::new(&server_binary_path, &root_path, background)?;
Ok(Some(server))
} else {
Ok(None)
}
})
}
pub fn disk_based_diagnostic_sources(&self) -> Option<&HashSet<String>> {
self.config
.language_server
@ -356,19 +369,6 @@ impl Language {
result
}
fn lsp_binary_path(&self) -> Option<impl Future<Output = Option<PathBuf>>> {
if let Some(lsp_ext) = self.lsp_ext.as_ref() {
Some(
self.lsp_binary_path
.lock()
.get_or_insert_with(|| lsp_ext.server_bin_path().shared())
.clone(),
)
} else {
None
}
}
pub fn brackets(&self) -> &[BracketPair] {
&self.config.brackets
}

View File

@ -23,30 +23,24 @@ fn init_logger() {
}
#[gpui::test]
fn test_select_language(cx: &mut MutableAppContext) {
fn test_select_language() {
let mut registry = LanguageRegistry::new();
registry.add(
Arc::new(Language::new(
LanguageConfig {
name: "Rust".to_string(),
path_suffixes: vec!["rs".to_string()],
..Default::default()
},
Some(tree_sitter_rust::language()),
)),
cx.background(),
);
registry.add(
Arc::new(Language::new(
LanguageConfig {
name: "Make".to_string(),
path_suffixes: vec!["Makefile".to_string(), "mk".to_string()],
..Default::default()
},
Some(tree_sitter_rust::language()),
)),
cx.background(),
);
registry.add(Arc::new(Language::new(
LanguageConfig {
name: "Rust".to_string(),
path_suffixes: vec!["rs".to_string()],
..Default::default()
},
Some(tree_sitter_rust::language()),
)));
registry.add(Arc::new(Language::new(
LanguageConfig {
name: "Make".to_string(),
path_suffixes: vec!["Makefile".to_string(), "mk".to_string()],
..Default::default()
},
Some(tree_sitter_rust::language()),
)));
// matching file extension
assert_eq!(

View File

@ -835,20 +835,21 @@ impl Project {
self.started_language_servers
.entry(key.clone())
.or_insert_with(|| {
let language_server = language.start_server(worktree_path, cx);
let language_server = self.languages.start_language_server(
&language,
worktree_path,
self.client.http_client(),
cx,
);
let rpc = self.client.clone();
cx.spawn_weak(|this, mut cx| async move {
let language_server = language_server.await.log_err().flatten();
let language_server = language_server?.await.log_err()?;
if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |this, _| {
if let Some(language_server) = language_server.clone() {
this.language_servers.insert(key, language_server);
}
this.language_servers.insert(key, language_server.clone());
});
}
let language_server = language_server?;
let disk_based_sources = language
.disk_based_diagnostic_sources()
.cloned()
@ -3069,10 +3070,8 @@ mod tests {
.await;
let project = Project::test(fs, &mut cx);
project.update(&mut cx, |project, cx| {
Arc::get_mut(&mut project.languages)
.unwrap()
.add(language, cx.background());
project.update(&mut cx, |project, _| {
Arc::get_mut(&mut project.languages).unwrap().add(language);
});
let (tree, _) = project
@ -3217,10 +3216,8 @@ mod tests {
.await;
let project = Project::test(fs, &mut cx);
project.update(&mut cx, |project, cx| {
Arc::get_mut(&mut project.languages)
.unwrap()
.add(language, cx.background());
project.update(&mut cx, |project, _| {
Arc::get_mut(&mut project.languages).unwrap().add(language);
});
let (tree, _) = project
@ -4112,10 +4109,8 @@ mod tests {
.await;
let project = Project::test(fs.clone(), &mut cx);
project.update(&mut cx, |project, cx| {
Arc::get_mut(&mut project.languages)
.unwrap()
.add(language, cx.background());
project.update(&mut cx, |project, _| {
Arc::get_mut(&mut project.languages).unwrap().add(language);
});
let (tree, _) = project

View File

@ -2011,7 +2011,7 @@ mod tests {
},
Some(tree_sitter_rust::language()),
)),
&cx_a.background(),
);
// Connect to a server as 2 clients.
@ -2243,7 +2243,7 @@ mod tests {
},
Some(tree_sitter_rust::language()),
)),
&cx_a.background(),
);
// Connect to a server as 2 clients.
@ -2446,7 +2446,7 @@ mod tests {
},
Some(tree_sitter_rust::language()),
)),
&cx_a.background(),
);
// Connect to a server as 2 clients.
@ -2564,7 +2564,7 @@ mod tests {
},
Some(tree_sitter_rust::language()),
)),
&cx_a.background(),
);
// Connect to a server as 2 clients.
@ -2713,7 +2713,7 @@ mod tests {
},
Some(tree_sitter_rust::language()),
)),
&cx_a.background(),
);
// Connect to a server as 2 clients.
@ -2815,7 +2815,7 @@ mod tests {
},
Some(tree_sitter_rust::language()),
)),
&cx_a.background(),
);
// Connect to a server as 2 clients.
@ -3055,7 +3055,7 @@ mod tests {
},
Some(tree_sitter_rust::language()),
)),
&cx_a.background(),
);
// Connect to a server as 2 clients.
@ -3862,7 +3862,7 @@ mod tests {
},
None,
)),
&cx.background(),
);
let fs = FakeFs::new(cx.background());

View File

@ -1,8 +1,7 @@
use anyhow::anyhow;
use anyhow::{anyhow, Result};
use async_compression::futures::bufread::GzipDecoder;
use client::http;
use client::http::{self, HttpClient, Method};
use futures::{future::BoxFuture, FutureExt, StreamExt};
use gpui::executor;
pub use language::*;
use lazy_static::lazy_static;
use regex::Regex;
@ -10,7 +9,7 @@ use rust_embed::RustEmbed;
use serde::Deserialize;
use smol::fs::{self, File};
use std::{borrow::Cow, env::consts, path::PathBuf, str, sync::Arc};
use util::{ResultExt, TryFutureExt};
use util::ResultExt;
#[derive(RustEmbed)]
#[folder = "languages"]
@ -31,13 +30,27 @@ struct GithubReleaseAsset {
}
impl RustLsp {
async fn download(destination_dir_path: PathBuf) -> anyhow::Result<PathBuf> {
let client = surf::client().with(surf::middleware::Redirect::default());
let release = client
.get("https://api.github.com/repos/rust-analyzer/rust-analyzer/releases/latest")
.recv_json::<GithubRelease>()
async fn download(
destination_dir_path: PathBuf,
http: Arc<dyn HttpClient>,
) -> anyhow::Result<PathBuf> {
let release = http
.send(
surf::RequestBuilder::new(
Method::Get,
http::Url::parse(
"https://api.github.com/repos/rust-analyzer/rust-analyzer/releases/latest",
)
.unwrap(),
)
.middleware(surf::middleware::Redirect::default())
.build(),
)
.await
.map_err(|err| anyhow!("error getting latest release: {}", err))?;
.map_err(|err| anyhow!("error fetching latest release: {}", err))?
.body_json::<GithubRelease>()
.await
.map_err(|err| anyhow!("error parsing latest release: {}", err))?;
let release_name = format!("rust-analyzer-{}-apple-darwin.gz", consts::ARCH);
let asset = release
.assets
@ -47,9 +60,12 @@ impl RustLsp {
let destination_path = destination_dir_path.join(format!("rust-analyzer-{}", release.name));
if fs::metadata(&destination_path).await.is_err() {
let response = client
.get(&asset.browser_download_url)
.send()
let response = http
.send(
surf::RequestBuilder::new(Method::Get, asset.browser_download_url.clone())
.middleware(surf::middleware::Redirect::default())
.build(),
)
.await
.map_err(|err| anyhow!("error downloading release: {}", err))?;
let decompressed_bytes = GzipDecoder::new(response);
@ -67,32 +83,40 @@ impl RustLsp {
}
impl LspExt for RustLsp {
fn server_bin_path(&self) -> BoxFuture<'static, Option<PathBuf>> {
fn fetch_latest_language_server(
&self,
http: Arc<dyn HttpClient>,
) -> BoxFuture<'static, Result<PathBuf>> {
async move {
let destination_dir_path = dirs::home_dir()
.ok_or_else(|| anyhow!("can't determine home directory"))?
.join(".zed/rust-analyzer");
fs::create_dir_all(&destination_dir_path).await?;
let mut server_bin_path = Self::download(destination_dir_path.clone()).await.log_err();
let downloaded_bin_path = Self::download(destination_dir_path.clone(), http).await;
let mut last_cached_bin_path = None;
if let Some(mut entries) = fs::read_dir(&destination_dir_path).await.log_err() {
while let Some(entry) = entries.next().await {
if let Some(entry) = entry.log_err() {
let entry_path = entry.path();
if let Some(downloaded_server_path) = server_bin_path.as_ref() {
if downloaded_server_path != entry_path.as_path() {
fs::remove_file(entry_path).await.log_err();
if let Ok(downloaded_bin_path) = downloaded_bin_path.as_ref() {
if downloaded_bin_path != entry_path.as_path() {
fs::remove_file(&entry_path).await.log_err();
}
} else {
server_bin_path = Some(entry_path);
}
last_cached_bin_path = Some(entry_path);
}
}
}
server_bin_path.ok_or_else(|| anyhow!("could not locate or download server"))
if downloaded_bin_path.is_err() {
if let Some(last_cached_bin_path) = last_cached_bin_path {
return Ok(last_cached_bin_path);
}
}
downloaded_bin_path
}
.log_err()
.boxed()
}
@ -196,10 +220,10 @@ impl LspExt for RustLsp {
}
}
pub fn build_language_registry(executor: &Arc<executor::Background>) -> LanguageRegistry {
pub fn build_language_registry() -> LanguageRegistry {
let mut languages = LanguageRegistry::new();
languages.add(Arc::new(rust()), executor);
languages.add(Arc::new(markdown()), executor);
languages.add(Arc::new(rust()));
languages.add(Arc::new(markdown()));
languages
}

View File

@ -39,7 +39,7 @@ fn main() {
},
);
let (settings_tx, settings) = postage::watch::channel_with(settings);
let languages = Arc::new(language::build_language_registry(&app.background()));
let languages = Arc::new(language::build_language_registry());
languages.set_theme(&settings.borrow().theme.editor.syntax);
app.run(move |cx| {

View File

@ -35,7 +35,7 @@ pub fn test_app_state(cx: &mut MutableAppContext) -> Arc<AppState> {
},
Some(tree_sitter_rust::language()),
)),
cx.background(),
);
Arc::new(AppState {
settings_tx: Arc::new(Mutex::new(settings_tx)),