Initial impl of NodeRuntime w/JSON borked and a deadlock :)

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
Julia 2023-03-26 23:59:49 -04:00
parent 1a2e509e35
commit c72d33e029
20 changed files with 435 additions and 375 deletions

View file

@ -1,23 +1,34 @@
use super::installation::{npm_install_packages, npm_package_latest_version};
use super::node_runtime::NodeRuntime;
use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use client::http::HttpClient;
use futures::StreamExt;
use language::{LanguageServerBinary, LanguageServerName, LspAdapter, ServerExecutionKind};
use language::{LanguageServerBinary, LanguageServerName, LspAdapter};
use serde_json::json;
use smol::fs;
use std::{any::Any, path::PathBuf, sync::Arc};
use std::{
any::Any,
ffi::OsString,
path::{Path, PathBuf},
sync::Arc,
};
use util::ResultExt;
fn server_binary_arguments() -> Vec<String> {
vec!["--stdio".into()]
fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
vec![server_path.into(), "--stdio".into()]
}
pub struct HtmlLspAdapter;
pub struct HtmlLspAdapter {
node: Arc<NodeRuntime>,
}
impl HtmlLspAdapter {
const BIN_PATH: &'static str =
const SERVER_PATH: &'static str =
"node_modules/vscode-langservers-extracted/bin/vscode-html-language-server";
pub fn new(node: Arc<NodeRuntime>) -> Self {
HtmlLspAdapter { node }
}
}
#[async_trait]
@ -26,24 +37,21 @@ impl LspAdapter for HtmlLspAdapter {
LanguageServerName("vscode-html-language-server".into())
}
async fn server_execution_kind(&self) -> ServerExecutionKind {
ServerExecutionKind::Node
}
async fn fetch_latest_server_version(
&self,
http: Arc<dyn HttpClient>,
_: Arc<dyn HttpClient>,
) -> Result<Box<dyn 'static + Any + Send>> {
Ok(
Box::new(npm_package_latest_version(http, "vscode-langservers-extracted").await?)
as Box<_>,
)
Ok(Box::new(
self.node
.npm_package_latest_version("vscode-langservers-extracted")
.await?,
) as Box<_>)
}
async fn fetch_server_binary(
&self,
version: Box<dyn 'static + Send + Any>,
http: Arc<dyn HttpClient>,
_: Arc<dyn HttpClient>,
container_dir: PathBuf,
) -> Result<LanguageServerBinary> {
let version = version.downcast::<String>().unwrap();
@ -51,15 +59,15 @@ impl LspAdapter for HtmlLspAdapter {
fs::create_dir_all(&version_dir)
.await
.context("failed to create version directory")?;
let binary_path = version_dir.join(Self::BIN_PATH);
let server_path = version_dir.join(Self::SERVER_PATH);
if fs::metadata(&binary_path).await.is_err() {
npm_install_packages(
http,
[("vscode-langservers-extracted", version.as_str())],
&version_dir,
)
.await?;
if fs::metadata(&server_path).await.is_err() {
self.node
.npm_install_packages(
[("vscode-langservers-extracted", version.as_str())],
&version_dir,
)
.await?;
if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
while let Some(entry) = entries.next().await {
@ -74,8 +82,8 @@ impl LspAdapter for HtmlLspAdapter {
}
Ok(LanguageServerBinary {
path: binary_path,
arguments: server_binary_arguments(),
path: self.node.binary_path().await?,
arguments: server_binary_arguments(&server_path),
})
}
@ -90,11 +98,11 @@ impl LspAdapter for HtmlLspAdapter {
}
}
let last_version_dir = last_version_dir.ok_or_else(|| anyhow!("no cached binary"))?;
let bin_path = last_version_dir.join(Self::BIN_PATH);
if bin_path.exists() {
let server_path = last_version_dir.join(Self::SERVER_PATH);
if server_path.exists() {
Ok(LanguageServerBinary {
path: bin_path,
arguments: server_binary_arguments(),
path: self.node.binary_path().await?,
arguments: server_binary_arguments(&server_path),
})
} else {
Err(anyhow!(