Separate creation and initialization of global state

This commit is contained in:
Aleksey Kladov 2020-06-25 23:11:59 +02:00
parent 3615347fce
commit 73d73077fe

View file

@ -81,7 +81,7 @@ pub(crate) struct GlobalState {
pub(crate) req_queue: ReqQueue, pub(crate) req_queue: ReqQueue,
latest_requests: Arc<RwLock<LatestRequests>>, latest_requests: Arc<RwLock<LatestRequests>>,
source_root_config: SourceRootConfig, source_root_config: SourceRootConfig,
_proc_macro_client: ProcMacroClient, proc_macro_client: ProcMacroClient,
workspaces: Arc<Vec<ProjectWorkspace>>, workspaces: Arc<Vec<ProjectWorkspace>>,
} }
@ -103,14 +103,48 @@ impl GlobalState {
config: Config, config: Config,
req_queue: ReqQueue, req_queue: ReqQueue,
) -> GlobalState { ) -> GlobalState {
let (task_sender, task_receiver) = unbounded::<vfs::loader::Message>();
let loader = {
let loader = vfs_notify::NotifyHandle::spawn(Box::new(move |msg| {
task_sender.send(msg).unwrap()
}));
Box::new(loader)
};
let task_pool = {
let (sender, receiver) = unbounded();
(TaskPool::new(sender), receiver)
};
let mut res = GlobalState {
sender,
config,
task_pool,
analysis_host: AnalysisHost::new(lru_capacity),
loader,
task_receiver,
flycheck: None,
diagnostics: Default::default(),
mem_docs: FxHashSet::default(),
vfs: Arc::new(RwLock::new((vfs::Vfs::default(), FxHashMap::default()))),
status: Status::default(),
req_queue,
latest_requests: Default::default(),
source_root_config: SourceRootConfig::default(),
proc_macro_client: ProcMacroClient::dummy(),
workspaces: Arc::new(Vec::new()),
};
res.reload(workspaces);
res
}
pub(crate) fn reload(&mut self, workspaces: Vec<ProjectWorkspace>) {
let mut change = AnalysisChange::new(); let mut change = AnalysisChange::new();
let project_folders = ProjectFolders::new(&workspaces); let project_folders = ProjectFolders::new(&workspaces);
let (task_sender, task_receiver) = unbounded::<vfs::loader::Message>(); self.proc_macro_client = match &self.config.proc_macro_srv {
let mut vfs = vfs::Vfs::default();
let proc_macro_client = match &config.proc_macro_srv {
None => ProcMacroClient::dummy(), None => ProcMacroClient::dummy(),
Some((path, args)) => match ProcMacroClient::extern_process(path.into(), args) { Some((path, args)) => match ProcMacroClient::extern_process(path.into(), args) {
Ok(it) => it, Ok(it) => it,
@ -124,21 +158,17 @@ impl GlobalState {
} }
}, },
}; };
let watch = match self.config.files.watcher {
let mut loader = {
let loader = vfs_notify::NotifyHandle::spawn(Box::new(move |msg| {
task_sender.send(msg).unwrap()
}));
Box::new(loader)
};
let watch = match config.files.watcher {
FilesWatcher::Client => vec![], FilesWatcher::Client => vec![],
FilesWatcher::Notify => project_folders.watch, FilesWatcher::Notify => project_folders.watch,
}; };
loader.set_config(vfs::loader::Config { load: project_folders.load, watch }); self.loader.set_config(vfs::loader::Config { load: project_folders.load, watch });
// Create crate graph from all the workspaces // Create crate graph from all the workspaces
let crate_graph = {
let mut crate_graph = CrateGraph::default(); let mut crate_graph = CrateGraph::default();
let vfs = &mut self.vfs.write().0;
let loader = &mut self.loader;
let mut load = |path: &AbsPath| { let mut load = |path: &AbsPath| {
let contents = loader.load_sync(path); let contents = loader.load_sync(path);
let path = vfs::VfsPath::from(path.to_path_buf()); let path = vfs::VfsPath::from(path.to_path_buf());
@ -147,43 +177,22 @@ impl GlobalState {
}; };
for ws in workspaces.iter() { for ws in workspaces.iter() {
crate_graph.extend(ws.to_crate_graph( crate_graph.extend(ws.to_crate_graph(
config.cargo.target.as_deref(), self.config.cargo.target.as_deref(),
&proc_macro_client, &self.proc_macro_client,
&mut load, &mut load,
)); ));
} }
crate_graph
};
change.set_crate_graph(crate_graph); change.set_crate_graph(crate_graph);
let flycheck = config.check.as_ref().and_then(|c| create_flycheck(&workspaces, c)); self.flycheck = self.config.check.as_ref().and_then(|c| create_flycheck(&workspaces, c));
self.source_root_config = project_folders.source_root_config;
self.workspaces = Arc::new(workspaces);
let mut analysis_host = AnalysisHost::new(lru_capacity); self.analysis_host.apply_change(change);
analysis_host.apply_change(change); self.process_changes();
let task_pool = {
let (sender, receiver) = unbounded();
(TaskPool::new(sender), receiver)
};
let mut res = GlobalState {
sender,
config,
task_pool,
analysis_host,
loader,
task_receiver,
flycheck,
diagnostics: Default::default(),
mem_docs: FxHashSet::default(),
vfs: Arc::new(RwLock::new((vfs, FxHashMap::default()))),
status: Status::default(),
req_queue,
latest_requests: Default::default(),
source_root_config: project_folders.source_root_config,
_proc_macro_client: proc_macro_client,
workspaces: Arc::new(workspaces),
};
res.process_changes();
res
} }
pub(crate) fn update_configuration(&mut self, config: Config) { pub(crate) fn update_configuration(&mut self, config: Config) {