mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
mod resolve work
This commit is contained in:
parent
6a3f819f79
commit
55e87e0b74
3 changed files with 50 additions and 7 deletions
|
@ -15,6 +15,8 @@ use once_cell::sync::OnceCell;
|
|||
use rayon::prelude::*;
|
||||
|
||||
use std::{
|
||||
fmt,
|
||||
path::Path,
|
||||
sync::{
|
||||
Arc,
|
||||
atomic::{AtomicUsize, Ordering::SeqCst},
|
||||
|
@ -35,15 +37,24 @@ pub use self::symbol_index::Query;
|
|||
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 @@ impl 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 @@ impl World {
|
|||
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);
|
||||
|
|
|
@ -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 || {
|
||||
|
|
|
@ -34,6 +34,11 @@ impl PathMap {
|
|||
.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());
|
||||
|
|
Loading…
Reference in a new issue