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();
110 log::info!("vfs change {:?}", c);
112 VfsChange::AddRoot { root, files } => {
113 let root_path = self.vfs.read().root2path(root);
114 if root_path.starts_with(&self.root) {
115 self.roots_to_scan -= 1;
116 for (file, path, text) in files {
117 change.add_file(SourceRootId(root.0), FileId(file.0), path, text);
122 .map(|(vfsfile, path, text)| (FileId(vfsfile.0), path, text))
124 libs.push((SourceRootId(root.0), files));
133 change.add_file(SourceRootId(root.0), FileId(file.0), path, text);
135 VfsChange::RemoveFile { root, file, path } => {
136 change.remove_file(SourceRootId(root.0), FileId(file.0), path)
138 VfsChange::ChangeFile { file, text } => {
139 change.change_file(FileId(file.0), text);
143 self.analysis_host.apply_change(change);
147 pub fn add_lib(&mut self, data: LibraryData) {
148 self.roots_to_scan -= 1;
149 let mut change = AnalysisChange::new();
150 change.add_library(data);
151 self.analysis_host.apply_change(change);
154 pub fn snapshot(&self) -> ServerWorld {
156 workspaces: Arc::clone(&self.workspaces),
157 analysis: self.analysis_host.analysis(),
158 vfs: Arc::clone(&self.vfs),
164 pub fn analysis(&self) -> &Analysis {
168 pub fn uri_to_file_id(&self, uri: &Url) -> Result<FileId> {
171 .map_err(|()| format_err!("invalid uri: {}", uri))?;
176 .ok_or_else(|| format_err!("unknown file: {}", path.display()))?;
180 pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> {
181 let path = self.vfs.read().file2path(VfsFile(id.0));
182 let url = Url::from_file_path(&path)
183 .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;
187 pub fn path_to_uri(&self, root: SourceRootId, path: &RelativePathBuf) -> Result<Url> {
188 let base = self.vfs.read().root2path(VfsRoot(root.0));
189 let path = path.to_path(base);
190 let url = Url::from_file_path(&path)
191 .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;