source-map-cache 0.1.0

Source map cache
Documentation
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp
 * Licensed under the MIT License. See LICENSE in the project root for license information.
 */
use pathdiff::diff_paths;
use seq_map::SeqMap;
use source_map_node::{Node, Span};
use std::fmt::Debug;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use std::{fs, io};
pub mod prelude;
pub type FileId = u16;

pub struct KeepTrackOfSourceLine {
    pub last_line_info: SourceFileLineInfo,
    pub current_line: usize,
}

impl Default for KeepTrackOfSourceLine {
    fn default() -> Self {
        Self::new()
    }
}

impl KeepTrackOfSourceLine {
    #[must_use]
    pub const fn new() -> Self {
        Self {
            last_line_info: SourceFileLineInfo {
                row: usize::MAX,
                file_id: usize::MAX,
            },
            current_line: usize::MAX,
        }
    }

    pub fn check_if_new_line(&mut self, found: &SourceFileLineInfo) -> Option<(usize, usize)> {
        if self.last_line_info.file_id != found.file_id || found.row != self.current_line {
            self.last_line_info = found.clone();
            self.current_line = self.last_line_info.row;
            Some((self.last_line_info.row, self.last_line_info.row))
        } else if found.row == self.current_line {
            None
        } else {
            let line_start = self.current_line;
            self.current_line = found.row;
            Some((line_start, found.row))
        }
    }
}

#[derive(Eq, PartialEq, Clone)]
pub struct SourceFileLineInfo {
    pub row: usize,
    pub file_id: usize,
}

#[derive(Debug)]
pub struct FileInfo {
    pub mount_name: String,
    pub relative_path: PathBuf,
    pub contents: String,
    pub line_offsets: Box<[u16]>,
}

#[derive(Debug)]
pub struct SourceMap {
    pub mounts: SeqMap<String, PathBuf>,
    pub cache: SeqMap<FileId, FileInfo>,
    pub file_cache: SeqMap<(String, String), FileId>,
    pub next_file_id: FileId,
}

#[derive(Debug)]
pub struct RelativePath(pub String);

