Skip to main content

find_surround_pair

Function find_surround_pair 

Source
pub fn find_surround_pair(
    buffer: &Buffer,
    cursor: Position,
    target: char,
) -> Option<(usize, usize)>
Expand description

Find the nearest enclosing pair matching target around the cursor position.

Returns Some((opener_byte, closer_byte)) (byte offsets in the buffer) or None if no matching pair encloses the cursor.

Algorithm: scan backward from cursor tracking closers, scan forward tracking openers. Stacks handle nesting correctly.

SU.3f: a delimiter sitting under the cursor counts as part of the pair it belongs to, matching vim — ds" works with the caret on either quote. The two scans are half-open around the cursor (backward is byte < cursor_byte, forward is byte >= cursor_byte), so a closer under the cursor was already found by the forward scan while an opener under it was skipped by both. Nudging the effective cursor one character right when it sits on an opener puts it just inside its own pair, which is the position the scans already handle.