Skip to main content

syster/base/
position.rs

1/// Position tracking for AST nodes
2///
3/// Stores the source location (line/column) of AST nodes for LSP features
4/// like hover, go-to-definition, and error reporting.
5/// A span representing a range in source code (0-indexed for LSP compatibility)
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub struct Span {
8    pub start: Position,
9    pub end: Position,
10}
11
12/// A position in source code (0-indexed)
13#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14pub struct Position {
15    pub line: usize,
16    pub column: usize,
17}
18
19impl Span {
20    pub fn new(start: Position, end: Position) -> Self {
21        Self { start, end }
22    }
23
24    /// Create a span from line/column coordinates
25    pub fn from_coords(
26        start_line: usize,
27        start_col: usize,
28        end_line: usize,
29        end_col: usize,
30    ) -> Self {
31        Self {
32            start: Position::new(start_line, start_col),
33            end: Position::new(end_line, end_col),
34        }
35    }
36
37    /// Check if a position falls within this span
38    pub fn contains(&self, position: Position) -> bool {
39        if position.line < self.start.line || position.line > self.end.line {
40            return false;
41        }
42        if position.line == self.start.line && position.column < self.start.column {
43            return false;
44        }
45        if position.line == self.end.line && position.column > self.end.column {
46            return false;
47        }
48        true
49    }
50}
51
52impl Position {
53    pub fn new(line: usize, column: usize) -> Self {
54        Self { line, column }
55    }
56}