From 43025e3f72ffaf3b785904bb24e024fa737ea21c Mon Sep 17 00:00:00 2001 From: GloamXun Date: Wed, 2 Dec 2026 12:07:31 +0800 Subject: [PATCH] Retain an earlier command checkpoint when an edit skips its line boundary --- src/native_checkpoint_guard.rs | 58 ++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/src/native_checkpoint_guard.rs b/src/native_checkpoint_guard.rs index bce00d4..5582a85 100644 --- a/src/native_checkpoint_guard.rs +++ b/src/native_checkpoint_guard.rs @@ -164,6 +164,7 @@ struct CaptureSchedule { next_page: usize, fixed_frontier: Option, pending_edit_frontier: Option, + pending_edit_fallback: Option, pending_line_end: Option<(usize, CaptureSlots, usize)>, settled_page: usize, } @@ -234,6 +235,7 @@ impl CaptureSchedule { next_page: 1, fixed_frontier, pending_edit_frontier: edit_frontier, + pending_edit_fallback: None, pending_line_end: None, settled_page: 0, } @@ -269,10 +271,18 @@ impl CaptureSchedule { } fn take_edit_frontier(&mut self, frontier: usize, ordinary_capture: bool) -> bool { - let edit = self + let exact = self .pending_edit_frontier .take_if(|pending| *pending == frontier) .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 { // Move a later byte-interval capture to the active editing line. // Keep the earlier prefix and all shipout capture opportunities. @@ -1082,8 +1092,17 @@ impl SelectedCheckpoint { .project .file(&checkpoint.guard.entry) .expect("a checkpoint records its source"); - let edit = changed_line_start(&previous.data, &source.data).or(self.set.edit_frontier); - let mut schedule = CaptureSchedule::new(source.data.len(), None, edit); + let changed = changed_line_start(&previous.data, &source.data); + 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 entry_frontier = checkpoint.entry.entry_frontier(); let current_nested = checkpoint.guard.nested_source.as_ref() @@ -1386,6 +1405,25 @@ fn changed_line_start(previous: &[u8], current: &[u8]) -> Option { ) } +fn preceding_command_frontier(bytes: &[u8], edit: usize) -> Option { + 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 { path: PathBuf, observed_size: Option, @@ -2526,6 +2564,20 @@ fn edit_only_entry_checkpoint_advances_to_its_line_end() { 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] fn automatic_checkpoints_refresh_the_previous_edit_line_after_a_child_edit() { let mut schedule = CaptureSchedule::new(400, None, Some(290));