DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge remote-tracking branch 'origin/main' into find-change-find-format
moppeeerc5b3c59cd1d7
diff --cc crates/engine/src/cmd/find.rsindex 1838207b8,a40e8da51..18b0b0342--- a/crates/engine/src/cmd/find.rs+++ b/crates/engine/src/cmd/find.rs@@@ -6,7 -6,7 +6,7 @@@ use std::sync::{Arc, Weak}use designcraft_compose::grep::{Grep, GrepOptions, translate_replacement};use designcraft_doc::story::floor_char_boundary;- use designcraft_doc::{Document, Selection, Story, StoryId, Styles, TextSel};-use designcraft_doc::{CellAddr, Document, Selection, StoryId, TextSel};++use designcraft_doc::{CellAddr, Document, Selection, Story, StoryId, Styles, TextSel};use serde_json::{Value, json};use super::{CommandSpec, bad, bool_or, cmd, has_doc, str_param};@@@ -61,7 -61,7 +61,7 @@@ pub fn specs() -> Vec<CommandSpec>}),cmd!(query "find.find", "Find", ["Edit", "Find/Change"], None,- "{find, grep?: bool, caseSensitive?: bool, wholeWord?: bool, scope?: document|story|selection, story?, findFormat?: {charStyle?, paraStyle?, chars?: resolved character attributes} (Find Format: only text with it all through; with an empty find, each stretch of text with it), change?|first?|attrs?|charStyle?|paraStyle?: ignored (find.change takes them; one request serves all three Find/Change commands)} → matches [{story, start, end, text}]",- "{find, grep?: bool, caseSensitive?: bool, wholeWord?: bool, scope?: document|story|selection, story?, change?|first?|attrs?: ignored (find.change takes them; one request serves all three Find/Change commands)} → matches [{story, start, end, text, cell?: {table, row, col}}] (document scope also searches table cells; start/end then index the cell's text)",++ "{find, grep?: bool, caseSensitive?: bool, wholeWord?: bool, scope?: document|story|selection, story?, findFormat?: {charStyle?, paraStyle?, chars?: resolved character attributes} (Find Format: only text with it all through; with an empty find, each stretch of text with it), change?|first?|attrs?|charStyle?|paraStyle?: ignored (find.change takes them; one request serves all three Find/Change commands)} → matches [{story, start, end, text, cell?: {table, row, col}}] (document scope also searches table cells; start/end then index the cell's text)",has_doc, find),cmd!("find.change",@@@ -108,197 -108,6 +108,199 @@@ fn build(p: &Value) -> Result<Grep>re.map_err(|e| bad("find", e))}+/// Find/Change's Find Format: the text a search finds must have it all through.+#[derive(Clone, Debug, Default, PartialEq, Eq)]+struct FindFormat {+ char_style: Option<String>,+ para_style: Option<String>,+ /// Resolved character attributes, as `type.selectionAttrs` reports them.+ chars: serde_json::Map<String, Value>,+}++impl FindFormat {+ fn from_params(p: &Value) -> Result<Option<Self>> {+ let Some(f) = p.get("findFormat").filter(|f| !f.is_null()) else { return Ok(None) };+ let o = f.as_object().ok_or_else(|| bad("find", "`findFormat` is an object"))?;+ // A style is named; a null asks for none. Any other value is refused rather than+ // searched without.+ let name = |k: &str| match o.get(k) {+ None | Some(Value::Null) => Ok(None),+ Some(Value::String(n)) => Ok(Some(n.clone())),+ Some(v) => Err(bad("find", format!("findFormat: `{k}` is a style name, not {v}"))),+ };+ let chars = match o.get("chars") {+ None | Some(Value::Null) => None,+ Some(Value::Object(m)) => Some(m),+ Some(v) => return Err(bad("find", format!("findFormat: `chars` is an object of character attributes, not {v}"))),+ };+ // Only attributes text has, kept as the document writes them: `font_style` and+ // `fontStyle`, or 12 and 12.0, then find the same text. A null asks for nothing.+ let mut known = designcraft_doc::CharAttrs::default();+ for (k, v) in chars.into_iter().flatten() {+ known.set_json(k, v).map_err(|e| bad("find", format!("findFormat: {e}")))?;+ }+ let chars = match serde_json::to_value(&known) {+ Ok(Value::Object(m)) => m,+ _ => serde_json::Map::new(),+ };+ let f = Self { char_style: name("charStyle")?, para_style: name("paraStyle")?, chars };+ Ok((f != Self::default()).then_some(f))+ }++ /// Styles the document doesn't have are refused, as `find.change` refuses them.+ fn check(&self, styles: &Styles) -> Result<()> {+ if let Some(n) = self.char_style.as_ref().filter(|n| styles.char_style(n).is_none()) {+ return Err(bad("find", format!("no character style `{n}`")));+ }+ if let Some(n) = self.para_style.as_ref().filter(|n| styles.para(n).is_none()) {+ return Err(bad("find", format!("no paragraph style `{n}`")));+ }+ Ok(())+ }++ /// The text in `r` of `story` that has the format, relative to `r.start`: the longest+ /// stretches, each paragraph's apart (a paragraph's `\n` isn't in one).+ fn spans(&self, styles: &Styles, story: &Story, r: &Range<usize>) -> Vec<Range<usize>> {+ let mut out: Vec<Range<usize>> = Vec::new();+ // Paragraphs and runs both go in text order: the runs before a paragraph are passed once.+ let runs: Vec<_> = story.runs().collect();+ let mut first = 0;+ for (pi, pr) in story.para_ranges().into_iter().enumerate() {+ let (a, b) = (pr.start.max(r.start), pr.end.min(r.end));+ let Some(pf) = story.paras.get(pi) else { continue };+ if a >= b || self.para_style.as_ref().is_some_and(|n| *n != pf.style) {+ continue;+ }+ while runs.get(first).is_some_and(|(run, _)| run.end <= a) {+ first += 1;+ }+ let base = (!self.chars.is_empty()).then(|| styles.resolve_para(pf).1);+ for (run, cf) in runs[first..].iter().take_while(|(run, _)| run.start < b) {+ let (x, y) = (run.start.max(a), run.end.min(b));+ if x >= y || self.char_style.as_ref().is_some_and(|n| *n != cf.style) {+ continue;+ }+ if let Some(base) = &base {+ let cp = serde_json::to_value(styles.resolve_char(base, cf)).unwrap_or_default();+ if !self.chars.iter().all(|(k, v)| cp.get(k).is_some_and(|c| same_value(c, v))) {+ continue;+ }+ }+ match out.last_mut() {+ Some(last) if last.end == x - r.start => last.end = y - r.start,+ _ => out.push(x - r.start..y - r.start),+ }+ }+ }+ out+ }+}++/// Attribute values alike: numbers to within rounding (inside objects and lists too), the rest+/// exactly.+fn same_value(a: &Value, b: &Value) -> bool {+ match (a, b) {+ (Value::Number(_), Value::Number(_)) => match (a.as_f64(), b.as_f64()) {+ (Some(x), Some(y)) => (x - y).abs() < 1e-6,+ _ => a == b,+ },+ (Value::Array(x), Value::Array(y)) => x.len() == y.len() && x.iter().zip(y).all(|(a, b)| same_value(a, b)),+ (Value::Object(x), Value::Object(y)) => x.len() == y.len() && x.iter().all(|(k, v)| y.get(k).is_some_and(|w| same_value(v, w))),+ _ => a == b,+ }+}++/// Whether match `m` lies in one of `spans`.+fn within(spans: &[Range<usize>], m: &Range<usize>) -> bool {+ spans.iter().any(|s| s.start <= m.start && m.end <= s.end)+}++/// What a search finds: the pattern's matches (with a Find Format, those whose text has it all+/// through), or, with no pattern, the stretches of text with the Find Format.+struct Search {+ re: Option<Grep>,+ format: Option<FindFormat>,+}++/// The search of a Find/Change request: an empty `find` needs a Find Format.+fn search(p: &Value) -> Result<Search> {+ let format = FindFormat::from_params(p)?;+ if format.is_some() && str_param(p, "find") == Some("") {+ return Ok(Search { re: None, format });+ }+ Ok(Search { re: Some(build(p)?), format })+}++impl Search {+ fn check(&self, styles: &Styles) -> Result<()> {+ self.format.as_ref().map_or(Ok(()), |f| f.check(styles))+ }+- /// Where the Find Format holds in target `r` of story `sid` (`None`: no Find Format).- fn spans(&self, d: &Document, sid: StoryId, r: &Range<usize>) -> Option<Vec<Range<usize>>> {++ /// Where the Find Format holds in target `r` of story `sid` (of its table cell `cell`, if any;++ /// `None`: no Find Format).++ fn spans(&self, d: &Document, sid: StoryId, cell: Option<CellAddr>, r: &Range<usize>) -> Option<Vec<Range<usize>>> {+ let f = self.format.as_ref()?;- Some(d.story(sid).map(|story| f.spans(&d.styles, story, r)).unwrap_or_default())++ Some(d.text_story(sid, cell).map(|story| f.spans(&d.styles, story, r)).unwrap_or_default())+ }++ /// Up to `limit` matches in `text`, target `r` of story `sid` (offsets in `text`).- fn find_all(&self, d: &Document, sid: StoryId, r: &Range<usize>, text: &str, limit: usize) -> Result<Vec<Range<usize>>> {- let spans = self.spans(d, sid, r);++ fn find_all(&self, d: &Document, sid: StoryId, cell: Option<CellAddr>, r: &Range<usize>, text: &str, limit: usize) -> Result<Vec<Range<usize>>> {++ let spans = self.spans(d, sid, cell, r);+ let Some(re) = &self.re else { return Ok(spans.unwrap_or_default().into_iter().take(limit).collect()) };+ let Some(spans) = spans else { return re.find_all(text, limit).map_err(|e| bad("find", e)) };+ Ok(re.find_all(text, usize::MAX).map_err(|e| bad("find", e))?.into_iter().filter(|m| within(&spans, m)).take(limit).collect())+ }++ /// The first match in `text` (target `r` of story `sid`) at or after byte `from`.- fn find_at(&self, d: &Document, sid: StoryId, r: &Range<usize>, text: &str, from: usize) -> Result<Option<Range<usize>>> {- let spans = self.spans(d, sid, r);++ fn find_at(&self, d: &Document, sid: StoryId, cell: Option<CellAddr>, r: &Range<usize>, text: &str, from: usize) -> Result<Option<Range<usize>>> {++ let spans = self.spans(d, sid, cell, r);+ let Some(re) = &self.re else {+ // From inside a stretch, the rest of it.+ return Ok(spans.unwrap_or_default().into_iter().find(|s| s.end > from).map(|s| s.start.max(from)..s.end));+ };+ let mut at = from;+ loop {+ let Some(m) = re.find_at(text, at).map_err(|e| bad("find.next", e))? else { return Ok(None) };+ if spans.as_ref().is_none_or(|s| within(s, &m)) {+ return Ok(Some(m));+ }+ // On past it: its end, or a character on from an empty match.+ at = if m.is_empty() { text.get(m.end..).and_then(|t| t.chars().next()).map_or(text.len() + 1, |c| m.end + c.len_utf8()) } else { m.end };+ if at > text.len() {+ return Ok(None);+ }+ }+ }++ /// The edits Change All makes in `text` (target `r` of story `sid`): `rep` expanded for each+ /// GREP match, else `rep` itself.+ #[allow(clippy::too_many_arguments)]+ fn replacements(+ &self,+ d: &Document,+ sid: StoryId,++ cell: Option<CellAddr>,+ r: &Range<usize>,+ text: &str,+ rep: &str,+ grep: bool,+ first_only: bool,+ ) -> Result<Vec<(Range<usize>, String)>> {+ let limit = if first_only { 1 } else { usize::MAX };- match (&self.re, self.spans(d, sid, r)) {++ match (&self.re, self.spans(d, sid, cell, r)) {+ (Some(re), None) if grep => re.replacements(text, rep, first_only).map_err(|e| bad("find.change", e)),+ (Some(re), Some(spans)) if grep => Ok(re+ .replacements(text, rep, false)+ .map_err(|e| bad("find.change", e))?+ .into_iter()+ .filter(|(m, _)| within(&spans, m))+ .take(limit)+ .collect()),- _ => Ok(self.find_all(d, sid, r, text, limit)?.into_iter().map(|m| (m, rep.to_string())).collect()),++ _ => Ok(self.find_all(d, sid, cell, r, text, limit)?.into_iter().map(|m| (m, rep.to_string())).collect()),+ }+ }+}+#[derive(Clone, Debug, PartialEq, Eq)]enum Domain {Document,@@@ -479,10 -303,14 +499,14 @@@ fn find(s: &mut Session, p: &Value) ->let context = scope(s, p)?;let st = s.doc()?;let mut out = Vec::new();- for (sid, r) in &context.targets {- let text = target_text(&st.doc, *sid, r)?;- for m in re.find_all(&st.doc, *sid, r, text, MAX_MATCHES.saturating_sub(out.len()))? {- out.push(json!({"story": sid.0, "start": r.start + m.start, "end": r.start + m.end, "text": text.get(m)}));+ for (sid, cell, r) in &context.targets {+ let text = target_text(&st.doc, *sid, *cell, r)?;- for m in re.find_all(text, MAX_MATCHES.saturating_sub(out.len())).map_err(|e| bad("find", e))? {++ for m in re.find_all(&st.doc, *sid, *cell, r, text, MAX_MATCHES.saturating_sub(out.len()))? {+ let mut hit = json!({"story": sid.0, "start": r.start + m.start, "end": r.start + m.end, "text": text.get(m)});+ if let Some(c) = cell {+ hit["cell"] = json!({"table": c.table, "row": c.row, "col": c.col});+ }+ out.push(hit);}}s.find_context = Some(context);@@@ -510,48 -336,27 +534,48 @@@ fn change(s: &mut Session, p: &Value) -}}super::text::validate_char_fill(&s.doc()?.doc, &over, "find.change")?;+ let char_style = str_param(p, "charStyle").map(str::to_string);+ let para_style = str_param(p, "paraStyle").map(str::to_string);+ let styles = &s.doc()?.doc.styles;+ if let Some(n) = char_style.as_ref().filter(|n| styles.char_style(n).is_none()) {+ return Err(bad("find.change", format!("no character style `{n}`")));+ }+ if let Some(n) = para_style.as_ref().filter(|n| styles.para(n).is_none()) {+ return Err(bad("find.change", format!("no paragraph style `{n}`")));+ }+ // InDesign: with a Change Format and nothing in Change to, the found text is only formatted.+ let format_only = str_param(p, "change").unwrap_or("").is_empty() && (char_style.is_some() || para_style.is_some() || !over.is_empty());let targets = scope(s, p)?.targets;s.edit(|d, sel| {+ let styles = d.styles.clone();let mut count = 0usize;- for (sid, r) in targets {- let Some(story) = d.story(sid) else { continue };+ for (sid, cell, r) in targets {+ let Some(story) = d.text_story(sid, cell) else { continue };// Collect matches first (positions refer to the original text), apply back to front.let text = story.text.get(r.clone()).ok_or_else(|| bad("find", "invalid selection bounds or UTF-8 boundary"))?;- let edits: Vec<(std::ops::Range<usize>, String)> = if grep {- re.replacements(text, &rep, first_only)- } else {- re.find_all(text, if first_only { 1 } else { usize::MAX }).map(|ms| ms.into_iter().map(|m| (m, rep.clone())).collect())- }- .map_err(|e| bad("find.change", e))?- .into_iter()- .map(|(m, out)| (r.start + m.start..r.start + m.end, out))- .collect();+ let edits: Vec<(std::ops::Range<usize>, String)> = re- .replacements(d, sid, &r, text, &rep, grep, first_only)?++ .replacements(d, sid, cell, &r, text, &rep, grep, first_only)?+ .into_iter()+ .map(|(m, out)| (r.start + m.start..r.start + m.end, out))+ .collect();- let Some(st) = d.story_mut(sid) else { continue };+ let Some(st) = d.text_story_mut(sid, cell) else { continue };for (range, new) in edits.iter().rev() {- st.replace(range.clone(), new);- if !over.is_empty() && !new.is_empty() {- st.format_chars(range.start..range.start + new.len(), |f| f.over.merge(&over));+ let changed = if format_only {+ range.clone()+ } else {+ st.replace(range.clone(), new);+ range.start..range.start + new.len()+ };+ if !changed.is_empty() {+ if let Some(n) = ¶_style {+ super::style::apply_para_style(st, &styles, n, &changed, false);+ }+ if let Some(n) = &char_style {+ super::style::apply_char_style(st, &styles, n, &changed);+ }+ if !over.is_empty() {+ st.format_chars(changed, |f| f.over.merge(&over));+ }}count += 1;}@@@ -589,7 -393,7 +613,7 @@@ fn find_next(s: &mut Session, p: &Valueif text.get(from..).is_none() {return Err(bad("find", "invalid UTF-8 search cursor"));}- if let Some(m) = re.find_at(&st.doc, sid, &r, text, from)? {- if let Some(m) = re.find_at(text, from).map_err(|e| bad("find.next", e))? {++ if let Some(m) = re.find_at(&st.doc, sid, cell, &r, text, from)? {let (a, b) = (r.start + m.start, r.start + m.end);// Match find_iter/captures_iter: suppress an empty match adjacent to the// previous match's end, but keep adjacent nonempty matches.