summaryrefslogtreecommitdiff
path: root/support/texlab/src/protocol/range.rs
blob: 774df1e6993d55c921b6926e34188d0dc1587647 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use lsp_types::{Position, Range};

pub trait RangeExt {
    fn new_simple(start_line: u64, start_character: u64, end_line: u64, end_character: u64)
        -> Self;

    fn contains(&self, pos: Position) -> bool;

    fn contains_exclusive(&self, pos: Position) -> bool;
}

impl RangeExt for Range {
    fn new_simple(
        start_line: u64,
        start_character: u64,
        end_line: u64,
        end_character: u64,
    ) -> Self {
        Self {
            start: Position::new(start_line, start_character),
            end: Position::new(end_line, end_character),
        }
    }

    fn contains(&self, pos: Position) -> bool {
        pos >= self.start && pos <= self.end
    }

    fn contains_exclusive(&self, pos: Position) -> bool {
        pos > self.start && pos < self.end
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn contains_inside() {
        let range = Range::new_simple(1, 2, 3, 4);
        assert!(range.contains(Position::new(2, 5)));
    }

    #[test]
    fn contains_begin() {
        let range = Range::new_simple(1, 2, 3, 4);
        assert!(range.contains(range.start));
    }

    #[test]
    fn contains_end() {
        let range = Range::new_simple(1, 2, 3, 4);
        assert!(range.contains(range.end));
    }

    #[test]
    fn contains_outside_left() {
        let range = Range::new_simple(1, 2, 3, 4);
        assert!(!range.contains(Position::new(0, 5)));
    }

    #[test]
    fn contains_outside_right() {
        let range = Range::new_simple(1, 2, 3, 4);
        assert!(!range.contains(Position::new(5, 1)));
    }

    #[test]
    fn contains_exclusive_inside() {
        let range = Range::new_simple(1, 2, 3, 4);
        assert!(range.contains_exclusive(Position::new(2, 5)));
    }

    #[test]
    fn contains_exclusive_begin() {
        let range = Range::new_simple(1, 2, 3, 4);
        assert!(!range.contains_exclusive(range.start));
    }

    #[test]
    fn contains_exclusive_end() {
        let range = Range::new_simple(1, 2, 3, 4);
        assert!(!range.contains_exclusive(range.end));
    }

    #[test]
    fn contains_exclusive_outside_left() {
        let range = Range::new_simple(1, 2, 3, 4);
        assert!(!range.contains_exclusive(Position::new(0, 5)));
    }

    #[test]
    fn contains_exclusive_outside_right() {
        let range = Range::new_simple(1, 2, 3, 4);
        assert!(!range.contains_exclusive(Position::new(5, 1)));
    }
}