2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-01-13 02:51:32 -05:00
|
|
|
|
2021-12-15 13:23:43 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-03-15 10:34:23 -04:00
|
|
|
use async_trait::async_trait;
|
2021-12-15 13:23:43 -05:00
|
|
|
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;
|
2023-10-24 16:27:27 -04:00
|
|
|
use deno_core::serde_json::json;
|
2023-08-23 19:03:05 -04:00
|
|
|
use deno_core::unsync::spawn;
|
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;
|
|
|
|
|
2023-09-21 01:46:39 -04:00
|
|
|
use super::config::WorkspaceSettings;
|
2022-01-25 10:30:38 -05:00
|
|
|
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;
|
2023-03-23 10:23:04 -04:00
|
|
|
use super::urls::LspClientUrl;
|
2022-03-29 18:59:27 -04:00
|
|
|
|
|
|
|
#[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))
|
|
|
|
}
|
|
|
|
|
2023-03-15 10:34:23 -04:00
|
|
|
/// Gets additional methods that should only be called outside
|
|
|
|
/// the LSP's lock to prevent deadlocking scenarios.
|
|
|
|
pub fn when_outside_lsp_lock(&self) -> OutsideLockClient {
|
|
|
|
OutsideLockClient(self.0.clone())
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
|
2024-05-08 01:34:42 -04:00
|
|
|
pub async fn publish_diagnostics(
|
|
|
|
&self,
|
|
|
|
uri: LspClientUrl,
|
|
|
|
diags: Vec<lsp::Diagnostic>,
|
|
|
|
version: Option<i32>,
|
|
|
|
) {
|
|
|
|
self
|
|
|
|
.0
|
|
|
|
.publish_diagnostics(uri.into_url(), diags, version)
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
2023-03-15 10:34:23 -04:00
|
|
|
pub fn send_registry_state_notification(
|
2021-12-15 13:23:43 -05:00
|
|
|
&self,
|
|
|
|
params: lsp_custom::RegistryStateNotificationParams,
|
|
|
|
) {
|
2023-03-15 10:34:23 -04:00
|
|
|
// do on a task in case the caller currently is in the lsp lock
|
|
|
|
let client = self.0.clone();
|
2023-05-14 17:40:01 -04:00
|
|
|
spawn(async move {
|
2023-03-15 10:34:23 -04:00
|
|
|
client.send_registry_state_notification(params).await;
|
|
|
|
});
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
|
2023-05-26 02:10:18 -04:00
|
|
|
/// This notification is sent to the client during internal testing
|
|
|
|
/// purposes only in order to let the test client know when the latest
|
|
|
|
/// diagnostics have been published.
|
|
|
|
pub fn send_diagnostic_batch_notification(
|
|
|
|
&self,
|
|
|
|
params: lsp_custom::DiagnosticBatchNotificationParams,
|
|
|
|
) {
|
|
|
|
// do on a task in case the caller currently is in the lsp lock
|
|
|
|
let client = self.0.clone();
|
|
|
|
spawn(async move {
|
|
|
|
client.send_diagnostic_batch_notification(params).await;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-29 18:59:27 -04:00
|
|
|
pub fn send_test_notification(&self, params: TestingNotification) {
|
2023-03-15 10:34:23 -04:00
|
|
|
// do on a task in case the caller currently is in the lsp lock
|
|
|
|
let client = self.0.clone();
|
2023-05-14 17:40:01 -04:00
|
|
|
spawn(async move {
|
2023-03-15 10:34:23 -04:00
|
|
|
client.send_test_notification(params).await;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-10-12 11:07:27 -04:00
|
|
|
pub fn send_did_change_deno_configuration_notification(
|
|
|
|
&self,
|
|
|
|
params: lsp_custom::DidChangeDenoConfigurationNotificationParams,
|
|
|
|
) {
|
|
|
|
// do on a task in case the caller currently is in the lsp lock
|
|
|
|
let client = self.0.clone();
|
|
|
|
spawn(async move {
|
|
|
|
client
|
|
|
|
.send_did_change_deno_configuration_notification(params)
|
|
|
|
.await;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:10:51 -05:00
|
|
|
pub fn send_did_upgrade_check_notification(
|
|
|
|
&self,
|
|
|
|
params: lsp_custom::DidUpgradeCheckNotificationParams,
|
|
|
|
) {
|
|
|
|
// do on a task in case the caller currently is in the lsp lock
|
|
|
|
let client = self.0.clone();
|
|
|
|
spawn(async move {
|
|
|
|
client.send_did_upgrade_check_notification(params).await;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-15 10:34:23 -04:00
|
|
|
pub fn show_message(
|
|
|
|
&self,
|
|
|
|
message_type: lsp::MessageType,
|
|
|
|
message: impl std::fmt::Display,
|
|
|
|
) {
|
|
|
|
// do on a task in case the caller currently is in the lsp lock
|
|
|
|
let client = self.0.clone();
|
|
|
|
let message = message.to_string();
|
2023-05-14 17:40:01 -04:00
|
|
|
spawn(async move {
|
2023-03-15 10:34:23 -04:00
|
|
|
client.show_message(message_type, message).await;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// DANGER: The methods on this client should only be called outside
|
|
|
|
/// the LSP's lock. The reason is you never want to call into the client
|
|
|
|
/// while holding the lock because the client might call back into the
|
|
|
|
/// server and cause a deadlock.
|
|
|
|
pub struct OutsideLockClient(Arc<dyn ClientTrait>);
|
|
|
|
|
|
|
|
impl OutsideLockClient {
|
|
|
|
pub async fn register_capability(
|
|
|
|
&self,
|
|
|
|
registrations: Vec<lsp::Registration>,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
self.0.register_capability(registrations).await
|
2022-03-29 18:59:27 -04:00
|
|
|
}
|
|
|
|
|
2023-09-21 01:46:39 -04:00
|
|
|
pub async fn workspace_configuration(
|
|
|
|
&self,
|
2023-10-24 16:27:27 -04:00
|
|
|
scopes: Vec<Option<lsp::Url>>,
|
|
|
|
) -> Result<Vec<WorkspaceSettings>, AnyError> {
|
|
|
|
self.0.workspace_configuration(scopes).await
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-15 10:34:23 -04:00
|
|
|
#[async_trait]
|
2021-12-15 13:23:43 -05:00
|
|
|
trait ClientTrait: Send + Sync {
|
2023-03-15 10:34:23 -04:00
|
|
|
async fn publish_diagnostics(
|
2021-12-15 13:23:43 -05:00
|
|
|
&self,
|
|
|
|
uri: lsp::Url,
|
|
|
|
diagnostics: Vec<lsp::Diagnostic>,
|
|
|
|
version: Option<i32>,
|
2023-03-15 10:34:23 -04:00
|
|
|
);
|
|
|
|
async fn send_registry_state_notification(
|
2021-12-15 13:23:43 -05:00
|
|
|
&self,
|
|
|
|
params: lsp_custom::RegistryStateNotificationParams,
|
2023-03-15 10:34:23 -04:00
|
|
|
);
|
2023-05-26 02:10:18 -04:00
|
|
|
async fn send_diagnostic_batch_notification(
|
|
|
|
&self,
|
|
|
|
params: lsp_custom::DiagnosticBatchNotificationParams,
|
|
|
|
);
|
2023-03-15 10:34:23 -04:00
|
|
|
async fn send_test_notification(&self, params: TestingNotification);
|
2023-10-12 11:07:27 -04:00
|
|
|
async fn send_did_change_deno_configuration_notification(
|
|
|
|
&self,
|
|
|
|
params: lsp_custom::DidChangeDenoConfigurationNotificationParams,
|
|
|
|
);
|
2023-11-14 17:10:51 -05:00
|
|
|
async fn send_did_upgrade_check_notification(
|
|
|
|
&self,
|
|
|
|
params: lsp_custom::DidUpgradeCheckNotificationParams,
|
|
|
|
);
|
2023-09-21 01:46:39 -04:00
|
|
|
async fn workspace_configuration(
|
|
|
|
&self,
|
2023-10-24 16:27:27 -04:00
|
|
|
scopes: Vec<Option<lsp::Url>>,
|
|
|
|
) -> Result<Vec<WorkspaceSettings>, AnyError>;
|
2023-03-15 10:34:23 -04:00
|
|
|
async fn show_message(&self, message_type: lsp::MessageType, text: String);
|
|
|
|
async fn register_capability(
|
2021-12-15 13:23:43 -05:00
|
|
|
&self,
|
|
|
|
registrations: Vec<lsp::Registration>,
|
2023-03-15 10:34:23 -04:00
|
|
|
) -> Result<(), AnyError>;
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2022-04-03 00:17:30 -04:00
|
|
|
struct TowerClient(tower_lsp::Client);
|
2021-12-15 13:23:43 -05:00
|
|
|
|
2023-03-15 10:34:23 -04:00
|
|
|
#[async_trait]
|
2022-04-03 00:17:30 -04:00
|
|
|
impl ClientTrait for TowerClient {
|
2023-03-15 10:34:23 -04:00
|
|
|
async fn publish_diagnostics(
|
2021-12-15 13:23:43 -05:00
|
|
|
&self,
|
|
|
|
uri: lsp::Url,
|
|
|
|
diagnostics: Vec<lsp::Diagnostic>,
|
|
|
|
version: Option<i32>,
|
2023-03-15 10:34:23 -04:00
|
|
|
) {
|
|
|
|
self.0.publish_diagnostics(uri, diagnostics, version).await
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
|
2023-03-15 10:34:23 -04:00
|
|
|
async fn send_registry_state_notification(
|
2021-12-15 13:23:43 -05:00
|
|
|
&self,
|
|
|
|
params: lsp_custom::RegistryStateNotificationParams,
|
2023-03-15 10:34:23 -04:00
|
|
|
) {
|
|
|
|
self
|
|
|
|
.0
|
|
|
|
.send_notification::<lsp_custom::RegistryStateNotification>(params)
|
|
|
|
.await
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
|
2023-05-26 02:10:18 -04:00
|
|
|
async fn send_diagnostic_batch_notification(
|
|
|
|
&self,
|
|
|
|
params: lsp_custom::DiagnosticBatchNotificationParams,
|
|
|
|
) {
|
|
|
|
self
|
|
|
|
.0
|
|
|
|
.send_notification::<lsp_custom::DiagnosticBatchNotification>(params)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2023-03-15 10:34:23 -04:00
|
|
|
async fn send_test_notification(&self, notification: TestingNotification) {
|
|
|
|
match notification {
|
|
|
|
TestingNotification::Module(params) => {
|
|
|
|
self
|
|
|
|
.0
|
|
|
|
.send_notification::<testing_lsp_custom::TestModuleNotification>(
|
2022-03-29 18:59:27 -04:00
|
|
|
params,
|
|
|
|
)
|
2023-03-15 10:34:23 -04:00
|
|
|
.await
|
|
|
|
}
|
|
|
|
TestingNotification::DeleteModule(params) => self
|
|
|
|
.0
|
|
|
|
.send_notification::<testing_lsp_custom::TestModuleDeleteNotification>(
|
|
|
|
params,
|
|
|
|
)
|
|
|
|
.await,
|
|
|
|
TestingNotification::Progress(params) => {
|
|
|
|
self
|
|
|
|
.0
|
2022-04-03 00:17:30 -04:00
|
|
|
.send_notification::<testing_lsp_custom::TestRunProgressNotification>(
|
2022-03-29 18:59:27 -04:00
|
|
|
params,
|
|
|
|
)
|
2023-03-15 10:34:23 -04:00
|
|
|
.await
|
2022-03-29 18:59:27 -04:00
|
|
|
}
|
2023-03-15 10:34:23 -04:00
|
|
|
}
|
2022-03-29 18:59:27 -04:00
|
|
|
}
|
|
|
|
|
2023-10-12 11:07:27 -04:00
|
|
|
async fn send_did_change_deno_configuration_notification(
|
|
|
|
&self,
|
|
|
|
params: lsp_custom::DidChangeDenoConfigurationNotificationParams,
|
|
|
|
) {
|
|
|
|
self
|
|
|
|
.0
|
|
|
|
.send_notification::<lsp_custom::DidChangeDenoConfigurationNotification>(
|
|
|
|
params,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:10:51 -05:00
|
|
|
async fn send_did_upgrade_check_notification(
|
|
|
|
&self,
|
|
|
|
params: lsp_custom::DidUpgradeCheckNotificationParams,
|
|
|
|
) {
|
|
|
|
self
|
|
|
|
.0
|
|
|
|
.send_notification::<lsp_custom::DidUpgradeCheckNotification>(params)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2023-10-24 16:27:27 -04:00
|
|
|
async fn workspace_configuration(
|
2021-12-15 13:23:43 -05:00
|
|
|
&self,
|
2023-10-24 16:27:27 -04:00
|
|
|
scopes: Vec<Option<lsp::Url>>,
|
|
|
|
) -> Result<Vec<WorkspaceSettings>, AnyError> {
|
2023-03-15 10:34:23 -04:00
|
|
|
let config_response = self
|
|
|
|
.0
|
|
|
|
.configuration(
|
2023-10-24 16:27:27 -04:00
|
|
|
scopes
|
|
|
|
.iter()
|
|
|
|
.flat_map(|scope_uri| {
|
|
|
|
vec![
|
|
|
|
ConfigurationItem {
|
|
|
|
scope_uri: scope_uri.clone(),
|
|
|
|
section: Some(SETTINGS_SECTION.to_string()),
|
|
|
|
},
|
|
|
|
ConfigurationItem {
|
|
|
|
scope_uri: scope_uri.clone(),
|
|
|
|
section: Some("javascript".to_string()),
|
|
|
|
},
|
|
|
|
ConfigurationItem {
|
|
|
|
scope_uri: scope_uri.clone(),
|
|
|
|
section: Some("typescript".to_string()),
|
|
|
|
},
|
|
|
|
]
|
2022-01-25 10:30:38 -05:00
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
2023-03-15 10:34:23 -04:00
|
|
|
.await;
|
|
|
|
match config_response {
|
2023-09-21 01:46:39 -04:00
|
|
|
Ok(configs) => {
|
|
|
|
let mut configs = configs.into_iter();
|
2023-10-24 16:27:27 -04:00
|
|
|
let mut result = Vec::with_capacity(scopes.len());
|
|
|
|
for _ in 0..scopes.len() {
|
|
|
|
let deno = json!(configs.next());
|
|
|
|
let javascript = json!(configs.next());
|
|
|
|
let typescript = json!(configs.next());
|
|
|
|
result.push(WorkspaceSettings::from_raw_settings(
|
|
|
|
deno, javascript, typescript,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(result)
|
2023-09-21 01:46:39 -04:00
|
|
|
}
|
2023-03-15 10:34:23 -04:00
|
|
|
Err(err) => {
|
2023-10-24 16:27:27 -04:00
|
|
|
bail!("Error getting workspace configurations: {}", err)
|
2022-01-25 10:30:38 -05:00
|
|
|
}
|
2023-03-15 10:34:23 -04:00
|
|
|
}
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
|
2023-03-15 10:34:23 -04:00
|
|
|
async fn show_message(
|
2021-12-15 13:23:43 -05:00
|
|
|
&self,
|
|
|
|
message_type: lsp::MessageType,
|
|
|
|
message: String,
|
2023-03-15 10:34:23 -04:00
|
|
|
) {
|
|
|
|
self.0.show_message(message_type, message).await
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
|
2023-03-15 10:34:23 -04:00
|
|
|
async fn register_capability(
|
2021-12-15 13:23:43 -05:00
|
|
|
&self,
|
|
|
|
registrations: Vec<lsp::Registration>,
|
2023-03-15 10:34:23 -04:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
self
|
|
|
|
.0
|
|
|
|
.register_capability(registrations)
|
|
|
|
.await
|
|
|
|
.map_err(|err| anyhow!("{}", err))
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct ReplClient;
|
|
|
|
|
2023-03-15 10:34:23 -04:00
|
|
|
#[async_trait]
|
2021-12-15 13:23:43 -05:00
|
|
|
impl ClientTrait for ReplClient {
|
2023-03-15 10:34:23 -04:00
|
|
|
async fn publish_diagnostics(
|
2021-12-15 13:23:43 -05:00
|
|
|
&self,
|
|
|
|
_uri: lsp::Url,
|
|
|
|
_diagnostics: Vec<lsp::Diagnostic>,
|
|
|
|
_version: Option<i32>,
|
2023-03-15 10:34:23 -04:00
|
|
|
) {
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
|
2023-03-15 10:34:23 -04:00
|
|
|
async fn send_registry_state_notification(
|
2021-12-15 13:23:43 -05:00
|
|
|
&self,
|
|
|
|
_params: lsp_custom::RegistryStateNotificationParams,
|
2023-03-15 10:34:23 -04:00
|
|
|
) {
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
|
2023-05-26 02:10:18 -04:00
|
|
|
async fn send_diagnostic_batch_notification(
|
|
|
|
&self,
|
|
|
|
_params: lsp_custom::DiagnosticBatchNotificationParams,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2023-03-15 10:34:23 -04:00
|
|
|
async fn send_test_notification(&self, _params: TestingNotification) {}
|
2022-03-29 18:59:27 -04:00
|
|
|
|
2023-10-12 11:07:27 -04:00
|
|
|
async fn send_did_change_deno_configuration_notification(
|
|
|
|
&self,
|
|
|
|
_params: lsp_custom::DidChangeDenoConfigurationNotificationParams,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:10:51 -05:00
|
|
|
async fn send_did_upgrade_check_notification(
|
|
|
|
&self,
|
|
|
|
_params: lsp_custom::DidUpgradeCheckNotificationParams,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2023-09-21 01:46:39 -04:00
|
|
|
async fn workspace_configuration(
|
|
|
|
&self,
|
2023-10-24 16:27:27 -04:00
|
|
|
scopes: Vec<Option<lsp::Url>>,
|
|
|
|
) -> Result<Vec<WorkspaceSettings>, AnyError> {
|
|
|
|
Ok(vec![get_repl_workspace_settings(); scopes.len()])
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
|
2023-03-15 10:34:23 -04:00
|
|
|
async fn show_message(
|
2021-12-15 13:23:43 -05:00
|
|
|
&self,
|
|
|
|
_message_type: lsp::MessageType,
|
|
|
|
_message: String,
|
2023-03-15 10:34:23 -04:00
|
|
|
) {
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
|
2023-03-15 10:34:23 -04:00
|
|
|
async fn register_capability(
|
2021-12-15 13:23:43 -05:00
|
|
|
&self,
|
|
|
|
_registrations: Vec<lsp::Registration>,
|
2023-03-15 10:34:23 -04:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
Ok(())
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
}
|