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

View file

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

View file

@ -34,6 +34,11 @@ impl PathMap {
.as_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) { fn insert(&mut self, path: PathBuf, id: FileId) {
self.path2id.insert(path.clone(), id); self.path2id.insert(path.clone(), id);
self.id2path.insert(id, path.clone()); self.id2path.insert(id, path.clone());