8 Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData,
11 use ra_vfs::{Vfs, VfsChange, VfsFile, VfsRoot};
12 use rustc_hash::FxHashMap;
13 use relative_path::RelativePathBuf;
14 use parking_lot::RwLock;
15 use failure::format_err;
18 project_model::{ProjectWorkspace, TargetKind},
23 pub struct ServerWorldState {
24 pub roots_to_scan: usize,
26 pub workspaces: Arc<Vec<ProjectWorkspace>>,
27 pub analysis_host: AnalysisHost,
28 pub vfs: Arc<RwLock<Vfs>>,
31 pub struct ServerWorld {
32 pub workspaces: Arc<Vec<ProjectWorkspace>>,
33 pub analysis: Analysis,
34 pub vfs: Arc<RwLock<Vfs>>,
37 impl ServerWorldState {
38 pub fn new(root: PathBuf, workspaces: Vec<ProjectWorkspace>) -> ServerWorldState {
39 let mut change = AnalysisChange::new();
41 let mut roots = Vec::new();
42 roots.push(root.clone());
43 for ws in workspaces.iter() {
44 for pkg in ws.cargo.packages() {
45 roots.push(pkg.root(&ws.cargo).to_path_buf());
47 for krate in ws.sysroot.crates() {
48 roots.push(krate.root_dir(&ws.sysroot).to_path_buf())
53 let roots_to_scan = roots.len();
54 let (mut vfs, roots) = Vfs::new(roots);
56 let is_local = vfs.root2path(r).starts_with(&root);
57 change.add_root(SourceRootId(r.0.into()), is_local);
60 let mut crate_graph = CrateGraph::default();
61 for ws in workspaces.iter() {
63 let mut sysroot_crates = FxHashMap::default();
64 for krate in ws.sysroot.crates() {
65 if let Some(file_id) = vfs.load(krate.root(&ws.sysroot)) {
66 let file_id = FileId(file_id.0.into());
67 sysroot_crates.insert(krate, crate_graph.add_crate_root(file_id));
70 for from in ws.sysroot.crates() {
71 for to in from.deps(&ws.sysroot) {
72 let name = to.name(&ws.sysroot);
73 if let (Some(&from), Some(&to)) =
74 (sysroot_crates.get(&from), sysroot_crates.get(&to))
76 if let Err(_) = crate_graph.add_dep(from, name.clone(), to) {
77 log::error!("cyclic dependency between sysroot crates")
86 .and_then(|it| sysroot_crates.get(&it).map(|&it| it));
88 let mut pkg_to_lib_crate = FxHashMap::default();
89 let mut pkg_crates = FxHashMap::default();
90 // Next, create crates for each package, target pair
91 for pkg in ws.cargo.packages() {
92 let mut lib_tgt = None;
93 for tgt in pkg.targets(&ws.cargo) {
94 let root = tgt.root(&ws.cargo);
95 if let Some(file_id) = vfs.load(root) {
96 let file_id = FileId(file_id.0.into());
97 let crate_id = crate_graph.add_crate_root(file_id);
98 if tgt.kind(&ws.cargo) == TargetKind::Lib {
99 lib_tgt = Some(crate_id);
100 pkg_to_lib_crate.insert(pkg, crate_id);
104 .or_insert_with(Vec::new)
109 // Set deps to the std and to the lib target of the current package
110 for &from in pkg_crates.get(&pkg).into_iter().flatten() {
111 if let Some(to) = lib_tgt {
114 crate_graph.add_dep(from, pkg.name(&ws.cargo).into(), to)
117 "cyclic dependency between targets of {}",
123 if let Some(std) = libstd {
124 if let Err(_) = crate_graph.add_dep(from, "std".into(), std) {
125 log::error!("cyclic dependency on std for {}", pkg.name(&ws.cargo))
131 // Now add a dep ednge from all targets of upstream to the lib
132 // target of downstream.
133 for pkg in ws.cargo.packages() {
134 for dep in pkg.dependencies(&ws.cargo) {
135 if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
136 for &from in pkg_crates.get(&pkg).into_iter().flatten() {
137 if let Err(_) = crate_graph.add_dep(from, dep.name.clone(), to) {
139 "cyclic dependency {} -> {}",
141 dep.pkg.name(&ws.cargo)
149 change.set_crate_graph(crate_graph);
151 let mut analysis_host = AnalysisHost::default();
152 analysis_host.apply_change(change);
156 workspaces: Arc::new(workspaces),
158 vfs: Arc::new(RwLock::new(vfs)),
162 /// Returns a vec of libraries
163 /// FIXME: better API here
164 pub fn process_changes(
166 ) -> Vec<(SourceRootId, Vec<(FileId, RelativePathBuf, Arc<String>)>)> {
167 let changes = self.vfs.write().commit_changes();
168 if changes.is_empty() {
171 let mut libs = Vec::new();
172 let mut change = AnalysisChange::new();
175 VfsChange::AddRoot { root, files } => {
176 let root_path = self.vfs.read().root2path(root);
177 if root_path.starts_with(&self.root) {
178 self.roots_to_scan -= 1;
179 for (file, path, text) in files {
181 SourceRootId(root.0.into()),
182 FileId(file.0.into()),
190 .map(|(vfsfile, path, text)| (FileId(vfsfile.0.into()), path, text))
192 libs.push((SourceRootId(root.0.into()), files));
202 SourceRootId(root.0.into()),
203 FileId(file.0.into()),
208 VfsChange::RemoveFile { root, file, path } => {
209 change.remove_file(SourceRootId(root.0.into()), FileId(file.0.into()), path)
211 VfsChange::ChangeFile { file, text } => {
212 change.change_file(FileId(file.0.into()), text);
216 self.analysis_host.apply_change(change);
220 pub fn add_lib(&mut self, data: LibraryData) {
221 self.roots_to_scan -= 1;
222 let mut change = AnalysisChange::new();
223 change.add_library(data);
224 self.analysis_host.apply_change(change);
227 pub fn snapshot(&self) -> ServerWorld {
229 workspaces: Arc::clone(&self.workspaces),
230 analysis: self.analysis_host.analysis(),
231 vfs: Arc::clone(&self.vfs),
237 pub fn analysis(&self) -> &Analysis {
241 pub fn uri_to_file_id(&self, uri: &Url) -> Result<FileId> {
244 .map_err(|()| format_err!("invalid uri: {}", uri))?;
249 .ok_or_else(|| format_err!("unknown file: {}", path.display()))?;
250 Ok(FileId(file.0.into()))
253 pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> {
254 let path = self.vfs.read().file2path(VfsFile(id.0.into()));
255 let url = Url::from_file_path(&path)
256 .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;
260 pub fn path_to_uri(&self, root: SourceRootId, path: &RelativePathBuf) -> Result<Url> {
261 let base = self.vfs.read().root2path(VfsRoot(root.0.into()));
262 let path = path.to_path(base);
263 let url = Url::from_file_path(&path)
264 .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;
268 pub fn status(&self) -> String {
269 let mut res = String::new();
270 if self.workspaces.is_empty() {
271 res.push_str("no workspaces\n")
273 res.push_str("workspaces:\n");
274 for w in self.workspaces.iter() {
275 res += &format!("{} packages loaded\n", w.cargo.packages().count());
278 res.push_str("\nanalysis:\n");
279 res.push_str(&self.analysis.status());