swamp_script_source_map/
lib.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/script
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5
6use pathdiff::diff_paths;
7use seq_map::SeqMap;
8use std::io::ErrorKind;
9use std::path::{Path, PathBuf};
10use std::{fs, io};
11use tracing::trace;
12
13pub mod prelude;
14
15pub type FileId = u16;
16
17#[derive(Debug)]
18pub struct FileInfo {
19    pub mount_name: String,
20    pub relative_path: PathBuf,
21    pub contents: String,
22    pub line_offsets: Box<[u16]>,
23}
24
25#[derive(Debug)]
26pub struct SourceMap {
27    pub mounts: SeqMap<String, PathBuf>,
28    pub cache: SeqMap<FileId, FileInfo>,
29    pub file_cache: SeqMap<(String, String), FileId>,
30    pub next_file_id: FileId,
31}
32
33#[derive(Debug)]
34pub struct RelativePath(pub String);
35
36impl SourceMap {
37    /// # Errors
38    ///
39    pub fn new(mounts: &SeqMap<String, PathBuf>) -> io::Result<Self> {
40        let mut canonical_mounts = SeqMap::new();
41        for (mount_name, base_path) in mounts {
42            let canon_path = base_path.canonicalize().map_err(|_| {
43                io::Error::new(
44                    io::ErrorKind::InvalidData,
45                    format!("could not canonicalize {base_path:?}"),
46                )
47            })?;
48
49            if !canon_path.is_dir() {
50                return Err(io::Error::new(
51                    ErrorKind::NotFound,
52                    format!("{canon_path:?} is not a directory"),
53                ));
54            }
55            canonical_mounts
56                .insert(mount_name.clone(), canon_path)
57                .map_err(|_| {
58                    io::Error::new(io::ErrorKind::InvalidData, "could not insert mount")
59                })?;
60        }
61        Ok(Self {
62            mounts: canonical_mounts,
63            cache: SeqMap::new(),
64            file_cache: SeqMap::new(),
65            next_file_id: 1,
66        })
67    }
68
69    /// # Errors
70    ///
71    pub fn add_mount(&mut self, name: &str, path: &Path) -> io::Result<()> {
72        if !path.is_dir() {
73            return Err(io::Error::new(
74                ErrorKind::NotFound,
75                format!("{path:?} is not a directory"),
76            ));
77        }
78        self.mounts
79            .insert(name.to_string(), path.to_path_buf())
80            .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "could not insert mount"))
81    }
82
83    #[must_use]
84    pub fn base_path(&self, name: &str) -> &Path {
85        self.mounts.get(&name.to_string()).map_or_else(
86            || {
87                panic!("could not find path {name}");
88            },
89            |found| found,
90        )
91    }
92
93    pub fn read_file(&mut self, path: &Path, mount_name: &str) -> io::Result<(FileId, String)> {
94        let found_base_path = self.base_path(mount_name);
95        let relative_path = diff_paths(path, found_base_path)
96            .unwrap_or_else(|| panic!("could not find relative path {path:?} {found_base_path:?}"));
97
98        let contents = fs::read_to_string(path)?;
99
100        let id = self.next_file_id;
101        self.next_file_id += 1;
102
103        self.add_manual(id, mount_name, &relative_path, &contents);
104
105        Ok((id, contents))
106    }
107
108    pub fn add_to_cache(
109        &mut self,
110        mount_name: &str,
111        relative_path: &Path,
112        contents: &str,
113        file_id: FileId,
114    ) {
115        self.add_manual(file_id, mount_name, relative_path, contents);
116        self.file_cache
117            .insert(
118                (
119                    mount_name.to_string(),
120                    relative_path.to_str().unwrap().to_string(),
121                ),
122                file_id,
123            )
124            .unwrap();
125    }
126
127    pub fn add_manual(
128        &mut self,
129        id: FileId,
130        mount_name: &str,
131        relative_path: &Path,
132        contents: &str,
133    ) {
134        let line_offsets = Self::compute_line_offsets(contents);
135
136        self.cache
137            .insert(
138                id,
139                FileInfo {
140                    mount_name: mount_name.to_string(),
141                    relative_path: relative_path.to_path_buf(),
142                    contents: contents.to_string(),
143                    line_offsets,
144                },
145            )
146            .expect("could not add file info");
147    }
148
149    pub fn add_manual_no_id(
150        &mut self,
151        mount_name: &str,
152        relative_path: &Path,
153        contents: &str,
154    ) -> FileId {
155        let line_offsets = Self::compute_line_offsets(contents);
156        let id = self.next_file_id;
157        self.next_file_id += 1;
158
159        self.cache
160            .insert(
161                id,
162                FileInfo {
163                    mount_name: mount_name.to_string(),
164                    relative_path: relative_path.to_path_buf(),
165                    contents: contents.to_string(),
166                    line_offsets,
167                },
168            )
169            .expect("could not add file info");
170        id
171    }
172
173    pub fn read_file_relative(
174        &mut self,
175        mount_name: &str,
176        relative_path: &str,
177    ) -> io::Result<(FileId, String)> {
178        if let Some(found_in_cache) = self
179            .file_cache
180            .get(&(mount_name.to_string(), relative_path.to_string()))
181        {
182            let contents = self.cache.get(found_in_cache).unwrap().contents.clone();
183            return Ok((found_in_cache.clone(), contents));
184        }
185
186        let buf = self.to_file_system_path(mount_name, relative_path)?;
187        self.read_file(&buf, mount_name)
188    }
189
190    /*
191
192    fn to_relative_path(path: &ModulePath) -> RelativePath {
193        RelativePath(
194            path.0
195                .iter()
196                .map(|local_type_identifier| local_type_identifier.as_str())
197                .collect::<Vec<_>>()
198                .join("/"),
199        )
200    }
201
202     */
203
204    fn to_file_system_path(&self, mount_name: &str, relative_path: &str) -> io::Result<PathBuf> {
205        let base_path = self.base_path(mount_name).to_path_buf();
206        let mut path_buf = base_path;
207
208        path_buf.push(relative_path);
209
210        path_buf.canonicalize().map_err(|_| {
211            io::Error::new(
212                ErrorKind::Other,
213                format!("path is wrong mount:{mount_name} relative:{relative_path}",),
214            )
215        })
216    }
217
218    fn compute_line_offsets(contents: &str) -> Box<[u16]> {
219        let mut offsets = Vec::new();
220        offsets.push(0);
221        for (i, &byte) in contents.as_bytes().iter().enumerate() {
222            if byte == b'\n' {
223                // Safety: new line is always encoded as single octet
224                let next_line_start = u16::try_from(i + 1).expect("too big file");
225                offsets.push(next_line_start);
226            }
227        }
228        offsets.into_boxed_slice()
229    }
230
231    #[must_use]
232    pub fn get_span_source(&self, file_id: FileId, offset: usize, length: usize) -> &str {
233        self.cache.get(&file_id).map_or_else(
234            || {
235                "ERROR"
236                //panic!("{}", &format!("Invalid file_id {file_id} in span"));
237            },
238            |file_info| {
239                let start = offset;
240                let end = start + length;
241                &file_info.contents[start..end]
242            },
243        )
244    }
245
246    #[must_use]
247    pub fn get_source_line(&self, file_id: FileId, line_number: usize) -> Option<&str> {
248        let file_info = self.cache.get(&file_id)?;
249
250        let start_offset = file_info.line_offsets[line_number - 1] as usize;
251        let end_offset = file_info.line_offsets[line_number] as usize;
252        Some(&file_info.contents[start_offset..end_offset - 1])
253    }
254
255    #[must_use]
256    pub fn get_span_location_utf8(&self, file_id: FileId, offset: usize) -> (usize, usize) {
257        let file_info = self.cache.get(&file_id).expect("Invalid file_id in span");
258
259        let offset = offset as u16;
260
261        // Find the line containing 'offset' via binary search.
262        let line_idx = file_info
263            .line_offsets
264            .binary_search(&offset)
265            .unwrap_or_else(|insert_point| insert_point.saturating_sub(1));
266
267        // Determine the start of the line in bytes
268        let line_start = file_info.line_offsets[line_idx] as usize;
269        let octet_offset = offset as usize;
270
271        // Extract the line slice from line_start to offset
272        let line_text = &file_info.contents[line_start..octet_offset];
273
274        // Count UTF-8 characters in that range, because that is what the end user sees in their editor.
275        let column_character_offset = line_text.chars().count();
276
277        // Add one so it makes more sense to the end user
278        (line_idx + 1, column_character_offset + 1)
279    }
280
281    #[must_use]
282    pub fn fetch_relative_filename(&self, file_id: FileId) -> &str {
283        self.cache
284            .get(&file_id)
285            .unwrap()
286            .relative_path
287            .to_str()
288            .unwrap()
289    }
290    pub fn minimal_relative_path(target: &Path, current_dir: &Path) -> io::Result<PathBuf> {
291        //let target = target.canonicalize()?;
292
293        let current_dir_components = current_dir.components().collect::<Vec<_>>();
294        let target_components = target.components().collect::<Vec<_>>();
295
296        let mut common_prefix_len = 0;
297        for i in 0..std::cmp::min(current_dir_components.len(), target_components.len()) {
298            if current_dir_components[i] == target_components[i] {
299                common_prefix_len += 1;
300            } else {
301                break;
302            }
303        }
304
305        let mut relative_path = PathBuf::new();
306
307        for _ in 0..(current_dir_components.len() - common_prefix_len) {
308            relative_path.push("..");
309        }
310
311        for component in &target_components[common_prefix_len..] {
312            relative_path.push(component);
313        }
314        Ok(relative_path)
315    }
316    pub fn get_relative_path_to(&self, file_id: FileId, current_dir: &Path) -> io::Result<PathBuf> {
317        let file_info = self.cache.get(&file_id).unwrap();
318        let mount_path = self.mounts.get(&file_info.mount_name).unwrap();
319
320        let absolute_path = mount_path.join(&file_info.relative_path);
321
322        Self::minimal_relative_path(&absolute_path, current_dir)
323    }
324}