Skip to content

Commit

Permalink
Add python attach implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
RemcoSmitsDev committed Jan 21, 2025
1 parent 4ded4d1 commit 03626e8
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions crates/dap_adapters/src/python.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use dap::transport::{TcpTransport, Transport};
use dap::{
transport::{TcpTransport, Transport},
DebugRequestType,
};
use language::LanguageName;
use std::{ffi::OsStr, net::Ipv4Addr, path::PathBuf, sync::Arc};
use regex::Regex;
use std::{collections::HashMap, ffi::OsStr, net::Ipv4Addr, path::PathBuf, sync::Arc};
use sysinfo::{Pid, Process};

use crate::*;

Expand Down Expand Up @@ -133,10 +138,39 @@ impl DebugAdapter for PythonDebugAdapter {
}

fn request_args(&self, config: &DebugAdapterConfig) -> Value {
let pid = if let DebugRequestType::Attach(attach_config) = &config.request {
attach_config.process_id
} else {
None
};

json!({
"request": match config.request {
DebugRequestType::Launch => "launch",
DebugRequestType::Attach(_) => "attach",
},
"processId": pid,
"program": config.program,
"subProcess": true,
"cwd": config.cwd,
})
}

fn supports_attach(&self) -> bool {
true
}

fn attach_processes<'a>(
&self,
processes: &'a HashMap<Pid, Process>,
) -> Option<Vec<(&'a Pid, &'a Process)>> {
let regex = Regex::new(r"(?i)^(?:python3|python|py)(?:$|\b)").unwrap();

Some(
processes
.iter()
.filter(|(_, process)| regex.is_match(&process.name().to_string_lossy()))
.collect::<Vec<_>>(),
)
}
}

0 comments on commit 03626e8

Please sign in to comment.