58: even less hacks r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2018-09-05 17:23:25 +00:00
commit 529ebd5840

View file

@ -4,40 +4,38 @@ extern crate test_utils;
use std::{ use std::{
collections::HashMap, collections::HashMap,
path::{Path},
}; };
use relative_path::{RelativePath, RelativePathBuf}; use relative_path::{RelativePath};
use libanalysis::{AnalysisHost, FileId, FileResolver, JobHandle, CrateGraph, CrateId}; use libanalysis::{AnalysisHost, FileId, FileResolver, JobHandle, CrateGraph, CrateId};
use test_utils::assert_eq_dbg; use test_utils::assert_eq_dbg;
struct FileMap(&'static [(u32, &'static str)]); struct FileMap(&'static [(u32, &'static str)]);
impl FileMap { impl FileMap {
fn path(&self, id: FileId) -> &'static Path { fn iter<'a>(&'a self) -> impl Iterator<Item=(FileId, &'a RelativePath)> + 'a {
let s = self.0.iter() self.0.iter().map(|&(id, path)| {
.find(|it| it.0 == id.0) assert!(path.starts_with('/'));
(FileId(id), RelativePath::new(&path[1..]))
})
}
fn path(&self, id: FileId) -> &RelativePath {
self.iter()
.find(|&(it, _)| it == id)
.unwrap() .unwrap()
.1; .1
Path::new(s)
} }
} }
impl FileResolver for FileMap { impl FileResolver for FileMap {
fn file_stem(&self, id: FileId) -> String { fn file_stem(&self, id: FileId) -> String {
self.path(id).file_stem().unwrap().to_str().unwrap().to_string() self.path(id).file_stem().unwrap().to_string()
} }
fn resolve(&self, id: FileId, rel: &RelativePath) -> Option<FileId> { fn resolve(&self, id: FileId, rel: &RelativePath) -> Option<FileId> {
let path = rel.to_path(self.path(id)); let path = self.path(id).join(rel).normalize();
let path = path.strip_prefix("/").unwrap(); let id = self.iter().find(|&(_, p)| path == p)?.0;
let path = RelativePathBuf::from_path(&path).unwrap().normalize(); Some(id)
let &(id, _) = self.0.iter()
.find(|it| {
let p = Path::new(it.1).strip_prefix("/").unwrap();
let p = RelativePathBuf::from_path(p).unwrap();
path == p
})?;
Some(FileId(id))
} }
} }