kcl_error/
source_range.rs

1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4
5/// Identifier of a source file.  Uses a u32 to keep the size small.
6#[derive(Debug, Default, Ord, PartialOrd, Eq, PartialEq, Clone, Copy, Hash, Deserialize, Serialize, ts_rs::TS)]
7#[ts(export)]
8pub struct ModuleId(u32);
9
10impl ModuleId {
11    pub fn from_usize(id: usize) -> Self {
12        Self(u32::try_from(id).expect("module ID should fit in a u32"))
13    }
14
15    pub fn as_usize(&self) -> usize {
16        usize::try_from(self.0).expect("module ID should fit in a usize")
17    }
18
19    /// Top-level file is the one being executed.
20    /// Represented by module ID of 0, i.e. the default value.
21    pub fn is_top_level(&self) -> bool {
22        *self == Self::default()
23    }
24}
25
26impl std::fmt::Display for ModuleId {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        write!(f, "{}", self.0)
29    }
30}
31
32/// The first two items are the start and end points (byte offsets from the start of the file).
33/// The third item is whether the source range belongs to the 'main' file, i.e., the file currently
34/// being rendered/displayed in the editor.
35//
36// Don't use a doc comment for the below since the above goes in the website docs.
37// @see isTopLevelModule() in wasm.ts.
38// TODO we need to handle modules better in the frontend.
39#[derive(Debug, Default, Deserialize, Serialize, PartialEq, Copy, Clone, ts_rs::TS, Hash, Eq)]
40#[ts(export, type = "[number, number, number]")]
41pub struct SourceRange([usize; 3]);
42
43impl From<[usize; 3]> for SourceRange {
44    fn from(value: [usize; 3]) -> Self {
45        Self(value)
46    }
47}
48
49impl Ord for SourceRange {
50    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
51        // Sort by module id first, then by start and end.
52        let module_id_cmp = self.module_id().cmp(&other.module_id());
53        if module_id_cmp != std::cmp::Ordering::Equal {
54            return module_id_cmp;
55        }
56        let start_cmp = self.start().cmp(&other.start());
57        if start_cmp != std::cmp::Ordering::Equal {
58            return start_cmp;
59        }
60        self.end().cmp(&other.end())
61    }
62}
63
64impl PartialOrd for SourceRange {
65    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
66        Some(self.cmp(other))
67    }
68}
69
70impl From<&SourceRange> for miette::SourceSpan {
71    fn from(source_range: &SourceRange) -> Self {
72        let length = source_range.end() - source_range.start();
73        let start = miette::SourceOffset::from(source_range.start());
74        Self::new(start, length)
75    }
76}
77
78impl From<SourceRange> for miette::SourceSpan {
79    fn from(source_range: SourceRange) -> Self {
80        Self::from(&source_range)
81    }
82}
83
84impl SourceRange {
85    /// Create a new source range.
86    pub fn new(start: usize, end: usize, module_id: ModuleId) -> Self {
87        Self([start, end, module_id.as_usize()])
88    }
89
90    /// A source range that doesn't correspond to any source code.
91    pub fn synthetic() -> Self {
92        Self::default()
93    }
94
95    pub fn merge(mut ranges: impl Iterator<Item = SourceRange>) -> Self {
96        let mut result = ranges.next().unwrap_or_default();
97
98        for r in ranges {
99            debug_assert!(r.0[2] == result.0[2], "Merging source ranges from different files");
100            if r.0[0] < result.0[0] {
101                result.0[0] = r.0[0]
102            }
103            if r.0[1] > result.0[1] {
104                result.0[1] = r.0[1];
105            }
106        }
107
108        result
109    }
110
111    /// True if this is a source range that doesn't correspond to any source
112    /// code.
113    pub fn is_synthetic(&self) -> bool {
114        self.start() == 0 && self.end() == 0
115    }
116
117    /// Get the start of the range.
118    pub fn start(&self) -> usize {
119        self.0[0]
120    }
121
122    /// Get the start of the range as a zero-length SourceRange, effectively collapse `self` to it's
123    /// start.
124    pub fn start_as_range(&self) -> Self {
125        Self([self.0[0], self.0[0], self.0[2]])
126    }
127
128    /// Get the end of the range.
129    pub fn end(&self) -> usize {
130        self.0[1]
131    }
132
133    /// Get the module ID of the range.
134    pub fn module_id(&self) -> ModuleId {
135        ModuleId::from_usize(self.0[2])
136    }
137
138    /// True if this source range is from the top-level module.
139    pub fn is_top_level_module(&self) -> bool {
140        self.module_id().is_top_level()
141    }
142
143    /// Check if the range contains a position.
144    pub fn contains(&self, pos: usize) -> bool {
145        pos >= self.start() && pos <= self.end()
146    }
147
148    /// Check if the range contains another range.  Modules must match.
149    pub fn contains_range(&self, other: &Self) -> bool {
150        self.module_id() == other.module_id() && self.start() <= other.start() && self.end() >= other.end()
151    }
152}