2021-12-15 13:23:43 -05:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use deno_core::anyhow::anyhow;
|
2022-01-25 10:30:38 -05:00
|
|
|
use deno_core::anyhow::bail;
|
2021-12-15 13:23:43 -05:00
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::futures::future;
|
|
|
|
use deno_core::serde_json;
|
2022-01-25 10:30:38 -05:00
|
|
|
use deno_core::serde_json::Value;
|
2022-04-03 00:17:30 -04:00
|
|
|
use tower_lsp::lsp_types as lsp;
|
|
|
|
use tower_lsp::lsp_types::ConfigurationItem;
|
2021-12-15 13:23:43 -05:00
|
|
|
|
|
|
|
use crate::lsp::repl::get_repl_workspace_settings;
|
|
|
|
|
2022-01-25 10:30:38 -05:00
|
|
|
use super::config::SpecifierSettings;
|
|
|
|
use super::config::SETTINGS_SECTION;
|
2021-12-15 13:23:43 -05:00
|
|
|
use super::lsp_custom;
|
2022-03-29 18:59:27 -04:00
|
|
|
use super::testing::lsp_custom as testing_lsp_custom;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum TestingNotification {
|
|
|
|
Module(testing_lsp_custom::TestModuleNotificationParams),
|
|
|
|
DeleteModule(testing_lsp_custom::TestModuleDeleteNotificationParams),
|
|
|
|
Progress(testing_lsp_custom::TestRunProgressParams),
|
|
|
|
}
|
2021-12-15 13:23:43 -05:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Client(Arc<dyn ClientTrait>);
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Client {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_tuple("Client").finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
2022-04-03 00:17:30 -04:00
|
|
|
pub fn from_tower(client: tower_lsp::Client) -> Self {
|
|
|
|
Self(Arc::new(TowerClient(client)))
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_for_repl() -> Self {
|
|
|
|
Self(Arc::new(ReplClient))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn publish_diagnostics(
|
|
|
|
&self,
|
|
|
|
uri: lsp::Url,
|
|
|
|
diags: Vec<lsp::Diagnostic>,
|
|
|
|
version: Option<i32>,
|
|
|
|
) {
|
|
|
|
self.0.publish_diagnostics(uri, diags, version).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn send_registry_state_notification(
|
|
|
|
&self,
|
|
|
|
params: lsp_custom::RegistryStateNotificationParams,
|
|
|
|
) {
|
|
|
|
self.0.send_registry_state_notification(params).await;
|
|
|
|
}
|
|
|
|
|
2022-03-29 18:59:27 -04:00
|
|
|
pub fn send_test_notification(&self, params: TestingNotification) {
|
|
|
|
self.0.send_test_notification(params);
|
|
|
|
}
|
|
|
|
|
2022-01-25 10:30:38 -05:00
|
|
|
pub async fn specifier_configurations(
|
2021-12-15 13:23:43 -05:00
|
|
|
&self,
|
2022-01-25 10:30:38 -05:00
|
|
|
specifiers: Vec<lsp::Url>,
|
|
|
|
) -> Result<Vec<Result<SpecifierSettings, AnyError>>, AnyError> {
|
|
|
|
self.0.specifier_configurations(specifiers).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn specifier_configuration(
|
|
|
|
&self,
|
|
|
|
specifier: &lsp::Url,
|
|
|
|
) -> Result<SpecifierSettings, AnyError> {
|
|
|
|
let values = self
|
|
|
|
.0
|
|
|
|
.specifier_configurations(vec![specifier.clone()])
|
|
|
|
.await?;
|
|
|
|
if let Some(value) = values.into_iter().next() {
|
|
|
|
value.map_err(|err| {
|
|
|
|
anyhow!(
|
|
|
|
"Error converting specifier settings ({}): {}",
|
|
|
|
specifier,
|
|
|
|
err
|
|
|
|
)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
bail!(
|
|
|
|
"Expected the client to return a configuration item for specifier: {}",
|
|
|
|
specifier
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn workspace_configuration(&self) -> Result<Value, AnyError> {
|
|
|
|
self.0.workspace_configuration().await
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn show_message(
|
|
|
|
&self,
|
|
|
|
message_type: lsp::MessageType,
|
|
|
|
message: impl std::fmt::Display,
|
|
|
|
) {
|
|
|
|
self
|
|
|
|
.0
|
|
|
|
.show_message(message_type, format!("{}", message))
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn register_capability(
|
|
|
|
&self,
|
|
|
|
registrations: Vec<lsp::Registration>,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
self.0.register_capability(registrations).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type AsyncReturn<T> = Pin<Box<dyn Future<Output = T> + 'static + Send>>;
|
|
|
|
|
|
|
|
trait ClientTrait: Send + Sync {
|
|
|
|
fn publish_diagnostics(
|
|
|
|
&self,
|
|
|
|
uri: lsp::Url,
|
|
|
|
diagnostics: Vec<lsp::Diagnostic>,
|
|
|
|
version: Option<i32>,
|
|
|
|
) -> AsyncReturn<()>;
|
|
|
|
fn send_registry_state_notification(
|
|
|
|
&self,
|
|
|
|
params: lsp_custom::RegistryStateNotificationParams,
|
|
|
|
) -> AsyncReturn<()>;
|
2022-03-29 18:59:27 -04:00
|
|
|
fn send_test_notification(&self, params: TestingNotification);
|
2022-01-25 10:30:38 -05:00
|
|
|
fn specifier_configurations(
|
2021-12-15 13:23:43 -05:00
|
|
|
&self,
|
2022-01-25 10:30:38 -05:00
|
|
|
uris: Vec<lsp::Url>,
|
|
|
|
) -> AsyncReturn<Result<Vec<Result<SpecifierSettings, AnyError>>, AnyError>>;
|
|
|
|
fn workspace_configuration(&self) -> AsyncReturn<Result<Value, AnyError>>;
|
2021-12-15 13:23:43 -05:00
|
|
|
fn show_message(
|
|
|
|
&self,
|
|
|
|
message_type: lsp::MessageType,
|
|
|
|
text: String,
|
|
|
|
) -> AsyncReturn<()>;
|
|
|
|
fn register_capability(
|
|
|
|
&self,
|
|
|
|
registrations: Vec<lsp::Registration>,
|
|
|
|
) -> AsyncReturn<Result<(), AnyError>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2022-04-03 00:17:30 -04:00
|
|
|
struct TowerClient(tower_lsp::Client);
|
2021-12-15 13:23:43 -05:00
|
|
|
|
2022-04-03 00:17:30 -04:00
|
|
|
impl ClientTrait for TowerClient {
|
2021-12-15 13:23:43 -05:00
|
|
|
fn publish_diagnostics(
|
|
|
|
&self,
|
|
|
|
uri: lsp::Url,
|
|
|
|
diagnostics: Vec<lsp::Diagnostic>,
|
|
|
|
version: Option<i32>,
|
|
|
|
) -> AsyncReturn<()> {
|
|
|
|
let client = self.0.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
client.publish_diagnostics(uri, diagnostics, version).await
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_registry_state_notification(
|
|
|
|
&self,
|
|
|
|
params: lsp_custom::RegistryStateNotificationParams,
|
|
|
|
) -> AsyncReturn<()> {
|
|
|
|
let client = self.0.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
client
|
2022-04-03 00:17:30 -04:00
|
|
|
.send_notification::<lsp_custom::RegistryStateNotification>(params)
|
2021-12-15 13:23:43 -05:00
|
|
|
.await
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-29 18:59:27 -04:00
|
|
|
fn send_test_notification(&self, notification: TestingNotification) {
|
|
|
|
let client = self.0.clone();
|
|
|
|
tokio::task::spawn(async move {
|
|
|
|
match notification {
|
|
|
|
TestingNotification::Module(params) => {
|
|
|
|
client
|
2022-04-03 00:17:30 -04:00
|
|
|
.send_notification::<testing_lsp_custom::TestModuleNotification>(
|
2022-03-29 18:59:27 -04:00
|
|
|
params,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
TestingNotification::DeleteModule(params) => client
|
2022-04-03 00:17:30 -04:00
|
|
|
.send_notification::<testing_lsp_custom::TestModuleDeleteNotification>(
|
2022-03-29 18:59:27 -04:00
|
|
|
params,
|
|
|
|
)
|
|
|
|
.await,
|
|
|
|
TestingNotification::Progress(params) => client
|
2022-04-03 00:17:30 -04:00
|
|
|
.send_notification::<testing_lsp_custom::TestRunProgressNotification>(
|
2022-03-29 18:59:27 -04:00
|
|
|
params,
|
|
|
|
)
|
|
|
|
.await,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-25 10:30:38 -05:00
|
|
|
fn specifier_configurations(
|
2021-12-15 13:23:43 -05:00
|
|
|
&self,
|
2022-01-25 10:30:38 -05:00
|
|
|
uris: Vec<lsp::Url>,
|
|
|
|
) -> AsyncReturn<Result<Vec<Result<SpecifierSettings, AnyError>>, AnyError>>
|
|
|
|
{
|
2021-12-15 13:23:43 -05:00
|
|
|
let client = self.0.clone();
|
|
|
|
Box::pin(async move {
|
2022-01-25 10:30:38 -05:00
|
|
|
let config_response = client
|
|
|
|
.configuration(
|
|
|
|
uris
|
|
|
|
.into_iter()
|
|
|
|
.map(|uri| ConfigurationItem {
|
|
|
|
scope_uri: Some(uri),
|
|
|
|
section: Some(SETTINGS_SECTION.to_string()),
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(
|
|
|
|
config_response
|
|
|
|
.into_iter()
|
|
|
|
.map(|value| {
|
|
|
|
serde_json::from_value::<SpecifierSettings>(value).map_err(|err| {
|
|
|
|
anyhow!("Error converting specifier settings: {}", err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn workspace_configuration(&self) -> AsyncReturn<Result<Value, AnyError>> {
|
|
|
|
let client = self.0.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
let config_response = client
|
|
|
|
.configuration(vec![ConfigurationItem {
|
|
|
|
scope_uri: None,
|
|
|
|
section: Some(SETTINGS_SECTION.to_string()),
|
|
|
|
}])
|
|
|
|
.await;
|
|
|
|
match config_response {
|
|
|
|
Ok(value_vec) => match value_vec.get(0).cloned() {
|
|
|
|
Some(value) => Ok(value),
|
|
|
|
None => bail!("Missing response workspace configuration."),
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
bail!("Error getting workspace configuration: {}", err)
|
|
|
|
}
|
|
|
|
}
|
2021-12-15 13:23:43 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn show_message(
|
|
|
|
&self,
|
|
|
|
message_type: lsp::MessageType,
|
|
|
|
message: String,
|
|
|
|
) -> AsyncReturn<()> {
|
|
|
|
let client = self.0.clone();
|
|
|
|
Box::pin(async move { client.show_message(message_type, message).await })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn register_capability(
|
|
|
|
&self,
|
|
|
|
registrations: Vec<lsp::Registration>,
|
|
|
|
) -> AsyncReturn<Result<(), AnyError>> {
|
|
|
|
let client = self.0.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
client
|
|
|
|
.register_capability(registrations)
|
|
|
|
.await
|
|
|
|
.map_err(|err| anyhow!("{}", err))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct ReplClient;
|
|
|
|
|
|
|
|
impl ClientTrait for ReplClient {
|
|
|
|
fn publish_diagnostics(
|
|
|
|
&self,
|
|
|
|
_uri: lsp::Url,
|
|
|
|
_diagnostics: Vec<lsp::Diagnostic>,
|
|
|
|
_version: Option<i32>,
|
|
|
|
) -> AsyncReturn<()> {
|
|
|
|
Box::pin(future::ready(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_registry_state_notification(
|
|
|
|
&self,
|
|
|
|
_params: lsp_custom::RegistryStateNotificationParams,
|
|
|
|
) -> AsyncReturn<()> {
|
|
|
|
Box::pin(future::ready(()))
|
|
|
|
}
|
|
|
|
|
2022-03-29 18:59:27 -04:00
|
|
|
fn send_test_notification(&self, _params: TestingNotification) {}
|
|
|
|
|
2022-01-25 10:30:38 -05:00
|
|
|
fn specifier_configurations(
|
2021-12-15 13:23:43 -05:00
|
|
|
&self,
|
2022-01-25 10:30:38 -05:00
|
|
|
uris: Vec<lsp::Url>,
|
|
|
|
) -> AsyncReturn<Result<Vec<Result<SpecifierSettings, AnyError>>, AnyError>>
|
|
|
|
{
|
|
|
|
// all specifiers are enabled for the REPL
|
|
|
|
let settings = uris
|
|
|
|
.into_iter()
|
|
|
|
.map(|_| {
|
|
|
|
Ok(SpecifierSettings {
|
|
|
|
enable: true,
|
|
|
|
..Default::default()
|
2021-12-15 13:23:43 -05:00
|
|
|
})
|
2022-01-25 10:30:38 -05:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
Box::pin(future::ready(Ok(settings)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn workspace_configuration(&self) -> AsyncReturn<Result<Value, AnyError>> {
|
|
|
|
Box::pin(future::ready(Ok(
|
|
|
|
serde_json::to_value(get_repl_workspace_settings()).unwrap(),
|
|
|
|
)))
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn show_message(
|
|
|
|
&self,
|
|
|
|
_message_type: lsp::MessageType,
|
|
|
|
_message: String,
|
|
|
|
) -> AsyncReturn<()> {
|
|
|
|
Box::pin(future::ready(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn register_capability(
|
|
|
|
&self,
|
|
|
|
_registrations: Vec<lsp::Registration>,
|
|
|
|
) -> AsyncReturn<Result<(), AnyError>> {
|
|
|
|
Box::pin(future::ready(Ok(())))
|
|
|
|
}
|
|
|
|
}
|