use super::*;
pub(super) fn comment_text_of(selector: &Selector) -> Option<u32> {
match selector {
Selector::Name(name) | Selector::Id { id: name } => crate::ids::comment_text_id(name),
_ => None,
}
}
pub(super) fn selector_of(kind: &OperationKind) -> Option<&Selector> {
match kind {
OperationKind::Replace { paragraph, .. }
| OperationKind::Insert { paragraph, .. }
| OperationKind::Delete { paragraph, .. }
| OperationKind::Comment { paragraph, .. }
| OperationKind::InsertParagraph { paragraph, .. }
| OperationKind::DeleteParagraph { paragraph, .. }
| OperationKind::FormatParagraph { paragraph, .. }
| OperationKind::MergeParagraphs { paragraph, .. }
| OperationKind::Rewrite { paragraph, .. }
| OperationKind::InsertTable { paragraph, .. }
| OperationKind::InsertToc { paragraph, .. }
| OperationKind::FormatRun { paragraph, .. }
| OperationKind::InsertFootnote { paragraph, .. }
| OperationKind::InsertImage { paragraph, .. }
| OperationKind::Redact { paragraph, .. } => Some(paragraph),
_ => None,
}
}
pub(super) fn selectors_mut(kind: &mut OperationKind) -> Vec<&mut Selector> {
match kind {
OperationKind::Comment {
paragraph, through, ..
} => std::iter::once(paragraph).chain(through.as_mut()).collect(),
OperationKind::InsertParagraph {
paragraph, like, ..
} => std::iter::once(paragraph).chain(like.as_mut()).collect(),
OperationKind::List { paragraphs, .. } => paragraphs.iter_mut().collect(),
OperationKind::Replace { paragraph, .. }
| OperationKind::Insert { paragraph, .. }
| OperationKind::Delete { paragraph, .. }
| OperationKind::DeleteParagraph { paragraph, .. }
| OperationKind::FormatParagraph { paragraph, .. }
| OperationKind::MergeParagraphs { paragraph, .. }
| OperationKind::Rewrite { paragraph, .. }
| OperationKind::InsertTable { paragraph, .. }
| OperationKind::InsertToc { paragraph, .. }
| OperationKind::FormatRun { paragraph, .. }
| OperationKind::InsertFootnote { paragraph, .. }
| OperationKind::InsertImage { paragraph, .. }
| OperationKind::Redact { paragraph, .. } => vec![paragraph],
_ => Vec::new(),
}
}
pub(super) fn finds_text(kind: &OperationKind) -> bool {
match kind {
OperationKind::Replace { .. }
| OperationKind::Delete { .. }
| OperationKind::FormatRun { .. }
| OperationKind::Redact { .. } => true,
OperationKind::Insert { after, before, .. } => after.is_some() || before.is_some(),
OperationKind::Comment { find, through, .. } => find.is_some() && through.is_none(),
_ => false,
}
}
pub(super) fn anchor_of(kind: &OperationKind) -> Option<&str> {
match kind {
OperationKind::Replace { find, .. }
| OperationKind::Delete { find, .. }
| OperationKind::FormatRun { find, .. }
| OperationKind::Redact { find, .. }
| OperationKind::InsertFootnote { after: find, .. } => Some(find),
OperationKind::Insert { after, before, .. } => after.as_deref().or(before.as_deref()),
OperationKind::Comment { find, .. } => find.as_deref(),
_ => None,
}
}
pub(super) fn check_text(text: &str) -> Result<(), String> {
if text
.chars()
.any(|c| c.is_control() || matches!(c, '\u{fffe}' | '\u{ffff}'))
{
return Err("text must be plain, without control characters (tabs and line breaks are not supported inside run text)".into());
}
Ok(())
}
pub(super) fn check_run_text(text: &str) -> Result<(), String> {
let bad = text.chars().find(|&c| {
(c.is_control() && !matches!(c, '\t' | '\n'))
|| matches!(c, '\u{fffe}' | '\u{ffff}' | '\u{FFFC}')
});
match bad {
None => Ok(()),
Some('\r') => Err("text holds U+000D, a carriage return; write a line break as \\n".into()),
Some('\u{FFFC}') => Err(
"text holds U+FFFC, which stands for a symbol in the view; a symbol cannot be written as text".into(),
),
Some(c) => Err(format!(
"text holds U+{:04X}, which a run cannot carry; write a tab as \\t and a line break as \\n",
u32::from(c)
)),
}
}
pub(super) fn blank_is_none<'de, D: serde::Deserializer<'de>>(
de: D,
) -> Result<Option<String>, D::Error> {
let text: Option<String> = serde::Deserialize::deserialize(de)?;
Ok(text.filter(|t| !t.trim().is_empty()))
}
pub(super) fn check_comment(text: &str) -> Result<(), String> {
if text.trim().is_empty() {
return Err("comment text must be nonempty".into());
}
text.split('\n').try_for_each(check_text)
}
pub(super) fn redaction(find: &str) -> String {
"\u{2588}".repeat(find.chars().count())
}
pub(super) fn context(text: &str, start: usize, end: usize, mark: &str) -> String {
const WINDOW: usize = 20;
let before: String = text[..start]
.chars()
.rev()
.take(WINDOW)
.collect::<Vec<_>>()
.into_iter()
.rev()
.collect();
let after: String = text[end..].chars().take(WINDOW).collect();
format!("{before}{mark}{after}")
}
pub(super) fn excerpt(text: &str, max: usize) -> String {
let mut s: String = text.chars().take(max).collect();
if text.chars().count() > max {
s.push('…');
}
s
}
pub(super) fn comment_bounds(
edits: &[ScheduledEdit],
start: usize,
end: usize,
op: usize,
) -> (usize, usize) {
let own_at_end: usize = edits
.iter()
.filter(|x| x.op == op && x.start == end && x.end == end)
.map(|x| x.replacement.len())
.sum();
(
new_position(edits, start, true, Some(op)),
new_position(edits, end, false, None) + own_at_end,
)
}
pub(super) fn previous_block(dom: &Dom, node: NodeId) -> Option<NodeId> {
dom.nodes_before_self(node).into_iter().rev().find(|&n| {
dom.name(n).is_some_and(|name| {
name.namespace_name() != W::URI
|| matches!(name.local_name(), "p" | "tbl" | "sdt" | "customXml")
})
})
}
pub(super) fn new_position(
edits: &[ScheduledEdit],
pos: usize,
inclusive: bool,
own: Option<usize>,
) -> usize {
let own_at = own.and_then(|o| edits.iter().position(|e| e.op == o));
let mut delta: i64 = 0;
for (k, edit) in edits.iter().enumerate() {
let (start, end, i) = (edit.start, edit.end, edit.op);
if Some(i) == own {
continue;
}
let shifts = if start == end {
if inclusive {
start < pos || (start == pos && own_at.is_none_or(|o| k < o))
} else {
start < pos
}
} else {
end <= pos
};
if shifts {
delta += edit.replacement.len() as i64 - (end - start) as i64;
}
}
(pos as i64 + delta).max(0) as usize
}
pub(super) fn anchor_readings<'a>(
text: &'a str,
find: &'a str,
) -> impl Iterator<Item = Vec<(usize, usize)>> + 'a {
let exact = move |needle: &str| -> Vec<(usize, usize)> {
text.char_indices()
.map(|(i, _)| i)
.filter(|&i| text[i..].starts_with(needle))
.map(|i| (i, i + needle.len()))
.collect()
};
let mut plain = None;
(0..4).filter_map(move |rung| match rung {
0 => Some(exact(find)),
1 => {
let stripped = crate::markdown::plain_anchor(find);
plain = (!stripped.is_empty() && stripped != find).then_some(stripped);
plain.as_deref().map(exact)
}
2 => Some(anchor::reading_hits(text, find)),
_ => plain
.as_deref()
.map(|plain| anchor::reading_hits(text, plain)),
})
}
pub(super) fn locate(
projection: &Projection,
find: &str,
occurrence: Option<usize>,
outcome: &mut EditOutcome,
window: Option<(u32, usize, usize)>,
syntax: PlanSyntax,
) -> Result<(usize, usize), (String, String)> {
if find.is_empty() {
return Err(("INVALID_EDIT".into(), "find must be nonempty".into()));
}
let text = &projection.text;
let outside = std::cell::Cell::new(false);
let inside = |hits: Vec<(usize, usize)>| -> Vec<(usize, usize)> {
let Some((_, from, to)) = window else {
return hits;
};
let kept: Vec<_> = hits
.iter()
.copied()
.filter(|&(s, e)| from <= s && e <= to)
.collect();
if kept.len() < hits.len() {
outside.set(true);
}
kept
};
let mut readings = anchor_readings(text, find);
let mut hits = inside(readings.next().expect("the literal is the first try"));
if hits.is_empty()
&& let Some(found) = readings.map(inside).find(|t| !t.is_empty())
{
let read_as: String = text[found[0].0..found[0].1].to_string();
if read_as != find {
outcome.anchor_given = Some(find.to_string());
outcome.anchor_read_as = Some(read_as);
}
hits = found;
}
outcome.matches = hits.len();
let shown = anchor::shown(find);
let (start, end) = match (hits.as_slice(), occurrence) {
([], _) => {
if window.is_none() {
outcome.near = anchor::near_matches(text, find);
}
return Err((
"ANCHOR_NOT_FOUND".into(),
match window {
Some((n, ..)) if outside.get() => {
format!("{shown} is not in the text c{n} covers")
}
Some((n, ..)) => format!("{shown} does not occur in the text c{n} covers"),
None => format!("{shown} does not occur in the paragraph"),
},
));
}
(_, Some(0)) => {
return Err(("INVALID_EDIT".into(), "occurrence is 1-based".into()));
}
([one], None) => *one,
(many, None) => {
return Err((
"AMBIGUOUS_ANCHOR".into(),
syntax.ambiguous_anchor(text, find, many),
));
}
(many, Some(k)) if k <= many.len() => many[k - 1],
(many, Some(k)) => {
let n = many.len();
return Err((
"AMBIGUOUS_ANCHOR".into(),
format!(
"{shown} occurs {n} times in the paragraph; occurrence {k} is outside occurrence 1..={n}"
),
));
}
};
Ok((start, end))
}
pub(super) fn deleted_field_revisions(dom: &Dom, run: NodeId) -> Vec<NodeId> {
let mut paragraph = dom.parent(run);
while let Some(node) = paragraph
&& !dom.name_is(node, &W::p())
{
paragraph = dom.parent(node);
}
let Some(paragraph) = paragraph else {
return Vec::new();
};
let carrier = |node: NodeId| crate::revision_processor::accept_deleted(dom, node);
let mut stack: Vec<Option<NodeId>> = Vec::new();
let mut open = Vec::new();
let mut found = false;
let mut holders = Vec::new();
dom.for_each_descendant_element(paragraph, None, |node| {
if !found && node == run {
found = true;
open = stack.iter().copied().flatten().collect();
}
if !dom.name_is(node, &W::fld_char()) {
return;
}
match dom.attribute(node, &W::fld_char_type()) {
Some("begin") => stack.push(carrier(node)),
Some("end") => {
if let Some(Some(begin)) = stack.pop()
&& found
&& open.contains(&begin)
&& let Some(end) = carrier(node)
{
holders.push(begin);
if end != begin {
holders.push(end);
}
}
}
_ => {}
}
});
holders
}
pub(super) fn editable_insertion(dom: &Dom, piece: &Piece) -> Option<NodeId> {
let run = run_of(piece);
let ins = dom.parent(run).filter(|&n| dom.name_is(n, &W::ins()))?;
let paragraph = dom.parent(ins).filter(|&n| dom.name_is(n, &W::p()))?;
let target = match *piece {
Piece::Text { t, .. } => t,
Piece::Glyph { element, .. } => element,
};
let mut fields = 0usize;
for node in dom.descendants(paragraph, None) {
if node == target {
return (fields == 0).then_some(ins);
}
if dom.name_is(node, &W::fld_char()) {
match dom.attribute(node, &W::name("fldCharType")) {
Some("begin") => fields += 1,
Some("end") => fields = fields.saturating_sub(1),
_ => {}
}
}
}
None
}
pub(super) fn is_symbol(projection: &Projection, seg: &crate::inspect::Segment) -> bool {
matches!(seg.piece, Piece::Glyph { .. }) && projection.text[seg.start..seg.end] == *"\u{FFFC}"
}
pub(super) fn attach_segment(
projection: &Projection,
pos: usize,
attach_before: bool,
) -> Option<&crate::inspect::Segment> {
let segs = &projection.segments;
if attach_before {
segs.iter()
.rev()
.find(|s| s.start < pos && pos <= s.end)
.or_else(|| segs.iter().find(|s| s.start == pos))
} else {
segs.iter()
.find(|s| s.start <= pos && pos < s.end)
.or_else(|| segs.iter().rev().find(|s| s.end == pos))
}
}
pub(super) const SELECTOR_CHARS: usize = 60;
pub(super) const NAMED_ANCHOR_CANDIDATES: usize = 3;
const ANCHOR_CANDIDATE_CHARS: usize = 42;
pub(super) fn anchor_candidate_excerpt(text: &str, (start, end): (usize, usize)) -> String {
let boundaries: Vec<usize> = text
.grapheme_indices(true)
.map(|(i, _)| i)
.chain(std::iter::once(text.len()))
.collect();
let count = boundaries.len() - 1;
let first = boundaries
.partition_point(|&i| i <= start)
.saturating_sub(1);
let last = boundaries.partition_point(|&i| i < end);
let width = ANCHOR_CANDIDATE_CHARS.min(count);
let matched = last - first;
let from = if matched <= width {
first.saturating_sub((width - matched) / 2)
} else {
first + (matched - width) / 2
}
.min(count - width);
let to = from + width;
format!(
"{}{}{}",
if from > 0 { "…" } else { "" },
&text[boundaries[from]..boundaries[to]],
if to < count { "…" } else { "" }
)
}
pub(super) fn word_window(
text: &str,
start: usize,
end: usize,
separators: &[char],
) -> (usize, usize) {
let wordy = |c: char| !c.is_whitespace() && !separators.contains(&c);
let from = text[..start]
.char_indices()
.rev()
.take_while(|&(_, c)| wordy(c))
.last()
.map_or(start, |(i, _)| i);
let to = text[end..]
.char_indices()
.find(|&(_, c)| !wordy(c))
.map_or(text.len(), |(i, _)| end + i);
(from, to)
}
pub(super) fn range_marker(dom: &Dom, node: NodeId) -> Option<&'static str> {
let name = dom.name(node).filter(|n| n.namespace_name() == W::URI)?;
tracked::RANGE_STARTS
.into_iter()
.chain(tracked::RANGE_ENDS)
.chain([tracked::COMMENT_RANGE_END])
.find(|&marker| marker == name.local_name())
}
pub(super) fn insert_outside_ranges(
dom: &mut Dom,
run: NodeId,
t: NodeId,
at: usize,
len: usize,
replacement: &str,
) -> bool {
let content: Vec<NodeId> = dom
.elements(run, None)
.into_iter()
.filter(|&n| !dom.name_is(n, &W::r_pr()))
.collect();
let before = if at == 0 && content.first() == Some(&t) {
true
} else if at == len && content.last() == Some(&t) {
false
} else {
return false;
};
let beside = tracked::outside_ranges(dom, run, before);
if beside == run {
return false;
}
let new = dom.new_element(W::r());
if let Some(rpr) = dom.element(run, &W::r_pr()) {
let rpr = tracked::clean_rpr(dom, rpr);
dom.add(new, rpr);
}
let text = dom.new_element(W::t());
dom.add(new, text);
write_text(dom, text, replacement);
if before {
dom.add_before_self(beside, new);
} else {
dom.add_after_self(beside, new);
}
true
}