pub fn span_to_whole_lines(
start_line: u32,
start_byte: u32,
end_line: u32,
end_byte: u32,
) -> (u32, u32)Expand description
The inclusive whole lines an operator’s byte span covers, given its start
and end (line, byte) in either order.
A span ending at byte 0 of a later line is half-open: nothing on that line
is covered, so the last covered line is the one before. That’s the shape a
forward exclusive motion leaves (}, G), and vim agrees:
:h exclusive-linewise, “the end is moved to the end of the previous line”.
Known gap: a BACKWARD exclusive motion (k from column 0) also ends at byte
0 of the cursor’s own line, and from the span alone that can’t be told apart,
so the cursor’s line is dropped. Lattice has no linewise operator targets
yet (dk is charwise too); threading the cursor through is the fix when it
matters.
Shared by the narrow operator (zn) and the fold operator (zf), which is
why it lives here rather than in either.
Lines are 0-based; the returned (first, last) is inclusive and ordered.
§Examples
use lattice_grammar::range::span_to_whole_lines;
// Ends mid-line on line 3: lines 0..=3 are covered.
assert_eq!(span_to_whole_lines(0, 0, 3, 5), (0, 3));
// Ends at byte 0 of line 3 (a forward exclusive motion like `}`):
// line 3 is not covered.
assert_eq!(span_to_whole_lines(0, 0, 3, 0), (0, 2));
// Reversed input is ordered.
assert_eq!(span_to_whole_lines(4, 2, 1, 7), (1, 4));