debugger: Mark DapLocator::create_scenario as an async function (#32680)

Paves way for locators in extensions.

Release Notes:

- N/A
This commit is contained in:
Piotr Osiewicz 2025-06-13 13:19:03 +02:00 committed by GitHub
parent 2c491d3a66
commit d5b8c21a75
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 290 additions and 218 deletions

View file

@ -287,11 +287,17 @@ impl DapStore {
adapter: DebugAdapterName,
label: SharedString,
cx: &mut App,
) -> Option<DebugScenario> {
DapRegistry::global(cx)
.locators()
.values()
.find_map(|locator| locator.create_scenario(&build, &label, adapter.clone()))
) -> Task<Option<DebugScenario>> {
let locators = DapRegistry::global(cx).locators();
cx.background_spawn(async move {
for locator in locators.values() {
if let Some(scenario) = locator.create_scenario(&build, &label, &adapter).await {
return Some(scenario);
}
}
None
})
}
pub fn run_debug_locator(

View file

@ -41,11 +41,11 @@ impl DapLocator for CargoLocator {
fn name(&self) -> SharedString {
SharedString::new_static("rust-cargo-locator")
}
fn create_scenario(
async fn create_scenario(
&self,
build_config: &TaskTemplate,
resolved_label: &str,
adapter: DebugAdapterName,
adapter: &DebugAdapterName,
) -> Option<DebugScenario> {
if build_config.command != "cargo" {
return None;
@ -77,7 +77,7 @@ impl DapLocator for CargoLocator {
}
Some(DebugScenario {
adapter: adapter.0,
adapter: adapter.0.clone(),
label: resolved_label.to_string().into(),
build: Some(BuildTaskDefinition::Template {
task_template,

View file

@ -89,11 +89,11 @@ impl DapLocator for GoLocator {
SharedString::new_static("go-debug-locator")
}
fn create_scenario(
async fn create_scenario(
&self,
build_config: &TaskTemplate,
resolved_label: &str,
adapter: DebugAdapterName,
adapter: &DebugAdapterName,
) -> Option<DebugScenario> {
if build_config.command != "go" {
return None;
@ -170,7 +170,7 @@ impl DapLocator for GoLocator {
Some(DebugScenario {
label: resolved_label.to_string().into(),
adapter: adapter.0,
adapter: adapter.0.clone(),
build: None,
config: config,
tcp_connection: None,
@ -214,7 +214,7 @@ impl DapLocator for GoLocator {
Some(DebugScenario {
label: resolved_label.to_string().into(),
adapter: adapter.0,
adapter: adapter.0.clone(),
build: None,
config,
tcp_connection: None,
@ -232,10 +232,11 @@ impl DapLocator for GoLocator {
#[cfg(test)]
mod tests {
use super::*;
use gpui::TestAppContext;
use task::{HideStrategy, RevealStrategy, RevealTarget, Shell, TaskTemplate};
#[test]
fn test_create_scenario_for_go_build() {
#[gpui::test]
async fn test_create_scenario_for_go_build(_: &mut TestAppContext) {
let locator = GoLocator;
let task = TaskTemplate {
label: "go build".into(),
@ -254,14 +255,15 @@ mod tests {
show_command: true,
};
let scenario =
locator.create_scenario(&task, "test label", DebugAdapterName("Delve".into()));
let scenario = locator
.create_scenario(&task, "test label", &DebugAdapterName("Delve".into()))
.await;
assert!(scenario.is_none());
}
#[test]
fn test_skip_non_go_commands_with_non_delve_adapter() {
#[gpui::test]
async fn test_skip_non_go_commands_with_non_delve_adapter(_: &mut TestAppContext) {
let locator = GoLocator;
let task = TaskTemplate {
label: "cargo build".into(),
@ -280,19 +282,22 @@ mod tests {
show_command: true,
};
let scenario = locator.create_scenario(
&task,
"test label",
DebugAdapterName("SomeOtherAdapter".into()),
);
let scenario = locator
.create_scenario(
&task,
"test label",
&DebugAdapterName("SomeOtherAdapter".into()),
)
.await;
assert!(scenario.is_none());
let scenario =
locator.create_scenario(&task, "test label", DebugAdapterName("Delve".into()));
let scenario = locator
.create_scenario(&task, "test label", &DebugAdapterName("Delve".into()))
.await;
assert!(scenario.is_none());
}
#[test]
fn test_go_locator_run() {
#[gpui::test]
async fn test_go_locator_run(_: &mut TestAppContext) {
let locator = GoLocator;
let delve = DebugAdapterName("Delve".into());
@ -319,7 +324,8 @@ mod tests {
};
let scenario = locator
.create_scenario(&task, "test run label", delve)
.create_scenario(&task, "test run label", &delve)
.await
.unwrap();
let config: DelveLaunchRequest = serde_json::from_value(scenario.config).unwrap();
@ -350,8 +356,8 @@ mod tests {
);
}
#[test]
fn test_go_locator_test() {
#[gpui::test]
async fn test_go_locator_test(_: &mut TestAppContext) {
let locator = GoLocator;
let delve = DebugAdapterName("Delve".into());
@ -370,7 +376,8 @@ mod tests {
..Default::default()
};
let result = locator
.create_scenario(&task_with_tags, "", delve.clone())
.create_scenario(&task_with_tags, "", &delve)
.await
.unwrap();
let config: DelveLaunchRequest = serde_json::from_value(result.config).unwrap();
@ -393,8 +400,8 @@ mod tests {
);
}
#[test]
fn test_skip_unsupported_go_commands() {
#[gpui::test]
async fn test_skip_unsupported_go_commands(_: &mut TestAppContext) {
let locator = GoLocator;
let task = TaskTemplate {
label: "go clean".into(),
@ -413,8 +420,9 @@ mod tests {
show_command: true,
};
let scenario =
locator.create_scenario(&task, "test label", DebugAdapterName("Delve".into()));
let scenario = locator
.create_scenario(&task, "test label", &DebugAdapterName("Delve".into()))
.await;
assert!(scenario.is_none());
}
}

View file

@ -19,11 +19,11 @@ impl DapLocator for NodeLocator {
}
/// Determines whether this locator can generate debug target for given task.
fn create_scenario(
async fn create_scenario(
&self,
build_config: &TaskTemplate,
resolved_label: &str,
adapter: DebugAdapterName,
adapter: &DebugAdapterName,
) -> Option<DebugScenario> {
if adapter.0.as_ref() != "JavaScript" {
return None;
@ -68,7 +68,7 @@ impl DapLocator for NodeLocator {
});
Some(DebugScenario {
adapter: adapter.0,
adapter: adapter.0.clone(),
label: resolved_label.to_string().into(),
build: None,
config,

View file

@ -16,11 +16,11 @@ impl DapLocator for PythonLocator {
}
/// Determines whether this locator can generate debug target for given task.
fn create_scenario(
async fn create_scenario(
&self,
build_config: &TaskTemplate,
resolved_label: &str,
adapter: DebugAdapterName,
adapter: &DebugAdapterName,
) -> Option<DebugScenario> {
if adapter.0.as_ref() != "Debugpy" {
return None;
@ -92,7 +92,7 @@ impl DapLocator for PythonLocator {
}
Some(DebugScenario {
adapter: adapter.0,
adapter: adapter.0.clone(),
label: resolved_label.to_string().into(),
build: None,
config,

View file

@ -265,7 +265,7 @@ impl Inventory {
current_resolved_tasks: Vec<(TaskSourceKind, task::ResolvedTask)>,
add_current_language_tasks: bool,
cx: &mut App,
) -> (Vec<DebugScenario>, Vec<(TaskSourceKind, DebugScenario)>) {
) -> Task<(Vec<DebugScenario>, Vec<(TaskSourceKind, DebugScenario)>)> {
let mut scenarios = Vec::new();
if let Some(worktree_id) = task_contexts
@ -279,9 +279,13 @@ impl Inventory {
}
scenarios.extend(self.global_debug_scenarios_from_settings());
if let Some(location) = task_contexts.location() {
let file = location.buffer.read(cx).file();
let language = location.buffer.read(cx).language();
let last_scheduled_scenarios = self.last_scheduled_scenarios.iter().cloned().collect();
let adapter = task_contexts.location().and_then(|location| {
let (file, language) = {
let buffer = location.buffer.read(cx);
(buffer.file(), buffer.language())
};
let language_name = language.as_ref().map(|l| l.name());
let adapter = language_settings(language_name, file, cx)
.debuggers
@ -290,7 +294,10 @@ impl Inventory {
.or_else(|| {
language.and_then(|l| l.config().debuggers.first().map(SharedString::from))
});
if let Some(adapter) = adapter {
adapter.map(|adapter| (adapter, DapRegistry::global(cx).locators()))
});
cx.background_spawn(async move {
if let Some((adapter, locators)) = adapter {
for (kind, task) in
lsp_tasks
.into_iter()
@ -299,28 +306,21 @@ impl Inventory {
|| !matches!(kind, TaskSourceKind::Language { .. })
}))
{
if let Some(scenario) =
DapRegistry::global(cx)
.locators()
.values()
.find_map(|locator| {
locator.create_scenario(
&task.original_task().clone(),
&task.display_label(),
adapter.clone().into(),
)
})
{
scenarios.push((kind, scenario));
let adapter = adapter.clone().into();
for locator in locators.values() {
if let Some(scenario) = locator
.create_scenario(&task.original_task(), &task.display_label(), &adapter)
.await
{
scenarios.push((kind, scenario));
break;
}
}
}
}
}
(
self.last_scheduled_scenarios.iter().cloned().collect(),
scenarios,
)
(last_scheduled_scenarios, scenarios)
})
}
pub fn task_template_by_label(
@ -1275,7 +1275,7 @@ mod tests {
init_test(cx);
let fs = FakeFs::new(cx.executor());
let inventory = cx.update(|cx| Inventory::new(fs, cx));
inventory.update(cx, |inventory, cx| {
inventory.update(cx, |inventory, _| {
inventory
.update_file_based_scenarios(
TaskSettingsLocation::Global(Path::new("")),
@ -1291,31 +1291,40 @@ mod tests {
),
)
.unwrap();
});
let (_, scenario) = inventory
.list_debug_scenarios(&TaskContexts::default(), vec![], vec![], false, cx)
.1
let (_, scenario) = inventory
.update(cx, |this, cx| {
this.list_debug_scenarios(&TaskContexts::default(), vec![], vec![], false, cx)
})
.await
.1
.first()
.unwrap()
.clone();
inventory.update(cx, |this, _| {
this.scenario_scheduled(scenario.clone());
});
assert_eq!(
inventory
.update(cx, |this, cx| {
this.list_debug_scenarios(&TaskContexts::default(), vec![], vec![], false, cx)
})
.await
.0
.first()
.unwrap()
.clone();
.clone(),
scenario
);
inventory.scenario_scheduled(scenario.clone());
assert_eq!(
inventory
.list_debug_scenarios(&TaskContexts::default(), vec![], vec![], false, cx)
.0
.first()
.unwrap()
.clone(),
scenario
);
inventory
.update_file_based_scenarios(
TaskSettingsLocation::Global(Path::new("")),
Some(
r#"
inventory.update(cx, |this, _| {
this.update_file_based_scenarios(
TaskSettingsLocation::Global(Path::new("")),
Some(
r#"
[{
"label": "test scenario",
"adapter": "Delve",
@ -1323,25 +1332,29 @@ mod tests {
"program": "wowzer",
}]
"#,
),
)
.unwrap();
assert_eq!(
inventory
.list_debug_scenarios(&TaskContexts::default(), vec![], vec![], false, cx)
.0
.first()
.unwrap()
.adapter,
"Delve",
);
),
)
.unwrap();
});
assert_eq!(
inventory
.update_file_based_scenarios(
TaskSettingsLocation::Global(Path::new("")),
Some(
r#"
.update(cx, |this, cx| {
this.list_debug_scenarios(&TaskContexts::default(), vec![], vec![], false, cx)
})
.await
.0
.first()
.unwrap()
.adapter,
"Delve",
);
inventory.update(cx, |this, _| {
this.update_file_based_scenarios(
TaskSettingsLocation::Global(Path::new("")),
Some(
r#"
[{
"label": "testing scenario",
"adapter": "Delve",
@ -1349,18 +1362,21 @@ mod tests {
"program": "wowzer",
}]
"#,
),
)
.unwrap();
assert_eq!(
inventory
.list_debug_scenarios(&TaskContexts::default(), vec![], vec![], false, cx)
.0
.first(),
None
);
),
)
.unwrap();
});
assert_eq!(
inventory
.update(cx, |this, cx| {
this.list_debug_scenarios(&TaskContexts::default(), vec![], vec![], false, cx)
})
.await
.0
.first(),
None
);
}
#[gpui::test]