]> git.lizzy.rs Git - rust.git/blob - crates/ra_lsp_server/src/server_world.rs
Merge #310
[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_analysis::{
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), 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);
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(SourceRootId(root.0), FileId(file.0), path, text);
117                         }
118                     } else {
119                         let files = files
120                             .into_iter()
121                             .map(|(vfsfile, path, text)| (FileId(vfsfile.0), path, text))
122                             .collect();
123                         libs.push((SourceRootId(root.0), files));
124                     }
125                 }
126                 VfsChange::AddFile {
127                     root,
128                     file,
129                     path,
130                     text,
131                 } => {
132                     change.add_file(SourceRootId(root.0), FileId(file.0), path, text);
133                 }
134                 VfsChange::RemoveFile { root, file, path } => {
135                     change.remove_file(SourceRootId(root.0), FileId(file.0), path)
136                 }
137                 VfsChange::ChangeFile { file, text } => {
138                     change.change_file(FileId(file.0), text);
139                 }
140             }
141         }
142         self.analysis_host.apply_change(change);
143         libs
144     }
145
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);
151     }
152
153     pub fn snapshot(&self) -> ServerWorld {
154         ServerWorld {
155             workspaces: Arc::clone(&self.workspaces),
156             analysis: self.analysis_host.analysis(),
157             vfs: Arc::clone(&self.vfs),
158         }
159     }
160 }
161
162 impl ServerWorld {
163     pub fn analysis(&self) -> &Analysis {
164         &self.analysis
165     }
166
167     pub fn uri_to_file_id(&self, uri: &Url) -> Result<FileId> {
168         let path = uri
169             .to_file_path()
170             .map_err(|()| format_err!("invalid uri: {}", uri))?;
171         let file = self
172             .vfs
173             .read()
174             .path2file(&path)
175             .ok_or_else(|| format_err!("unknown file: {}", path.display()))?;
176         Ok(FileId(file.0))
177     }
178
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()))?;
183         Ok(url)
184     }
185
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()))?;
191         Ok(url)
192     }
193 }