2018-08-17 11:54:08 -05:00
|
|
|
use std::{
|
2019-01-08 13:33:36 -06:00
|
|
|
path::PathBuf,
|
2018-09-02 06:46:15 -05:00
|
|
|
sync::Arc,
|
2018-08-17 11:54:08 -05:00
|
|
|
};
|
|
|
|
|
2019-01-14 04:55:56 -06:00
|
|
|
use lsp_types::Url;
|
2019-01-08 13:33:36 -06:00
|
|
|
use ra_ide_api::{
|
2018-12-19 03:48:34 -06:00
|
|
|
Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData,
|
2018-12-19 06:04:15 -06:00
|
|
|
SourceRootId
|
2018-10-31 15:41:43 -05:00
|
|
|
};
|
2018-12-21 03:18:14 -06:00
|
|
|
use ra_vfs::{Vfs, VfsChange, VfsFile, VfsRoot};
|
2018-10-15 16:44:23 -05:00
|
|
|
use rustc_hash::FxHashMap;
|
2018-12-19 06:04:15 -06:00
|
|
|
use relative_path::RelativePathBuf;
|
|
|
|
use parking_lot::RwLock;
|
2019-01-08 13:33:36 -06:00
|
|
|
use failure::format_err;
|
2018-08-17 11:54:08 -05:00
|
|
|
|
2018-10-15 12:15:53 -05:00
|
|
|
use crate::{
|
2019-01-10 11:13:08 -06:00
|
|
|
project_model::{ProjectWorkspace, TargetKind},
|
2018-10-15 16:44:23 -05:00
|
|
|
Result,
|
2018-08-17 11:54:08 -05:00
|
|
|
};
|
|
|
|
|
2018-12-19 06:04:15 -06:00
|
|
|
#[derive(Debug)]
|
2018-08-17 11:54:08 -05:00
|
|
|
pub struct ServerWorldState {
|
2018-12-19 06:40:42 -06:00
|
|
|
pub roots_to_scan: usize,
|
|
|
|
pub root: PathBuf,
|
2019-01-10 11:13:08 -06:00
|
|
|
pub workspaces: Arc<Vec<ProjectWorkspace>>,
|
2018-08-30 04:51:46 -05:00
|
|
|
pub analysis_host: AnalysisHost,
|
2018-12-19 06:04:15 -06:00
|
|
|
pub vfs: Arc<RwLock<Vfs>>,
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ServerWorld {
|
2019-01-10 11:13:08 -06:00
|
|
|
pub workspaces: Arc<Vec<ProjectWorkspace>>,
|
2018-08-29 10:03:14 -05:00
|
|
|
pub analysis: Analysis,
|
2018-12-19 06:04:15 -06:00
|
|
|
pub vfs: Arc<RwLock<Vfs>>,
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ServerWorldState {
|
2019-01-10 11:13:08 -06:00
|
|
|
pub fn new(root: PathBuf, workspaces: Vec<ProjectWorkspace>) -> ServerWorldState {
|
2018-10-25 02:57:55 -05:00
|
|
|
let mut change = AnalysisChange::new();
|
2018-08-17 11:54:08 -05:00
|
|
|
|
2018-12-19 06:04:15 -06:00
|
|
|
let mut roots = Vec::new();
|
2018-12-19 06:40:42 -06:00
|
|
|
roots.push(root.clone());
|
2018-12-19 06:04:15 -06:00
|
|
|
for ws in workspaces.iter() {
|
2019-01-10 11:13:08 -06:00
|
|
|
for pkg in ws.cargo.packages() {
|
|
|
|
roots.push(pkg.root(&ws.cargo).to_path_buf());
|
2018-10-25 02:57:55 -05:00
|
|
|
}
|
2019-01-10 15:37:10 -06:00
|
|
|
for krate in ws.sysroot.crates() {
|
|
|
|
roots.push(krate.root_dir(&ws.sysroot).to_path_buf())
|
|
|
|
}
|
2018-09-04 03:40:45 -05:00
|
|
|
}
|
2019-01-10 15:37:10 -06:00
|
|
|
roots.sort();
|
|
|
|
roots.dedup();
|
2018-12-19 06:40:42 -06:00
|
|
|
let roots_to_scan = roots.len();
|
2018-12-19 06:04:15 -06:00
|
|
|
let (mut vfs, roots) = Vfs::new(roots);
|
|
|
|
for r in roots {
|
2018-12-19 07:19:53 -06:00
|
|
|
let is_local = vfs.root2path(r).starts_with(&root);
|
2019-01-04 07:01:06 -06:00
|
|
|
change.add_root(SourceRootId(r.0.into()), is_local);
|
2018-09-04 03:40:45 -05:00
|
|
|
}
|
2018-08-17 11:54:08 -05:00
|
|
|
|
2018-11-27 18:25:20 -06:00
|
|
|
let mut crate_graph = CrateGraph::default();
|
2018-12-19 06:04:15 -06:00
|
|
|
for ws in workspaces.iter() {
|
2019-01-10 15:37:10 -06:00
|
|
|
// First, load std
|
|
|
|
let mut sysroot_crates = FxHashMap::default();
|
|
|
|
for krate in ws.sysroot.crates() {
|
|
|
|
if let Some(file_id) = vfs.load(krate.root(&ws.sysroot)) {
|
|
|
|
let file_id = FileId(file_id.0.into());
|
|
|
|
sysroot_crates.insert(krate, crate_graph.add_crate_root(file_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for from in ws.sysroot.crates() {
|
|
|
|
for to in from.deps(&ws.sysroot) {
|
|
|
|
let name = to.name(&ws.sysroot);
|
|
|
|
if let (Some(&from), Some(&to)) =
|
|
|
|
(sysroot_crates.get(&from), sysroot_crates.get(&to))
|
|
|
|
{
|
2019-01-13 03:27:26 -06:00
|
|
|
if let Err(_) = crate_graph.add_dep(from, name.clone(), to) {
|
|
|
|
log::error!("cyclic dependency between sysroot crates")
|
|
|
|
}
|
2019-01-10 15:37:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let libstd = ws
|
|
|
|
.sysroot
|
|
|
|
.std()
|
|
|
|
.and_then(|it| sysroot_crates.get(&it).map(|&it| it));
|
|
|
|
|
|
|
|
let mut pkg_to_lib_crate = FxHashMap::default();
|
|
|
|
let mut pkg_crates = FxHashMap::default();
|
|
|
|
// Next, create crates for each package, target pair
|
2019-01-10 11:13:08 -06:00
|
|
|
for pkg in ws.cargo.packages() {
|
2019-01-10 15:37:10 -06:00
|
|
|
let mut lib_tgt = None;
|
2019-01-10 11:13:08 -06:00
|
|
|
for tgt in pkg.targets(&ws.cargo) {
|
|
|
|
let root = tgt.root(&ws.cargo);
|
2018-12-19 06:04:15 -06:00
|
|
|
if let Some(file_id) = vfs.load(root) {
|
2019-01-04 07:01:06 -06:00
|
|
|
let file_id = FileId(file_id.0.into());
|
2018-12-08 14:16:11 -06:00
|
|
|
let crate_id = crate_graph.add_crate_root(file_id);
|
2019-01-10 11:13:08 -06:00
|
|
|
if tgt.kind(&ws.cargo) == TargetKind::Lib {
|
2019-01-10 15:37:10 -06:00
|
|
|
lib_tgt = Some(crate_id);
|
2018-12-08 14:16:11 -06:00
|
|
|
pkg_to_lib_crate.insert(pkg, crate_id);
|
|
|
|
}
|
|
|
|
pkg_crates
|
|
|
|
.entry(pkg)
|
|
|
|
.or_insert_with(Vec::new)
|
|
|
|
.push(crate_id);
|
|
|
|
}
|
|
|
|
}
|
2019-01-10 15:37:10 -06:00
|
|
|
|
|
|
|
// Set deps to the std and to the lib target of the current package
|
|
|
|
for &from in pkg_crates.get(&pkg).into_iter().flatten() {
|
|
|
|
if let Some(to) = lib_tgt {
|
|
|
|
if to != from {
|
2019-01-13 03:27:26 -06:00
|
|
|
if let Err(_) =
|
|
|
|
crate_graph.add_dep(from, pkg.name(&ws.cargo).into(), to)
|
|
|
|
{
|
|
|
|
log::error!(
|
|
|
|
"cyclic dependency between targets of {}",
|
|
|
|
pkg.name(&ws.cargo)
|
|
|
|
)
|
|
|
|
}
|
2019-01-10 15:37:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(std) = libstd {
|
2019-01-13 03:27:26 -06:00
|
|
|
if let Err(_) = crate_graph.add_dep(from, "std".into(), std) {
|
|
|
|
log::error!("cyclic dependency on std for {}", pkg.name(&ws.cargo))
|
|
|
|
}
|
2019-01-10 15:37:10 -06:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 14:16:11 -06:00
|
|
|
}
|
2019-01-10 15:37:10 -06:00
|
|
|
|
|
|
|
// Now add a dep ednge from all targets of upstream to the lib
|
|
|
|
// target of downstream.
|
2019-01-10 11:13:08 -06:00
|
|
|
for pkg in ws.cargo.packages() {
|
|
|
|
for dep in pkg.dependencies(&ws.cargo) {
|
2018-12-08 16:02:53 -06:00
|
|
|
if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
|
2018-12-08 14:16:11 -06:00
|
|
|
for &from in pkg_crates.get(&pkg).into_iter().flatten() {
|
2019-01-13 03:27:26 -06:00
|
|
|
if let Err(_) = crate_graph.add_dep(from, dep.name.clone(), to) {
|
|
|
|
log::error!(
|
|
|
|
"cyclic dependency {} -> {}",
|
|
|
|
pkg.name(&ws.cargo),
|
|
|
|
dep.pkg.name(&ws.cargo)
|
|
|
|
)
|
|
|
|
}
|
2018-12-08 14:16:11 -06:00
|
|
|
}
|
|
|
|
}
|
2018-10-15 16:44:23 -05:00
|
|
|
}
|
2018-12-08 14:16:11 -06:00
|
|
|
}
|
|
|
|
}
|
2018-10-25 02:57:55 -05:00
|
|
|
change.set_crate_graph(crate_graph);
|
2018-12-19 06:04:15 -06:00
|
|
|
|
|
|
|
let mut analysis_host = AnalysisHost::default();
|
|
|
|
analysis_host.apply_change(change);
|
|
|
|
ServerWorldState {
|
2018-12-19 06:40:42 -06:00
|
|
|
roots_to_scan,
|
|
|
|
root,
|
2018-12-19 06:04:15 -06:00
|
|
|
workspaces: Arc::new(workspaces),
|
|
|
|
analysis_host,
|
|
|
|
vfs: Arc::new(RwLock::new(vfs)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a vec of libraries
|
|
|
|
/// FIXME: better API here
|
|
|
|
pub fn process_changes(
|
|
|
|
&mut self,
|
|
|
|
) -> Vec<(SourceRootId, Vec<(FileId, RelativePathBuf, Arc<String>)>)> {
|
2018-12-19 06:40:42 -06:00
|
|
|
let changes = self.vfs.write().commit_changes();
|
|
|
|
if changes.is_empty() {
|
|
|
|
return Vec::new();
|
|
|
|
}
|
2018-12-19 06:04:15 -06:00
|
|
|
let mut libs = Vec::new();
|
|
|
|
let mut change = AnalysisChange::new();
|
2018-12-19 06:40:42 -06:00
|
|
|
for c in changes {
|
2018-12-19 06:04:15 -06:00
|
|
|
match c {
|
|
|
|
VfsChange::AddRoot { root, files } => {
|
2018-12-19 06:40:42 -06:00
|
|
|
let root_path = self.vfs.read().root2path(root);
|
|
|
|
if root_path.starts_with(&self.root) {
|
|
|
|
self.roots_to_scan -= 1;
|
|
|
|
for (file, path, text) in files {
|
2019-01-04 07:01:06 -06:00
|
|
|
change.add_file(
|
|
|
|
SourceRootId(root.0.into()),
|
|
|
|
FileId(file.0.into()),
|
|
|
|
path,
|
|
|
|
text,
|
|
|
|
);
|
2018-12-19 06:40:42 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let files = files
|
|
|
|
.into_iter()
|
2019-01-04 07:01:06 -06:00
|
|
|
.map(|(vfsfile, path, text)| (FileId(vfsfile.0.into()), path, text))
|
2018-12-19 06:40:42 -06:00
|
|
|
.collect();
|
2019-01-04 07:01:06 -06:00
|
|
|
libs.push((SourceRootId(root.0.into()), files));
|
2018-12-19 06:40:42 -06:00
|
|
|
}
|
2018-12-19 06:04:15 -06:00
|
|
|
}
|
|
|
|
VfsChange::AddFile {
|
|
|
|
root,
|
|
|
|
file,
|
|
|
|
path,
|
|
|
|
text,
|
|
|
|
} => {
|
2019-01-04 07:01:06 -06:00
|
|
|
change.add_file(
|
|
|
|
SourceRootId(root.0.into()),
|
|
|
|
FileId(file.0.into()),
|
|
|
|
path,
|
|
|
|
text,
|
|
|
|
);
|
2018-12-19 06:04:15 -06:00
|
|
|
}
|
|
|
|
VfsChange::RemoveFile { root, file, path } => {
|
2019-01-04 07:01:06 -06:00
|
|
|
change.remove_file(SourceRootId(root.0.into()), FileId(file.0.into()), path)
|
2018-12-19 06:04:15 -06:00
|
|
|
}
|
|
|
|
VfsChange::ChangeFile { file, text } => {
|
2019-01-04 07:01:06 -06:00
|
|
|
change.change_file(FileId(file.0.into()), text);
|
2018-12-19 06:04:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-25 02:57:55 -05:00
|
|
|
self.analysis_host.apply_change(change);
|
2018-12-19 06:04:15 -06:00
|
|
|
libs
|
2018-09-02 06:46:15 -05:00
|
|
|
}
|
2018-12-19 06:04:15 -06:00
|
|
|
|
|
|
|
pub fn add_lib(&mut self, data: LibraryData) {
|
2018-12-19 06:40:42 -06:00
|
|
|
self.roots_to_scan -= 1;
|
2018-12-19 06:04:15 -06:00
|
|
|
let mut change = AnalysisChange::new();
|
|
|
|
change.add_library(data);
|
|
|
|
self.analysis_host.apply_change(change);
|
|
|
|
}
|
|
|
|
|
2018-08-21 14:24:59 -05:00
|
|
|
pub fn snapshot(&self) -> ServerWorld {
|
2018-08-17 11:54:08 -05:00
|
|
|
ServerWorld {
|
2018-09-02 06:46:15 -05:00
|
|
|
workspaces: Arc::clone(&self.workspaces),
|
2018-09-10 04:57:40 -05:00
|
|
|
analysis: self.analysis_host.analysis(),
|
2018-12-19 06:04:15 -06:00
|
|
|
vfs: Arc::clone(&self.vfs),
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
|
|
|
}
|
2019-01-25 10:11:58 -06:00
|
|
|
|
2019-01-26 11:33:33 -06:00
|
|
|
pub fn maybe_collect_garbage(&mut self) {
|
|
|
|
self.analysis_host.maybe_collect_garbage()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn collect_garbage(&mut self) {
|
2019-01-25 10:11:58 -06:00
|
|
|
self.analysis_host.collect_garbage()
|
|
|
|
}
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ServerWorld {
|
2018-08-29 10:03:14 -05:00
|
|
|
pub fn analysis(&self) -> &Analysis {
|
2018-08-17 11:54:08 -05:00
|
|
|
&self.analysis
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn uri_to_file_id(&self, uri: &Url) -> Result<FileId> {
|
2018-10-15 16:44:23 -05:00
|
|
|
let path = uri
|
|
|
|
.to_file_path()
|
2018-08-17 11:54:08 -05:00
|
|
|
.map_err(|()| format_err!("invalid uri: {}", uri))?;
|
2018-12-19 06:04:15 -06:00
|
|
|
let file = self
|
|
|
|
.vfs
|
|
|
|
.read()
|
|
|
|
.path2file(&path)
|
|
|
|
.ok_or_else(|| format_err!("unknown file: {}", path.display()))?;
|
2019-01-04 07:01:06 -06:00
|
|
|
Ok(FileId(file.0.into()))
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> {
|
2019-01-04 07:01:06 -06:00
|
|
|
let path = self.vfs.read().file2path(VfsFile(id.0.into()));
|
2018-12-19 06:04:15 -06:00
|
|
|
let url = Url::from_file_path(&path)
|
|
|
|
.map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;
|
2018-08-17 11:54:08 -05:00
|
|
|
Ok(url)
|
|
|
|
}
|
2018-12-21 03:18:14 -06:00
|
|
|
|
|
|
|
pub fn path_to_uri(&self, root: SourceRootId, path: &RelativePathBuf) -> Result<Url> {
|
2019-01-04 07:01:06 -06:00
|
|
|
let base = self.vfs.read().root2path(VfsRoot(root.0.into()));
|
2018-12-21 03:18:14 -06:00
|
|
|
let path = path.to_path(base);
|
|
|
|
let url = Url::from_file_path(&path)
|
|
|
|
.map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;
|
|
|
|
Ok(url)
|
|
|
|
}
|
2019-01-22 15:15:03 -06:00
|
|
|
|
|
|
|
pub fn status(&self) -> String {
|
|
|
|
let mut res = String::new();
|
|
|
|
if self.workspaces.is_empty() {
|
|
|
|
res.push_str("no workspaces\n")
|
|
|
|
} else {
|
|
|
|
res.push_str("workspaces:\n");
|
|
|
|
for w in self.workspaces.iter() {
|
|
|
|
res += &format!("{} packages loaded\n", w.cargo.packages().count());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.push_str("\nanalysis:\n");
|
|
|
|
res.push_str(&self.analysis.status());
|
|
|
|
res
|
|
|
|
}
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|