Retain an earlier command checkpoint when an edit skips its line boundary

This commit is contained in:
gloamxun committed 2026-12-02 12:07:31 +08:00
1 parent 0cf639b8e4
commit 43025e3f72
1 file changed
+55 -3
+55 -3
View File
@@ -164,6 +164,7 @@ struct CaptureSchedule {
next_page: usize, next_page: usize,
fixed_frontier: Option<usize>, fixed_frontier: Option<usize>,
pending_edit_frontier: Option<usize>, pending_edit_frontier: Option<usize>,
pending_edit_fallback: Option<usize>,
pending_line_end: Option<(usize, CaptureSlots, usize)>, pending_line_end: Option<(usize, CaptureSlots, usize)>,
settled_page: usize, settled_page: usize,
} }
@@ -234,6 +235,7 @@ impl CaptureSchedule {
next_page: 1, next_page: 1,
fixed_frontier, fixed_frontier,
pending_edit_frontier: edit_frontier, pending_edit_frontier: edit_frontier,
pending_edit_fallback: None,
pending_line_end: None, pending_line_end: None,
settled_page: 0, settled_page: 0,
} }
@@ -269,10 +271,18 @@ impl CaptureSchedule {
} }
fn take_edit_frontier(&mut self, frontier: usize, ordinary_capture: bool) -> bool { fn take_edit_frontier(&mut self, frontier: usize, ordinary_capture: bool) -> bool {
let edit = self let exact = self
.pending_edit_frontier .pending_edit_frontier
.take_if(|pending| *pending == frontier) .take_if(|pending| *pending == frontier)
.is_some(); .is_some();
let edit = if exact {
self.pending_edit_fallback = None;
true
} else {
self.pending_edit_fallback
.take_if(|pending| *pending == frontier)
.is_some()
};
if edit && !ordinary_capture { if edit && !ordinary_capture {
// Move a later byte-interval capture to the active editing line. // Move a later byte-interval capture to the active editing line.
// Keep the earlier prefix and all shipout capture opportunities. // Keep the earlier prefix and all shipout capture opportunities.
@@ -1082,8 +1092,17 @@ impl SelectedCheckpoint {
.project .project
.file(&checkpoint.guard.entry) .file(&checkpoint.guard.entry)
.expect("a checkpoint records its source"); .expect("a checkpoint records its source");
let edit = changed_line_start(&previous.data, &source.data).or(self.set.edit_frontier); let changed = changed_line_start(&previous.data, &source.data);
let mut schedule = CaptureSchedule::new(source.data.len(), None, edit); let mut schedule = CaptureSchedule::new(
source.data.len(),
None,
changed.or(self.set.edit_frontier),
);
// Comment-only lines may be consumed without an observation, and a
// paragraph can cross several physical lines in one command. Keep a
// nearby earlier anchor when the exact edit-line start is not visited.
schedule.pending_edit_fallback =
changed.and_then(|line| preceding_command_frontier(&source.data, line));
let mut nested_schedule = NestedSchedule::default(); let mut nested_schedule = NestedSchedule::default();
let entry_frontier = checkpoint.entry.entry_frontier(); let entry_frontier = checkpoint.entry.entry_frontier();
let current_nested = checkpoint.guard.nested_source.as_ref() let current_nested = checkpoint.guard.nested_source.as_ref()
@@ -1386,6 +1405,25 @@ fn changed_line_start(previous: &[u8], current: &[u8]) -> Option<usize> {
) )
} }
fn preceding_command_frontier(bytes: &[u8], edit: usize) -> Option<usize> {
let mut frontier = edit;
while frontier > 0 {
let prefix = &bytes[..frontier];
// Each frontier is a physical line start. Treat CRLF as one ending.
let ending = if prefix.ends_with(b"\r\n") { 2 } else { 1 };
let line = &prefix[..frontier - ending];
let start = line.iter().rposition(|byte| matches!(*byte, b'\r' | b'\n'))
.map_or(0, |offset| offset + 1);
// This is a scheduling hint. The actual restart still goes through
// the ordinary scanner, host and immutable-prefix checks.
if line[start..].trim_ascii_start().first() == Some(&b'\\') {
return (frontier < edit).then_some(frontier);
}
frontier = start;
}
None
}
struct NestedSourceGuard { struct NestedSourceGuard {
path: PathBuf, path: PathBuf,
observed_size: Option<u64>, observed_size: Option<u64>,
@@ -2526,6 +2564,20 @@ fn edit_only_entry_checkpoint_advances_to_its_line_end() {
assert!(schedule.select_source(source(290, true), 0).is_none()); assert!(schedule.select_source(source(290, true), 0).is_none());
} }
#[test]
fn edit_checkpoint_keeps_a_command_anchor_when_the_exact_line_is_skipped() {
let source = b"\\section{Title}\r\nParagraph beginning\r\nsecond line\r\nchanged text\r\n\\end";
let command = b"\\section{Title}\r\n".len();
let edit = source.windows(b"changed text".len())
.position(|bytes| bytes == b"changed text").unwrap();
assert_eq!(preceding_command_frontier(source, edit), Some(command));
let mut schedule = CaptureSchedule::new(source.len(), None, Some(edit));
schedule.pending_edit_fallback = preceding_command_frontier(source, edit);
assert!(schedule.select(command, 0).unwrap().edit);
assert!(schedule.select(edit, 0).unwrap().edit);
assert_eq!(preceding_command_frontier(source, command), None);
}
#[test] #[test]
fn automatic_checkpoints_refresh_the_previous_edit_line_after_a_child_edit() { fn automatic_checkpoints_refresh_the_previous_edit_line_after_a_child_edit() {
let mut schedule = CaptureSchedule::new(400, None, Some(290)); let mut schedule = CaptureSchedule::new(400, None, Some(290));