mod resolve work

This commit is contained in:
Aleksey Kladov 2018-08-17 00:18:14 +03:00
parent 6a3f819f79
commit 55e87e0b74
3 changed files with 50 additions and 7 deletions

View File

@ -15,6 +15,8 @@
use rayon::prelude::*;
use std::{
fmt,
path::Path,
sync::{
Arc,
atomic::{AtomicUsize, Ordering::SeqCst},
@ -35,15 +37,24 @@
pub type Result<T> = ::std::result::Result<T, ::failure::Error>;
const INDEXING_THRESHOLD: usize = 128;
pub type FileResolver = dyn Fn(FileId, &Path) -> Option<FileId> + Send + Sync;
pub struct WorldState {
data: Arc<WorldData>
}
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct World {
file_resolver: Arc<FileResolver>,
data: Arc<WorldData>,
}
impl fmt::Debug for World {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
(&*self.data).fmt(f)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FileId(pub u32);
@ -54,8 +65,11 @@ pub fn new() -> WorldState {
}
}
pub fn snapshot(&self) -> World {
World { data: self.data.clone() }
pub fn snapshot(&self, file_resolver: impl Fn(FileId, &Path) -> Option<FileId> + 'static + Send + Sync) -> World {
World {
file_resolver: Arc::new(file_resolver),
data: self.data.clone()
}
}
pub fn change_file(&mut self, file_id: FileId, text: Option<String>) {
@ -134,6 +148,10 @@ pub fn approximately_resolve_symbol<'a>(
Ok(self.world_symbols(query).collect())
}
fn resolve_relative_path(&self, id: FileId, path: &Path) -> Option<FileId> {
(self.file_resolver)(id, path)
}
fn reindex(&self) {
let data = &*self.data;
let unindexed = data.unindexed.load(SeqCst);

View File

@ -167,7 +167,10 @@ fn on_request(
dispatch::handle_request::<req::ExecuteCommand, _>(&mut req, |params, resp| {
io.send(RawMsg::Response(resp.into_response(Ok(None))?));
let world = world.snapshot();
let world = world.snapshot({
let pm = path_map.clone();
move |id, path| pm.resolve(id, path)
});
let path_map = path_map.clone();
let sender = sender.clone();
pool.execute(move || {
@ -234,7 +237,14 @@ fn on_notification(
mem_map.insert(file_id, None);
world.change_file(file_id, Some(params.text_document.text));
update_file_notifications_on_threadpool(
pool, world.snapshot(), path_map.clone(), sender.clone(), uri,
pool,
world.snapshot({
let pm = path_map.clone();
move |id, path| pm.resolve(id, path)
}),
path_map.clone(),
sender.clone(),
uri,
);
Ok(())
})?;
@ -245,7 +255,14 @@ fn on_notification(
.text;
world.change_file(file_id, Some(text));
update_file_notifications_on_threadpool(
pool, world.snapshot(), path_map.clone(), sender.clone(), params.text_document.uri,
pool,
world.snapshot({
let pm = path_map.clone();
move |id, path| pm.resolve(id, path)
}),
path_map.clone(),
sender.clone(),
params.text_document.uri,
);
Ok(())
})?;
@ -281,7 +298,10 @@ fn handle_request_on_threadpool<R: req::ClientRequest>(
) -> Result<()>
{
dispatch::handle_request::<R, _>(req, |params, resp| {
let world = world.snapshot();
let world = world.snapshot({
let pm = path_map.clone();
move |id, path| pm.resolve(id, path)
});
let path_map = path_map.clone();
let sender = sender.clone();
pool.execute(move || {

View File

@ -34,6 +34,11 @@ pub fn get_path(&self, id: FileId) -> &Path {
.as_path()
}
pub fn resolve(&self, id: FileId, relpath: &Path) -> Option<FileId> {
let path = self.get_path(id).join(relpath);
self.get_id(&path)
}
fn insert(&mut self, path: PathBuf, id: FileId) {
self.path2id.insert(path.clone(), id);
self.id2path.insert(id, path.clone());