Replay multiple paragraph edits in route-D previews
Apply same-count paragraph edits sequentially from the earliest changed paragraph, reuse the earlier page checkpoints, and report the union of changed pages. Adds a multi-page regression.
This commit is contained in:
1 parent
506904bbdc
commit
ceb31c4612
3 files changed
+77
-25
No files matched your search
+71
-20
@@ -5,7 +5,7 @@
|
||||
//! core. The v1 preview remains the default and the fallback.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{BTreeSet, HashMap};
|
||||
use std::fmt::Write as _;
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
|
||||
@@ -40,9 +40,9 @@ fn latex_body(source: &str) -> &str {
|
||||
}
|
||||
|
||||
/// Project-scoped route-D preview. Reuses the previous engine when the
|
||||
/// paragraph list is unchanged and applies exactly one paragraph edit through
|
||||
/// `Engine::edit`; multi-paragraph or structural edits fall back to a full
|
||||
/// layout. The v1 project preview remains the default.
|
||||
/// paragraph list is unchanged and applies each changed paragraph through
|
||||
/// `Engine::edit`, starting from the earliest changed paragraph. Structural
|
||||
/// edits fall back to a full layout. The v1 project preview remains the default.
|
||||
pub fn project_preview_json(project_id: &str, source: &str) -> Result<String, String> {
|
||||
let paragraphs = paragraphs(latex_body(source))?;
|
||||
let texts: Vec<String> = paragraphs.iter().map(|entry| entry.text.clone()).collect();
|
||||
@@ -66,26 +66,32 @@ pub fn project_preview_json(project_id: &str, source: &str) -> Result<String, St
|
||||
}
|
||||
Some(mut session)
|
||||
if session.paragraphs.len() == texts.len()
|
||||
&& session
|
||||
.paragraphs
|
||||
.iter()
|
||||
.zip(&texts)
|
||||
.filter(|(a, b)| a != b)
|
||||
.count()
|
||||
== 1 =>
|
||||
&& session.paragraphs.iter().zip(&texts).any(|(a, b)| a != b) =>
|
||||
{
|
||||
let index = session
|
||||
let changed_indices: Vec<usize> = session
|
||||
.paragraphs
|
||||
.iter()
|
||||
.zip(&texts)
|
||||
.position(|(a, b)| a != b)
|
||||
.expect("one changed paragraph");
|
||||
session
|
||||
.engine
|
||||
.edit(&document, index as u64, texts[index].clone());
|
||||
let changed = session.engine.changed_pages().to_vec();
|
||||
let recomputed = session.engine.recomputed_pages();
|
||||
(session.engine, changed, recomputed)
|
||||
.enumerate()
|
||||
.filter(|(_, (a, b))| a != b)
|
||||
.map(|(index, _)| index)
|
||||
.collect();
|
||||
let mut changed = BTreeSet::new();
|
||||
let mut recomputed = 0;
|
||||
for (edit, &index) in changed_indices.iter().enumerate() {
|
||||
session
|
||||
.engine
|
||||
.edit(&document, index as u64, texts[index].clone());
|
||||
if edit == 0 {
|
||||
recomputed = session.engine.recomputed_pages();
|
||||
}
|
||||
changed.extend(session.engine.changed_pages().iter().copied());
|
||||
}
|
||||
(
|
||||
session.engine,
|
||||
changed.into_iter().collect::<Vec<_>>(),
|
||||
recomputed,
|
||||
)
|
||||
}
|
||||
_ => {
|
||||
let engine = Engine::layout(&document);
|
||||
@@ -412,3 +418,48 @@ fn escape_json(value: &str) -> String {
|
||||
}
|
||||
escaped
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn multi_page_source() -> String {
|
||||
let mut source = String::from("\\documentclass{article}\n\\begin{document}\n\n");
|
||||
for index in 0..36 {
|
||||
source.push_str(&format!("Paragraph {index:02} "));
|
||||
for _ in 0..4 {
|
||||
source.push_str(
|
||||
"lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor. ",
|
||||
);
|
||||
}
|
||||
source.push_str("\n\n");
|
||||
}
|
||||
source.push_str("\\end{document}\n");
|
||||
source
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multi_paragraph_edits_reuse_earlier_pages() {
|
||||
let source = multi_page_source();
|
||||
let first = project_preview_json("route-d-multi-edit", &source).expect("first preview");
|
||||
let first: serde_json::Value = serde_json::from_str(&first).expect("first JSON");
|
||||
let page_count = first["pageCount"].as_u64().expect("page count");
|
||||
assert!(
|
||||
page_count >= 2,
|
||||
"expected a multi-page preview, got {page_count}"
|
||||
);
|
||||
|
||||
let edited = source
|
||||
.replace("Paragraph 30 ", "Paragraph 30 changed ")
|
||||
.replace("Paragraph 33 ", "Paragraph 33 changed ");
|
||||
let second = project_preview_json("route-d-multi-edit", &edited).expect("edited preview");
|
||||
let second: serde_json::Value = serde_json::from_str(&second).expect("edited JSON");
|
||||
assert!(
|
||||
second["changedPages"]
|
||||
.as_array()
|
||||
.is_some_and(|pages| !pages.is_empty())
|
||||
);
|
||||
let recomputed = second["recomputedPages"].as_u64().expect("recomputed");
|
||||
assert!(recomputed < page_count, "changed pages should be a subset");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user