2020-06-25 16:44:58 -05:00
|
|
|
//! Project loading & configuration updates
|
2020-06-26 10:28:04 -05:00
|
|
|
use std::{mem, sync::Arc};
|
2020-06-25 16:44:58 -05:00
|
|
|
|
2020-09-15 20:51:57 -05:00
|
|
|
use flycheck::{FlycheckConfig, FlycheckHandle};
|
2020-10-02 08:45:09 -05:00
|
|
|
use ide::Change;
|
2020-10-24 03:39:57 -05:00
|
|
|
use ide_db::base_db::{CrateGraph, SourceRoot, VfsPath};
|
2020-08-13 05:05:30 -05:00
|
|
|
use project_model::{ProcMacroClient, ProjectWorkspace};
|
2020-07-10 11:48:39 -05:00
|
|
|
use vfs::{file_set::FileSetConfig, AbsPath, AbsPathBuf, ChangeKind};
|
2020-06-25 16:44:58 -05:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
config::{Config, FilesWatcher, LinkedProject},
|
2020-07-15 07:37:44 -05:00
|
|
|
global_state::{GlobalState, Status},
|
2020-07-10 08:27:34 -05:00
|
|
|
lsp_ext,
|
2020-07-02 09:47:42 -05:00
|
|
|
main_loop::Task,
|
2020-06-25 16:44:58 -05:00
|
|
|
};
|
2020-08-17 06:56:27 -05:00
|
|
|
use lsp_ext::StatusParams;
|
2020-06-25 16:44:58 -05:00
|
|
|
|
2021-01-07 11:08:46 -06:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) enum ProjectWorkspaceProgress {
|
|
|
|
Begin,
|
|
|
|
Report(String),
|
2021-01-10 09:02:02 -06:00
|
|
|
End(Vec<anyhow::Result<ProjectWorkspace>>),
|
2021-01-07 11:08:46 -06:00
|
|
|
}
|
|
|
|
|
2020-06-25 16:44:58 -05:00
|
|
|
impl GlobalState {
|
2020-06-26 10:28:04 -05:00
|
|
|
pub(crate) fn update_configuration(&mut self, config: Config) {
|
2020-08-12 09:32:36 -05:00
|
|
|
let _p = profile::span("GlobalState::update_configuration");
|
2021-01-06 06:46:31 -06:00
|
|
|
let old_config = mem::replace(&mut self.config, Arc::new(config));
|
2021-01-06 04:54:28 -06:00
|
|
|
if self.config.lru_capacity() != old_config.lru_capacity() {
|
|
|
|
self.analysis_host.update_lru_capacity(self.config.lru_capacity());
|
2020-06-26 10:28:04 -05:00
|
|
|
}
|
2021-01-06 04:54:28 -06:00
|
|
|
if self.config.linked_projects() != old_config.linked_projects() {
|
2021-01-10 09:02:02 -06:00
|
|
|
self.fetch_workspaces_request()
|
2021-01-06 04:54:28 -06:00
|
|
|
} else if self.config.flycheck() != old_config.flycheck() {
|
2020-06-25 16:44:58 -05:00
|
|
|
self.reload_flycheck();
|
|
|
|
}
|
|
|
|
}
|
2020-07-10 11:48:39 -05:00
|
|
|
pub(crate) fn maybe_refresh(&mut self, changes: &[(AbsPathBuf, ChangeKind)]) {
|
|
|
|
if !changes.iter().any(|(path, kind)| is_interesting(path, *kind)) {
|
2020-07-10 08:27:34 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
match self.status {
|
|
|
|
Status::Loading | Status::NeedsReload => return,
|
|
|
|
Status::Ready | Status::Invalid => (),
|
|
|
|
}
|
2021-01-06 04:54:28 -06:00
|
|
|
if self.config.cargo_autoreload() {
|
2021-01-10 09:02:02 -06:00
|
|
|
self.fetch_workspaces_request();
|
2020-07-10 08:27:34 -05:00
|
|
|
} else {
|
|
|
|
self.transition(Status::NeedsReload);
|
|
|
|
}
|
2020-07-10 11:48:39 -05:00
|
|
|
|
|
|
|
fn is_interesting(path: &AbsPath, change_kind: ChangeKind) -> bool {
|
|
|
|
const IMPLICIT_TARGET_FILES: &[&str] = &["build.rs", "src/main.rs", "src/lib.rs"];
|
|
|
|
const IMPLICIT_TARGET_DIRS: &[&str] = &["src/bin", "examples", "tests", "benches"];
|
|
|
|
|
|
|
|
if path.ends_with("Cargo.toml") || path.ends_with("Cargo.lock") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if change_kind == ChangeKind::Modify {
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-10 16:57:10 -05:00
|
|
|
if path.extension().unwrap_or_default() != "rs" {
|
2020-07-10 11:48:39 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if IMPLICIT_TARGET_FILES.iter().any(|it| path.ends_with(it)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
let parent = match path.parent() {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return false,
|
|
|
|
};
|
|
|
|
if IMPLICIT_TARGET_DIRS.iter().any(|it| parent.ends_with(it)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if path.ends_with("main.rs") {
|
|
|
|
let grand_parent = match parent.parent() {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return false,
|
|
|
|
};
|
|
|
|
if IMPLICIT_TARGET_DIRS.iter().any(|it| grand_parent.ends_with(it)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2020-07-10 08:27:34 -05:00
|
|
|
}
|
|
|
|
pub(crate) fn transition(&mut self, new_status: Status) {
|
|
|
|
self.status = new_status;
|
2021-01-05 07:57:05 -06:00
|
|
|
if self.config.status_notification() {
|
2020-07-10 08:27:34 -05:00
|
|
|
let lsp_status = match new_status {
|
|
|
|
Status::Loading => lsp_ext::Status::Loading,
|
|
|
|
Status::Ready => lsp_ext::Status::Ready,
|
|
|
|
Status::Invalid => lsp_ext::Status::Invalid,
|
|
|
|
Status::NeedsReload => lsp_ext::Status::NeedsReload,
|
|
|
|
};
|
2020-08-17 06:56:27 -05:00
|
|
|
self.send_notification::<lsp_ext::StatusNotification>(StatusParams {
|
|
|
|
status: lsp_status,
|
|
|
|
});
|
2020-07-10 08:27:34 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-10 09:02:02 -06:00
|
|
|
|
|
|
|
pub(crate) fn fetch_workspaces_request(&mut self) {
|
|
|
|
self.fetch_workspaces_queue.request_op()
|
|
|
|
}
|
|
|
|
pub(crate) fn fetch_workspaces_if_needed(&mut self) {
|
|
|
|
if !self.fetch_workspaces_queue.should_start_op() {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-28 08:04:44 -06:00
|
|
|
log::info!("will fetch workspaces");
|
2021-01-07 11:08:46 -06:00
|
|
|
|
|
|
|
self.task_pool.handle.spawn_with_sender({
|
2021-01-06 04:54:28 -06:00
|
|
|
let linked_projects = self.config.linked_projects();
|
|
|
|
let cargo_config = self.config.cargo();
|
2021-01-07 11:08:46 -06:00
|
|
|
|
|
|
|
move |sender| {
|
|
|
|
let progress = {
|
|
|
|
let sender = sender.clone();
|
|
|
|
move |msg| {
|
|
|
|
sender
|
|
|
|
.send(Task::FetchWorkspace(ProjectWorkspaceProgress::Report(msg)))
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
sender.send(Task::FetchWorkspace(ProjectWorkspaceProgress::Begin)).unwrap();
|
|
|
|
|
2020-07-02 09:47:42 -05:00
|
|
|
let workspaces = linked_projects
|
|
|
|
.iter()
|
|
|
|
.map(|project| match project {
|
|
|
|
LinkedProject::ProjectManifest(manifest) => {
|
2021-01-07 11:08:46 -06:00
|
|
|
project_model::ProjectWorkspace::load(
|
|
|
|
manifest.clone(),
|
|
|
|
&cargo_config,
|
|
|
|
&progress,
|
|
|
|
)
|
2020-07-02 09:47:42 -05:00
|
|
|
}
|
|
|
|
LinkedProject::InlineJsonProject(it) => {
|
2021-01-18 05:52:12 -06:00
|
|
|
project_model::ProjectWorkspace::load_inline(
|
|
|
|
it.clone(),
|
|
|
|
cargo_config.target.as_deref(),
|
|
|
|
)
|
2020-07-02 09:47:42 -05:00
|
|
|
}
|
2020-06-26 09:33:57 -05:00
|
|
|
})
|
2020-07-02 09:47:42 -05:00
|
|
|
.collect::<Vec<_>>();
|
2021-01-07 11:08:46 -06:00
|
|
|
|
2020-08-25 04:27:22 -05:00
|
|
|
log::info!("did fetch workspaces {:?}", workspaces);
|
2021-01-10 09:02:02 -06:00
|
|
|
sender
|
|
|
|
.send(Task::FetchWorkspace(ProjectWorkspaceProgress::End(workspaces)))
|
|
|
|
.unwrap();
|
2020-07-02 09:47:42 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-01-10 09:02:02 -06:00
|
|
|
pub(crate) fn fetch_workspaces_completed(&mut self) {
|
|
|
|
self.fetch_workspaces_queue.op_completed()
|
|
|
|
}
|
|
|
|
|
2020-07-02 09:47:42 -05:00
|
|
|
pub(crate) fn switch_workspaces(&mut self, workspaces: Vec<anyhow::Result<ProjectWorkspace>>) {
|
2020-08-12 09:32:36 -05:00
|
|
|
let _p = profile::span("GlobalState::switch_workspaces");
|
2020-08-25 04:27:22 -05:00
|
|
|
log::info!("will switch workspaces: {:?}", workspaces);
|
2020-07-10 08:27:34 -05:00
|
|
|
|
|
|
|
let mut has_errors = false;
|
2020-07-02 09:47:42 -05:00
|
|
|
let workspaces = workspaces
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|res| {
|
|
|
|
res.map_err(|err| {
|
2020-07-10 08:27:34 -05:00
|
|
|
has_errors = true;
|
2020-07-02 09:47:42 -05:00
|
|
|
log::error!("failed to load workspace: {:#}", err);
|
2020-07-10 18:39:06 -05:00
|
|
|
if self.workspaces.is_empty() {
|
|
|
|
self.show_message(
|
|
|
|
lsp_types::MessageType::Error,
|
|
|
|
format!("rust-analyzer failed to load workspace: {:#}", err),
|
|
|
|
);
|
|
|
|
}
|
2020-06-26 09:33:57 -05:00
|
|
|
})
|
2020-07-02 09:47:42 -05:00
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2020-06-25 16:44:58 -05:00
|
|
|
|
2020-07-10 08:27:34 -05:00
|
|
|
if &*self.workspaces == &workspaces {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.workspaces.is_empty() && has_errors {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-06 04:54:28 -06:00
|
|
|
if let FilesWatcher::Client = self.config.files().watcher {
|
2021-01-10 13:38:35 -06:00
|
|
|
if self.config.did_change_watched_files_dynamic_registration() {
|
|
|
|
let registration_options = lsp_types::DidChangeWatchedFilesRegistrationOptions {
|
|
|
|
watchers: workspaces
|
|
|
|
.iter()
|
|
|
|
.flat_map(ProjectWorkspace::to_roots)
|
|
|
|
.filter(|it| it.is_member)
|
|
|
|
.flat_map(|root| {
|
|
|
|
root.include.into_iter().map(|it| format!("{}/**/*.rs", it.display()))
|
|
|
|
})
|
|
|
|
.map(|glob_pattern| lsp_types::FileSystemWatcher {
|
|
|
|
glob_pattern,
|
|
|
|
kind: None,
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
};
|
|
|
|
let registration = lsp_types::Registration {
|
|
|
|
id: "workspace/didChangeWatchedFiles".to_string(),
|
|
|
|
method: "workspace/didChangeWatchedFiles".to_string(),
|
|
|
|
register_options: Some(serde_json::to_value(registration_options).unwrap()),
|
|
|
|
};
|
|
|
|
self.send_request::<lsp_types::request::RegisterCapability>(
|
|
|
|
lsp_types::RegistrationParams { registrations: vec![registration] },
|
|
|
|
|_, _| (),
|
|
|
|
);
|
|
|
|
}
|
2020-06-25 16:44:58 -05:00
|
|
|
}
|
|
|
|
|
2020-10-02 08:45:09 -05:00
|
|
|
let mut change = Change::new();
|
2020-06-25 16:44:58 -05:00
|
|
|
|
2021-01-26 07:18:01 -06:00
|
|
|
let files_config = self.config.files();
|
|
|
|
let project_folders = ProjectFolders::new(&workspaces, &files_config.exclude);
|
2020-06-25 16:44:58 -05:00
|
|
|
|
2021-01-06 04:54:28 -06:00
|
|
|
self.proc_macro_client = match self.config.proc_macro_srv() {
|
2020-12-07 10:16:50 -06:00
|
|
|
None => None,
|
2021-01-06 04:54:28 -06:00
|
|
|
Some((path, args)) => match ProcMacroClient::extern_process(path.clone(), args) {
|
2020-12-07 10:16:50 -06:00
|
|
|
Ok(it) => Some(it),
|
2020-06-25 16:44:58 -05:00
|
|
|
Err(err) => {
|
|
|
|
log::error!(
|
2020-08-12 19:57:26 -05:00
|
|
|
"Failed to run proc_macro_srv from path {}, error: {:?}",
|
2020-06-25 16:44:58 -05:00
|
|
|
path.display(),
|
|
|
|
err
|
|
|
|
);
|
2020-12-07 10:16:50 -06:00
|
|
|
None
|
2020-06-25 16:44:58 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2020-07-10 16:39:25 -05:00
|
|
|
|
2021-01-26 07:18:01 -06:00
|
|
|
let watch = match files_config.watcher {
|
2020-06-25 16:44:58 -05:00
|
|
|
FilesWatcher::Client => vec![],
|
|
|
|
FilesWatcher::Notify => project_folders.watch,
|
|
|
|
};
|
2020-06-25 17:27:39 -05:00
|
|
|
self.loader.handle.set_config(vfs::loader::Config { load: project_folders.load, watch });
|
2020-06-25 16:44:58 -05:00
|
|
|
|
|
|
|
// 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;
|
2020-09-18 14:15:44 -05:00
|
|
|
let mem_docs = &self.mem_docs;
|
2020-06-25 16:44:58 -05:00
|
|
|
let mut load = |path: &AbsPath| {
|
2020-09-18 14:15:44 -05:00
|
|
|
let vfs_path = vfs::VfsPath::from(path.to_path_buf());
|
|
|
|
if !mem_docs.contains_key(&vfs_path) {
|
|
|
|
let contents = loader.handle.load_sync(path);
|
|
|
|
vfs.set_file_contents(vfs_path.clone(), contents);
|
|
|
|
}
|
2020-11-17 05:17:24 -06:00
|
|
|
let res = vfs.file_id(&vfs_path);
|
|
|
|
if res.is_none() {
|
2020-11-27 11:35:22 -06:00
|
|
|
log::warn!("failed to load {}", path.display())
|
2020-11-17 05:17:24 -06:00
|
|
|
}
|
|
|
|
res
|
2020-06-25 16:44:58 -05:00
|
|
|
};
|
|
|
|
for ws in workspaces.iter() {
|
2021-01-18 05:52:12 -06:00
|
|
|
crate_graph.extend(ws.to_crate_graph(self.proc_macro_client.as_ref(), &mut load));
|
2020-06-25 16:44:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2020-08-25 04:27:22 -05:00
|
|
|
log::info!("did switch workspaces");
|
2020-06-25 16:44:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn reload_flycheck(&mut self) {
|
2021-01-18 04:25:57 -06:00
|
|
|
let _p = profile::span("GlobalState::reload_flycheck");
|
2021-01-06 04:54:28 -06:00
|
|
|
let config = match self.config.flycheck() {
|
2020-06-25 16:44:58 -05:00
|
|
|
Some(it) => it,
|
|
|
|
None => {
|
2020-09-17 11:50:30 -05:00
|
|
|
self.flycheck = Vec::new();
|
2020-06-25 16:44:58 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-15 07:37:44 -05:00
|
|
|
let sender = self.flycheck_sender.clone();
|
|
|
|
self.flycheck = self
|
|
|
|
.workspaces
|
|
|
|
.iter()
|
2020-09-17 11:50:30 -05:00
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(id, w)| match w {
|
2021-01-18 05:52:12 -06:00
|
|
|
ProjectWorkspace::Cargo { cargo, .. } => Some((id, cargo.workspace_root())),
|
2020-09-15 20:51:57 -05:00
|
|
|
ProjectWorkspace::Json { project, .. } => {
|
|
|
|
// Enable flychecks for json projects if a custom flycheck command was supplied
|
|
|
|
// in the workspace configuration.
|
|
|
|
match config {
|
2020-09-17 11:50:30 -05:00
|
|
|
FlycheckConfig::CustomCommand { .. } => Some((id, project.path())),
|
2020-09-15 20:51:57 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 07:37:44 -05:00
|
|
|
})
|
2020-09-17 11:50:30 -05:00
|
|
|
.map(|(id, root)| {
|
|
|
|
let sender = sender.clone();
|
|
|
|
FlycheckHandle::spawn(
|
|
|
|
id,
|
|
|
|
Box::new(move |msg| sender.send(msg).unwrap()),
|
|
|
|
config.clone(),
|
|
|
|
root.to_path_buf().into(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
2020-06-25 16:44:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 {
|
2021-01-26 07:18:01 -06:00
|
|
|
pub(crate) fn new(
|
|
|
|
workspaces: &[ProjectWorkspace],
|
|
|
|
global_excludes: &[AbsPathBuf],
|
|
|
|
) -> ProjectFolders {
|
2020-06-25 16:44:58 -05:00
|
|
|
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()) {
|
2020-07-21 05:52:51 -05:00
|
|
|
let file_set_roots: Vec<VfsPath> =
|
|
|
|
root.include.iter().cloned().map(VfsPath::from).collect();
|
2020-06-25 16:44:58 -05:00
|
|
|
|
2020-07-21 05:52:51 -05:00
|
|
|
let entry = {
|
|
|
|
let mut dirs = vfs::loader::Directories::default();
|
|
|
|
dirs.extensions.push("rs".into());
|
|
|
|
dirs.include.extend(root.include);
|
|
|
|
dirs.exclude.extend(root.exclude);
|
2021-01-26 07:18:01 -06:00
|
|
|
for excl in global_excludes {
|
|
|
|
if dirs.include.iter().any(|incl| incl.starts_with(excl)) {
|
|
|
|
dirs.exclude.push(excl.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 05:52:51 -05:00
|
|
|
vfs::loader::Entry::Directories(dirs)
|
2020-06-25 16:44:58 -05:00
|
|
|
};
|
|
|
|
|
2020-07-21 05:52:51 -05:00
|
|
|
if root.is_member {
|
|
|
|
res.watch.push(res.load.len());
|
2020-06-25 16:44:58 -05:00
|
|
|
}
|
2020-07-21 05:52:51 -05:00
|
|
|
res.load.push(entry);
|
2020-06-25 16:44:58 -05:00
|
|
|
|
2020-07-21 05:52:51 -05:00
|
|
|
if root.is_member {
|
2020-06-25 16:44:58 -05:00
|
|
|
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> {
|
2020-08-12 09:32:36 -05:00
|
|
|
let _p = profile::span("SourceRootConfig::partition");
|
2020-06-25 16:44:58 -05:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|