summaryrefslogtreecommitdiff
path: root/support/texlab/crates/texlab/src/features/completion/matcher.rs
blob: a2b1a45e6adb53ecc2a83b9897eeef2c60216334 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
pub trait Matcher {
    fn score(&mut self, choice: &str, pattern: &str) -> Option<i32>;
}

impl<T: fuzzy_matcher::FuzzyMatcher> Matcher for T {
    fn score(&mut self, choice: &str, pattern: &str) -> Option<i32> {
        fuzzy_matcher::FuzzyMatcher::fuzzy_match(self, choice, pattern)
    }
}

#[derive(Debug)]
pub struct Prefix;

impl Matcher for Prefix {
    fn score(&mut self, choice: &str, pattern: &str) -> Option<i32> {
        if choice.starts_with(pattern) {
            Some(-(choice.len() as i32))
        } else {
            None
        }
    }
}

#[derive(Debug)]
pub struct PrefixIgnoreCase;

impl Matcher for PrefixIgnoreCase {
    fn score(&mut self, choice: &str, pattern: &str) -> Option<i32> {
        if pattern.len() > choice.len() {
            return None;
        }

        let mut cs = choice.chars();
        for p in pattern.chars() {
            if !cs.next().unwrap().eq_ignore_ascii_case(&p) {
                return None;
            }
        }

        return Some(-(choice.len() as i32));
    }
}