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,21 +1,32 @@
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 smol::fs;
use std::{any::Any, path::PathBuf, sync::Arc};
use std::{
any::Any,
ffi::OsString,
path::{Path, PathBuf},
sync::Arc,
};
use util::ResultExt;
pub struct PythonLspAdapter;
fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
vec![server_path.into(), "--stdio".into()]
}
fn server_binary_arguments() -> Vec<String> {
vec!["--stdio".into()]
pub struct PythonLspAdapter {
node: Arc<NodeRuntime>,
}
impl PythonLspAdapter {
const BIN_PATH: &'static str = "node_modules/pyright/langserver.index.js";
const SERVER_PATH: &'static str = "node_modules/pyright/langserver.index.js";
pub fn new(node: Arc<NodeRuntime>) -> Self {
PythonLspAdapter { node }
}
}
#[async_trait]
@ -24,21 +35,17 @@ impl LspAdapter for PythonLspAdapter {
LanguageServerName("pyright".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, "pyright").await?) as Box<_>)
Ok(Box::new(self.node.npm_package_latest_version("pyright").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();
@ -46,10 +53,12 @@ impl LspAdapter for PythonLspAdapter {
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, [("pyright", version.as_str())], &version_dir).await?;
if fs::metadata(&server_path).await.is_err() {
self.node
.npm_install_packages([("pyright", 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 {
@ -64,8 +73,8 @@ impl LspAdapter for PythonLspAdapter {
}
Ok(LanguageServerBinary {
path: binary_path,
arguments: server_binary_arguments(),
path: self.node.binary_path().await?,
arguments: server_binary_arguments(&server_path),
})
}
@ -80,11 +89,11 @@ impl LspAdapter for PythonLspAdapter {
}
}
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!(