use techy::core::node::NodeRef;
use crate::lang::text::{is_blank, is_space};
use crate::lang::{Flm, FlmSpan, structural};
use crate::stdlib::features::refs::{self, LabelUse};
use super::LABELS_ARG;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Rejected {
pub found: String,
pub span: FlmSpan,
}
const QUOTED_CHARS: usize = 40;
pub fn labels_of(node: &NodeRef<'_, Flm>) -> (Vec<LabelUse>, Vec<Rejected>) {
let Ok(Some(run)) = node.argument_content_nodes_named(LABELS_ARG) else {
return (Vec::new(), Vec::new());
};
let uses = refs::label_uses_in(run.iter());
let rejected = run
.iter()
.filter(|node| refs::label_use(node).is_none() && !is_layout(node))
.map(|node| Rejected { found: quoted(node.span_content()), span: node.span().clone() })
.collect();
(uses, rejected)
}
fn is_layout(node: &NodeRef<'_, Flm>) -> bool {
node.is_comment() || is_blank(node) || node.ext().element.name.as_str() == structural::PARBREAK
}
fn quoted(text: &str) -> String {
let text = text.trim_matches(is_space);
match text.char_indices().nth(QUOTED_CHARS) {
Some((at, _)) => format!("{}\u{2026}", &text[..at]),
None => text.to_string(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_long_text_is_cut() {
assert_eq!(quoted(" \\textbf{x} "), "\\textbf{x}");
let long = "a".repeat(50);
assert_eq!(quoted(&long), format!("{}\u{2026}", "a".repeat(40)));
assert_eq!(quoted(&"é".repeat(40)), "é".repeat(40));
}
}