use crate::{
ByteRange, Occurrence, ProblemKind, Rung,
edits::{edit, text_of},
find::Hit,
matcher::line_of,
overlay::Slot,
pattern_locator::PatternLocator,
plan::OpResult,
splice::splice,
structural::Shape,
};
use std::path::Path;
pub struct PatternSpec<'a> {
pub locator: &'a PatternLocator,
pub replace: &'a str,
pub occurrence: Occurrence,
pub target: Option<&'a str>,
}
pub fn replace_pattern(
slot: &mut Slot,
index: usize,
path: &Path,
spec: &PatternSpec<'_>,
) -> OpResult {
let text = text_of(slot)?;
let hits = pattern_hits(text, path, spec.locator)?;
let chosen = choose_hits(text, hits, spec.occurrence)?;
let many = chosen.len() > 1;
let targets: Vec<ByteRange> = chosen
.iter()
.map(|hit| target_range(hit, spec.target))
.collect::<Result<_, _>>()?;
let edits: Vec<_> = targets
.iter()
.enumerate()
.map(|(n, range)| {
let id = if many {
format!("e{index}.{}", n + 1)
} else {
format!("e{index}")
};
edit(id, index, path, line_of(text, range.start), Rung::Pattern)
})
.collect();
let mut next = text.to_string();
for range in targets.iter().rev() {
next = splice(&next, *range, spec.replace);
slot.log.record(*range, spec.replace.len());
}
slot.current = Some(next);
Ok(edits)
}
fn pattern_hits(
text: &str,
path: &Path,
locator: &PatternLocator,
) -> Result<Vec<Hit>, ProblemKind> {
let shape = Shape::new(&locator.pattern, locator.language.as_deref())
.map_err(|message| ProblemKind::Invalid { message })?;
shape
.hits(path, text)
.map_err(|message| ProblemKind::Invalid { message })
}
fn choose_hits(
text: &str,
hits: Vec<Hit>,
occurrence: Occurrence,
) -> Result<Vec<Hit>, ProblemKind> {
if hits.is_empty() {
return Err(ProblemKind::Missing { nearest: None });
}
match occurrence {
Occurrence::All => Ok(hits),
Occurrence::Unique if hits.len() == 1 => Ok(hits),
Occurrence::Unique => Err(ProblemKind::Ambiguous {
lines: hits.iter().map(|h| line_of(text, h.range.start)).collect(),
}),
Occurrence::Nth(n) => hits
.get(n.wrapping_sub(1))
.cloned()
.map(|hit| vec![hit])
.ok_or_else(|| ProblemKind::Invalid {
message: format!("occurrence {n} requested but {} matched", hits.len()),
}),
}
}
fn target_range(hit: &Hit, target: Option<&str>) -> Result<ByteRange, ProblemKind> {
let Some(target) = target else {
return Ok(ByteRange {
start: hit.range.start,
end: hit.range.end,
});
};
let name = target.strip_prefix('$').unwrap_or(target);
hit.var_ranges
.get(name)
.map(|range| ByteRange {
start: range.start,
end: range.end,
})
.ok_or_else(|| ProblemKind::Invalid {
message: format!("pattern has no capture `{target}`"),
})
}