Skip to content

Commit

Permalink
feat(lsp): send "deno/didChangeDenoConfiguration" on init (#21965)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn authored Jan 17, 2024
1 parent 7662b05 commit 2141543
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 25 deletions.
34 changes: 32 additions & 2 deletions cli/lsp/language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,10 @@ impl Inner {
.iter()
.filter(|e| files_to_check.contains(&e.uri))
.map(|e| lsp_custom::DenoConfigurationChangeEvent {
file_event: e.clone(),
uri: e.uri.clone(),
typ: lsp_custom::DenoConfigurationChangeType::from_file_change_type(
e.typ,
),
configuration_type: lsp_custom::DenoConfigurationType::DenoJson,
}),
);
Expand Down Expand Up @@ -1660,7 +1663,10 @@ impl Inner {
.iter()
.filter(|e| files_to_check.contains(&e.uri))
.map(|e| lsp_custom::DenoConfigurationChangeEvent {
file_event: e.clone(),
uri: e.uri.clone(),
typ: lsp_custom::DenoConfigurationChangeType::from_file_change_type(
e.typ,
),
configuration_type: lsp_custom::DenoConfigurationType::PackageJson,
}),
);
Expand Down Expand Up @@ -3303,6 +3309,30 @@ impl tower_lsp::LanguageServer for LanguageServer {
);
ls.maybe_testing_server = Some(test_server);
}

let mut config_events = vec![];
if let Some(config_file) = ls.config.maybe_config_file() {
config_events.push(lsp_custom::DenoConfigurationChangeEvent {
uri: config_file.specifier.clone(),
typ: lsp_custom::DenoConfigurationChangeType::Added,
configuration_type: lsp_custom::DenoConfigurationType::DenoJson,
});
}
if let Some(package_json) = &ls.maybe_package_json {
config_events.push(lsp_custom::DenoConfigurationChangeEvent {
uri: package_json.specifier(),
typ: lsp_custom::DenoConfigurationChangeType::Added,
configuration_type: lsp_custom::DenoConfigurationType::PackageJson,
});
}
if !config_events.is_empty() {
ls.client.send_did_change_deno_configuration_notification(
lsp_custom::DidChangeDenoConfigurationNotificationParams {
changes: config_events,
},
);
}

(ls.client.clone(), ls.http_client.clone())
};

Expand Down
24 changes: 22 additions & 2 deletions cli/lsp/lsp_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ pub struct DiagnosticBatchNotificationParams {
pub messages_len: usize,
}

#[derive(Debug, Eq, Hash, PartialEq, Copy, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum DenoConfigurationChangeType {
Added,
Changed,
Removed,
}

impl DenoConfigurationChangeType {
pub fn from_file_change_type(file_event: lsp::FileChangeType) -> Self {
match file_event {
lsp::FileChangeType::CREATED => Self::Added,
lsp::FileChangeType::CHANGED => Self::Changed,
lsp::FileChangeType::DELETED => Self::Removed,
_ => Self::Changed, // non-exhaustable enum
}
}
}

#[derive(Debug, Eq, Hash, PartialEq, Copy, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum DenoConfigurationType {
Expand All @@ -70,8 +89,9 @@ pub enum DenoConfigurationType {
#[derive(Debug, Eq, Hash, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DenoConfigurationChangeEvent {
#[serde(flatten)]
pub file_event: lsp::FileEvent,
pub uri: lsp::Url,
#[serde(rename = "type")]
pub typ: DenoConfigurationChangeType,
pub configuration_type: DenoConfigurationType,
}

