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
use std::io;
use std::path::PathBuf;
use std::sync::Arc;
use filemap::{FileMap, FileName};
use index::{ByteIndex, ByteOffset};
#[derive(Clone, Debug, Default)]
pub struct CodeMap {
files: Vec<Arc<FileMap>>,
}
impl CodeMap {
pub fn new() -> CodeMap {
CodeMap::default()
}
fn next_start_index(&self) -> ByteIndex {
let end_index = self.files
.last()
.map(|x| x.span().end())
.unwrap_or_else(ByteIndex::none);
end_index + ByteOffset(1)
}
pub fn add_filemap(&mut self, name: FileName, src: String) -> Arc<FileMap> {
let file = Arc::new(FileMap::with_index(name, src, self.next_start_index()));
self.files.push(file.clone());
file
}
pub fn add_filemap_from_disk<P: Into<PathBuf>>(&mut self, name: P) -> io::Result<Arc<FileMap>> {
let file = Arc::new(FileMap::from_disk(name, self.next_start_index())?);
self.files.push(file.clone());
Ok(file)
}
pub fn find_file(&self, index: ByteIndex) -> Option<&Arc<FileMap>> {
use std::cmp::Ordering;
self.files
.binary_search_by(|file| match () {
() if file.span().start() > index => Ordering::Greater,
() if file.span().end() < index => Ordering::Less,
() => Ordering::Equal,
})
.ok()
.map(|i| &self.files[i])
}
}