genco_macros/
cursor.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
use proc_macro2::Span;

use crate::fake::LineColumn;

#[derive(Clone, Copy, Debug)]
pub(crate) struct Cursor {
    // Span to use for diagnostics associated with the cursor.
    pub(crate) span: Span,
    // The start of the cursor.
    pub(crate) start: LineColumn,
    // The end of the cursor.
    pub(crate) end: LineColumn,
}

impl Cursor {
    /// Construt a cursor.
    pub(crate) fn new(span: Span, start: LineColumn, end: LineColumn) -> Cursor {
        Self { span, start, end }
    }

    /// Calculate the start character for the cursor.
    pub(crate) fn first_character(self) -> Self {
        Cursor {
            span: self.span,
            start: self.start,
            end: LineColumn {
                line: self.start.line,
                column: self.start.column + 1,
            },
        }
    }

    /// Calculate the end character for the cursor.
    pub(crate) fn last_character(self) -> Self {
        Cursor {
            span: self.span,
            start: LineColumn {
                line: self.end.line,
                column: self.end.column.saturating_sub(1),
            },
            end: self.end,
        }
    }
}