1use std::path::Path;
2
3use anyhow::{Result, bail};
4use async_trait::async_trait;
5use dap::{DapLocator, DebugRequest, adapters::DebugAdapterName};
6use gpui::SharedString;
7
8use task::{DebugScenario, SpawnInTerminal, TaskTemplate, VariableName};
9
10pub(crate) struct PythonLocator;
11
12#[async_trait]
13impl DapLocator for PythonLocator {
14 fn name(&self) -> SharedString {
15 SharedString::new_static("Python")
16 }
17
18 /// Determines whether this locator can generate debug target for given task.
19 async fn create_scenario(
20 &self,
21 build_config: &TaskTemplate,
22 resolved_label: &str,
23 adapter: &DebugAdapterName,
24 ) -> Option<DebugScenario> {
25 if adapter.0.as_ref() != "Debugpy" {
26 return None;
27 }
28 let valid_program = build_config.command.starts_with("$ZED_")
29 || Path::new(&build_config.command)
30 .file_name()
31 .is_some_and(|name| name.to_str().is_some_and(|path| path.starts_with("python")));
32 if !valid_program || build_config.args.iter().any(|arg| arg == "-c") {
33 // We cannot debug selections.
34 return None;
35 }
36 let command = if build_config.command
37 == VariableName::Custom("PYTHON_ACTIVE_ZED_TOOLCHAIN".into()).template_value()
38 {
39 VariableName::Custom("PYTHON_ACTIVE_ZED_TOOLCHAIN_RAW".into()).template_value()
40 } else {
41 build_config.command.clone()
42 };
43 let module_specifier_position = build_config
44 .args
45 .iter()
46 .position(|arg| arg == "-m")
47 .map(|position| position + 1);
48 // Skip the -m and module name, get all that's after.
49 let mut rest_of_the_args = module_specifier_position
50 .and_then(|position| build_config.args.get(position..))
51 .into_iter()
52 .flatten()
53 .fuse();
54 let mod_name = rest_of_the_args.next();
55 let args = rest_of_the_args.collect::<Vec<_>>();
56
57 let program_position = mod_name
58 .is_none()
59 .then(|| {
60 build_config
61 .args
62 .iter()
63 .position(|arg| *arg == "\"$ZED_FILE\"")
64 })
65 .flatten();
66 let args = if let Some(position) = program_position {
67 args.into_iter().skip(position).collect::<Vec<_>>()
68 } else {
69 args
70 };
71 if program_position.is_none() && mod_name.is_none() {
72 return None;
73 }
74 let mut config = serde_json::json!({
75 "request": "launch",
76 "python": command,
77 "args": args,
78 "cwd": build_config.cwd.clone()
79 });
80 if let Some(config_obj) = config.as_object_mut() {
81 if let Some(module) = mod_name {
82 config_obj.insert("module".to_string(), module.clone().into());
83 }
84 if let Some(program) = program_position {
85 config_obj.insert(
86 "program".to_string(),
87 build_config.args[program].clone().into(),
88 );
89 }
90 }
91
92 Some(DebugScenario {
93 adapter: adapter.0.clone(),
94 label: resolved_label.to_string().into(),
95 build: None,
96 config,
97 tcp_connection: None,
98 })
99 }
100
101 async fn run(&self, _: SpawnInTerminal) -> Result<DebugRequest> {
102 bail!("Python locator should not require DapLocator::run to be ran");
103 }
104}