aboutsummaryrefslogtreecommitdiff
path: root/crates/rust-analyzer
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-06-18 07:29:34 +0100
committerAleksey Kladov <[email protected]>2020-06-18 07:29:34 +0100
commitd1d0b5a88c666048c21fd225a08170fcc06060e5 (patch)
tree6b647cb5279784687a9b2d416ef87b19d97f1ea1 /crates/rust-analyzer
parent1ce8c2b5a08949e78de7ff9dfbae594f6c17b951 (diff)
Remove special casing for library symbols
We might as well handle them internally, via queries. I am not sure, but it looks like the current LibraryData setup might even predate salsa? It's not really needed and creates a bunch of complexity.
Diffstat (limited to 'crates/rust-analyzer')
-rw-r--r--crates/rust-analyzer/src/global_state.rs37
-rw-r--r--crates/rust-analyzer/src/main_loop.rs58
2 files changed, 11 insertions, 84 deletions
diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs
index ef6c7d44d..ee1d37ea2 100644
--- a/crates/rust-analyzer/src/global_state.rs
+++ b/crates/rust-analyzer/src/global_state.rs
@@ -12,12 +12,9 @@ use crossbeam_channel::{unbounded, Receiver};
12use lsp_types::Url; 12use lsp_types::Url;
13use parking_lot::RwLock; 13use parking_lot::RwLock;
14use ra_flycheck::{Flycheck, FlycheckConfig}; 14use ra_flycheck::{Flycheck, FlycheckConfig};
15use ra_ide::{ 15use ra_ide::{Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, SourceRootId};
16 Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId,
17};
18use ra_project_model::{CargoWorkspace, ProcMacroClient, ProjectWorkspace, Target}; 16use ra_project_model::{CargoWorkspace, ProcMacroClient, ProjectWorkspace, Target};
19use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsTask, Watch}; 17use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsTask, Watch};
20use relative_path::RelativePathBuf;
21use stdx::format_to; 18use stdx::format_to;
22 19
23use crate::{ 20use crate::{
@@ -195,32 +192,18 @@ impl GlobalState {
195 192
196 /// Returns a vec of libraries 193 /// Returns a vec of libraries
197 /// FIXME: better API here 194 /// FIXME: better API here
198 pub fn process_changes( 195 pub fn process_changes(&mut self, roots_scanned: &mut usize) -> bool {
199 &mut self,
200 roots_scanned: &mut usize,
201 ) -> Option<Vec<(SourceRootId, Vec<(FileId, RelativePathBuf, Arc<String>)>)>> {
202 let changes = self.vfs.write().commit_changes(); 196 let changes = self.vfs.write().commit_changes();
203 if changes.is_empty() { 197 if changes.is_empty() {
204 return None; 198 return false;
205 } 199 }
206 let mut libs = Vec::new();
207 let mut change = AnalysisChange::new(); 200 let mut change = AnalysisChange::new();
208 for c in changes { 201 for c in changes {
209 match c { 202 match c {
210 VfsChange::AddRoot { root, files } => { 203 VfsChange::AddRoot { root, files } => {
211 let root_path = self.vfs.read().root2path(root); 204 *roots_scanned += 1;
212 let is_local = self.local_roots.iter().any(|r| root_path.starts_with(r)); 205 for (file, path, text) in files {
213 if is_local { 206 change.add_file(SourceRootId(root.0), FileId(file.0), path, text);
214 *roots_scanned += 1;
215 for (file, path, text) in files {
216 change.add_file(SourceRootId(root.0), FileId(file.0), path, text);
217 }
218 } else {
219 let files = files
220 .into_iter()
221 .map(|(vfsfile, path, text)| (FileId(vfsfile.0), path, text))
222 .collect();
223 libs.push((SourceRootId(root.0), files));
224 } 207 }
225 } 208 }
226 VfsChange::AddFile { root, file, path, text } => { 209 VfsChange::AddFile { root, file, path, text } => {
@@ -235,13 +218,7 @@ impl GlobalState {
235 } 218 }
236 } 219 }
237 self.analysis_host.apply_change(change); 220 self.analysis_host.apply_change(change);
238 Some(libs) 221 true
239 }
240
241 pub fn add_lib(&mut self, data: LibraryData) {
242 let mut change = AnalysisChange::new();
243 change.add_library(data);
244 self.analysis_host.apply_change(change);
245 } 222 }
246 223
247 pub fn snapshot(&self) -> GlobalStateSnapshot { 224 pub fn snapshot(&self) -> GlobalStateSnapshot {
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs
index 80cfd3c28..08b0a5a16 100644
--- a/crates/rust-analyzer/src/main_loop.rs
+++ b/crates/rust-analyzer/src/main_loop.rs
@@ -24,11 +24,10 @@ use lsp_types::{
24 WorkDoneProgressReport, 24 WorkDoneProgressReport,
25}; 25};
26use ra_flycheck::{CheckTask, Status}; 26use ra_flycheck::{CheckTask, Status};
27use ra_ide::{Canceled, FileId, LibraryData, LineIndex, SourceRootId}; 27use ra_ide::{Canceled, FileId, LineIndex};
28use ra_prof::profile; 28use ra_prof::profile;
29use ra_project_model::{PackageRoot, ProjectWorkspace}; 29use ra_project_model::{PackageRoot, ProjectWorkspace};
30use ra_vfs::{VfsTask, Watch}; 30use ra_vfs::{VfsTask, Watch};
31use relative_path::RelativePathBuf;
32use rustc_hash::FxHashSet; 31use rustc_hash::FxHashSet;
33use serde::{de::DeserializeOwned, Serialize}; 32use serde::{de::DeserializeOwned, Serialize};
34use threadpool::ThreadPool; 33use threadpool::ThreadPool;
@@ -174,12 +173,10 @@ pub fn main_loop(config: Config, connection: Connection) -> Result<()> {
174 173
175 let pool = ThreadPool::default(); 174 let pool = ThreadPool::default();
176 let (task_sender, task_receiver) = unbounded::<Task>(); 175 let (task_sender, task_receiver) = unbounded::<Task>();
177 let (libdata_sender, libdata_receiver) = unbounded::<LibraryData>();
178 176
179 log::info!("server initialized, serving requests"); 177 log::info!("server initialized, serving requests");
180 { 178 {
181 let task_sender = task_sender; 179 let task_sender = task_sender;
182 let libdata_sender = libdata_sender;
183 loop { 180 loop {
184 log::trace!("selecting"); 181 log::trace!("selecting");
185 let event = select! { 182 let event = select! {
@@ -192,7 +189,6 @@ pub fn main_loop(config: Config, connection: Connection) -> Result<()> {
192 Ok(task) => Event::Vfs(task), 189 Ok(task) => Event::Vfs(task),
193 Err(RecvError) => return Err("vfs died".into()), 190 Err(RecvError) => return Err("vfs died".into()),
194 }, 191 },
195 recv(libdata_receiver) -> data => Event::Lib(data.unwrap()),
196 recv(global_state.flycheck.as_ref().map_or(&never(), |it| &it.task_recv)) -> task => match task { 192 recv(global_state.flycheck.as_ref().map_or(&never(), |it| &it.task_recv)) -> task => match task {
197 Ok(task) => Event::CheckWatcher(task), 193 Ok(task) => Event::CheckWatcher(task),
198 Err(RecvError) => return Err("check watcher died".into()), 194 Err(RecvError) => return Err("check watcher died".into()),
@@ -203,15 +199,7 @@ pub fn main_loop(config: Config, connection: Connection) -> Result<()> {
203 break; 199 break;
204 }; 200 };
205 } 201 }
206 loop_turn( 202 loop_turn(&pool, &task_sender, &connection, &mut global_state, &mut loop_state, event)?;
207 &pool,
208 &task_sender,
209 &libdata_sender,
210 &connection,
211 &mut global_state,
212 &mut loop_state,
213 event,
214 )?;
215 } 203 }
216 } 204 }
217 global_state.analysis_host.request_cancellation(); 205 global_state.analysis_host.request_cancellation();
@@ -219,7 +207,6 @@ pub fn main_loop(config: Config, connection: Connection) -> Result<()> {
219 task_receiver.into_iter().for_each(|task| { 207 task_receiver.into_iter().for_each(|task| {
220 on_task(task, &connection.sender, &mut loop_state.pending_requests, &mut global_state) 208 on_task(task, &connection.sender, &mut loop_state.pending_requests, &mut global_state)
221 }); 209 });
222 libdata_receiver.into_iter().for_each(drop);
223 log::info!("...tasks have finished"); 210 log::info!("...tasks have finished");
224 log::info!("joining threadpool..."); 211 log::info!("joining threadpool...");
225 pool.join(); 212 pool.join();
@@ -243,7 +230,6 @@ enum Event {
243 Msg(Message), 230 Msg(Message),
244 Task(Task), 231 Task(Task),
245 Vfs(VfsTask), 232 Vfs(VfsTask),
246 Lib(LibraryData),
247 CheckWatcher(CheckTask), 233 CheckWatcher(CheckTask),
248} 234}
249 235
@@ -279,7 +265,6 @@ impl fmt::Debug for Event {
279 Event::Msg(it) => fmt::Debug::fmt(it, f), 265 Event::Msg(it) => fmt::Debug::fmt(it, f),
280 Event::Task(it) => fmt::Debug::fmt(it, f), 266 Event::Task(it) => fmt::Debug::fmt(it, f),
281 Event::Vfs(it) => fmt::Debug::fmt(it, f), 267 Event::Vfs(it) => fmt::Debug::fmt(it, f),
282 Event::Lib(it) => fmt::Debug::fmt(it, f),
283 Event::CheckWatcher(it) => fmt::Debug::fmt(it, f), 268 Event::CheckWatcher(it) => fmt::Debug::fmt(it, f),
284 } 269 }
285 } 270 }
@@ -291,10 +276,6 @@ struct LoopState {
291 pending_responses: FxHashSet<RequestId>, 276 pending_responses: FxHashSet<RequestId>,
292 pending_requests: PendingRequests, 277 pending_requests: PendingRequests,
293 subscriptions: Subscriptions, 278 subscriptions: Subscriptions,
294 // We try not to index more than MAX_IN_FLIGHT_LIBS libraries at the same
295 // time to always have a thread ready to react to input.
296 in_flight_libraries: usize,
297 pending_libraries: Vec<(SourceRootId, Vec<(FileId, RelativePathBuf, Arc<String>)>)>,
298 workspace_loaded: bool, 279 workspace_loaded: bool,
299 roots_progress_reported: Option<usize>, 280 roots_progress_reported: Option<usize>,
300 roots_scanned: usize, 281 roots_scanned: usize,
@@ -315,7 +296,6 @@ impl LoopState {
315fn loop_turn( 296fn loop_turn(
316 pool: &ThreadPool, 297 pool: &ThreadPool,
317 task_sender: &Sender<Task>, 298 task_sender: &Sender<Task>,
318 libdata_sender: &Sender<LibraryData>,
319 connection: &Connection, 299 connection: &Connection,
320 global_state: &mut GlobalState, 300 global_state: &mut GlobalState,
321 loop_state: &mut LoopState, 301 loop_state: &mut LoopState,
@@ -339,12 +319,6 @@ fn loop_turn(
339 Event::Vfs(task) => { 319 Event::Vfs(task) => {
340 global_state.vfs.write().handle_task(task); 320 global_state.vfs.write().handle_task(task);
341 } 321 }
342 Event::Lib(lib) => {
343 global_state.add_lib(lib);
344 global_state.maybe_collect_garbage();
345 loop_state.in_flight_libraries -= 1;
346 loop_state.roots_scanned += 1;
347 }
348 Event::CheckWatcher(task) => on_check_task(task, global_state, task_sender)?, 322 Event::CheckWatcher(task) => on_check_task(task, global_state, task_sender)?,
349 Event::Msg(msg) => match msg { 323 Event::Msg(msg) => match msg {
350 Message::Request(req) => on_request( 324 Message::Request(req) => on_request(
@@ -390,36 +364,12 @@ fn loop_turn(
390 }, 364 },
391 }; 365 };
392 366
393 let mut state_changed = false; 367 let mut state_changed = global_state.process_changes(&mut loop_state.roots_scanned);
394 if let Some(changes) = global_state.process_changes(&mut loop_state.roots_scanned) {
395 state_changed = true;
396 loop_state.pending_libraries.extend(changes);
397 }
398
399 let max_in_flight_libs = pool.max_count().saturating_sub(2).max(1);
400 while loop_state.in_flight_libraries < max_in_flight_libs {
401 let (root, files) = match loop_state.pending_libraries.pop() {
402 Some(it) => it,
403 None => break,
404 };
405
406 loop_state.in_flight_libraries += 1;
407 let sender = libdata_sender.clone();
408 pool.execute(move || {
409 log::info!("indexing {:?} ... ", root);
410 let data = LibraryData::prepare(root, files);
411 sender.send(data).unwrap();
412 });
413 }
414 368
415 let show_progress = 369 let show_progress =
416 !loop_state.workspace_loaded && global_state.config.client_caps.work_done_progress; 370 !loop_state.workspace_loaded && global_state.config.client_caps.work_done_progress;
417 371
418 if !loop_state.workspace_loaded 372 if !loop_state.workspace_loaded && loop_state.roots_scanned == loop_state.roots_total {
419 && loop_state.roots_scanned == loop_state.roots_total
420 && loop_state.pending_libraries.is_empty()
421 && loop_state.in_flight_libraries == 0
422 {
423 state_changed = true; 373 state_changed = true;
424 loop_state.workspace_loaded = true; 374 loop_state.workspace_loaded = true;
425 if let Some(flycheck) = &global_state.flycheck { 375 if let Some(flycheck) = &global_state.flycheck {