Skip to main content

token_at_cursor

Function token_at_cursor 

Source
pub fn token_at_cursor<'a>(
    spans: impl Iterator<Item = &'a Range<usize>> + Clone,
    source: &str,
    offset: usize,
) -> Option<Range<usize>>
Expand description

The token a cursor at byte offset belongs to, given the tokens’ spans.

A cursor sits between characters, so for ex:fo|; it belongs to ex:fo (the word being typed), and for |ex:name to ex:name. In order:

  1. a word token that contains the cursor,
  2. a word token that ends at the cursor,
  3. a punctuation token that contains the cursor or ends at it,
  4. none: the cursor is in whitespace.