]> git.lizzy.rs Git - rust.git/blob - crates/ra_lsp_server/src/server_world.rs
swtich lsp server to vfs
[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 workspaces: Arc<Vec<CargoWorkspace>>,
25     pub analysis_host: AnalysisHost,
26     pub vfs: Arc<RwLock<Vfs>>,
27 }
28
29 pub struct ServerWorld {
30     pub workspaces: Arc<Vec<CargoWorkspace>>,
31     pub analysis: Analysis,
32     pub vfs: Arc<RwLock<Vfs>>,
33 }
34
35 impl ServerWorldState {
36     pub fn new(root: PathBuf, workspaces: Vec<CargoWorkspace>) -> ServerWorldState {
37         let mut change = AnalysisChange::new();
38
39         let mut roots = Vec::new();
40         roots.push(root);
41         for ws in workspaces.iter() {
42             for pkg in ws.packages() {
43                 roots.push(pkg.root(&ws).to_path_buf());
44             }
45         }
46         let (mut vfs, roots) = Vfs::new(roots);
47         for r in roots {
48             change.add_root(SourceRootId(r.0));
49         }
50
51         let mut crate_graph = CrateGraph::default();
52         let mut pkg_to_lib_crate = FxHashMap::default();
53         let mut pkg_crates = FxHashMap::default();
54         for ws in workspaces.iter() {
55             for pkg in ws.packages() {
56                 for tgt in pkg.targets(ws) {
57                     let root = tgt.root(ws);
58                     if let Some(file_id) = vfs.load(root) {
59                         let file_id = FileId(file_id.0);
60                         let crate_id = crate_graph.add_crate_root(file_id);
61                         if tgt.kind(ws) == TargetKind::Lib {
62                             pkg_to_lib_crate.insert(pkg, crate_id);
63                         }
64                         pkg_crates
65                             .entry(pkg)
66                             .or_insert_with(Vec::new)
67                             .push(crate_id);
68                     }
69                 }
70             }
71             for pkg in ws.packages() {
72                 for dep in pkg.dependencies(ws) {
73                     if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
74                         for &from in pkg_crates.get(&pkg).into_iter().flatten() {
75                             crate_graph.add_dep(from, dep.name.clone(), to);
76                         }
77                     }
78                 }
79             }
80         }
81         change.set_crate_graph(crate_graph);
82
83         let mut analysis_host = AnalysisHost::default();
84         analysis_host.apply_change(change);
85         ServerWorldState {
86             workspaces: Arc::new(workspaces),
87             analysis_host,
88             vfs: Arc::new(RwLock::new(vfs)),
89         }
90     }
91
92     /// Returns a vec of libraries
93     /// FIXME: better API here
94     pub fn process_changes(
95         &mut self,
96     ) -> Vec<(SourceRootId, Vec<(FileId, RelativePathBuf, Arc<String>)>)> {
97         let mut libs = Vec::new();
98         let mut change = AnalysisChange::new();
99         for c in self.vfs.write().commit_changes() {
100             match c {
101                 VfsChange::AddRoot { root, files } => {
102                     let files = files
103                         .into_iter()
104                         .map(|(vfsfile, path, text)| (FileId(vfsfile.0), path, text))
105                         .collect();
106                     libs.push((SourceRootId(root.0), files));
107                 }
108                 VfsChange::AddFile {
109                     root,
110                     file,
111                     path,
112                     text,
113                 } => {
114                     change.add_file(SourceRootId(root.0), FileId(file.0), path, text);
115                 }
116                 VfsChange::RemoveFile { root, file, path } => {
117                     change.remove_file(SourceRootId(root.0), FileId(file.0), path)
118                 }
119                 VfsChange::ChangeFile { file, text } => {
120                     change.change_file(FileId(file.0), text);
121                 }
122             }
123         }
124         self.analysis_host.apply_change(change);
125         libs
126     }
127
128     pub fn add_lib(&mut self, data: LibraryData) {
129         let mut change = AnalysisChange::new();
130         change.add_library(data);
131         self.analysis_host.apply_change(change);
132     }
133
134     pub fn snapshot(&self) -> ServerWorld {
135         ServerWorld {
136             workspaces: Arc::clone(&self.workspaces),
137             analysis: self.analysis_host.analysis(),
138             vfs: Arc::clone(&self.vfs),
139         }
140     }
141 }
142
143 impl ServerWorld {
144     pub fn analysis(&self) -> &Analysis {
145         &self.analysis
146     }
147
148     pub fn uri_to_file_id(&self, uri: &Url) -> Result<FileId> {
149         let path = uri
150             .to_file_path()
151             .map_err(|()| format_err!("invalid uri: {}", uri))?;
152         let file = self
153             .vfs
154             .read()
155             .path2file(&path)
156             .ok_or_else(|| format_err!("unknown file: {}", path.display()))?;
157         Ok(FileId(file.0))
158     }
159
160     pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> {
161         let path = self.vfs.read().file2path(VfsFile(id.0));
162         let url = Url::from_file_path(&path)
163             .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;
164         Ok(url)
165     }
166 }