Skip to content

Commit 9a7dcdc

Browse files
committed
complete emmy new debug launch debug
1 parent 5065f06 commit 9a7dcdc

File tree

10 files changed

+51
-33
lines changed

10 files changed

+51
-33
lines changed

src/context/debugger/cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl DebuggerCacheItem {
196196
match eval_rsp_result {
197197
Ok(eval_rsp) => {
198198
if eval_rsp.success {
199-
children = eval_rsp.value.children;
199+
children = eval_rsp.value.unwrap().children;
200200
}
201201
}
202202
Err(err) => {

src/context/debugger/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,17 @@ impl DebuggerConnection {
142142
let mut start = 0;
143143
let mut id_line = None;
144144
let mut i = 0;
145+
let mut msg_id = 0;
145146

146147
while i < pos {
147148
// 查找换行符
148149
if buffer[i] == b'\n' {
149150
if id_line.is_none() {
150151
// 解析第一行作为消息ID
151152
if let Ok(id_str) = std::str::from_utf8(&buffer[start..i]) {
152-
if let Ok(_msg_id) = id_str.parse::<i32>() {
153+
if let Ok(parsed_msg_id) = id_str.parse::<i32>() {
153154
// 记录ID并继续寻找JSON内容
155+
msg_id = parsed_msg_id;
154156
id_line = Some(start);
155157
start = i + 1;
156158
}
@@ -159,9 +161,10 @@ impl DebuggerConnection {
159161
// 已有ID,这一行是JSON内容
160162
if let Ok(msg_str) = std::str::from_utf8(&buffer[start..i])
161163
{
162-
if let Ok(message) =
163-
serde_json::from_str::<Message>(msg_str)
164-
{
164+
if let Ok(message) = Message::from_str(
165+
msg_str,
166+
MessageCMD::from(msg_id as i64),
167+
) {
165168
Self::dispatch_message(
166169
message,
167170
&senders,
@@ -436,6 +439,7 @@ pub struct DebuggerData {
436439
pub stacks: Vec<Stack>,
437440
pub file_cache: HashMap<String, Option<String>>,
438441
pub extension: Vec<String>,
442+
pub sources: Vec<String>,
439443
pub current_frame_id: i64,
440444
pub cache: DebuggerCache,
441445
pub breakpoints: HashMap<(String, i64), BreakPoint>,

src/context/debugger/proto.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[allow(unused)]
12
use serde::{Deserialize, Deserializer, Serialize, de};
23

34
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
@@ -124,21 +125,10 @@ impl Message {
124125
Message::LogNotify(_) => MessageCMD::LogNotify,
125126
}
126127
}
127-
}
128-
129-
impl<'de> Deserialize<'de> for Message {
130-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
131-
where
132-
D: Deserializer<'de>,
133-
{
134-
let value = serde_json::Value::deserialize(deserializer)?;
135-
136-
let cmd = value
137-
.get("cmd")
138-
.and_then(|v| v.as_i64())
139-
.ok_or_else(|| de::Error::missing_field("cmd"))?;
140128

141-
match MessageCMD::from(cmd) {
129+
pub fn from_str(s: &str, cmd: MessageCMD) -> Result<Self, serde_json::Error> {
130+
let value: serde_json::Value = serde_json::from_str(s)?;
131+
match cmd {
142132
MessageCMD::InitReq => {
143133
let init_req: InitReq = serde_json::from_value(value).map_err(de::Error::custom)?;
144134
Ok(Message::InitReq(init_req))
@@ -220,7 +210,7 @@ impl<'de> Deserialize<'de> for Message {
220210
serde_json::from_value(value).map_err(de::Error::custom)?;
221211
Ok(Message::LogNotify(log_notify))
222212
}
223-
_ => Err(de::Error::custom(format!("Unknown command: {}", cmd))),
213+
_ => Err(de::Error::custom("Unknown command")),
224214
}
225215
}
226216
}
@@ -448,8 +438,10 @@ pub struct EvalReq {
448438
pub struct EvalRsp {
449439
pub seq: i32,
450440
pub success: bool,
451-
pub error: String,
452-
pub value: Variable,
441+
#[serde(skip_serializing_if = "Option::is_none")]
442+
pub error: Option<String>,
443+
#[serde(skip_serializing_if = "Option::is_none")]
444+
pub value: Option<Variable>,
453445
}
454446

455447
#[derive(Debug, Serialize, Deserialize)]

src/context/emmy_new_debugger/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ pub struct EmmyNewDebugArguments {
77
pub port: u16,
88
pub ext: Vec<String>,
99
pub ide_connect_debugger: bool,
10+
pub source_paths: Vec<String>,
1011
}

src/handler/evaluate_request.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub async fn on_evaluate_request(
2121

2222
if eval_rsp.success {
2323
let mut data = dap.data.lock().await;
24-
let value = eval_rsp.value;
24+
let value = eval_rsp.value.unwrap();
2525
let ref_id = data.cache.allocate_cache_id();
2626
let variable_item = DebuggerCacheItem::Variable(
2727
DebuggerCacheRef::new(
@@ -44,7 +44,7 @@ pub async fn on_evaluate_request(
4444
}))
4545
} else {
4646
Ok(ResponseBody::Evaluate(EvaluateResponse {
47-
result: eval_rsp.error,
47+
result: eval_rsp.error.unwrap_or("".to_string()),
4848
type_field: Some("string".to_string()),
4949
variables_reference: 0,
5050
..Default::default()

src/handler/launch_request.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub async fn on_launch_request(
5858

5959
let mut data = dap.data.lock().await;
6060
data.extension = emmy_new_debug_argument.ext.clone();
61+
data.sources = emmy_new_debug_argument.source_paths.clone();
6162

6263
let dap = dap.clone();
6364
tokio::spawn(async move {

src/handler/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ pub async fn on_request_dispatch(
9999
.task(request, set_breakpoint_argument, on_set_breakpoints_request)
100100
.await;
101101
}
102-
103102
Command::Cancel(cancel_argument) => {
104103
if let Some(req_id) = cancel_argument.request_id {
105104
context.cancel(req_id).await;

src/handler/set_breakpoint_request.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub async fn on_set_breakpoints_request(
2121
let mut response_breakpoints = vec![];
2222
if let Some(path) = source.path {
2323
let mut data = dap.data.lock().await;
24+
log::info!("retain breakpoints");
25+
data.breakpoints.retain(|key, _| key.0 != path);
2426
if let Some(breakpoints) = set_breakpoints_arguments.breakpoints {
2527
for breakpoint in breakpoints {
2628
let line = breakpoint.line;
@@ -44,9 +46,9 @@ pub async fn on_set_breakpoints_request(
4446

4547
response_breakpoints.push(response_breakpoint);
4648
}
47-
48-
send_all_breakpoints(dap.clone()).await;
4949
}
50+
drop(data);
51+
send_all_breakpoints(dap.clone()).await;
5052
} else {
5153
log::error!("No path provided in source");
5254
}
@@ -59,6 +61,7 @@ pub async fn on_set_breakpoints_request(
5961
pub async fn send_all_breakpoints(dap: DapSnapShot) {
6062
let data = dap.data.lock().await;
6163
let breakpoints = data.breakpoints.values().cloned().collect::<Vec<_>>();
64+
log::info!("send all breakpoint: {:#?}", breakpoints);
6265
let debugger_conn = dap.debugger_conn.lock().await;
6366
match debugger_conn
6467
.send_message(Message::AddBreakPointReq(AddBreakPointReq {

src/handler/stack_trace_request.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::Path;
2+
13
use dap::{requests::StackTraceArguments, responses::ResponseBody, types::Source};
24
use tokio_util::sync::CancellationToken;
35

@@ -79,6 +81,11 @@ async fn find_file_path(
7981
} else {
8082
chunkname.clone()
8183
};
84+
let sources = data
85+
.sources
86+
.iter()
87+
.map(|s| Path::new(s))
88+
.collect::<Vec<_>>();
8289

8390
let mut file_paths = vec![];
8491
for ext in &data.extension {
@@ -94,11 +101,20 @@ async fn find_file_path(
94101
}
95102

96103
for file_path in &file_paths {
97-
if let Ok(metadata) = tokio::fs::metadata(file_path).await {
98-
if metadata.is_file() {
104+
let path = Path::new(file_path);
105+
if path.exists() {
106+
let real_file_path = path.to_string_lossy().to_string();
107+
data.file_cache
108+
.insert(chunkname.clone(), Some(real_file_path.clone()));
109+
return Ok(Some(real_file_path));
110+
}
111+
for source in &sources {
112+
let real_file_path = source.join(path);
113+
if real_file_path.exists() {
114+
let real_file_path_str = real_file_path.to_string_lossy().to_string();
99115
data.file_cache
100-
.insert(chunkname.clone(), Some(file_path.clone()));
101-
return Ok(Some(file_path.clone()));
116+
.insert(chunkname.clone(), Some(real_file_path_str.clone()));
117+
return Ok(Some(real_file_path_str));
102118
}
103119
}
104120
}

src/logger/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ pub fn init_logger(cmd_args: &CmdArgs) {
7474
let logger = Dispatch::new()
7575
.format(|out, message, record| {
7676
out.finish(format_args!(
77-
"[{} {} {}] {}",
77+
"[{} {} {}:{}] {}",
7878
Local::now().format("%Y-%m-%d %H:%M:%S %:z"),
7979
record.level(),
8080
record.target(),
81+
record.line().unwrap_or(0),
8182
message
8283
))
8384
})
@@ -98,10 +99,11 @@ pub fn init_stderr_logger(level: LevelFilter) {
9899
let logger = Dispatch::new()
99100
.format(|out, message, record| {
100101
out.finish(format_args!(
101-
"[{} {} {}] {}",
102+
"[{} {} {}:{}] {}",
102103
Local::now().format("%Y-%m-%d %H:%M:%S %:z"),
103104
record.level(),
104105
record.target(),
106+
record.line().unwrap_or(0),
105107
message
106108
))
107109
})

0 commit comments

Comments
 (0)