Expand Down
41 changes: 20 additions & 21 deletions cli/tests/integration/lsp_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ fn lsp_did_change_deno_configuration_notification() {
Some(json!({
"changes": [{
"uri": temp_dir.uri().join("deno.json").unwrap(),
"type": 1,
"type": "added",
"configurationType": "denoJson"
}],
}))
Expand All @@ -880,7 +880,7 @@ fn lsp_did_change_deno_configuration_notification() {
Some(json!({
"changes": [{
"uri": temp_dir.uri().join("deno.json").unwrap(),
"type": 2,
"type": "changed",
"configurationType": "denoJson"
}],
}))
Expand All @@ -900,7 +900,7 @@ fn lsp_did_change_deno_configuration_notification() {
Some(json!({
"changes": [{
"uri": temp_dir.uri().join("deno.json").unwrap(),
"type": 3,
"type": "removed",
"configurationType": "denoJson"
}],
}))
Expand All @@ -920,7 +920,7 @@ fn lsp_did_change_deno_configuration_notification() {
Some(json!({
"changes": [{
"uri": temp_dir.uri().join("package.json").unwrap(),
"type": 1,
"type": "added",
"configurationType": "packageJson"
}],
}))
Expand All @@ -940,7 +940,7 @@ fn lsp_did_change_deno_configuration_notification() {
Some(json!({
"changes": [{
"uri": temp_dir.uri().join("package.json").unwrap(),
"type": 2,
"type": "changed",
"configurationType": "packageJson"
}],
}))
Expand All @@ -960,7 +960,7 @@ fn lsp_did_change_deno_configuration_notification() {
Some(json!({
"changes": [{
"uri": temp_dir.uri().join("package.json").unwrap(),
"type": 3,
"type": "removed",
"configurationType": "packageJson"
}],
}))
Expand Down Expand Up @@ -10037,9 +10037,8 @@ Deno.test({
assert_eq!(res.enqueued[0].text_document.uri, specifier);
assert_eq!(res.enqueued[0].ids.len(), 1);
let id = res.enqueued[0].ids[0].clone();

let (method, notification) = client.read_notification::<Value>();
assert_eq!(method, "deno/testRunProgress");
let notification =
client.read_notification_with_method::<Value>("deno/testRunProgress");
assert_eq!(
notification,
Some(json!({
Expand All @@ -10056,8 +10055,8 @@ Deno.test({
}))
);

let (method, notification) = client.read_notification::<Value>();
assert_eq!(method, "deno/testRunProgress");
let notification =
client.read_notification_with_method::<Value>("deno/testRunProgress");
let notification_value = notification
.as_ref()
.unwrap()
Expand Down Expand Up @@ -10092,8 +10091,8 @@ Deno.test({
}))
);

let (method, notification) = client.read_notification::<Value>();
assert_eq!(method, "deno/testRunProgress");
let notification =
client.read_notification_with_method::<Value>("deno/testRunProgress");
assert_eq!(
notification,
Some(json!({
Expand All @@ -10111,8 +10110,8 @@ Deno.test({
}))
);

let (method, notification) = client.read_notification::<Value>();
assert_eq!(method, "deno/testRunProgress");
let notification =
client.read_notification_with_method::<Value>("deno/testRunProgress");
let mut notification = notification.unwrap();
let duration = notification
.as_object_mut()
Expand Down Expand Up @@ -10140,8 +10139,8 @@ Deno.test({
})
);

let (method, notification) = client.read_notification::<Value>();
assert_eq!(method, "deno/testRunProgress");
let notification =
client.read_notification_with_method::<Value>("deno/testRunProgress");
let notification = notification.unwrap();
let obj = notification.as_object().unwrap();
assert_eq!(obj.get("id"), Some(&json!(1)));
Expand All @@ -10159,8 +10158,8 @@ Deno.test({
);
assert!(message.contains_key("duration"));

let (method, notification) = client.read_notification::<Value>();
assert_eq!(method, "deno/testRunProgress");
let notification =
client.read_notification_with_method::<Value>("deno/testRunProgress");
assert_eq!(
notification,
Some(json!({
Expand Down Expand Up @@ -10193,8 +10192,8 @@ Deno.test({

assert_eq!(client.read_diagnostics().all().len(), 0);

let (method, notification) = client.read_notification::<Value>();
assert_eq!(method, "deno/testModuleDelete");
let notification =
client.read_notification_with_method::<Value>("deno/testModuleDelete");
assert_eq!(
notification,
Some(json!({
Expand Down

0 comments on commit 2141543

Please sign in to comment.