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::{CargoWorkspace, TargetKind},
23 pub struct ServerWorldState {
24 pub roots_to_scan: usize,
26 pub workspaces: Arc<Vec<CargoWorkspace>>,
27 pub analysis_host: AnalysisHost,
28 pub vfs: Arc<RwLock<Vfs>>,
31 pub struct ServerWorld {
32 pub workspaces: Arc<Vec<CargoWorkspace>>,
33 pub analysis: Analysis,
34 pub vfs: Arc<RwLock<Vfs>>,
37 impl ServerWorldState {
38 pub fn new(root: PathBuf, workspaces: Vec<CargoWorkspace>) -> 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.packages() {
45 roots.push(pkg.root(&ws).to_path_buf());
48 let roots_to_scan = roots.len();
49 let (mut vfs, roots) = Vfs::new(roots);
51 let is_local = vfs.root2path(r).starts_with(&root);
52 change.add_root(SourceRootId(r.0), is_local);
55 let mut crate_graph = CrateGraph::default();
56 let mut pkg_to_lib_crate = FxHashMap::default();
57 let mut pkg_crates = FxHashMap::default();
58 for ws in workspaces.iter() {
59 for pkg in ws.packages() {
60 for tgt in pkg.targets(ws) {
61 let root = tgt.root(ws);
62 if let Some(file_id) = vfs.load(root) {
63 let file_id = FileId(file_id.0);
64 let crate_id = crate_graph.add_crate_root(file_id);
65 if tgt.kind(ws) == TargetKind::Lib {
66 pkg_to_lib_crate.insert(pkg, crate_id);
70 .or_insert_with(Vec::new)
75 for pkg in ws.packages() {
76 for dep in pkg.dependencies(ws) {
77 if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
78 for &from in pkg_crates.get(&pkg).into_iter().flatten() {
79 crate_graph.add_dep(from, dep.name.clone(), to);
85 change.set_crate_graph(crate_graph);
87 let mut analysis_host = AnalysisHost::default();
88 analysis_host.apply_change(change);
92 workspaces: Arc::new(workspaces),
94 vfs: Arc::new(RwLock::new(vfs)),
98 /// Returns a vec of libraries
99 /// FIXME: better API here
100 pub fn process_changes(
102 ) -> Vec<(SourceRootId, Vec<(FileId, RelativePathBuf, Arc<String>)>)> {
103 let changes = self.vfs.write().commit_changes();
104 if changes.is_empty() {
107 let mut libs = Vec::new();
108 let mut change = AnalysisChange::new();
111 VfsChange::AddRoot { root, files } => {
112 let root_path = self.vfs.read().root2path(root);
113 if root_path.starts_with(&self.root) {
114 self.roots_to_scan -= 1;
115 for (file, path, text) in files {
116 change.add_file(SourceRootId(root.0), FileId(file.0), path, text);
121 .map(|(vfsfile, path, text)| (FileId(vfsfile.0), path, text))
123 libs.push((SourceRootId(root.0), files));
132 change.add_file(SourceRootId(root.0), FileId(file.0), path, text);
134 VfsChange::RemoveFile { root, file, path } => {
135 change.remove_file(SourceRootId(root.0), FileId(file.0), path)
137 VfsChange::ChangeFile { file, text } => {
138 change.change_file(FileId(file.0), text);
142 self.analysis_host.apply_change(change);
146 pub fn add_lib(&mut self, data: LibraryData) {
147 self.roots_to_scan -= 1;
148 let mut change = AnalysisChange::new();
149 change.add_library(data);
150 self.analysis_host.apply_change(change);
153 pub fn snapshot(&self) -> ServerWorld {
155 workspaces: Arc::clone(&self.workspaces),
156 analysis: self.analysis_host.analysis(),
157 vfs: Arc::clone(&self.vfs),
163 pub fn analysis(&self) -> &Analysis {
167 pub fn uri_to_file_id(&self, uri: &Url) -> Result<FileId> {
170 .map_err(|()| format_err!("invalid uri: {}", uri))?;
175 .ok_or_else(|| format_err!("unknown file: {}", path.display()))?;
179 pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> {
180 let path = self.vfs.read().file2path(VfsFile(id.0));
181 let url = Url::from_file_path(&path)
182 .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;
186 pub fn path_to_uri(&self, root: SourceRootId, path: &RelativePathBuf) -> Result<Url> {
187 let base = self.vfs.read().root2path(VfsRoot(root.0));
188 let path = path.to_path(base);
189 let url = Url::from_file_path(&path)
190 .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;