aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db/src/mock.rs
blob: 14d9e79b50eafb5e8009f2caf7a0bddfff8b44e1 (plain)
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
use rustc_hash::FxHashSet;
use relative_path::{RelativePath, RelativePathBuf};

use crate::{FileId, FileResolver, SourceRoot};

#[derive(Default, Debug, Clone)]
pub struct FileMap(Vec<(FileId, RelativePathBuf)>);

impl FileMap {
    pub fn add(&mut self, path: RelativePathBuf) -> FileId {
        let file_id = FileId((self.0.len() + 1) as u32);
        self.0.push((file_id, path));
        file_id
    }

    pub fn into_source_root(self) -> SourceRoot {
        let files = self.files();
        SourceRoot { files }
    }

    pub fn files(&self) -> FxHashSet<FileId> {
        self.iter().map(|(id, _)| id).collect()
    }

    pub fn file_id(&self, path: &str) -> FileId {
        assert!(path.starts_with('/'));
        self.iter().find(|(_, p)| p == &path[1..]).unwrap().0
    }

    fn iter<'a>(&'a self) -> impl Iterator<Item = (FileId, &'a RelativePath)> + 'a {
        self.0
            .iter()
            .map(|(id, path)| (*id, path.as_relative_path()))
    }

    fn path(&self, id: FileId) -> &RelativePath {
        self.iter().find(|&(it, _)| it == id).unwrap().1
    }
}

impl FileResolver for FileMap {
    fn file_stem(&self, id: FileId) -> String {
        self.path(id).file_stem().unwrap().to_string()
    }
    fn resolve(&self, id: FileId, rel: &RelativePath) -> Option<FileId> {
        let path = self.path(id).join(rel).normalize();
        let id = self.iter().find(|&(_, p)| path == p)?.0;
        Some(id)
    }
}