Download the JSON LSP package instead of our own bundled binary

This commit is contained in:
Julia 2023-03-28 11:48:00 -04:00
parent d4560fe321
commit 350f8ed304

View file

@ -1,21 +1,16 @@
use super::{ use super::node_runtime::NodeRuntime;
installation::{latest_github_release, GitHubLspBinaryVersion}, use anyhow::{anyhow, Context, Result};
node_runtime::NodeRuntime,
};
use anyhow::{anyhow, Result};
use async_compression::futures::bufread::GzipDecoder;
use async_trait::async_trait; use async_trait::async_trait;
use client::http::HttpClient; use client::http::HttpClient;
use collections::HashMap; use collections::HashMap;
use futures::{future::BoxFuture, io::BufReader, FutureExt, StreamExt}; use futures::{future::BoxFuture, FutureExt, StreamExt};
use gpui::MutableAppContext; use gpui::MutableAppContext;
use language::{LanguageRegistry, LanguageServerBinary, LanguageServerName, LspAdapter}; use language::{LanguageRegistry, LanguageServerBinary, LanguageServerName, LspAdapter};
use serde_json::json; use serde_json::json;
use settings::{keymap_file_json_schema, settings_file_json_schema}; use settings::{keymap_file_json_schema, settings_file_json_schema};
use smol::fs::{self, File}; use smol::fs;
use std::{ use std::{
any::Any, any::Any,
env::consts,
ffi::OsString, ffi::OsString,
future, future,
path::{Path, PathBuf}, path::{Path, PathBuf},
@ -24,8 +19,11 @@ use std::{
use theme::ThemeRegistry; use theme::ThemeRegistry;
use util::{paths, ResultExt, StaffMode}; use util::{paths, ResultExt, StaffMode};
const SERVER_PATH: &'static str =
"node_modules/vscode-json-languageserver/bin/vscode-json-languageserver";
fn server_binary_arguments(server_path: &Path) -> Vec<OsString> { fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
dbg!(vec![server_path.into(), "--stdio".into()]) vec![server_path.into(), "--stdio".into()]
} }
pub struct JsonLspAdapter { pub struct JsonLspAdapter {
@ -56,55 +54,42 @@ impl LspAdapter for JsonLspAdapter {
async fn fetch_latest_server_version( async fn fetch_latest_server_version(
&self, &self,
http: Arc<dyn HttpClient>, _: Arc<dyn HttpClient>,
) -> Result<Box<dyn 'static + Send + Any>> { ) -> Result<Box<dyn 'static + Send + Any>> {
let release = latest_github_release("zed-industries/json-language-server", http).await?; Ok(Box::new(
let asset_name = format!("json-language-server-darwin-{}.gz", consts::ARCH); self.node
let asset = release .npm_package_latest_version("vscode-json-languageserver")
.assets .await?,
.iter() ) as Box<_>)
.find(|asset| asset.name == asset_name)
.ok_or_else(|| anyhow!("no asset found matching {:?}", asset_name))?;
let version = GitHubLspBinaryVersion {
name: release.name,
url: asset.browser_download_url.clone(),
};
Ok(Box::new(version) as Box<_>)
} }
async fn fetch_server_binary( async fn fetch_server_binary(
&self, &self,
version: Box<dyn 'static + Send + Any>, version: Box<dyn 'static + Send + Any>,
http: Arc<dyn HttpClient>, _: Arc<dyn HttpClient>,
container_dir: PathBuf, container_dir: PathBuf,
) -> Result<LanguageServerBinary> { ) -> Result<LanguageServerBinary> {
let version = version.downcast::<GitHubLspBinaryVersion>().unwrap(); let version = version.downcast::<String>().unwrap();
let destination_path = container_dir.join(format!( let version_dir = container_dir.join(version.as_str());
"json-language-server-{}-{}", fs::create_dir_all(&version_dir)
version.name, .await
consts::ARCH .context("failed to create version directory")?;
)); let server_path = version_dir.join(SERVER_PATH);
if fs::metadata(&destination_path).await.is_err() { if fs::metadata(&server_path).await.is_err() {
let mut response = http self.node
.get(&version.url, Default::default(), true) .npm_install_packages(
.await [("vscode-json-languageserver", version.as_str())],
.map_err(|err| anyhow!("error downloading release: {}", err))?; &version_dir,
let decompressed_bytes = GzipDecoder::new(BufReader::new(response.body_mut())); )
let mut file = File::create(&destination_path).await?; .await?;
futures::io::copy(decompressed_bytes, &mut file).await?;
fs::set_permissions(
&destination_path,
<fs::Permissions as fs::unix::PermissionsExt>::from_mode(0o755),
)
.await?;
if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() { if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
while let Some(entry) = entries.next().await { while let Some(entry) = entries.next().await {
if let Some(entry) = entry.log_err() { if let Some(entry) = entry.log_err() {
let entry_path = entry.path(); let entry_path = entry.path();
if entry_path.as_path() != destination_path { if entry_path.as_path() != version_dir {
fs::remove_file(&entry_path).await.log_err(); fs::remove_dir_all(&entry_path).await.log_err();
} }
} }
} }
@ -113,23 +98,34 @@ impl LspAdapter for JsonLspAdapter {
Ok(LanguageServerBinary { Ok(LanguageServerBinary {
path: self.node.binary_path().await?, path: self.node.binary_path().await?,
arguments: server_binary_arguments(&destination_path), arguments: server_binary_arguments(&server_path),
}) })
} }
async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<LanguageServerBinary> { async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<LanguageServerBinary> {
(|| async move { (|| async move {
let mut last = None; let mut last_version_dir = None;
let mut entries = fs::read_dir(&container_dir).await?; let mut entries = fs::read_dir(&container_dir).await?;
while let Some(entry) = entries.next().await { while let Some(entry) = entries.next().await {
last = Some(entry?.path()); let entry = entry?;
if entry.file_type().await?.is_dir() {
last_version_dir = Some(entry.path());
}
}
let last_version_dir = last_version_dir.ok_or_else(|| anyhow!("no cached binary"))?;
let server_path = last_version_dir.join(SERVER_PATH);
if server_path.exists() {
Ok(LanguageServerBinary {
path: self.node.binary_path().await?,
arguments: server_binary_arguments(&server_path),
})
} else {
Err(anyhow!(
"missing executable in directory {:?}",
last_version_dir
))
} }
anyhow::Ok(LanguageServerBinary {
path: self.node.binary_path().await?,
arguments: server_binary_arguments(
&last.ok_or_else(|| anyhow!("no cached binary"))?,
),
})
})() })()
.await .await
.log_err() .log_err()