use super::*;
pub fn discover_compounds(root: &Path) -> Vec<Compound> {
let mut out: Vec<Compound> = Vec::new();
for rel in [".croft/launch.json", ".vscode/launch.json"] {
let Ok(text) = std::fs::read_to_string(root.join(rel)) else {
continue;
};
for c in parse_compounds(&text, rel) {
if !out.iter().any(|existing| existing.name == c.name) {
out.push(c);
}
}
}
out
}
pub fn discover_configs_all(root: &Path) -> Vec<DebugConfig> {
let mut out: Vec<DebugConfig> = Vec::new();
for (rel, source) in [
(".croft/launch.json", ".croft/launch.json"),
(".vscode/launch.json", ".vscode/launch.json"),
] {
let Ok(text) = std::fs::read_to_string(root.join(rel)) else {
continue;
};
out.extend(parse_launch_json(&text, source));
}
out
}
pub fn discover_configs(root: &Path) -> Vec<DebugConfig> {
let mut out: Vec<DebugConfig> = Vec::new();
for (rel, source) in [
(".croft/launch.json", ".croft/launch.json"),
(".vscode/launch.json", ".vscode/launch.json"),
] {
let Ok(text) = std::fs::read_to_string(root.join(rel)) else {
continue;
};
for cfg in parse_launch_json(&text, source) {
if !out.iter().any(|c| c.name == cfg.name) {
out.push(cfg);
}
}
}
out
}
pub fn parse_launch_json(text: &str, source: &'static str) -> Vec<DebugConfig> {
let Ok(v) = serde_json::from_str::<Value>(&crate::jsonc::strip_jsonc(text)) else {
return Vec::new();
};
let list = match (&v, v.get("configurations")) {
(_, Some(Value::Array(a))) => a.clone(),
(Value::Array(a), _) => a.clone(),
_ => return Vec::new(),
};
let inputs = parse_inputs(&v);
list.into_iter()
.filter_map(|c| {
let obj = c.as_object()?;
let name = obj.get("name")?.as_str()?.to_string();
let type_name = obj.get("type")?.as_str()?.to_string();
let request = match obj.get("request").and_then(Value::as_str) {
Some("attach") => RequestKind::Attach,
_ => RequestKind::Launch,
};
Some(DebugConfig {
name,
type_name,
request,
source,
presentation: parse_presentation(obj),
inputs: inputs.clone(),
answers: BTreeMap::new(),
root: None,
raw: obj.clone(),
})
})
.collect()
}
pub(super) fn folder_root(folders: &[(String, PathBuf)], name: &str) -> Option<PathBuf> {
folders
.iter()
.find(|(n, _)| n == name)
.map(|(_, root)| root.clone())
}
pub fn workspace_folders<'a>(roots: impl Iterator<Item = &'a Path>) -> Vec<(String, PathBuf)> {
roots
.map(|r| {
let name = r
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default();
(name, r.to_path_buf())
})
.collect()
}