Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lsp): send "deno/didChangeDenoConfiguration" notifications #20827

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions cli/lsp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ impl Client {
});
}

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;
});
}

pub fn show_message(
&self,
message_type: lsp::MessageType,
Expand Down Expand Up @@ -184,6 +197,10 @@ trait ClientTrait: Send + Sync {
params: lsp_custom::DiagnosticBatchNotificationParams,
);
async fn send_test_notification(&self, params: TestingNotification);
async fn send_did_change_deno_configuration_notification(
&self,
params: lsp_custom::DidChangeDenoConfigurationNotificationParams,
);
async fn specifier_configurations(
&self,
uris: Vec<lsp::Url>,
Expand Down Expand Up @@ -259,6 +276,18 @@ impl ClientTrait for TowerClient {
}
}

async fn send_did_change_deno_configuration_notification(
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
&self,
params: lsp_custom::DidChangeDenoConfigurationNotificationParams,
) {
self
.0
.send_notification::<lsp_custom::DidChangeDenoConfigurationNotification>(
params,
)
.await
}

async fn specifier_configurations(
&self,
uris: Vec<lsp::Url>,
Expand Down Expand Up @@ -371,6 +400,12 @@ impl ClientTrait for ReplClient {

async fn send_test_notification(&self, _params: TestingNotification) {}

async fn send_did_change_deno_configuration_notification(
&self,
_params: lsp_custom::DidChangeDenoConfigurationNotificationParams,
) {
}

async fn specifier_configurations(
&self,
uris: Vec<lsp::Url>,
Expand Down
52 changes: 46 additions & 6 deletions cli/lsp/language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use deno_runtime::deno_tls::RootCertStoreProvider;
use import_map::ImportMap;
use log::error;
use serde_json::from_value;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::env;
Expand Down Expand Up @@ -1517,9 +1518,37 @@ impl Inner {

// if the current deno.json has changed, we need to reload it
if has_config_changed(&self.config, &changes) {
let mut files_to_check = HashSet::with_capacity(4);
nayeemrmn marked this conversation as resolved.
Show resolved Hide resolved
if let Some(url) = self.config.maybe_config_file().map(|c| &c.specifier) {
files_to_check.insert(url.clone());
}
if let Some(url) = self.config.maybe_config_file_canonicalized_specifier()
{
files_to_check.insert(url.clone());
}
if let Err(err) = self.update_config_file().await {
self.client.show_message(MessageType::WARNING, err);
}
if let Some(url) = self.config.maybe_config_file().map(|c| &c.specifier) {
files_to_check.insert(url.clone());
}
if let Some(url) = self.config.maybe_config_file_canonicalized_specifier()
{
files_to_check.insert(url.clone());
}
nayeemrmn marked this conversation as resolved.
Show resolved Hide resolved
let changes = params
.changes
.iter()
.filter(|e| files_to_check.contains(&e.uri))
.cloned()
.collect::<Vec<_>>();
if !changes.is_empty() {
self.client.send_did_change_deno_configuration_notification(
lsp_custom::DidChangeDenoConfigurationNotificationParams {
changes: { changes },
},
);
}
if let Err(err) = self.update_tsconfig().await {
self.client.show_message(MessageType::WARNING, err);
}
Expand Down Expand Up @@ -3573,12 +3602,23 @@ impl Inner {
}

fn get_tasks(&self) -> LspResult<Option<Value>> {
Ok(
self
.config
.maybe_config_file()
.and_then(|cf| cf.to_lsp_tasks()),
)
Ok(self.config.maybe_config_file().and_then(|cf| {
let value = cf.json.tasks.clone()?;
let tasks: BTreeMap<String, String> =
serde_json::from_value(value).ok()?;
Some(
tasks
.into_iter()
.map(|(key, value)| {
json!({
"name": key,
"detail": value,
"sourceUri": &cf.specifier,
})
})
.collect(),
)
}))
}

async fn inlay_hint(
Expand Down
15 changes: 15 additions & 0 deletions cli/lsp/lsp_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ pub struct DiagnosticBatchNotificationParams {
pub messages_len: usize,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct DidChangeDenoConfigurationNotificationParams {
pub changes: Vec<lsp::FileEvent>,
}

pub enum DidChangeDenoConfigurationNotification {}

impl lsp::notification::Notification
for DidChangeDenoConfigurationNotification
{
type Params = DidChangeDenoConfigurationNotificationParams;

const METHOD: &'static str = "deno/didChangeDenoConfiguration";
}

/// This notification is only sent for testing purposes
/// in order to know what the latest diagnostics are.
pub enum DiagnosticBatchNotification {}
Expand Down
74 changes: 72 additions & 2 deletions cli/tests/integration/lsp_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,74 @@ fn lsp_workspace_enable_paths_no_workspace_configuration() {
client.shutdown();
}

#[test]
fn lsp_did_change_deno_configuration_notification() {
let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir();
let mut client = context.new_lsp_command().build();
client.initialize_default();

temp_dir.write("deno.json", json!({}).to_string());
client.did_change_watched_files(json!({
"changes": [{
"uri": temp_dir.uri().join("deno.json").unwrap(),
"type": 1,
}],
}));
let res = client
.read_notification_with_method::<Value>("deno/didChangeDenoConfiguration");
assert_eq!(
res,
Some(json!({
"changes": [{
"uri": temp_dir.uri().join("deno.json").unwrap(),
"type": 1,
}],
}))
);

temp_dir.write(
"deno.json",
json!({ "fmt": { "semiColons": false } }).to_string(),
);
client.did_change_watched_files(json!({
"changes": [{
"uri": temp_dir.uri().join("deno.json").unwrap(),
"type": 2,
}],
}));
let res = client
.read_notification_with_method::<Value>("deno/didChangeDenoConfiguration");
assert_eq!(
res,
Some(json!({
"changes": [{
"uri": temp_dir.uri().join("deno.json").unwrap(),
"type": 2,
}],
}))
);

temp_dir.remove_file("deno.json");
client.did_change_watched_files(json!({
"changes": [{
"uri": temp_dir.uri().join("deno.json").unwrap(),
"type": 3,
}],
}));
let res = client
.read_notification_with_method::<Value>("deno/didChangeDenoConfiguration");
assert_eq!(
res,
Some(json!({
"changes": [{
"uri": temp_dir.uri().join("deno.json").unwrap(),
"type": 3,
}],
}))
);
}

#[test]
fn lsp_deno_task() {
let context = TestContextBuilder::new().use_temp_cwd().build();
Expand All @@ -795,10 +863,12 @@ fn lsp_deno_task() {
json!([
{
"name": "build",
"detail": "deno test"
"detail": "deno test",
"sourceUri": temp_dir.uri().join("deno.jsonc").unwrap(),
}, {
"name": "some:test",
"detail": "deno bundle mod.ts"
"detail": "deno bundle mod.ts",
"sourceUri": temp_dir.uri().join("deno.jsonc").unwrap(),
}
])
);
Expand Down
6 changes: 6 additions & 0 deletions test_util/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,12 @@ impl InitializeParamsBuilder {
self
}

pub fn unset_deno_enable(&mut self) -> &mut Self {
nayeemrmn marked this conversation as resolved.
Show resolved Hide resolved
let options = self.initialization_options_mut();
options.insert("enable".to_string(), json!(null));
self
}

pub fn set_import_map(&mut self, value: impl AsRef<str>) -> &mut Self {
let options = self.initialization_options_mut();
options.insert("importMap".to_string(), value.as_ref().to_string().into());
Expand Down