Reuse settled TeX suffixes after physical line edits

This commit is contained in:
gloamxun committed 2026-12-01 23:49:39 +08:00
1 parent 5b86a7ac7e
commit ac5f691a48
15 files changed
+518 -30

No files matched your search

+184
View File
@@ -1917,6 +1917,190 @@ fn settled_suffix_relocates_source_spans_after_insertions_and_deletions() {
fs::remove_dir_all(cwd).unwrap();
}
#[test]
fn whole_line_edits_reuse_the_settled_suffix_and_relocate_provenance() {
check_whole_line_suffix_edits("whole-line-safe-suffix", false);
}
#[test]
fn whole_line_edits_reexecute_a_delayed_input_line_read_on_the_same_source() {
check_whole_line_suffix_edits("whole-line-delayed-inputlineno", true);
}
fn check_whole_line_suffix_edits(label: &str, observe_delayed_line: bool) {
let cwd = directory(label);
let entry = cwd.join("main.tex");
let delayed = if observe_delayed_line {
r"\write16{Suffix line \number\inputlineno}"
} else {
""
};
let body = |inserted| {
format!(
r"
\tracingstats=2
\count42=\inputlineno\relax
\shipout\hbox{{Cover}}\relax% Preserve the comment catcode read before either edit.
{inserted}\shipout\hbox{{Checkpoint}}\relax
\gdef\saved{{Suffix glyphs}}
\shipout\hbox{{\saved}}\relax
\setbox5=\hbox{{Final{delayed}}}
\endinput\shipout\box5\relax\end
"
)
};
let final_line = |body: &str| {
body.lines()
.position(|line| line.starts_with(r"\endinput"))
.unwrap()
+ 1
};
let comment = "% Inserted physical line\n";
for (seed, edited) in [("", comment), (comment, "")] {
// Give each direction one donor, so the deletion cannot reuse a
// completed revision that already contained the shorter source.
let original = body(seed);
let project = ProjectSnapshot::new(atomic_definition(&cwd, 1, &original)).unwrap();
let mut input = request(&cwd).observe_generated_initial();
input.world = project.configuration();
let session = NativeCompiler::default().with_automatic_entry_checkpoints();
let donor = session
.compile(input.clone().with_project(project.clone()))
.unwrap();
let donor_execution = &donor.execution.execution;
let donor_line = final_line(&original);
assert_eq!(donor_execution.input_position, Some((0, donor_line)));
let changed = body(edited);
let current_line = final_line(&changed);
assert_eq!(
current_line,
if edited.is_empty() {
donor_line - 1
} else {
donor_line + 1
}
);
let source = format!("{HEADER}{changed}");
let prefix_read_end = source.find(r"\shipout\hbox{Cover}").unwrap();
let glyph_offset = source.find("Suffix glyphs").unwrap();
let project = project
.with_file_edits(2, [(entry.clone(), Arc::from(source.into_bytes()))].into())
.unwrap();
let input = input.with_project(project);
let updated = session.compile(input.clone()).unwrap();
let execution = &updated.execution.execution;
let work = &updated.entry_checkpoint_work;
assert!(!work.reused_completion, "{work:?}");
assert!(
work.reused_frontier.is_some_and(|frontier| frontier >= prefix_read_end),
"{work:?}"
);
assert_eq!(execution.input_position, Some((0, current_line)));
assert_eq!(execution.state.count(42), 3);
assert_eq!(updated.pages.len(), 4);
assert!(updated.source_map.pages[2].entries.iter().any(|glyph| {
glyph.source.tokens.iter().any(|origin| origin.span.start == glyph_offset)
}));
if observe_delayed_line {
// The write's tokens were saved in the preceding box. The
// terminal line ends input before shipout, so no later checkpoint
// can legitimately skip work after the delayed observation.
assert_eq!(work.reused_suffix_steps, 0, "{work:?}");
for (execution, line) in [(donor_execution, donor_line), (execution, current_line)] {
let expected = format!("Suffix line {line}\n");
assert!(execution.events.iter().any(|event| {
matches!(event, texrra_tex_core::execution::Event::Write { text, .. } if text == &expected)
}), "{expected:?}");
}
} else {
assert!(work.reused_suffix_steps > 0, "{work:?}");
assert!(updated.shipout_layout_work.reused_pages >= 2, "{work:?}");
}
assert!(
updated.preview_since(Some(donor.revision())).unwrap().pages.is_empty(),
"a physical source line moved without changing any page paint"
);
let log = fs::read(cwd.join("job.log")).unwrap();
let independent = NativeCompiler::default().compile(input).unwrap();
assert_eq!(execution, &independent.execution.execution);
assert_eq!(updated.execution.source_versions, independent.execution.source_versions);
assert_eq!(updated.resources.trace, independent.resources.trace);
assert_eq!(updated.resources.stream_io, independent.resources.stream_io);
assert_eq!(updated.export_diagnostics, independent.export_diagnostics);
assert_eq!(updated.pdf, independent.pdf);
assert_eq!(updated.pages, independent.pages);
assert_eq!(updated.source_map.sources, independent.source_map.sources);
assert_eq!(updated.source_map.pages, independent.source_map.pages);
assert_eq!(
updated.preview_since(None).unwrap().pages,
independent.preview_since(None).unwrap().pages
);
assert_eq!(log, fs::read(cwd.join("job.log")).unwrap());
}
fs::remove_dir_all(cwd).unwrap();
}
#[test]
fn equal_length_line_edits_refresh_suffix_diagnostics_from_saved_prefix_tokens() {
let cwd = directory("equal-length-suffix-line-diagnostic");
let entry = cwd.join("main.tex");
let body = |before, after| {
format!(
"\n\
\\TeXXeTstate=1\\relax\n\
\\shipout\\hbox{{Cover}}\\relax\n\
\\relax{before}\\afterassignment}}\\relax{after}\\relax\n\
\\shipout\\hbox{{Checkpoint}}\\relax\n\
\\endinput\\shipout\\hbox{{\\beginR X\\count0=1\\relax\\end\n"
)
};
// The scheduled right brace keeps its byte offset and closes the last
// box after the assignment. Its source line supplies the LR warning.
let original = body(" ", "\n");
let changed = body("\n", " ");
let scheduled_brace = |body: &str| {
body.find(r"\afterassignment}").unwrap() + r"\afterassignment".len()
};
let warning = |body: &str| {
let line = body[..scheduled_brace(body)].matches('\n').count() + 1;
format!("(1 missing, 0 extra) detected at line {line}\n")
};
assert_eq!(original.len(), changed.len());
assert_eq!(scheduled_brace(&original), scheduled_brace(&changed));
assert_ne!(warning(&original), warning(&changed));
let project = ProjectSnapshot::new(atomic_definition(&cwd, 1, &original)).unwrap();
let mut input = request(&cwd).observe_generated_initial();
input.world = project.configuration();
let session = NativeCompiler::default().with_automatic_entry_checkpoints();
session.compile(input.clone().with_project(project.clone())).unwrap();
let log_file = cwd.join("job.log");
let donor_log = fs::read_to_string(&log_file).unwrap();
assert!(donor_log.contains(&warning(&original)), "{donor_log}");
let source = format!("{HEADER}{changed}");
let cover = source.find(r"\shipout\hbox{Cover}").unwrap();
let project = project
.with_file_edits(2, [(entry, Arc::from(source.into_bytes()))].into())
.unwrap();
let input = input.with_project(project);
let updated = session.compile(input.clone()).unwrap();
let work = &updated.entry_checkpoint_work;
assert!(work.reused_frontier.is_some_and(|frontier| frontier >= cover), "{work:?}");
assert_eq!(work.reused_suffix_steps, 0, "{work:?}");
let updated_log = fs::read_to_string(&log_file).unwrap();
assert!(updated_log.contains(&warning(&changed)), "{updated_log}");
assert!(!updated_log.contains(&warning(&original)), "{updated_log}");
let cold = NativeCompiler::default().compile(input).unwrap();
assert_eq!(updated.execution.execution, cold.execution.execution);
assert_eq!(updated_log, fs::read_to_string(log_file).unwrap());
fs::remove_dir_all(cwd).unwrap();
}
#[test]
fn relocated_suffix_cannot_reuse_layouts_from_a_different_donor() {
let cwd = directory("relocated-suffix-older-donor");