aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server/src/server_world.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-12-19 12:04:15 +0000
committerAleksey Kladov <[email protected]>2018-12-20 09:15:38 +0000
commita5ef8ad05b7c1f7148c59814b55d641fd75aff75 (patch)
treedf6c46378d81dc7cb3242ff7f57d836353bde5ed /crates/ra_lsp_server/src/server_world.rs
parent6a755ed83a583d1f70a5fbcff2d4933b52628cfe (diff)
swtich lsp server to vfs
Diffstat (limited to 'crates/ra_lsp_server/src/server_world.rs')
-rw-r--r--crates/ra_lsp_server/src/server_world.rs203
1 files changed, 81 insertions, 122 deletions
diff --git a/crates/ra_lsp_server/src/server_world.rs b/crates/ra_lsp_server/src/server_world.rs
index c0d1338a2..f2fd09e85 100644
--- a/crates/ra_lsp_server/src/server_world.rs
+++ b/crates/ra_lsp_server/src/server_world.rs
@@ -1,154 +1,62 @@
1use std::{ 1use std::{
2 fs, 2 path::{PathBuf},
3 path::{Path, PathBuf},
4 sync::Arc, 3 sync::Arc,
5}; 4};
6 5
7use languageserver_types::Url; 6use languageserver_types::Url;
8use ra_analysis::{ 7use ra_analysis::{
9 Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, 8 Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData,
9 SourceRootId
10}; 10};
11use ra_vfs::{Vfs, VfsChange, VfsFile};
11use rustc_hash::FxHashMap; 12use rustc_hash::FxHashMap;
12use failure::{bail, format_err}; 13use relative_path::RelativePathBuf;
14use parking_lot::RwLock;
15use failure::{format_err};
13 16
14use crate::{ 17use crate::{
15 path_map::{PathMap, Root},
16 project_model::{CargoWorkspace, TargetKind}, 18 project_model::{CargoWorkspace, TargetKind},
17 vfs::{FileEvent, FileEventKind},
18 Result, 19 Result,
19}; 20};
20 21
21#[derive(Debug, Default)] 22#[derive(Debug)]
22pub struct ServerWorldState { 23pub struct ServerWorldState {
23 pub workspaces: Arc<Vec<CargoWorkspace>>, 24 pub workspaces: Arc<Vec<CargoWorkspace>>,
24 pub analysis_host: AnalysisHost, 25 pub analysis_host: AnalysisHost,
25 pub path_map: PathMap, 26 pub vfs: Arc<RwLock<Vfs>>,
26 pub mem_map: FxHashMap<FileId, Option<String>>,
27} 27}
28 28
29pub struct ServerWorld { 29pub struct ServerWorld {
30 pub workspaces: Arc<Vec<CargoWorkspace>>, 30 pub workspaces: Arc<Vec<CargoWorkspace>>,
31 pub analysis: Analysis, 31 pub analysis: Analysis,
32 pub path_map: PathMap, 32 pub vfs: Arc<RwLock<Vfs>>,
33} 33}
34 34
35impl ServerWorldState { 35impl ServerWorldState {
36 pub fn apply_fs_changes(&mut self, events: Vec<FileEvent>) { 36 pub fn new(root: PathBuf, workspaces: Vec<CargoWorkspace>) -> ServerWorldState {
37 let mut change = AnalysisChange::new(); 37 let mut change = AnalysisChange::new();
38 let mut inserted = false;
39 {
40 let pm = &mut self.path_map;
41 let mm = &mut self.mem_map;
42 events
43 .into_iter()
44 .map(|event| {
45 let text = match event.kind {
46 FileEventKind::Add(text) => text,
47 };
48 (event.path, text)
49 })
50 .map(|(path, text)| {
51 let (ins, file_id) = pm.get_or_insert(path, Root::Workspace);
52 inserted |= ins;
53 (file_id, text)
54 })
55 .filter_map(|(file_id, text)| {
56 if mm.contains_key(&file_id) {
57 mm.insert(file_id, Some(text));
58 None
59 } else {
60 Some((file_id, text))
61 }
62 })
63 .for_each(|(file_id, text)| change.add_file(file_id, text));
64 }
65 if inserted {
66 change.set_file_resolver(Arc::new(self.path_map.clone()))
67 }
68 self.analysis_host.apply_change(change);
69 }
70 pub fn events_to_files(
71 &mut self,
72 events: Vec<FileEvent>,
73 ) -> (Vec<(FileId, String)>, Arc<FileResolver>) {
74 let files = {
75 let pm = &mut self.path_map;
76 events
77 .into_iter()
78 .map(|event| {
79 let FileEventKind::Add(text) = event.kind;
80 (event.path, text)
81 })
82 .map(|(path, text)| (pm.get_or_insert(path, Root::Lib).1, text))
83 .collect()
84 };
85 let resolver = Arc::new(self.path_map.clone());
86 (files, resolver)
87 }
88 pub fn add_lib(&mut self, data: LibraryData) {
89 let mut change = AnalysisChange::new();
90 change.add_library(data);
91 self.analysis_host.apply_change(change);
92 }
93 38
94 pub fn add_mem_file(&mut self, path: PathBuf, text: String) -> FileId { 39 let mut roots = Vec::new();
95 let (inserted, file_id) = self.path_map.get_or_insert(path, Root::Workspace); 40 roots.push(root);
96 if self.path_map.get_root(file_id) != Root::Lib { 41 for ws in workspaces.iter() {
97 let mut change = AnalysisChange::new(); 42 for pkg in ws.packages() {
98 if inserted { 43 roots.push(pkg.root(&ws).to_path_buf());
99 change.add_file(file_id, text);
100 change.set_file_resolver(Arc::new(self.path_map.clone()));
101 } else {
102 change.change_file(file_id, text);
103 } 44 }
104 self.analysis_host.apply_change(change);
105 } 45 }
106 self.mem_map.insert(file_id, None); 46 let (mut vfs, roots) = Vfs::new(roots);
107 file_id 47 for r in roots {
108 } 48 change.add_root(SourceRootId(r.0));
109
110 pub fn change_mem_file(&mut self, path: &Path, text: String) -> Result<()> {
111 let file_id = self
112 .path_map
113 .get_id(path)
114 .ok_or_else(|| format_err!("change to unknown file: {}", path.display()))?;
115 if self.path_map.get_root(file_id) != Root::Lib {
116 let mut change = AnalysisChange::new();
117 change.change_file(file_id, text);
118 self.analysis_host.apply_change(change);
119 } 49 }
120 Ok(())
121 }
122 50
123 pub fn remove_mem_file(&mut self, path: &Path) -> Result<FileId> {
124 let file_id = self
125 .path_map
126 .get_id(path)
127 .ok_or_else(|| format_err!("change to unknown file: {}", path.display()))?;
128 match self.mem_map.remove(&file_id) {
129 Some(_) => (),
130 None => bail!("unmatched close notification"),
131 };
132 // Do this via file watcher ideally.
133 let text = fs::read_to_string(path).ok();
134 if self.path_map.get_root(file_id) != Root::Lib {
135 let mut change = AnalysisChange::new();
136 if let Some(text) = text {
137 change.change_file(file_id, text);
138 }
139 self.analysis_host.apply_change(change);
140 }
141 Ok(file_id)
142 }
143 pub fn set_workspaces(&mut self, ws: Vec<CargoWorkspace>) {
144 let mut crate_graph = CrateGraph::default(); 51 let mut crate_graph = CrateGraph::default();
145 let mut pkg_to_lib_crate = FxHashMap::default(); 52 let mut pkg_to_lib_crate = FxHashMap::default();
146 let mut pkg_crates = FxHashMap::default(); 53 let mut pkg_crates = FxHashMap::default();
147 for ws in ws.iter() { 54 for ws in workspaces.iter() {
148 for pkg in ws.packages() { 55 for pkg in ws.packages() {
149 for tgt in pkg.targets(ws) { 56 for tgt in pkg.targets(ws) {
150 let root = tgt.root(ws); 57 let root = tgt.root(ws);
151 if let Some(file_id) = self.path_map.get_id(root) { 58 if let Some(file_id) = vfs.load(root) {
59 let file_id = FileId(file_id.0);
152 let crate_id = crate_graph.add_crate_root(file_id); 60 let crate_id = crate_graph.add_crate_root(file_id);
153 if tgt.kind(ws) == TargetKind::Lib { 61 if tgt.kind(ws) == TargetKind::Lib {
154 pkg_to_lib_crate.insert(pkg, crate_id); 62 pkg_to_lib_crate.insert(pkg, crate_id);
@@ -170,16 +78,64 @@ impl ServerWorldState {
170 } 78 }
171 } 79 }
172 } 80 }
173 self.workspaces = Arc::new(ws);
174 let mut change = AnalysisChange::new();
175 change.set_crate_graph(crate_graph); 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 }
176 self.analysis_host.apply_change(change); 124 self.analysis_host.apply_change(change);
125 libs
177 } 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
178 pub fn snapshot(&self) -> ServerWorld { 134 pub fn snapshot(&self) -> ServerWorld {
179 ServerWorld { 135 ServerWorld {
180 workspaces: Arc::clone(&self.workspaces), 136 workspaces: Arc::clone(&self.workspaces),
181 analysis: self.analysis_host.analysis(), 137 analysis: self.analysis_host.analysis(),
182 path_map: self.path_map.clone(), 138 vfs: Arc::clone(&self.vfs),
183 } 139 }
184 } 140 }
185} 141}
@@ -193,15 +149,18 @@ impl ServerWorld {
193 let path = uri 149 let path = uri
194 .to_file_path() 150 .to_file_path()
195 .map_err(|()| format_err!("invalid uri: {}", uri))?; 151 .map_err(|()| format_err!("invalid uri: {}", uri))?;
196 self.path_map 152 let file = self
197 .get_id(&path) 153 .vfs
198 .ok_or_else(|| format_err!("unknown file: {}", path.display())) 154 .read()
155 .path2file(&path)
156 .ok_or_else(|| format_err!("unknown file: {}", path.display()))?;
157 Ok(FileId(file.0))
199 } 158 }
200 159
201 pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> { 160 pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> {
202 let path = self.path_map.get_path(id); 161 let path = self.vfs.read().file2path(VfsFile(id.0));
203 let url = Url::from_file_path(path) 162 let url = Url::from_file_path(&path)
204 .map_err(|()| format_err!("can't convert path to url: {}", path.display()))?; 163 .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;
205 Ok(url) 164 Ok(url)
206 } 165 }
207} 166}