Add new module for project loading stuff
This commit is contained in:
241
crates/rust-analyzer/src/reload.rs
Normal file
241
crates/rust-analyzer/src/reload.rs
Normal file
@@ -0,0 +1,241 @@
|
||||
//! Project loading & configuration updates
|
||||
use std::sync::Arc;
|
||||
|
||||
use crossbeam_channel::unbounded;
|
||||
use flycheck::FlycheckHandle;
|
||||
use lsp_types::request::Request;
|
||||
use ra_db::{CrateGraph, SourceRoot, VfsPath};
|
||||
use ra_ide::AnalysisChange;
|
||||
use ra_project_model::{PackageRoot, ProcMacroClient, ProjectWorkspace};
|
||||
use vfs::{file_set::FileSetConfig, AbsPath};
|
||||
|
||||
use crate::{
|
||||
config::{Config, FilesWatcher, LinkedProject},
|
||||
global_state::GlobalState,
|
||||
};
|
||||
|
||||
impl GlobalState {
|
||||
pub(crate) fn update_configuration(&mut self, new_config: Config) {
|
||||
self.analysis_host.update_lru_capacity(new_config.lru_capacity);
|
||||
if new_config.flycheck != self.config.flycheck {
|
||||
self.reload_flycheck();
|
||||
}
|
||||
self.config = new_config;
|
||||
}
|
||||
pub(crate) fn reload(&mut self) {
|
||||
let workspaces = {
|
||||
if self.config.linked_projects.is_empty()
|
||||
&& self.config.notifications.cargo_toml_not_found
|
||||
{
|
||||
self.show_message(
|
||||
lsp_types::MessageType::Error,
|
||||
"rust-analyzer failed to discover workspace".to_string(),
|
||||
);
|
||||
};
|
||||
|
||||
self.config
|
||||
.linked_projects
|
||||
.iter()
|
||||
.filter_map(|project| match project {
|
||||
LinkedProject::ProjectManifest(manifest) => {
|
||||
ra_project_model::ProjectWorkspace::load(
|
||||
manifest.clone(),
|
||||
&self.config.cargo,
|
||||
self.config.with_sysroot,
|
||||
)
|
||||
.map_err(|err| {
|
||||
log::error!("failed to load workspace: {:#}", err);
|
||||
self.show_message(
|
||||
lsp_types::MessageType::Error,
|
||||
format!("rust-analyzer failed to load workspace: {:#}", err),
|
||||
);
|
||||
})
|
||||
.ok()
|
||||
}
|
||||
LinkedProject::InlineJsonProject(it) => {
|
||||
Some(ra_project_model::ProjectWorkspace::Json { project: it.clone() })
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
|
||||
if let FilesWatcher::Client = self.config.files.watcher {
|
||||
let registration_options = lsp_types::DidChangeWatchedFilesRegistrationOptions {
|
||||
watchers: workspaces
|
||||
.iter()
|
||||
.flat_map(ProjectWorkspace::to_roots)
|
||||
.filter(PackageRoot::is_member)
|
||||
.map(|root| format!("{}/**/*.rs", root.path().display()))
|
||||
.map(|glob_pattern| lsp_types::FileSystemWatcher { glob_pattern, kind: None })
|
||||
.collect(),
|
||||
};
|
||||
let registration = lsp_types::Registration {
|
||||
id: "file-watcher".to_string(),
|
||||
method: "workspace/didChangeWatchedFiles".to_string(),
|
||||
register_options: Some(serde_json::to_value(registration_options).unwrap()),
|
||||
};
|
||||
let params = lsp_types::RegistrationParams { registrations: vec![registration] };
|
||||
let request = self.req_queue.outgoing.register(
|
||||
lsp_types::request::RegisterCapability::METHOD.to_string(),
|
||||
params,
|
||||
|_, _| (),
|
||||
);
|
||||
self.send(request.into());
|
||||
}
|
||||
|
||||
let mut change = AnalysisChange::new();
|
||||
|
||||
let project_folders = ProjectFolders::new(&workspaces);
|
||||
|
||||
self.proc_macro_client = match &self.config.proc_macro_srv {
|
||||
None => ProcMacroClient::dummy(),
|
||||
Some((path, args)) => match ProcMacroClient::extern_process(path.into(), args) {
|
||||
Ok(it) => it,
|
||||
Err(err) => {
|
||||
log::error!(
|
||||
"Failed to run ra_proc_macro_srv from path {}, error: {:?}",
|
||||
path.display(),
|
||||
err
|
||||
);
|
||||
ProcMacroClient::dummy()
|
||||
}
|
||||
},
|
||||
};
|
||||
let watch = match self.config.files.watcher {
|
||||
FilesWatcher::Client => vec![],
|
||||
FilesWatcher::Notify => project_folders.watch,
|
||||
};
|
||||
self.loader.set_config(vfs::loader::Config { load: project_folders.load, watch });
|
||||
|
||||
// Create crate graph from all the workspaces
|
||||
let crate_graph = {
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
let vfs = &mut self.vfs.write().0;
|
||||
let loader = &mut self.loader;
|
||||
let mut load = |path: &AbsPath| {
|
||||
let contents = loader.load_sync(path);
|
||||
let path = vfs::VfsPath::from(path.to_path_buf());
|
||||
vfs.set_file_contents(path.clone(), contents);
|
||||
vfs.file_id(&path)
|
||||
};
|
||||
for ws in workspaces.iter() {
|
||||
crate_graph.extend(ws.to_crate_graph(
|
||||
self.config.cargo.target.as_deref(),
|
||||
&self.proc_macro_client,
|
||||
&mut load,
|
||||
));
|
||||
}
|
||||
|
||||
crate_graph
|
||||
};
|
||||
change.set_crate_graph(crate_graph);
|
||||
|
||||
self.source_root_config = project_folders.source_root_config;
|
||||
self.workspaces = Arc::new(workspaces);
|
||||
|
||||
self.analysis_host.apply_change(change);
|
||||
self.process_changes();
|
||||
self.reload_flycheck();
|
||||
}
|
||||
|
||||
fn reload_flycheck(&mut self) {
|
||||
let config = match self.config.flycheck.clone() {
|
||||
Some(it) => it,
|
||||
None => {
|
||||
self.flycheck = None;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// FIXME: Figure out the multi-workspace situation
|
||||
self.flycheck = self.workspaces.iter().find_map(move |w| match w {
|
||||
ProjectWorkspace::Cargo { cargo, .. } => {
|
||||
let (sender, receiver) = unbounded();
|
||||
let sender = Box::new(move |msg| sender.send(msg).unwrap());
|
||||
let cargo_project_root = cargo.workspace_root().to_path_buf();
|
||||
let flycheck =
|
||||
FlycheckHandle::spawn(sender, config.clone(), cargo_project_root.into());
|
||||
Some((flycheck, receiver))
|
||||
}
|
||||
ProjectWorkspace::Json { .. } => {
|
||||
log::warn!("Cargo check watching only supported for cargo workspaces, disabling");
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct ProjectFolders {
|
||||
pub(crate) load: Vec<vfs::loader::Entry>,
|
||||
pub(crate) watch: Vec<usize>,
|
||||
pub(crate) source_root_config: SourceRootConfig,
|
||||
}
|
||||
|
||||
impl ProjectFolders {
|
||||
pub(crate) fn new(workspaces: &[ProjectWorkspace]) -> ProjectFolders {
|
||||
let mut res = ProjectFolders::default();
|
||||
let mut fsc = FileSetConfig::builder();
|
||||
let mut local_filesets = vec![];
|
||||
|
||||
for root in workspaces.iter().flat_map(|it| it.to_roots()) {
|
||||
let path = root.path().to_owned();
|
||||
|
||||
let mut file_set_roots: Vec<VfsPath> = vec![];
|
||||
|
||||
let entry = if root.is_member() {
|
||||
vfs::loader::Entry::local_cargo_package(path.to_path_buf())
|
||||
} else {
|
||||
vfs::loader::Entry::cargo_package_dependency(path.to_path_buf())
|
||||
};
|
||||
res.load.push(entry);
|
||||
if root.is_member() {
|
||||
res.watch.push(res.load.len() - 1);
|
||||
}
|
||||
|
||||
if let Some(out_dir) = root.out_dir() {
|
||||
let out_dir = out_dir.to_path_buf();
|
||||
res.load.push(vfs::loader::Entry::rs_files_recursively(out_dir.clone()));
|
||||
if root.is_member() {
|
||||
res.watch.push(res.load.len() - 1);
|
||||
}
|
||||
file_set_roots.push(out_dir.into());
|
||||
}
|
||||
file_set_roots.push(path.to_path_buf().into());
|
||||
|
||||
if root.is_member() {
|
||||
local_filesets.push(fsc.len());
|
||||
}
|
||||
fsc.add_file_set(file_set_roots)
|
||||
}
|
||||
|
||||
let fsc = fsc.build();
|
||||
res.source_root_config = SourceRootConfig { fsc, local_filesets };
|
||||
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub(crate) struct SourceRootConfig {
|
||||
pub(crate) fsc: FileSetConfig,
|
||||
pub(crate) local_filesets: Vec<usize>,
|
||||
}
|
||||
|
||||
impl SourceRootConfig {
|
||||
pub(crate) fn partition(&self, vfs: &vfs::Vfs) -> Vec<SourceRoot> {
|
||||
self.fsc
|
||||
.partition(vfs)
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(idx, file_set)| {
|
||||
let is_local = self.local_filesets.contains(&idx);
|
||||
if is_local {
|
||||
SourceRoot::new_local(file_set)
|
||||
} else {
|
||||
SourceRoot::new_library(file_set)
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user