use super::*;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BreakpointReport {
pub path: PathBuf,
pub line: usize,
pub verified: bool,
}
pub fn breakpoint_reports(msg: &Value) -> Vec<BreakpointReport> {
let body = match msg.get("body") {
Some(b) => b,
None => return Vec::new(),
};
let mut raw: Vec<&Value> = Vec::new();
if let Some(arr) = body.get("breakpoints").and_then(Value::as_array) {
raw.extend(arr.iter());
}
if let Some(one) = body.get("breakpoint") {
raw.push(one);
}
raw.into_iter()
.filter_map(|bp| {
let path = bp.get("source")?.get("path")?.as_str()?;
let line = bp.get("line").and_then(Value::as_i64).unwrap_or(0);
if line <= 0 {
return None;
}
Some(BreakpointReport {
path: PathBuf::from(path),
line: line as usize,
verified: bp.get("verified").and_then(Value::as_bool).unwrap_or(false),
})
})
.collect()
}
pub fn set_breakpoints_request(path: &Path, breakpoints: &[SourceBreakpoint]) -> Value {
let bps: Vec<Value> = breakpoints
.iter()
.map(|b| {
let mut bp = json!({ "line": b.line });
if let Some(c) = &b.condition {
bp["condition"] = json!(c);
}
if let Some(m) = &b.log_message {
bp["logMessage"] = json!(m);
}
if let Some(h) = &b.hit_condition {
bp["hitCondition"] = json!(h);
}
bp
})
.collect();
let lines: Vec<u32> = breakpoints.iter().map(|b| b.line).collect();
json!({
"type": "request",
"command": "setBreakpoints",
"arguments": {
"source": { "path": path.to_string_lossy() },
"breakpoints": bps,
"lines": lines
}
})
}
pub fn set_function_breakpoints_request(names: &[String]) -> Value {
let bps: Vec<Value> = names.iter().map(|n| json!({ "name": n })).collect();
json!({
"type": "request",
"command": "setFunctionBreakpoints",
"arguments": { "breakpoints": bps }
})
}
pub fn data_breakpoint_info_request(variables_reference: i64, name: &str) -> Value {
json!({
"type": "request",
"command": "dataBreakpointInfo",
"arguments": { "variablesReference": variables_reference, "name": name }
})
}
pub fn set_data_breakpoints_request(data_ids: &[String]) -> Value {
let bps: Vec<Value> = data_ids
.iter()
.map(|id| json!({ "dataId": id, "accessType": "write" }))
.collect();
json!({
"type": "request",
"command": "setDataBreakpoints",
"arguments": { "breakpoints": bps }
})
}
pub fn update_breakpoint_lines(
map: &mut BTreeMap<PathBuf, std::collections::BTreeSet<usize>>,
reports: &[BreakpointReport],
) -> bool {
let mut changed = false;
for r in reports {
let set = map.entry(r.path.clone()).or_default();
changed |= if r.verified {
set.remove(&r.line)
} else {
set.insert(r.line)
};
if set.is_empty() {
map.remove(&r.path);
}
}
changed
}
pub fn fold_breakpoint_reports(
map: &mut BTreeMap<PathBuf, std::collections::BTreeSet<usize>>,
reports: &[BreakpointReport],
) -> bool {
use std::collections::{BTreeSet, btree_map::Entry};
if reports.is_empty() {
return false;
}
let mut by_path: BTreeMap<PathBuf, BTreeSet<usize>> = BTreeMap::new();
for r in reports {
let set = by_path.entry(r.path.clone()).or_default();
if !r.verified {
set.insert(r.line);
}
}
let mut changed = false;
for (path, unverified) in by_path {
match map.entry(path) {
Entry::Occupied(mut e) => {
if *e.get() != unverified {
if unverified.is_empty() {
e.remove();
} else {
e.insert(unverified);
}
changed = true;
}
}
Entry::Vacant(e) => {
if !unverified.is_empty() {
e.insert(unverified);
changed = true;
}
}
}
}
changed
}
impl DapSession {
pub(super) fn apply_breakpoint_reports(&mut self, reports: &[BreakpointReport]) -> bool {
fold_breakpoint_reports(&mut self.unverified_breakpoints, reports)
}
pub(super) fn push_breakpoints(&mut self) {
let t = self.active();
for (path, lines) in &self.breakpoints {
let _ = t.send(set_breakpoints_request(path, lines));
}
if !self.function_breakpoints.is_empty() {
let _ = t.send(set_function_breakpoints_request(&self.function_breakpoints));
}
}
pub fn set_function_breakpoints(&mut self, names: Vec<String>) {
self.function_breakpoints = names;
if !matches!(self.phase, SessionPhase::Initializing) {
let _ = self
.active()
.send(set_function_breakpoints_request(&self.function_breakpoints));
}
}
pub fn run_to_cursor(&mut self, path: &Path, line: u32) {
if let Some((old, _)) = self.run_to.take()
&& old != path
{
let own = self.breakpoints.get(&old).cloned().unwrap_or_default();
let _ = self.active().send(set_breakpoints_request(&old, &own));
}
self.run_to = Some((path.to_path_buf(), line));
let own = self.breakpoints.get(path).cloned().unwrap_or_default();
let set = self.with_run_to(path, &own);
let _ = self.active().send(set_breakpoints_request(path, &set));
self.continue_execution();
}
pub(super) fn with_run_to(
&self,
path: &Path,
own: &[SourceBreakpoint],
) -> Vec<SourceBreakpoint> {
with_run_to(self.run_to.as_ref(), path, own)
}
pub fn break_on_change(&mut self, variables_reference: i64, name: &str) {
if let Ok(seq) = self
.active()
.send(data_breakpoint_info_request(variables_reference, name))
{
self.pending_data_info.insert(seq, name.to_string());
}
}
pub fn clear_data_breakpoints(&mut self) {
self.data_breakpoints.clear();
self.push_data_breakpoints();
}
pub(super) fn push_data_breakpoints(&mut self) {
let ids: Vec<String> = self
.data_breakpoints
.iter()
.map(|(id, _)| id.clone())
.collect();
let _ = self.active().send(set_data_breakpoints_request(&ids));
}
pub fn update_breakpoints(&mut self, path: &Path, breakpoints: &[SourceBreakpoint]) {
self.breakpoints
.insert(path.to_path_buf(), breakpoints.to_vec());
let set = self.with_run_to(path, breakpoints);
let _ = self.active().send(set_breakpoints_request(path, &set));
}
}
pub(super) fn with_run_to(
run_to: Option<&(PathBuf, u32)>,
path: &Path,
own: &[SourceBreakpoint],
) -> Vec<SourceBreakpoint> {
let mut set = own.to_vec();
if let Some((target, line)) = run_to
&& target == path
{
set.retain(|b| b.line != *line);
set.push(SourceBreakpoint::plain(*line));
}
set
}