Skip to main content

kcl_error/
source_range.rs

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