aboutsummaryrefslogtreecommitdiff
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
parent6a755ed83a583d1f70a5fbcff2d4933b52628cfe (diff)
swtich lsp server to vfs
-rw-r--r--Cargo.lock25
-rw-r--r--crates/ra_analysis/src/imp.rs9
-rw-r--r--crates/ra_analysis/src/lib.rs8
-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
-rw-r--r--crates/ra_vfs/src/io.rs7
-rw-r--r--crates/ra_vfs/src/lib.rs53
-rw-r--r--crates/thread_worker/src/lib.rs3
14 files changed, 234 insertions, 398 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 0a7128d0c..7ebe6e67f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -542,6 +542,15 @@ dependencies = [
542] 542]
543 543
544[[package]] 544[[package]]
545name = "parking_lot"
546version = "0.7.0"
547source = "registry+https://github.com/rust-lang/crates.io-index"
548dependencies = [
549 "lock_api 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
550 "parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
551]
552
553[[package]]
545name = "parking_lot_core" 554name = "parking_lot_core"
546version = "0.3.1" 555version = "0.3.1"
547source = "registry+https://github.com/rust-lang/crates.io-index" 556source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -554,6 +563,18 @@ dependencies = [
554] 563]
555 564
556[[package]] 565[[package]]
566name = "parking_lot_core"
567version = "0.4.0"
568source = "registry+https://github.com/rust-lang/crates.io-index"
569dependencies = [
570 "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
571 "rand 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
572 "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
573 "smallvec 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
574 "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
575]
576
577[[package]]
557name = "percent-encoding" 578name = "percent-encoding"
558version = "1.0.1" 579version = "1.0.1"
559source = "registry+https://github.com/rust-lang/crates.io-index" 580source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -701,10 +722,12 @@ dependencies = [
701 "im 12.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 722 "im 12.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
702 "languageserver-types 0.53.0 (registry+https://github.com/rust-lang/crates.io-index)", 723 "languageserver-types 0.53.0 (registry+https://github.com/rust-lang/crates.io-index)",
703 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", 724 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
725 "parking_lot 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
704 "ra_analysis 0.1.0", 726 "ra_analysis 0.1.0",
705 "ra_editor 0.1.0", 727 "ra_editor 0.1.0",
706 "ra_syntax 0.1.0", 728 "ra_syntax 0.1.0",
707 "ra_text_edit 0.1.0", 729 "ra_text_edit 0.1.0",
730 "ra_vfs 0.1.0",
708 "rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", 731 "rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
709 "relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 732 "relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
710 "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 733 "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1496,7 +1519,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1496"checksum num_cpus 1.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5a69d464bdc213aaaff628444e99578ede64e9c854025aa43b9796530afa9238" 1519"checksum num_cpus 1.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5a69d464bdc213aaaff628444e99578ede64e9c854025aa43b9796530afa9238"
1497"checksum owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49a4b8ea2179e6a2e27411d3bca09ca6dd630821cf6894c6c7c8467a8ee7ef13" 1520"checksum owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49a4b8ea2179e6a2e27411d3bca09ca6dd630821cf6894c6c7c8467a8ee7ef13"
1498"checksum parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f0802bff09003b291ba756dc7e79313e51cc31667e94afbe847def490424cde5" 1521"checksum parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f0802bff09003b291ba756dc7e79313e51cc31667e94afbe847def490424cde5"
1522"checksum parking_lot 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9723236a9525c757d9725b993511e3fc941e33f27751942232f0058298297edf"
1499"checksum parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad7f7e6ebdc79edff6fdcb87a55b620174f7a989e3eb31b65231f4af57f00b8c" 1523"checksum parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad7f7e6ebdc79edff6fdcb87a55b620174f7a989e3eb31b65231f4af57f00b8c"
1524"checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9"
1500"checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" 1525"checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831"
1501"checksum pest 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a677051ad923732bb5c70f2d45f8985a96e3eee2e2bff86697e3b11b0c3fcfde" 1526"checksum pest 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a677051ad923732bb5c70f2d45f8985a96e3eee2e2bff86697e3b11b0c3fcfde"
1502"checksum pest_derive 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b76f477146419bc539a63f4ef40e902166cb43b3e51cecc71d9136fd12c567e7" 1527"checksum pest_derive 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b76f477146419bc539a63f4ef40e902166cb43b3e51cecc71d9136fd12c567e7"
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs
index 51bcd5a73..54f38b285 100644
--- a/crates/ra_analysis/src/imp.rs
+++ b/crates/ra_analysis/src/imp.rs
@@ -43,14 +43,17 @@ impl AnalysisHostImpl {
43 pub fn apply_change(&mut self, change: AnalysisChange) { 43 pub fn apply_change(&mut self, change: AnalysisChange) {
44 log::info!("apply_change {:?}", change); 44 log::info!("apply_change {:?}", change);
45 // self.gc_syntax_trees(); 45 // self.gc_syntax_trees();
46 for root_id in change.new_roots {
47 self.db
48 .query_mut(ra_db::SourceRootQuery)
49 .set(root_id, Default::default());
50 }
46 51
47 for (root_id, root_change) in change.roots_changed { 52 for (root_id, root_change) in change.roots_changed {
48 self.apply_root_change(root_id, root_change); 53 self.apply_root_change(root_id, root_change);
49 } 54 }
50 for (file_id, text) in change.files_changed { 55 for (file_id, text) in change.files_changed {
51 self.db 56 self.db.query_mut(ra_db::FileTextQuery).set(file_id, text)
52 .query_mut(ra_db::FileTextQuery)
53 .set(file_id, Arc::new(text))
54 } 57 }
55 if !change.libraries_added.is_empty() { 58 if !change.libraries_added.is_empty() {
56 let mut libraries = Vec::clone(&self.db.libraries()); 59 let mut libraries = Vec::clone(&self.db.libraries());
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs
index b806c974d..8882feca3 100644
--- a/crates/ra_analysis/src/lib.rs
+++ b/crates/ra_analysis/src/lib.rs
@@ -44,8 +44,9 @@ pub use ra_db::{
44 44
45#[derive(Default)] 45#[derive(Default)]
46pub struct AnalysisChange { 46pub struct AnalysisChange {
47 new_roots: Vec<SourceRootId>,
47 roots_changed: FxHashMap<SourceRootId, RootChange>, 48 roots_changed: FxHashMap<SourceRootId, RootChange>,
48 files_changed: Vec<(FileId, String)>, 49 files_changed: Vec<(FileId, Arc<String>)>,
49 libraries_added: Vec<LibraryData>, 50 libraries_added: Vec<LibraryData>,
50 crate_graph: Option<CrateGraph>, 51 crate_graph: Option<CrateGraph>,
51} 52}
@@ -93,6 +94,9 @@ impl AnalysisChange {
93 pub fn new() -> AnalysisChange { 94 pub fn new() -> AnalysisChange {
94 AnalysisChange::default() 95 AnalysisChange::default()
95 } 96 }
97 pub fn add_root(&mut self, root_id: SourceRootId) {
98 self.new_roots.push(root_id);
99 }
96 pub fn add_file( 100 pub fn add_file(
97 &mut self, 101 &mut self,
98 root_id: SourceRootId, 102 root_id: SourceRootId,
@@ -111,7 +115,7 @@ impl AnalysisChange {
111 .added 115 .added
112 .push(file); 116 .push(file);
113 } 117 }
114 pub fn change_file(&mut self, file_id: FileId, new_text: String) { 118 pub fn change_file(&mut self, file_id: FileId, new_text: Arc<String>) {
115 self.files_changed.push((file_id, new_text)) 119 self.files_changed.push((file_id, new_text))
116 } 120 }
117 pub fn remove_file(&mut self, root_id: SourceRootId, file_id: FileId, path: RelativePathBuf) { 121 pub fn remove_file(&mut self, root_id: SourceRootId, file_id: FileId, path: RelativePathBuf) {
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}
diff --git a/crates/ra_vfs/src/io.rs b/crates/ra_vfs/src/io.rs
index 178c9beff..be400bae9 100644
--- a/crates/ra_vfs/src/io.rs
+++ b/crates/ra_vfs/src/io.rs
@@ -1,4 +1,5 @@
1use std::{ 1use std::{
2 fmt,
2 fs, 3 fs,
3 path::{Path, PathBuf}, 4 path::{Path, PathBuf},
4}; 5};
@@ -20,6 +21,12 @@ pub struct TaskResult {
20 pub(crate) files: Vec<(RelativePathBuf, String)>, 21 pub(crate) files: Vec<(RelativePathBuf, String)>,
21} 22}
22 23
24impl fmt::Debug for TaskResult {
25 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26 f.write_str("TaskResult { ... }")
27 }
28}
29
23pub(crate) type Worker = thread_worker::Worker<Task, TaskResult>; 30pub(crate) type Worker = thread_worker::Worker<Task, TaskResult>;
24 31
25pub(crate) fn start() -> (Worker, WorkerHandle) { 32pub(crate) fn start() -> (Worker, WorkerHandle) {
diff --git a/crates/ra_vfs/src/lib.rs b/crates/ra_vfs/src/lib.rs
index 6c1af5ef9..dc980c3d2 100644
--- a/crates/ra_vfs/src/lib.rs
+++ b/crates/ra_vfs/src/lib.rs
@@ -15,6 +15,7 @@ mod arena;
15mod io; 15mod io;
16 16
17use std::{ 17use std::{
18 fmt,
18 mem, 19 mem,
19 thread, 20 thread,
20 cmp::Reverse, 21 cmp::Reverse,
@@ -34,6 +35,8 @@ use crate::{
34 arena::{ArenaId, Arena}, 35 arena::{ArenaId, Arena},
35}; 36};
36 37
38pub use crate::io::TaskResult as VfsTask;
39
37/// `RootFilter` is a predicate that checks if a file can belong to a root. If 40/// `RootFilter` is a predicate that checks if a file can belong to a root. If
38/// several filters match a file (nested dirs), the most nested one wins. 41/// several filters match a file (nested dirs), the most nested one wins.
39struct RootFilter { 42struct RootFilter {
@@ -68,7 +71,7 @@ fn has_rs_extension(p: &Path) -> bool {
68} 71}
69 72
70#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] 73#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
71pub struct VfsRoot(u32); 74pub struct VfsRoot(pub u32);
72 75
73impl ArenaId for VfsRoot { 76impl ArenaId for VfsRoot {
74 fn from_u32(idx: u32) -> VfsRoot { 77 fn from_u32(idx: u32) -> VfsRoot {
@@ -80,7 +83,7 @@ impl ArenaId for VfsRoot {
80} 83}
81 84
82#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] 85#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
83pub struct VfsFile(u32); 86pub struct VfsFile(pub u32);
84 87
85impl ArenaId for VfsFile { 88impl ArenaId for VfsFile {
86 fn from_u32(idx: u32) -> VfsFile { 89 fn from_u32(idx: u32) -> VfsFile {
@@ -106,8 +109,14 @@ pub struct Vfs {
106 worker_handle: WorkerHandle, 109 worker_handle: WorkerHandle,
107} 110}
108 111
112impl fmt::Debug for Vfs {
113 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
114 f.write_str("Vfs { ... }")
115 }
116}
117
109impl Vfs { 118impl Vfs {
110 pub fn new(mut roots: Vec<PathBuf>) -> Vfs { 119 pub fn new(mut roots: Vec<PathBuf>) -> (Vfs, Vec<VfsRoot>) {
111 let (worker, worker_handle) = io::start(); 120 let (worker, worker_handle) = io::start();
112 121
113 let mut res = Vfs { 122 let mut res = Vfs {
@@ -142,7 +151,32 @@ impl Vfs {
142 }; 151 };
143 res.worker.inp.send(task); 152 res.worker.inp.send(task);
144 } 153 }
145 res 154 let roots = res.roots.iter().map(|(id, _)| id).collect();
155 (res, roots)
156 }
157
158 pub fn path2file(&self, path: &Path) -> Option<VfsFile> {
159 if let Some((_root, _path, Some(file))) = self.find_root(path) {
160 return Some(file);
161 }
162 None
163 }
164
165 pub fn file2path(&self, file: VfsFile) -> PathBuf {
166 let rel_path = &self.files[file].path;
167 let root_path = &self.roots[self.files[file].root].root;
168 rel_path.to_path(root_path)
169 }
170
171 pub fn file_for_path(&self, path: &Path) -> Option<VfsFile> {
172 if let Some((_root, _path, Some(file))) = self.find_root(path) {
173 return Some(file);
174 }
175 None
176 }
177
178 pub fn load(&mut self, path: &Path) -> Option<VfsFile> {
179 None
146 } 180 }
147 181
148 pub fn task_receiver(&self) -> &Receiver<io::TaskResult> { 182 pub fn task_receiver(&self) -> &Receiver<io::TaskResult> {
@@ -163,14 +197,17 @@ impl Vfs {
163 self.pending_changes.push(change); 197 self.pending_changes.push(change);
164 } 198 }
165 199
166 pub fn add_file_overlay(&mut self, path: &Path, text: String) { 200 pub fn add_file_overlay(&mut self, path: &Path, text: String) -> Option<VfsFile> {
201 let mut res = None;
167 if let Some((root, path, file)) = self.find_root(path) { 202 if let Some((root, path, file)) = self.find_root(path) {
168 let text = Arc::new(text); 203 let text = Arc::new(text);
169 let change = if let Some(file) = file { 204 let change = if let Some(file) = file {
205 res = Some(file);
170 self.change_file(file, Arc::clone(&text)); 206 self.change_file(file, Arc::clone(&text));
171 VfsChange::ChangeFile { file, text } 207 VfsChange::ChangeFile { file, text }
172 } else { 208 } else {
173 let file = self.add_file(root, path.clone(), Arc::clone(&text)); 209 let file = self.add_file(root, path.clone(), Arc::clone(&text));
210 res = Some(file);
174 VfsChange::AddFile { 211 VfsChange::AddFile {
175 file, 212 file,
176 text, 213 text,
@@ -180,6 +217,7 @@ impl Vfs {
180 }; 217 };
181 self.pending_changes.push(change); 218 self.pending_changes.push(change);
182 } 219 }
220 res
183 } 221 }
184 222
185 pub fn change_file_overlay(&mut self, path: &Path, new_text: String) { 223 pub fn change_file_overlay(&mut self, path: &Path, new_text: String) {
@@ -192,9 +230,11 @@ impl Vfs {
192 } 230 }
193 } 231 }
194 232
195 pub fn remove_file_overlay(&mut self, path: &Path) { 233 pub fn remove_file_overlay(&mut self, path: &Path) -> Option<VfsFile> {
234 let mut res = None;
196 if let Some((root, path, file)) = self.find_root(path) { 235 if let Some((root, path, file)) = self.find_root(path) {
197 let file = file.expect("can't remove a file which wasn't added"); 236 let file = file.expect("can't remove a file which wasn't added");
237 res = Some(file);
198 let full_path = path.to_path(&self.roots[root].root); 238 let full_path = path.to_path(&self.roots[root].root);
199 let change = if let Ok(text) = fs::read_to_string(&full_path) { 239 let change = if let Ok(text) = fs::read_to_string(&full_path) {
200 let text = Arc::new(text); 240 let text = Arc::new(text);
@@ -206,6 +246,7 @@ impl Vfs {
206 }; 246 };
207 self.pending_changes.push(change); 247 self.pending_changes.push(change);
208 } 248 }
249 res
209 } 250 }
210 251
211 pub fn commit_changes(&mut self) -> Vec<VfsChange> { 252 pub fn commit_changes(&mut self) -> Vec<VfsChange> {
diff --git a/crates/thread_worker/src/lib.rs b/crates/thread_worker/src/lib.rs
index 24d7fcce1..12e8bf17e 100644
--- a/crates/thread_worker/src/lib.rs
+++ b/crates/thread_worker/src/lib.rs
@@ -37,6 +37,9 @@ impl<I, O> Worker<I, O> {
37 pub fn send(&self, item: I) { 37 pub fn send(&self, item: I) {
38 self.inp.send(item) 38 self.inp.send(item)
39 } 39 }
40 pub fn recv(&self) -> Option<O> {
41 self.out.recv()
42 }
40} 43}
41 44
42impl WorkerHandle { 45impl WorkerHandle {