aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_lsp_server')
-rw-r--r--crates/ra_lsp_server/Cargo.toml2
-rw-r--r--crates/ra_lsp_server/src/lib.rs2
-rw-r--r--crates/ra_lsp_server/src/main_loop.rs143
-rw-r--r--crates/ra_lsp_server/src/main_loop/handlers.rs4
-rw-r--r--crates/ra_lsp_server/src/path_map.rs105
-rw-r--r--crates/ra_lsp_server/src/project_model.rs1
-rw-r--r--crates/ra_lsp_server/src/server_world.rs203
-rw-r--r--crates/ra_lsp_server/src/vfs.rs67
8 files changed, 140 insertions, 387 deletions
diff --git a/crates/ra_lsp_server/Cargo.toml b/crates/ra_lsp_server/Cargo.toml
index 4e8e09584..fc10096e5 100644
--- a/crates/ra_lsp_server/Cargo.toml
+++ b/crates/ra_lsp_server/Cargo.toml
@@ -25,6 +25,7 @@ cargo_metadata = "0.6.0"
25text_unit = { version = "0.1.2", features = ["serde"] } 25text_unit = { version = "0.1.2", features = ["serde"] }
26smol_str = { version = "0.1.5", features = ["serde"] } 26smol_str = { version = "0.1.5", features = ["serde"] }
27rustc-hash = "1.0" 27rustc-hash = "1.0"
28parking_lot = "0.7.0"
28 29
29thread_worker = { path = "../thread_worker" } 30thread_worker = { path = "../thread_worker" }
30ra_syntax = { path = "../ra_syntax" } 31ra_syntax = { path = "../ra_syntax" }
@@ -32,6 +33,7 @@ ra_editor = { path = "../ra_editor" }
32ra_text_edit = { path = "../ra_text_edit" } 33ra_text_edit = { path = "../ra_text_edit" }
33ra_analysis = { path = "../ra_analysis" } 34ra_analysis = { path = "../ra_analysis" }
34gen_lsp_server = { path = "../gen_lsp_server" } 35gen_lsp_server = { path = "../gen_lsp_server" }
36ra_vfs = { path = "../ra_vfs" }
35 37
36[dev-dependencies] 38[dev-dependencies]
37tempdir = "0.3.7" 39tempdir = "0.3.7"
diff --git a/crates/ra_lsp_server/src/lib.rs b/crates/ra_lsp_server/src/lib.rs
index 1d7258c35..725b1258a 100644
--- a/crates/ra_lsp_server/src/lib.rs
+++ b/crates/ra_lsp_server/src/lib.rs
@@ -1,11 +1,9 @@
1mod caps; 1mod caps;
2mod conv; 2mod conv;
3mod main_loop; 3mod main_loop;
4mod path_map;
5mod project_model; 4mod project_model;
6pub mod req; 5pub mod req;
7mod server_world; 6mod server_world;
8mod vfs;
9 7
10pub type Result<T> = ::std::result::Result<T, ::failure::Error>; 8pub type Result<T> = ::std::result::Result<T, ::failure::Error>;
11pub use crate::{caps::server_capabilities, main_loop::main_loop, main_loop::LspError}; 9pub use crate::{caps::server_capabilities, main_loop::main_loop, main_loop::LspError};
diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs
index 9d3f83b4c..7904545d3 100644
--- a/crates/ra_lsp_server/src/main_loop.rs
+++ b/crates/ra_lsp_server/src/main_loop.rs
@@ -1,7 +1,10 @@
1mod handlers; 1mod handlers;
2mod subscriptions; 2mod subscriptions;
3 3
4use std::path::PathBuf; 4use std::{
5 path::PathBuf,
6 sync::Arc,
7};
5 8
6use crossbeam_channel::{unbounded, select, Receiver, Sender}; 9use crossbeam_channel::{unbounded, select, Receiver, Sender};
7use gen_lsp_server::{ 10use gen_lsp_server::{
@@ -9,8 +12,8 @@ use gen_lsp_server::{
9}; 12};
10use languageserver_types::NumberOrString; 13use languageserver_types::NumberOrString;
11use ra_analysis::{Canceled, FileId, LibraryData}; 14use ra_analysis::{Canceled, FileId, LibraryData};
15use ra_vfs::{VfsTask};
12use rayon; 16use rayon;
13use thread_worker::Worker;
14use threadpool::ThreadPool; 17use threadpool::ThreadPool;
15use rustc_hash::FxHashSet; 18use rustc_hash::FxHashSet;
16use serde::{de::DeserializeOwned, Serialize}; 19use serde::{de::DeserializeOwned, Serialize};
@@ -19,10 +22,9 @@ use failure_derive::Fail;
19 22
20use crate::{ 23use crate::{
21 main_loop::subscriptions::Subscriptions, 24 main_loop::subscriptions::Subscriptions,
22 project_model::{workspace_loader, CargoWorkspace}, 25 project_model::{workspace_loader},
23 req, 26 req,
24 server_world::{ServerWorld, ServerWorldState}, 27 server_world::{ServerWorld, ServerWorldState},
25 vfs::{self, FileEvent},
26 Result, 28 Result,
27}; 29};
28 30
@@ -50,32 +52,42 @@ enum Task {
50 52
51pub fn main_loop( 53pub fn main_loop(
52 internal_mode: bool, 54 internal_mode: bool,
53 root: PathBuf, 55 ws_root: PathBuf,
54 publish_decorations: bool, 56 publish_decorations: bool,
55 msg_receiver: &Receiver<RawMessage>, 57 msg_receiver: &Receiver<RawMessage>,
56 msg_sender: &Sender<RawMessage>, 58 msg_sender: &Sender<RawMessage>,
57) -> Result<()> { 59) -> Result<()> {
58 let pool = ThreadPool::new(8); 60 let pool = ThreadPool::new(8);
59 let (task_sender, task_receiver) = unbounded::<Task>(); 61 let (task_sender, task_receiver) = unbounded::<Task>();
60 let (fs_worker, fs_watcher) = vfs::roots_loader();
61 let (ws_worker, ws_watcher) = workspace_loader(); 62 let (ws_worker, ws_watcher) = workspace_loader();
62 63
64 ws_worker.send(ws_root.clone());
65 // FIXME: support dynamic workspace loading.
66 let workspaces = match ws_worker.recv().unwrap() {
67 Ok(ws) => vec![ws],
68 Err(e) => {
69 log::warn!("loading workspace failed: {}", e);
70 Vec::new()
71 }
72 };
73 ws_worker.shutdown();
74 ws_watcher
75 .shutdown()
76 .map_err(|_| format_err!("ws watcher died"))?;
77 let mut state = ServerWorldState::new(ws_root.clone(), workspaces);
78
63 log::info!("server initialized, serving requests"); 79 log::info!("server initialized, serving requests");
64 let mut state = ServerWorldState::default();
65 80
66 let mut pending_requests = FxHashSet::default(); 81 let mut pending_requests = FxHashSet::default();
67 let mut subs = Subscriptions::new(); 82 let mut subs = Subscriptions::new();
68 let main_res = main_loop_inner( 83 let main_res = main_loop_inner(
69 internal_mode, 84 internal_mode,
70 publish_decorations, 85 publish_decorations,
71 root,
72 &pool, 86 &pool,
73 msg_sender, 87 msg_sender,
74 msg_receiver, 88 msg_receiver,
75 task_sender, 89 task_sender,
76 task_receiver.clone(), 90 task_receiver.clone(),
77 fs_worker,
78 ws_worker,
79 &mut state, 91 &mut state,
80 &mut pending_requests, 92 &mut pending_requests,
81 &mut subs, 93 &mut subs,
@@ -88,12 +100,11 @@ pub fn main_loop(
88 drop(pool); 100 drop(pool);
89 log::info!("...threadpool has finished"); 101 log::info!("...threadpool has finished");
90 102
91 let fs_res = fs_watcher.shutdown(); 103 let vfs = Arc::try_unwrap(state.vfs).expect("all snapshots should be dead");
92 let ws_res = ws_watcher.shutdown(); 104 let vfs_res = vfs.into_inner().shutdown();
93 105
94 main_res?; 106 main_res?;
95 fs_res.map_err(|_| format_err!("fs watcher died"))?; 107 vfs_res.map_err(|_| format_err!("fs watcher died"))?;
96 ws_res.map_err(|_| format_err!("ws watcher died"))?;
97 108
98 Ok(()) 109 Ok(())
99} 110}
@@ -101,28 +112,22 @@ pub fn main_loop(
101fn main_loop_inner( 112fn main_loop_inner(
102 internal_mode: bool, 113 internal_mode: bool,
103 publish_decorations: bool, 114 publish_decorations: bool,
104 ws_root: PathBuf,
105 pool: &ThreadPool, 115 pool: &ThreadPool,
106 msg_sender: &Sender<RawMessage>, 116 msg_sender: &Sender<RawMessage>,
107 msg_receiver: &Receiver<RawMessage>, 117 msg_receiver: &Receiver<RawMessage>,
108 task_sender: Sender<Task>, 118 task_sender: Sender<Task>,
109 task_receiver: Receiver<Task>, 119 task_receiver: Receiver<Task>,
110 fs_worker: Worker<PathBuf, (PathBuf, Vec<FileEvent>)>,
111 ws_worker: Worker<PathBuf, Result<CargoWorkspace>>,
112 state: &mut ServerWorldState, 120 state: &mut ServerWorldState,
113 pending_requests: &mut FxHashSet<u64>, 121 pending_requests: &mut FxHashSet<u64>,
114 subs: &mut Subscriptions, 122 subs: &mut Subscriptions,
115) -> Result<()> { 123) -> Result<()> {
116 let (libdata_sender, libdata_receiver) = unbounded(); 124 let (libdata_sender, libdata_receiver) = unbounded();
117 ws_worker.send(ws_root.clone());
118 fs_worker.send(ws_root.clone());
119 loop { 125 loop {
120 #[derive(Debug)] 126 #[derive(Debug)]
121 enum Event { 127 enum Event {
122 Msg(RawMessage), 128 Msg(RawMessage),
123 Task(Task), 129 Task(Task),
124 Fs(PathBuf, Vec<FileEvent>), 130 Vfs(VfsTask),
125 Ws(Result<CargoWorkspace>),
126 Lib(LibraryData), 131 Lib(LibraryData),
127 } 132 }
128 log::trace!("selecting"); 133 log::trace!("selecting");
@@ -132,77 +137,19 @@ fn main_loop_inner(
132 None => bail!("client exited without shutdown"), 137 None => bail!("client exited without shutdown"),
133 }, 138 },
134 recv(task_receiver, task) => Event::Task(task.unwrap()), 139 recv(task_receiver, task) => Event::Task(task.unwrap()),
135 recv(fs_worker.out, events) => match events { 140 recv(state.vfs.read().task_receiver(), task) => match task {
136 None => bail!("roots watcher died"), 141 None => bail!("vfs died"),
137 Some((pb, events)) => Event::Fs(pb, events), 142 Some(task) => Event::Vfs(task),
138 }
139 recv(ws_worker.out, ws) => match ws {
140 None => bail!("workspace watcher died"),
141 Some(ws) => Event::Ws(ws),
142 } 143 }
143 recv(libdata_receiver, data) => Event::Lib(data.unwrap()) 144 recv(libdata_receiver, data) => Event::Lib(data.unwrap())
144 }; 145 };
145 let mut state_changed = false; 146 let mut state_changed = false;
146 match event { 147 match event {
147 Event::Task(task) => on_task(task, msg_sender, pending_requests), 148 Event::Task(task) => on_task(task, msg_sender, pending_requests),
148 Event::Fs(root, events) => { 149 Event::Vfs(task) => {
149 log::info!("fs change, {}, {} events", root.display(), events.len()); 150 state.vfs.write().handle_task(task);
150 if root == ws_root {
151 state.apply_fs_changes(events);
152 } else {
153 let (files, resolver) = state.events_to_files(events);
154 let sender = libdata_sender.clone();
155 pool.execute(move || {
156 let start = ::std::time::Instant::now();
157 log::info!("indexing {} ... ", root.display());
158 let data = LibraryData::prepare(files, resolver);
159 log::info!("indexed {:?} {}", start.elapsed(), root.display());
160 sender.send(data);
161 });
162 }
163 state_changed = true; 151 state_changed = true;
164 } 152 }
165 Event::Ws(ws) => match ws {
166 Ok(ws) => {
167 let workspaces = vec![ws];
168 feedback(internal_mode, "workspace loaded", msg_sender);
169 for ws in workspaces.iter() {
170 // Add each library as constant input. If library is
171 // within the workspace, don't treat it as a library.
172 //
173 // HACK: If source roots are nested, pick the outer one.
174
175 let mut roots = ws
176 .packages()
177 .filter(|pkg| !pkg.is_member(ws))
178 .filter_map(|pkg| {
179 let root = pkg.root(ws).to_path_buf();
180 if root.starts_with(&ws_root) {
181 None
182 } else {
183 Some(root)
184 }
185 })
186 .collect::<Vec<_>>();
187 roots.sort_by_key(|it| it.as_os_str().len());
188 let unique = roots
189 .iter()
190 .enumerate()
191 .filter(|&(idx, long)| {
192 !roots[..idx].iter().any(|short| long.starts_with(short))
193 })
194 .map(|(_idx, root)| root);
195
196 for root in unique {
197 log::debug!("sending root, {}", root.display());
198 fs_worker.send(root.to_owned());
199 }
200 }
201 state.set_workspaces(workspaces);
202 state_changed = true;
203 }
204 Err(e) => log::warn!("loading workspace failed: {}", e),
205 },
206 Event::Lib(lib) => { 153 Event::Lib(lib) => {
207 feedback(internal_mode, "library loaded", msg_sender); 154 feedback(internal_mode, "library loaded", msg_sender);
208 state.add_lib(lib); 155 state.add_lib(lib);
@@ -234,6 +181,18 @@ fn main_loop_inner(
234 }, 181 },
235 }; 182 };
236 183
184 for lib in state.process_changes() {
185 let (root, files) = lib;
186 let sender = libdata_sender.clone();
187 pool.execute(move || {
188 let start = ::std::time::Instant::now();
189 log::info!("indexing {:?} ... ", root);
190 let data = LibraryData::prepare(root, files);
191 log::info!("indexed {:?} {:?}", start.elapsed(), root);
192 sender.send(data);
193 });
194 }
195
237 if state_changed { 196 if state_changed {
238 update_file_notifications_on_threadpool( 197 update_file_notifications_on_threadpool(
239 pool, 198 pool,
@@ -336,8 +295,13 @@ fn on_notification(
336 let path = uri 295 let path = uri
337 .to_file_path() 296 .to_file_path()
338 .map_err(|()| format_err!("invalid uri: {}", uri))?; 297 .map_err(|()| format_err!("invalid uri: {}", uri))?;
339 let file_id = state.add_mem_file(path, params.text_document.text); 298 if let Some(file_id) = state
340 subs.add_sub(file_id); 299 .vfs
300 .write()
301 .add_file_overlay(&path, params.text_document.text)
302 {
303 subs.add_sub(FileId(file_id.0));
304 }
341 return Ok(()); 305 return Ok(());
342 } 306 }
343 Err(not) => not, 307 Err(not) => not,
@@ -353,7 +317,7 @@ fn on_notification(
353 .pop() 317 .pop()
354 .ok_or_else(|| format_err!("empty changes"))? 318 .ok_or_else(|| format_err!("empty changes"))?
355 .text; 319 .text;
356 state.change_mem_file(path.as_path(), text)?; 320 state.vfs.write().change_file_overlay(path.as_path(), text);
357 return Ok(()); 321 return Ok(());
358 } 322 }
359 Err(not) => not, 323 Err(not) => not,
@@ -364,8 +328,9 @@ fn on_notification(
364 let path = uri 328 let path = uri
365 .to_file_path() 329 .to_file_path()
366 .map_err(|()| format_err!("invalid uri: {}", uri))?; 330 .map_err(|()| format_err!("invalid uri: {}", uri))?;
367 let file_id = state.remove_mem_file(path.as_path())?; 331 if let Some(file_id) = state.vfs.write().remove_file_overlay(path.as_path()) {
368 subs.remove_sub(file_id); 332 subs.remove_sub(FileId(file_id.0));
333 }
369 let params = req::PublishDiagnosticsParams { 334 let params = req::PublishDiagnosticsParams {
370 uri, 335 uri,
371 diagnostics: Vec::new(), 336 diagnostics: Vec::new(),
diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs
index acca480c7..572ae7fb5 100644
--- a/crates/ra_lsp_server/src/main_loop/handlers.rs
+++ b/crates/ra_lsp_server/src/main_loop/handlers.rs
@@ -326,9 +326,9 @@ pub fn handle_runnables(
326 None => return Ok(None), 326 None => return Ok(None),
327 }; 327 };
328 let file_id = world.analysis().crate_root(crate_id)?; 328 let file_id = world.analysis().crate_root(crate_id)?;
329 let path = world.path_map.get_path(file_id); 329 let path = world.vfs.read().file2path(ra_vfs::VfsFile(file_id.0));
330 let res = world.workspaces.iter().find_map(|ws| { 330 let res = world.workspaces.iter().find_map(|ws| {
331 let tgt = ws.target_by_root(path)?; 331 let tgt = ws.target_by_root(&path)?;
332 let res = CargoTargetSpec { 332 let res = CargoTargetSpec {
333 package: tgt.package(ws).name(ws).to_string(), 333 package: tgt.package(ws).name(ws).to_string(),
334 target: tgt.name(ws).to_string(), 334 target: tgt.name(ws).to_string(),
diff --git a/crates/ra_lsp_server/src/path_map.rs b/crates/ra_lsp_server/src/path_map.rs
deleted file mode 100644
index 86cf29540..000000000
--- a/crates/ra_lsp_server/src/path_map.rs
+++ /dev/null
@@ -1,105 +0,0 @@
1use std::{
2 fmt,
3 path::{Component, Path, PathBuf},
4};
5
6use im;
7use ra_analysis::{FileId};
8use relative_path::RelativePath;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum Root {
12 Workspace,
13 Lib,
14}
15
16#[derive(Default, Clone)]
17pub struct PathMap {
18 next_id: u32,
19 path2id: im::HashMap<PathBuf, FileId>,
20 id2path: im::HashMap<FileId, PathBuf>,
21 id2root: im::HashMap<FileId, Root>,
22}
23
24impl fmt::Debug for PathMap {
25 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26 f.write_str("PathMap { ... }")
27 }
28}
29
30impl PathMap {
31 pub fn get_or_insert(&mut self, path: PathBuf, root: Root) -> (bool, FileId) {
32 let mut inserted = false;
33 let file_id = self
34 .path2id
35 .get(path.as_path())
36 .map(|&id| id)
37 .unwrap_or_else(|| {
38 inserted = true;
39 let id = self.new_file_id();
40 self.insert(path, id, root);
41 id
42 });
43 (inserted, file_id)
44 }
45 pub fn get_id(&self, path: &Path) -> Option<FileId> {
46 self.path2id.get(path).cloned()
47 }
48 pub fn get_path(&self, file_id: FileId) -> &Path {
49 self.id2path.get(&file_id).unwrap().as_path()
50 }
51 pub fn get_root(&self, file_id: FileId) -> Root {
52 self.id2root[&file_id]
53 }
54 fn insert(&mut self, path: PathBuf, file_id: FileId, root: Root) {
55 self.path2id.insert(path.clone(), file_id);
56 self.id2path.insert(file_id, path.clone());
57 self.id2root.insert(file_id, root);
58 }
59
60 fn new_file_id(&mut self) -> FileId {
61 let id = FileId(self.next_id);
62 self.next_id += 1;
63 id
64 }
65}
66
67fn normalize(path: &Path) -> PathBuf {
68 let mut components = path.components().peekable();
69 let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
70 components.next();
71 PathBuf::from(c.as_os_str())
72 } else {
73 PathBuf::new()
74 };
75
76 for component in components {
77 match component {
78 Component::Prefix(..) => unreachable!(),
79 Component::RootDir => {
80 ret.push(component.as_os_str());
81 }
82 Component::CurDir => {}
83 Component::ParentDir => {
84 ret.pop();
85 }
86 Component::Normal(c) => {
87 ret.push(c);
88 }
89 }
90 }
91 ret
92}
93
94#[cfg(test)]
95mod test {
96 use super::*;
97
98 #[test]
99 fn test_resolve() {
100 let mut m = PathMap::default();
101 let (_, id1) = m.get_or_insert(PathBuf::from("/foo"), Root::Workspace);
102 let (_, id2) = m.get_or_insert(PathBuf::from("/foo/bar.rs"), Root::Workspace);
103 assert_eq!(m.resolve(id1, &RelativePath::new("bar.rs")), Some(id2),)
104 }
105}
diff --git a/crates/ra_lsp_server/src/project_model.rs b/crates/ra_lsp_server/src/project_model.rs
index b881f8b6f..5852a157d 100644
--- a/crates/ra_lsp_server/src/project_model.rs
+++ b/crates/ra_lsp_server/src/project_model.rs
@@ -69,6 +69,7 @@ impl Package {
69 pub fn targets<'a>(self, ws: &'a CargoWorkspace) -> impl Iterator<Item = Target> + 'a { 69 pub fn targets<'a>(self, ws: &'a CargoWorkspace) -> impl Iterator<Item = Target> + 'a {
70 ws.pkg(self).targets.iter().cloned() 70 ws.pkg(self).targets.iter().cloned()
71 } 71 }
72 #[allow(unused)]
72 pub fn is_member(self, ws: &CargoWorkspace) -> bool { 73 pub fn is_member(self, ws: &CargoWorkspace) -> bool {
73 ws.pkg(self).is_member 74 ws.pkg(self).is_member
74 } 75 }
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}
diff --git a/crates/ra_lsp_server/src/vfs.rs b/crates/ra_lsp_server/src/vfs.rs
deleted file mode 100644
index fcf7693d8..000000000
--- a/crates/ra_lsp_server/src/vfs.rs
+++ /dev/null
@@ -1,67 +0,0 @@
1use std::{
2 fs,
3 path::{Path, PathBuf},
4};
5
6use walkdir::WalkDir;
7use thread_worker::{WorkerHandle, Worker};
8
9#[derive(Debug)]
10pub struct FileEvent {
11 pub path: PathBuf,
12 pub kind: FileEventKind,
13}
14
15#[derive(Debug)]
16pub enum FileEventKind {
17 Add(String),
18}
19
20pub fn roots_loader() -> (Worker<PathBuf, (PathBuf, Vec<FileEvent>)>, WorkerHandle) {
21 thread_worker::spawn::<PathBuf, (PathBuf, Vec<FileEvent>), _>(
22 "roots loader",
23 128,
24 |input_receiver, output_sender| {
25 input_receiver
26 .map(|path| {
27 log::debug!("loading {} ...", path.as_path().display());
28 let events = load_root(path.as_path());
29 log::debug!("... loaded {}", path.as_path().display());
30 (path, events)
31 })
32 .for_each(|it| output_sender.send(it))
33 },
34 )
35}
36
37fn load_root(path: &Path) -> Vec<FileEvent> {
38 let mut res = Vec::new();
39 for entry in WalkDir::new(path) {
40 let entry = match entry {
41 Ok(entry) => entry,
42 Err(e) => {
43 log::warn!("watcher error: {}", e);
44 continue;
45 }
46 };
47 if !entry.file_type().is_file() {
48 continue;
49 }
50 let path = entry.path();
51 if path.extension().and_then(|os| os.to_str()) != Some("rs") {
52 continue;
53 }
54 let text = match fs::read_to_string(path) {
55 Ok(text) => text,
56 Err(e) => {
57 log::warn!("watcher error: {}", e);
58 continue;
59 }
60 };
61 res.push(FileEvent {
62 path: path.to_owned(),
63 kind: FileEventKind::Add(text),
64 })
65 }
66 res
67}