impl SourceMap {
    /// # Errors
    ///
    pub fn new(mounts: &SeqMap<String, PathBuf>) -> io::Result<Self> {
        let mut canonical_mounts = SeqMap::new();
        for (mount_name, base_path) in mounts {
            let canon_path = base_path.canonicalize().map_err(|_| {
                io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("could not canonicalize {base_path:?}"),
                )
            })?;

            if !canon_path.is_dir() {
                return Err(io::Error::new(
                    ErrorKind::NotFound,
                    format!("{canon_path:?} is not a directory"),
                ));
            }
            canonical_mounts
                .insert(mount_name.clone(), canon_path)
                .map_err(|_| {
                    io::Error::new(io::ErrorKind::InvalidData, "could not insert mount")
                })?;
        }
        Ok(Self {
            mounts: canonical_mounts,
            cache: SeqMap::new(),
            file_cache: SeqMap::new(),
            next_file_id: 1,
        })
    }

    /// # Errors
    ///
    pub fn add_mount(&mut self, name: &str, path: &Path) -> io::Result<()> {
        if !path.is_dir() {
            return Err(io::Error::new(
                ErrorKind::NotFound,
                format!("{path:?} is not a directory"),
            ));
        }
        self.mounts
            .insert(name.to_string(), path.to_path_buf())
            .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "could not insert mount"))
    }

    #[must_use]
    pub fn base_path(&self, name: &str) -> &Path {
        self.mounts.get(&name.to_string()).unwrap_or_else(|| {
            panic!("could not find path {name}");
        })
    }

    pub fn read_file(&mut self, path: &Path, mount_name: &str) -> io::Result<(FileId, String)> {
        let found_base_path = self.base_path(mount_name);
        let relative_path = diff_paths(path, found_base_path)
            .unwrap_or_else(|| panic!("could not find relative path {path:?} {found_base_path:?}"));

        let contents = fs::read_to_string(path)?;

        let id = self.next_file_id;
        self.next_file_id += 1;

        self.add_manual(id, mount_name, &relative_path, &contents);

        Ok((id, contents))
    }

    pub fn add_to_cache(
        &mut self,
        mount_name: &str,
        relative_path: &Path,
        contents: &str,
        file_id: FileId,
    ) {
        self.add_manual(file_id, mount_name, relative_path, contents);
    }

    pub fn add_manual(
        &mut self,
        id: FileId,
        mount_name: &str,
        relative_path: &Path,
        contents: &str,
    ) {
        let line_offsets = Self::compute_line_offsets(contents);

        self.cache
            .insert(
                id,
                FileInfo {
                    mount_name: mount_name.to_string(),
                    relative_path: relative_path.to_path_buf(),
                    contents: contents.to_string(),
                    line_offsets,
                },
            )
            .expect("could not add file info");

        // Update file_cache to maintain consistency
        self.file_cache
            .insert(
                (
                    mount_name.to_string(),
                    relative_path.to_str().unwrap().to_string(),
                ),
                id,
            )
            .expect("could not add to file cache");
    }

    pub fn add_manual_no_id(
        &mut self,
        mount_name: &str,
        relative_path: &Path,
        contents: &str,
    ) -> FileId {
        let line_offsets = Self::compute_line_offsets(contents);
        let id = self.next_file_id;
        self.next_file_id += 1;

        self.cache
            .insert(
                id,
                FileInfo {
                    mount_name: mount_name.to_string(),
                    relative_path: relative_path.to_path_buf(),
                    contents: contents.to_string(),
                    line_offsets,
                },
            )
            .expect("could not add file info");

        // Update file_cache to maintain consistency
        self.file_cache
            .insert(
                (
                    mount_name.to_string(),
                    relative_path.to_str().unwrap().to_string(),
                ),
                id,
            )
            .expect("could not add to file cache");

        id
    }

    pub fn read_file_relative(
        &mut self,
        mount_name: &str,
        relative_path: &str,
    ) -> io::Result<(FileId, String)> {
        if let Some(found_in_cache) = self
            .file_cache
            .get(&(mount_name.to_string(), relative_path.to_string()))
        {
            let contents = self.cache.get(found_in_cache).unwrap().contents.clone();
            return Ok((*found_in_cache, contents));
        }

        let buf = self.to_file_system_path(mount_name, relative_path)?;
        self.read_file(&buf, mount_name)
    }

    fn to_file_system_path(&self, mount_name: &str, relative_path: &str) -> io::Result<PathBuf> {
        let base_path = self.base_path(mount_name).to_path_buf();
        let mut path_buf = base_path;

        path_buf.push(relative_path);

        path_buf.canonicalize().map_err(|_| {
            io::Error::other(format!(
                "path is wrong mount:{mount_name} relative:{relative_path}",
            ))
        })
    }

    fn compute_line_offsets(contents: &str) -> Box<[u16]> {
        let mut offsets = Vec::new();
        offsets.push(0);

        // Track positions of all newlines
        for (i, &byte) in contents.as_bytes().iter().enumerate() {
            if byte == b'\n' {
                // Safety: new line is always encoded as single octet
                let next_line_start = u16::try_from(i + 1).expect("too big file");
                offsets.push(next_line_start);
            }
        }

        // Always add the end of file position if it's not already there
        // (happens when file doesn't end with newline)
        let eof_offset = u16::try_from(contents.len()).expect("too big file");
        if offsets.last().is_none_or(|&last| last != eof_offset) {
            offsets.push(eof_offset);
        }

        offsets.into_boxed_slice()
    }

    #[must_use]
    pub fn get_span_source(&self, file_id: FileId, offset: usize, length: usize) -> Option<&str> {
        let file_info = self.cache.get(&file_id)?;

        let start = offset;
        let end = offset + length;

        // Verify both start and end are on character boundaries
        if start > file_info.contents.len()
            || end > file_info.contents.len()
            || !file_info.contents.is_char_boundary(start)
            || !file_info.contents.is_char_boundary(end)
        {
            // Invalid offsets - not on character boundaries
            return None;
        }

        Some(&file_info.contents[start..end])
    }

    #[must_use]
    pub fn get_source_line(&self, file_id: FileId, line_number: usize) -> Option<&str> {
        let file_info = self.cache.get(&file_id)?;

        // Check if the requested line number is valid
        if line_number == 0 || line_number >= file_info.line_offsets.len() {
            return None;
        }

        let start_offset = file_info.line_offsets[line_number - 1] as usize;
        let end_offset = file_info.line_offsets[line_number] as usize;

        let line = &file_info.contents[start_offset..end_offset];

        // Remove trailing newline if present.
        // Some files may not end with a newline.
        Some(line.strip_suffix('\n').unwrap_or(line))
    }

    #[must_use]
    pub fn get_span_location_utf8(&self, file_id: FileId, offset: usize) -> Option<(usize, usize)> {
        let file_info = self.cache.get(&file_id)?;
        let offset = offset as u16;

        let octet_offset = offset as usize;

        // Verify offset is on a character boundary
        if octet_offset > file_info.contents.len()
            || !file_info.contents.is_char_boundary(octet_offset)
        {
            // Invalid offset - not on a character boundary
            return None;
        }

        // Find the line containing 'offset' via binary search.
        let line_idx = file_info
            .line_offsets
            .binary_search(&offset)
            .unwrap_or_else(|insert_point| insert_point.saturating_sub(1));

        // Determine the start of the line in bytes
        let line_start = file_info.line_offsets[line_idx] as usize;

        // Extract the line slice from line_start to offset
        let line_text = &file_info.contents[line_start..octet_offset];

        // Count UTF-8 characters in that range, because that is what the end user sees in their editor.
        let column_character_offset = line_text.chars().count();

        // Add one so it makes more sense to the end user
        Some((line_idx + 1, column_character_offset + 1))
    }

    #[must_use]
    pub fn fetch_relative_filename(&self, file_id: FileId) -> &str {
        self.cache
            .get(&file_id)
            .unwrap()
            .relative_path
            .to_str()
            .unwrap()
    }

    pub fn minimal_relative_path(target: &Path, current_dir: &Path) -> io::Result<PathBuf> {
        let current_dir_components = current_dir.components().collect::<Vec<_>>();
        let target_components = target.components().collect::<Vec<_>>();

        let mut common_prefix_len = 0;
        for i in 0..std::cmp::min(current_dir_components.len(), target_components.len()) {
            if current_dir_components[i] == target_components[i] {
                common_prefix_len += 1;
            } else {
                break;
            }
        }

        let mut relative_path = PathBuf::new();

        for _ in 0..(current_dir_components.len() - common_prefix_len) {
            relative_path.push("..");
        }

        for component in &target_components[common_prefix_len..] {
            relative_path.push(component);
        }
        Ok(relative_path)
    }

    pub fn get_relative_path_to(&self, file_id: FileId, current_dir: &Path) -> io::Result<PathBuf> {
        let file_info = self.cache.get(&file_id).unwrap();
        let mount_path = self.mounts.get(&file_info.mount_name).unwrap();

        let absolute_path = mount_path.join(&file_info.relative_path);

        Self::minimal_relative_path(&absolute_path, current_dir)
    }

    #[must_use]
    pub fn get_text(&self, node: &Node) -> Option<&str> {
        self.get_span_source(
            node.span.file_id,
            node.span.offset as usize,
            node.span.length as usize,
        )
    }

    #[must_use]
    pub fn get_text_span(&self, span: &Span) -> Option<&str> {
        self.get_span_source(span.file_id, span.offset as usize, span.length as usize)
    }

    #[must_use]
    pub fn get_line(&self, span: &Span, current_dir: &Path) -> Option<FileLineInfo> {
        let relative_file_name = self.get_relative_path_to(span.file_id, current_dir).ok()?;
        let (row, col) = self.get_span_location_utf8(span.file_id, span.offset as usize)?;
        let line = self.get_source_line(span.file_id, row)?;

        Some(FileLineInfo {
            row,
            col,
            line: line.to_string(),
            relative_file_name: relative_file_name.to_str().unwrap().to_string(),
        })
    }

    pub fn set(&mut self, mount_name: &str, relative_path: &Path, contents: &str) -> FileId {
        // Check if file already exists in cache
        if let Some(&existing_file_id) = self.file_cache.get(&(
            mount_name.to_string(),
            relative_path.to_str().unwrap().to_string(),
        )) {
            // File exists, update its contents
            let line_offsets = Self::compute_line_offsets(contents);

            if let Some(file_info) = self.cache.get_mut(&existing_file_id) {
                file_info.contents = contents.to_string();
                file_info.line_offsets = line_offsets;
            }

            existing_file_id
        } else {
            // File doesn't exist, add it as new
            self.add_manual_no_id(mount_name, relative_path, contents)
        }
    }
}

