use super::*;
pub fn threads_request() -> Value {
json!({ "type": "request", "command": "threads" })
}
pub fn parse_threads(msg: &Value) -> Vec<(i64, String)> {
msg.pointer("/body/threads")
.and_then(Value::as_array)
.map(|ts| {
ts.iter()
.filter_map(|t| {
Some((
t.get("id")?.as_i64()?,
t.get("name")
.and_then(Value::as_str)
.unwrap_or("")
.to_string(),
))
})
.collect()
})
.unwrap_or_default()
}
pub fn stack_trace_request(thread_id: i64) -> Value {
json!({
"type": "request",
"command": "stackTrace",
"arguments": { "threadId": thread_id, "startFrame": 0 }
})
}
pub fn evaluate_request(expression: &str, frame_id: Option<i64>, context: &str) -> Value {
let mut args = json!({ "expression": expression, "context": context });
if let Some(fid) = frame_id {
args["frameId"] = Value::from(fid);
}
json!({ "type": "request", "command": "evaluate", "arguments": args })
}
pub fn parse_evaluate_result(response: &Value) -> Option<String> {
response
.get("body")?
.get("result")?
.as_str()
.map(str::to_string)
}
pub fn scopes_request(frame_id: i64) -> Value {
json!({
"type": "request",
"command": "scopes",
"arguments": { "frameId": frame_id }
})
}
pub fn variables_request(variables_reference: i64) -> Value {
json!({
"type": "request",
"command": "variables",
"arguments": { "variablesReference": variables_reference }
})
}
pub fn parse_stack_frames(stack_trace_response: &Value) -> Vec<StackFrame> {
stack_trace_response
.get("body")
.and_then(|b| b.get("stackFrames"))
.and_then(Value::as_array)
.map(|frames| {
frames
.iter()
.filter_map(|f| {
Some(StackFrame {
id: f.get("id")?.as_i64()?,
name: f
.get("name")
.and_then(Value::as_str)
.unwrap_or("")
.to_string(),
path: f
.get("source")
.and_then(|s| s.get("path"))
.and_then(Value::as_str)
.map(PathBuf::from),
line: f.get("line").and_then(Value::as_i64).unwrap_or(0) as usize,
})
})
.collect()
})
.unwrap_or_default()
}
pub fn parse_scopes(scopes_response: &Value) -> Vec<Scope> {
scopes_response
.get("body")
.and_then(|b| b.get("scopes"))
.and_then(Value::as_array)
.map(|scopes| {
scopes
.iter()
.filter_map(|s| {
Some(Scope {
name: s.get("name")?.as_str()?.to_string(),
variables_ref: s.get("variablesReference")?.as_i64()?,
expensive: s.get("expensive").and_then(Value::as_bool) == Some(true),
})
})
.collect()
})
.unwrap_or_default()
}
pub fn parse_variables(variables_response: &Value) -> Vec<Variable> {
variables_response
.get("body")
.and_then(|b| b.get("variables"))
.and_then(Value::as_array)
.map(|vars| {
vars.iter()
.filter_map(|v| {
Some(Variable {
name: v.get("name")?.as_str()?.to_string(),
value: v
.get("value")
.and_then(Value::as_str)
.unwrap_or("")
.to_string(),
type_name: v
.get("type")
.and_then(Value::as_str)
.unwrap_or("")
.to_string(),
variables_ref: v
.get("variablesReference")
.and_then(Value::as_i64)
.unwrap_or(0),
evaluate_name: v
.get("evaluateName")
.and_then(Value::as_str)
.map(str::to_string),
})
})
.collect()
})
.unwrap_or_default()
}
impl DapSession {
pub fn evaluate(&mut self, expression: &str, context: &str) {
let req = evaluate_request(expression, self.selected_frame, context);
if let Ok(seq) = self.active().send(req) {
self.pending_evals
.insert(seq, (context.to_string(), expression.to_string()));
}
}
pub(super) fn clear_inspection(&mut self) {
self.stack_frames.clear();
self.scopes.clear();
self.variables.clear();
self.selected_frame = None;
self.pending_var_refs.clear();
}
pub fn select_thread(&mut self, thread_id: i64) {
if self.phase != SessionPhase::Stopped || self.stopped_thread == Some(thread_id) {
return;
}
self.stopped_thread = Some(thread_id);
self.known_thread = Some(thread_id);
self.clear_inspection();
let _ = self.active().send(stack_trace_request(thread_id));
}
pub fn load_frame(&mut self, frame_id: i64) {
self.selected_frame = Some(frame_id);
self.scopes.clear();
self.variables.clear();
self.pending_var_refs.clear();
let _ = self.active().send(scopes_request(frame_id));
}
pub fn lookup_local(&self, name: &str) -> Option<&Variable> {
self.scopes
.iter()
.filter_map(|s| self.variables.get(&s.variables_ref))
.flatten()
.find(|v| v.name == name)
}
pub(super) fn scope_key(&self, index: usize) -> Option<(String, usize)> {
let scope = self.scopes.get(index)?;
let nth = self.scopes[..index]
.iter()
.filter(|s| s.name == scope.name)
.count();
Some((scope.name.clone(), nth))
}
pub fn scope_open(&self, index: usize) -> bool {
match self.scopes.get(index) {
Some(scope) if scope.expensive => self
.scope_key(index)
.is_some_and(|k| self.open_expensive_scopes.contains(&k)),
Some(_) => true,
None => false,
}
}
pub fn toggle_expensive_scope(&mut self, index: usize) {
let Some(key) = self.scope_key(index) else {
return;
};
if !self.open_expensive_scopes.remove(&key) {
self.open_expensive_scopes.insert(key);
let r = self.scopes[index].variables_ref;
self.expand_variable(r);
}
}
pub fn expand_variable(&mut self, variables_reference: i64) {
if variables_reference > 0 && !self.variables.contains_key(&variables_reference) {
self.request_variables(variables_reference);
}
}
pub(super) fn request_variables(&mut self, variables_reference: i64) {
if let Ok(seq) = self.active().send(variables_request(variables_reference)) {
self.pending_var_refs.insert(seq, variables_reference);
}
}
}
pub fn inline_locals(
scopes: &[Scope],
variables: &std::collections::BTreeMap<i64, Vec<Variable>>,
) -> Vec<(String, String)> {
let local_refs: Vec<i64> = scopes
.iter()
.filter(|s| s.name.to_lowercase().contains("local"))
.map(|s| s.variables_ref)
.collect();
let refs: Vec<i64> = if local_refs.is_empty() {
scopes
.first()
.map(|s| s.variables_ref)
.into_iter()
.collect()
} else {
local_refs
};
refs.iter()
.filter_map(|r| variables.get(r))
.flatten()
.map(|v| (v.name.clone(), v.value.clone()))
.collect()
}