use std::collections::{BTreeMap, HashMap, HashSet};
use super::{Broken, Capture, Checker, Placement, Repair, Spot};
use crate::{
Finding,
grammar::KeyText,
markdown::{Anchor, Body, render_anchor},
text,
};
pub fn placements(captures: &[Capture]) -> Vec<Placement> {
let firsts = identities(captures);
let mut by_key: BTreeMap<usize, Vec<&Capture>> = BTreeMap::new();
let mut order: Vec<usize> = Vec::new();
for (cap, &first) in captures.iter().zip(&firsts) {
let entry = by_key.entry(first).or_default();
if entry.is_empty() {
order.push(first);
}
entry.push(cap);
}
let mut ids: HashSet<String> = HashSet::new();
let mut out = Vec::new();
for key in order {
let caps = &by_key[&key];
let heading = caps.iter().find(|c| c.heading_slug.is_some());
let holder = holder(caps);
let anchor = caps[0].anchor.as_str();
let id = if !anchor.is_empty() && caps.iter().all(|c| c.anchor == anchor) {
anchor.to_string()
} else {
holder.unwrap_or(caps[0]).id()
};
if id.is_empty() || ids.contains(&id) {
continue;
}
let spot = match (heading, holder) {
(Some(h), _) if h.heading_slug.as_deref() == Some(id.as_str()) => None,
(_, holder) => holder.map(|c| c.spot),
};
ids.insert(id.clone());
if let (Some(spot), Some(holder)) = (spot, holder) {
out.push(Placement {
id,
key: holder.key.clone(),
spot,
});
}
}
out
}
pub(super) fn holder<'a>(caps: &[&'a Capture]) -> Option<&'a Capture> {
caps.iter()
.find(|c| c.heading_slug.is_some())
.or_else(|| caps.iter().find(|c| c.pairs.is_none() && !c.repeats))
.or_else(|| caps.iter().find(|c| c.pairs.is_none()))
.copied()
}
pub(super) fn identities(captures: &[Capture]) -> Vec<usize> {
let mut first: HashMap<&str, usize> = HashMap::new();
for (i, cap) in captures.iter().enumerate() {
first.entry(cap.key.as_str()).or_insert(i);
}
let own = |cap: &Capture| first[cap.key.as_str()];
if captures.iter().all(|cap| cap.pairs.is_none()) {
return captures.iter().map(own).collect();
}
let texts: Vec<KeyText> = captures.iter().map(Capture::text).collect();
captures
.iter()
.enumerate()
.map(|(i, cap)| {
let Some(paired) = &cap.pairs else {
return own(cap);
};
captures
.iter()
.enumerate()
.find(|(j, other)| {
other
.pairs
.as_ref()
.is_none_or(|q| !q.with.iter().any(|w| paired.with.contains(w)))
&& other
.rule_id
.as_ref()
.is_some_and(|r| paired.with.contains(r))
&& texts[*j].same(&texts[i])
})
.map_or_else(|| own(cap), |(_, other)| own(other))
})
.collect()
}
pub(super) fn anchor_ids(captures: &[Capture], firsts: &[usize]) -> Vec<String> {
let mut by_key: BTreeMap<usize, Vec<&Capture>> = BTreeMap::new();
for (cap, &first) in captures.iter().zip(firsts) {
by_key.entry(first).or_default().push(cap);
}
let ids: BTreeMap<usize, String> = by_key
.into_iter()
.map(|(first, caps)| (first, holder(&caps).unwrap_or(caps[0]).id()))
.collect();
firsts.iter().map(|first| ids[first].clone()).collect()
}
pub fn heading_col(body: &Body, text: &str, line: usize) -> usize {
let raw = text.lines().nth(line.saturating_sub(1)).unwrap_or("");
let clean = body.clean_line(line, raw);
let setext = body.headings.iter().any(|h| h.line == line && h.setext);
let lead = clean.len() - clean.trim_start().len();
if setext {
return lead;
}
let after_hashes = clean[lead..].trim_start_matches('#');
let hashes_end = clean.len() - after_hashes.len();
hashes_end + (after_hashes.len() - after_hashes.trim_start().len())
}
fn at_spot(anchor: &Anchor, spot: Spot, body: &Body, text: &str) -> bool {
match spot {
Spot::Heading(line) => {
!anchor.alone && anchor.line == line && anchor.col == heading_col(body, text, line)
}
Spot::Inline(line, col) => !anchor.alone && anchor.line == line && anchor.col == col,
Spot::Before(line) => {
anchor.alone
&& anchor.line < line
&& text
.lines()
.enumerate()
.skip(anchor.line)
.take(line.saturating_sub(anchor.line + 1))
.all(|(i, l)| body.clean_line(i + 1, l).trim().is_empty())
}
}
}
fn held(anchor: &Anchor, broken: &[&Broken], body: &Body, text: &str) -> Option<Spot> {
if body.headings.iter().any(|h| h.slug == anchor.id) {
return None;
}
broken.iter().find_map(|b| match b {
Broken::Text(lines) | Broken::Beyond(lines) | Broken::Heading(lines)
if !anchor.alone && lines.contains(&anchor.line) =>
{
Some(Spot::Inline(anchor.line, anchor.col))
}
Broken::Before(line) if at_spot(anchor, Spot::Before(*line), body, text) => {
Some(Spot::Before(*line))
}
Broken::Block(lines) if !anchor.alone && lines.contains(&anchor.line) => {
Some(Spot::Inline(anchor.line, anchor.col))
}
_ => None,
})
}
fn unrepaired<'a>(broken: &'a [Broken], repairs: &[Repair]) -> Vec<&'a Broken> {
let rewritten: HashSet<usize> = repairs.iter().map(|r| r.line).collect();
broken
.iter()
.filter(|b| !matches!(b, Broken::Text(lines) if rewritten.contains(lines.start())))
.collect()
}
#[expect(
clippy::too_many_lines,
reason = "one pass of the check, a split of which is a refactor of its own"
)]
pub(super) fn check(c: &mut Checker<'_>) {
let mut wanted = placements(&c.out.captures);
let body = c.doc.body;
let text = c.doc.text;
let mut findings = Vec::new();
let mut stale = false;
let mut claimed: HashSet<usize> = HashSet::new();
let fail = |findings: &mut Vec<Finding>, line: usize, message: String| {
findings.push(Finding::error(
c.doc.path.clone(),
Some(line),
"TLMD_109",
message,
));
};
for p in &wanted {
let found: Vec<usize> = (0..body.anchors.len())
.filter(|&i| body.anchors[i].id == p.id)
.collect();
match found
.iter()
.find(|&&i| at_spot(&body.anchors[i], p.spot, body, text))
{
Some(&i) => {
claimed.insert(i);
}
None if found.is_empty() => fail(
&mut findings,
p.spot.line(),
format!(
"key {} has no anchor; its repair writes `{}` before it",
text::code(&p.key),
render_anchor(&p.id)
),
),
None => fail(
&mut findings,
body.anchors[found[0]].line,
format!(
"the anchor {} is not where its key {} is; its repair moves it",
text::code(&format!("#{}", p.id)),
text::code(&p.key)
),
),
}
}
let wanted_ids: HashSet<&str> = wanted.iter().map(|p| p.id.as_str()).collect();
let broken = unrepaired(&c.broken, &c.out.repairs);
let mut kept = Vec::new();
for (i, a) in body.anchors.iter().enumerate() {
if claimed.contains(&i) {
continue;
}
let captured_here = |paired: bool| {
c.out
.captures
.iter()
.any(|cap| cap.pairs.is_some() == paired && cap.line == a.line && cap.id() == a.id)
};
if !wanted_ids.contains(a.id.as_str()) {
if captured_here(true) {
stale = true;
continue;
}
if let Some(spot) = held(a, &broken, body, text) {
if kept.iter().any(|k: &Placement| k.id == a.id) {
fail(
&mut findings,
a.line,
format!(
"the anchor {} occurs more than once; its repair keeps one",
text::code(&format!("#{}", a.id))
),
);
continue;
}
kept.push(Placement {
id: a.id.clone(),
key: a.id.clone(),
spot,
});
continue;
}
fail(
&mut findings,
a.line,
format!(
"the anchor {} names no key of this document; its repair removes it",
text::code(&format!("#{}", a.id))
),
);
} else if body.anchors.iter().filter(|o| o.id == a.id).count() > 1 {
let belongs_here = wanted
.iter()
.any(|p| p.id == a.id && p.spot.line() == a.line);
if captured_here(false) && !belongs_here {
stale = true;
continue;
}
fail(
&mut findings,
a.line,
format!(
"the anchor {} occurs more than once; its repair keeps one",
text::code(&format!("#{}", a.id))
),
);
}
}
c.out.anchors_stale = stale || !findings.is_empty();
wanted.extend(kept);
c.out.anchors = wanted;
if !c.options.scaffold {
c.out.findings.extend(findings);
}
}