]> git.lizzy.rs Git - rust.git/blob - crates/ra_lsp_server/src/server_world.rs
fix usages after rename
[rust.git] / crates / ra_lsp_server / src / server_world.rs
1 use std::{
2     path::PathBuf,
3     sync::Arc,
4 };
5
6 use languageserver_types::Url;
7 use ra_ide_api::{
8     Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData,
9     SourceRootId
10 };
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;
16
17 use crate::{
18     project_model::{CargoWorkspace, TargetKind},
19     Result,
20 };
21
22 #[derive(Debug)]
23 pub struct ServerWorldState {
24     pub roots_to_scan: usize,
25     pub root: PathBuf,
26     pub workspaces: Arc<Vec<CargoWorkspace>>,
27     pub analysis_host: AnalysisHost,
28     pub vfs: Arc<RwLock<Vfs>>,
29 }
30
31 pub struct ServerWorld {
32     pub workspaces: Arc<Vec<CargoWorkspace>>,
33     pub analysis: Analysis,
34     pub vfs: Arc<RwLock<Vfs>>,
35 }
36
37 impl ServerWorldState {
38     pub fn new(root: PathBuf, workspaces: Vec<CargoWorkspace>) -> ServerWorldState {
39         let mut change = AnalysisChange::new();
40
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());
46             }
47         }
48         let roots_to_scan = roots.len();
49         let (mut vfs, roots) = Vfs::new(roots);
50         for r in roots {
51             let is_local = vfs.root2path(r).starts_with(&root);
52             change.add_root(SourceRootId(r.0.into()), is_local);
53         }
54
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.into());
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);
67                         }
68                         pkg_crates
69                             .entry(pkg)
70                             .or_insert_with(Vec::new)
71                             .push(crate_id);
72                     }
73                 }
74             }
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);
80                         }
81                     }
82                 }
83             }
84         }
85         change.set_crate_graph(crate_graph);
86
87         let mut analysis_host = AnalysisHost::default();
88         analysis_host.apply_change(change);
89         ServerWorldState {
90             roots_to_scan,
91             root,
92             workspaces: Arc::new(workspaces),
93             analysis_host,
94             vfs: Arc::new(RwLock::new(vfs)),
95         }
96     }
97
98     /// Returns a vec of libraries
99     /// FIXME: better API here
100     pub fn process_changes(
101         &mut self,
102     ) -> Vec<(SourceRootId, Vec<(FileId, RelativePathBuf, Arc<String>)>)> {
103         let changes = self.vfs.write().commit_changes();
104         if changes.is_empty() {
105             return Vec::new();
106         }
107         let mut libs = Vec::new();
108         let mut change = AnalysisChange::new();
109         for c in changes {
110             match c {
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(
117                                 SourceRootId(root.0.into()),
118                                 FileId(file.0.into()),
119                                 path,
120                                 text,
121                             );
122                         }
123                     } else {
124                         let files = files
125                             .into_iter()
126                             .map(|(vfsfile, path, text)| (FileId(vfsfile.0.into()), path, text))
127                             .collect();
128                         libs.push((SourceRootId(root.0.into()), files));
129                     }
130                 }
131                 VfsChange::AddFile {
132                     root,
133                     file,
134                     path,
135                     text,
136                 } => {
137                     change.add_file(
138                         SourceRootId(root.0.into()),
139                         FileId(file.0.into()),
140                         path,
141                         text,
142                     );
143                 }
144                 VfsChange::RemoveFile { root, file, path } => {
145                     change.remove_file(SourceRootId(root.0.into()), FileId(file.0.into()), path)
146                 }
147                 VfsChange::ChangeFile { file, text } => {
148                     change.change_file(FileId(file.0.into()), text);
149                 }
150             }
151         }
152         self.analysis_host.apply_change(change);
153         libs
154     }
155
156     pub fn add_lib(&mut self, data: LibraryData) {
157         self.roots_to_scan -= 1;
158         let mut change = AnalysisChange::new();
159         change.add_library(data);
160         self.analysis_host.apply_change(change);
161     }
162
163     pub fn snapshot(&self) -> ServerWorld {
164         ServerWorld {
165             workspaces: Arc::clone(&self.workspaces),
166             analysis: self.analysis_host.analysis(),
167             vfs: Arc::clone(&self.vfs),
168         }
169     }
170 }
171
172 impl ServerWorld {
173     pub fn analysis(&self) -> &Analysis {
174         &self.analysis
175     }
176
177     pub fn uri_to_file_id(&self, uri: &Url) -> Result<FileId> {
178         let path = uri
179             .to_file_path()
180             .map_err(|()| format_err!("invalid uri: {}", uri))?;
181         let file = self
182             .vfs
183             .read()
184             .path2file(&path)
185             .ok_or_else(|| format_err!("unknown file: {}", path.display()))?;
186         Ok(FileId(file.0.into()))
187     }
188
189     pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> {
190         let path = self.vfs.read().file2path(VfsFile(id.0.into()));
191         let url = Url::from_file_path(&path)
192             .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;
193         Ok(url)
194     }
195
196     pub fn path_to_uri(&self, root: SourceRootId, path: &RelativePathBuf) -> Result<Url> {
197         let base = self.vfs.read().root2path(VfsRoot(root.0.into()));
198         let path = path.to_path(base);
199         let url = Url::from_file_path(&path)
200             .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;
201         Ok(url)
202     }
203 }