Retain an earlier command checkpoint when an edit skips its line boundary
This commit is contained in:
1 parent
0cf639b8e4
commit
43025e3f72
1 file changed
+55
-3
@@ -164,6 +164,7 @@ struct CaptureSchedule {
|
||||
next_page: usize,
|
||||
fixed_frontier: Option<usize>,
|
||||
pending_edit_frontier: Option<usize>,
|
||||
pending_edit_fallback: Option<usize>,
|
||||
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<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 {
|
||||
path: PathBuf,
|
||||
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());
|
||||
}
|
||||
|
||||
#[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));
|
||||
|
||||
Reference in new issue
Block a user