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