grit_pattern_matcher/pattern/
range.rs

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
98
99
100
101
102
103
104
105
106
107
use super::{
    patterns::{Matcher, PatternName},
    resolved_pattern::ResolvedPattern,
    state::State,
};
use crate::context::{ExecContext, QueryContext};
use grit_util::{
    error::{GritPatternError, GritResult},
    AnalysisLogs, UtilRange,
};

#[derive(Debug, Clone)]
pub struct Point {
    line: u32,
    column: Option<u32>,
}

impl Point {
    pub fn new(line: Option<u32>, column: Option<u32>) -> GritResult<Option<Self>> {
        if let Some(line) = line {
            Ok(Some(Self { line, column }))
        } else {
            column
                .map(|_| {
                    Err(GritPatternError::new(
                        "cannot have a point with a column index, but no line",
                    ))
                })
                .unwrap_or(Ok(None))
        }
    }
}

#[derive(Debug, Clone)]
pub struct Range {
    pub start: Option<Point>,
    pub end: Option<Point>,
}

impl From<UtilRange> for Range {
    fn from(util_range: UtilRange) -> Self {
        // TODO: there must be a smarter way
        match util_range {
            UtilRange::Range(range) => Self {
                start: Some(Point {
                    line: range.start.line,
                    column: Some(range.start.column),
                }),
                end: Some(Point {
                    line: range.end.line,
                    column: Some(range.end.column),
                }),
            },
            UtilRange::RangeWithoutByte(range_without_byte) => Self {
                start: Some(Point {
                    line: range_without_byte.start.line,
                    column: Some(range_without_byte.start.column),
                }),
                end: Some(Point {
                    line: range_without_byte.end.line,
                    column: Some(range_without_byte.end.column),
                }),
            },
        }
    }
}

impl<Q: QueryContext> Matcher<Q> for Range {
    fn execute<'a>(
        &'a self,
        binding: &Q::ResolvedPattern<'a>,
        _state: &mut State<'a, Q>,
        context: &'a Q::ExecContext<'a>,
        _logs: &mut AnalysisLogs,
    ) -> GritResult<bool> {
        if let Some(range) = binding.position(context.language()) {
            if let Some(start) = &self.start {
                if start.line > range.start.line {
                    return Ok(false);
                }
                if let Some(column) = start.column {
                    if start.line == range.start.line && column > range.start.column {
                        return Ok(false);
                    }
                }
            }
            if let Some(end) = &self.end {
                if end.line < range.end.line {
                    return Ok(false);
                }
                if let Some(column) = end.column {
                    if end.line == range.end.line && column < range.end.column {
                        return Ok(false);
                    }
                }
            }
            return Ok(true);
        }
        Ok(false)
    }
}

impl PatternName for Range {
    fn name(&self) -> &'static str {
        "RANGE"
    }
}