pub struct FileLineInfo {
    pub row: usize,
    pub col: usize,
    pub line: String,
    pub relative_file_name: String,
}

pub struct SourceLineInfo {
    pub line: String,
    pub relative_file_name: String,
}

pub trait SourceMapLookup: Debug {
    fn get_text(&self, node: &Node) -> Option<&str>;
    fn get_text_span(&self, span: &Span) -> Option<&str>;
    fn get_line(&self, span: &Span) -> Option<FileLineInfo>;
    fn get_relative_path(&self, file_id: FileId) -> String;
    fn get_source_line(&self, file_id: FileId, row: usize) -> Option<&str>;
}

#[derive(Debug)]
pub struct SourceMapWrapper<'a> {
    pub source_map: &'a SourceMap,
    pub current_dir: PathBuf,
}

impl SourceMapLookup for SourceMapWrapper<'_> {
    fn get_text(&self, resolved_node: &Node) -> Option<&str> {
        self.source_map.get_text(resolved_node)
    }

    fn get_text_span(&self, span: &Span) -> Option<&str> {
        self.source_map.get_text_span(span)
    }

    fn get_line(&self, span: &Span) -> Option<FileLineInfo> {
        self.source_map.get_line(span, &self.current_dir)
    }

    fn get_relative_path(&self, file_id: FileId) -> String {
        self.source_map
            .get_relative_path_to(file_id, &self.current_dir)
            .unwrap()
            .to_str()
            .unwrap()
            .to_string()
    }

    fn get_source_line(&self, file_id: FileId, line_number: usize) -> Option<&str> {
        self.source_map.get_source_line(file_id, line_number)
    }
}