harper_core/expr/
anchor_start.rs

1use crate::{Token, TokenStringExt};
2
3use super::Step;
4
5/// A [`Step`] which will match only if the cursor is over the first word-like of a token stream.
6/// It will return that token.
7pub struct AnchorStart;
8
9impl Step for AnchorStart {
10    fn step(&self, tokens: &[Token], cursor: usize, _source: &[char]) -> Option<isize> {
11        if tokens.iter_word_like_indices().next() == Some(cursor) {
12            Some(0)
13        } else {
14            None
15        }
16    }
17}
18
19#[cfg(test)]
20mod tests {
21    use crate::expr::ExprExt;
22    use crate::{Document, Span};
23
24    use super::AnchorStart;
25
26    #[test]
27    fn matches_first_word() {
28        let document = Document::new_markdown_default_curated("This is a test.");
29        let matches: Vec<_> = AnchorStart.iter_matches_in_doc(&document).collect();
30
31        assert_eq!(matches, vec![Span::new(0, 0)])
32    }
33
34    #[test]
35    fn does_not_match_empty() {
36        let document = Document::new_markdown_default_curated("");
37        let matches: Vec<_> = AnchorStart.iter_matches_in_doc(&document).collect();
38
39        assert_eq!(matches, vec![])
40    }
41}