DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Change Format: empty matches get the paragraph style; style helpers bounded
moppeeerc6a0e1e8137e
diff --git a/crates/doc/src/story.rs b/crates/doc/src/story.rsindex e3a0e39ee..c21d54c4b 100644--- a/crates/doc/src/story.rs+++ b/crates/doc/src/story.rs@@ -244,6 +244,24 @@ impl Story {}/// Index of the paragraph containing byte `pos`.+ /// The paragraphs `r` touches (at least the one at `r.start`), as (index, byte range without+ /// its return), found from `r` itself rather than by listing every paragraph of the story.+ pub fn paras_touching(&self, r: std::ops::Range<usize>) -> Vec<(usize, std::ops::Range<usize>)> {+ let start = floor_char_boundary(&self.text, r.start.min(self.text.len()));+ let mut pi = self.para_at(start);+ let mut a = self.text[..start].rfind('\n').map_or(0, |i| i + 1);+ let mut out = Vec::new();+ loop {+ let b = self.text[a..].find('\n').map_or(self.text.len(), |i| a + i);+ out.push((pi, a..b));+ // The next paragraph starts after this one's return: is it still inside `r`?+ if b >= self.text.len() || b + 1 >= r.end {+ return out;+ }+ (a, pi) = (b + 1, pi + 1);+ }+ }+pub fn para_at(&self, pos: usize) -> usize {self.text[..floor_char_boundary(&self.text, pos)].bytes().filter(|b| *b == b'\n').count()}@@ -596,6 +614,27 @@ mod tests {Story::with_text(StoryId(1), t, ParaFormat::default())}+ /// `paras_touching` gives the paragraphs a range reaches into (returns included), as listing+ /// every paragraph would, for every range of a story with empty and multi-byte paragraphs.+ #[test]+ fn paras_touching_matches_the_full_listing() {+ let s = st("ab++çé+last");+ let all = s.para_ranges();+ for a in 0..=s.len() {+ for b in a..=s.len() {+ if !s.text.is_char_boundary(a) || !s.text.is_char_boundary(b) {+ continue;+ }+ let want: Vec<(usize, std::ops::Range<usize>)> =+ all.iter().cloned().enumerate().filter(|(i, p)| *i == s.para_at(a) || (a < b && p.start < b && a <= p.end)).collect();+ assert_eq!(s.paras_touching(a..b), want, "{a}..{b}");+ }+ }+ }+#[test]fn paragraphs_track_newlines() {let mut s = st("one\ntwo");diff --git a/crates/engine/src/cmd/find.rs b/crates/engine/src/cmd/find.rsindex b87a7b226..9e6f984b4 100644--- a/crates/engine/src/cmd/find.rs+++ b/crates/engine/src/cmd/find.rs@@ -372,10 +372,12 @@ fn change(s: &mut Session, p: &Value) -> Result<Value> {st.replace(range.clone(), new);range.start..range.start + new.len()};+ // A paragraph style goes to the match's paragraphs, an empty match's (`^`) too;+ // character formatting needs characters.+ if let Some(n) = ¶_style {+ super::style::apply_para_style(st, &styles, n, &changed, false);+ }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);}@@ -485,6 +487,34 @@ mod tests {assert!(s.doc().unwrap().selection.text.is_some());}+ /// A GREP match that is empty (`^`, the start of each paragraph) gets the paragraph style:+ /// only character formatting needs characters.+ #[test]+ fn change_format_styles_paragraphs_of_empty_matches() {+ let mut s = Session::new();+ s.execute("file.new", &json!({})).unwrap();+ let r = s+ .execute(+ "frame.create",+ &json!({"rect": [36, 36, 300, 300], "content": "text", "text": "One+Two+Three"}),+ )+ .unwrap();+ let sid = designcraft_doc::StoryId(r["story"].as_u64().unwrap());+ s.execute("style.paragraph.create", &json!({"name": "Method"})).unwrap();+ let r = s.execute("find.change", &json!({"find": "^", "change": "", "grep": true, "paraStyle": "Method"})).unwrap();+ assert_eq!(r["count"], 3);+ let story = s.doc().unwrap().doc.story(sid).unwrap().clone();+ assert!(story.paras.iter().all(|p| p.style == "Method"), "{:?}", story.paras.iter().map(|p| &p.style).collect::<Vec<_>>());+ assert_eq!(+ story.text,+ "One+Two+Three"+ );+ }+#[test]fn change_format_styles_the_found_text_and_keeps_it() {// The course's chapter 22: every lowercase "eggs" gets the character style "eggs", withdiff --git a/crates/engine/src/cmd/style.rs b/crates/engine/src/cmd/style.rsindex 9ae967613..27414306e 100644--- a/crates/engine/src/cmd/style.rs+++ b/crates/engine/src/cmd/style.rs@@ -790,7 +790,7 @@ pub(super) fn apply_para_style(st: &mut designcraft_doc::Story, styles: &Styles,f.chars.retain_differences(&style_chars);}});- for (pi, range) in st.para_ranges().into_iter().enumerate().take(last.saturating_add(1)).skip(first) {+ for (pi, range) in st.paras_touching(r.clone()).into_iter().filter(|(pi, _)| (first..=last).contains(pi)) {let Some(pf) = st.paras.get(pi) else { continue };let (_, base) = styles.resolve_para(pf);let end = range.end.saturating_add(1).min(st.len());@@ -827,7 +827,7 @@ fn apply_char(s: &mut Session, p: &Value) -> Result<Value> {/// Character style `name` applied to `r` in `st`: the text's overrides that the style (over its/// paragraph) now gives go.pub(super) fn apply_char_style(st: &mut designcraft_doc::Story, styles: &Styles, name: &str, r: &std::ops::Range<usize>) {- for (pi, range) in st.para_ranges().into_iter().enumerate() {+ for (pi, range) in st.paras_touching(r.clone()) {let range = range.start.max(r.start)..range.end.saturating_add(1).min(st.len()).min(r.end);if range.is_empty() {continue;