6 use languageserver_types::Url;
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 crate_graph.add_dep(from, name.clone(), to);
84 .and_then(|it| sysroot_crates.get(&it).map(|&it| it));
86 let mut pkg_to_lib_crate = FxHashMap::default();
87 let mut pkg_crates = FxHashMap::default();
88 // Next, create crates for each package, target pair
89 for pkg in ws.cargo.packages() {
90 let mut lib_tgt = None;
91 for tgt in pkg.targets(&ws.cargo) {
92 let root = tgt.root(&ws.cargo);
93 if let Some(file_id) = vfs.load(root) {
94 let file_id = FileId(file_id.0.into());
95 let crate_id = crate_graph.add_crate_root(file_id);
96 if tgt.kind(&ws.cargo) == TargetKind::Lib {
97 lib_tgt = Some(crate_id);
98 pkg_to_lib_crate.insert(pkg, crate_id);
102 .or_insert_with(Vec::new)
107 // Set deps to the std and to the lib target of the current package
108 for &from in pkg_crates.get(&pkg).into_iter().flatten() {
109 if let Some(to) = lib_tgt {
111 crate_graph.add_dep(from, pkg.name(&ws.cargo).into(), to);
114 if let Some(std) = libstd {
115 crate_graph.add_dep(from, "std".into(), std);
120 // Now add a dep ednge from all targets of upstream to the lib
121 // target of downstream.
122 for pkg in ws.cargo.packages() {
123 for dep in pkg.dependencies(&ws.cargo) {
124 if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
125 for &from in pkg_crates.get(&pkg).into_iter().flatten() {
126 crate_graph.add_dep(from, dep.name.clone(), to);
132 change.set_crate_graph(crate_graph);
134 let mut analysis_host = AnalysisHost::default();
135 analysis_host.apply_change(change);
139 workspaces: Arc::new(workspaces),
141 vfs: Arc::new(RwLock::new(vfs)),
145 /// Returns a vec of libraries
146 /// FIXME: better API here
147 pub fn process_changes(
149 ) -> Vec<(SourceRootId, Vec<(FileId, RelativePathBuf, Arc<String>)>)> {
150 let changes = self.vfs.write().commit_changes();
151 if changes.is_empty() {
154 let mut libs = Vec::new();
155 let mut change = AnalysisChange::new();
158 VfsChange::AddRoot { root, files } => {
159 let root_path = self.vfs.read().root2path(root);
160 if root_path.starts_with(&self.root) {
161 self.roots_to_scan -= 1;
162 for (file, path, text) in files {
164 SourceRootId(root.0.into()),
165 FileId(file.0.into()),
173 .map(|(vfsfile, path, text)| (FileId(vfsfile.0.into()), path, text))
175 libs.push((SourceRootId(root.0.into()), files));
185 SourceRootId(root.0.into()),
186 FileId(file.0.into()),
191 VfsChange::RemoveFile { root, file, path } => {
192 change.remove_file(SourceRootId(root.0.into()), FileId(file.0.into()), path)
194 VfsChange::ChangeFile { file, text } => {
195 change.change_file(FileId(file.0.into()), text);
199 self.analysis_host.apply_change(change);
203 pub fn add_lib(&mut self, data: LibraryData) {
204 self.roots_to_scan -= 1;
205 let mut change = AnalysisChange::new();
206 change.add_library(data);
207 self.analysis_host.apply_change(change);
210 pub fn snapshot(&self) -> ServerWorld {
212 workspaces: Arc::clone(&self.workspaces),
213 analysis: self.analysis_host.analysis(),
214 vfs: Arc::clone(&self.vfs),
220 pub fn analysis(&self) -> &Analysis {
224 pub fn uri_to_file_id(&self, uri: &Url) -> Result<FileId> {
227 .map_err(|()| format_err!("invalid uri: {}", uri))?;
232 .ok_or_else(|| format_err!("unknown file: {}", path.display()))?;
233 Ok(FileId(file.0.into()))
236 pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> {
237 let path = self.vfs.read().file2path(VfsFile(id.0.into()));
238 let url = Url::from_file_path(&path)
239 .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;
243 pub fn path_to_uri(&self, root: SourceRootId, path: &RelativePathBuf) -> Result<Url> {
244 let base = self.vfs.read().root2path(VfsRoot(root.0.into()));
245 let path = path.to_path(base);
246 let url = Url::from_file_path(&path)
247 .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;