Skip to content

Commit

Permalink
Auto merge of #13428 - Veykril:fmt-stuck, r=Veykril
Browse files Browse the repository at this point in the history
fix: Fix formatting requests hanging when r-a is still starting

The reason for that was that we were calculating the crate defmaps of the file we are saving by accident causing us to get stuck waiting on their expensive computation, while we only need the relevant crate id.

Closes #4054
Closes #11654
  • Loading branch information
bors committed Oct 17, 2022
2 parents 106285b + a762bac commit 4d4c05d
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 24 deletions.
4 changes: 2 additions & 2 deletions crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,8 @@ impl Analysis {
}

/// Returns crates this file belongs too.
pub fn crate_for(&self, file_id: FileId) -> Cancellable<Vec<CrateId>> {
self.with_db(|db| parent_module::crate_for(db, file_id))
pub fn crates_for(&self, file_id: FileId) -> Cancellable<Vec<CrateId>> {
self.with_db(|db| parent_module::crates_for(db, file_id))
}

/// Returns the edition of the given crate.
Expand Down
12 changes: 5 additions & 7 deletions crates/ide/src/parent_module.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use hir::Semantics;
use ide_db::{
base_db::{CrateId, FileId, FilePosition},
base_db::{CrateId, FileId, FileLoader, FilePosition},
RootDatabase,
};
use itertools::Itertools;
use syntax::{
algo::find_node_at_offset,
ast::{self, AstNode},
Expand Down Expand Up @@ -55,9 +54,8 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<Na
}

/// Returns `Vec` for the same reason as `parent_module`
pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
let sema = Semantics::new(db);
sema.to_module_defs(file_id).map(|module| module.krate().into()).unique().collect()
pub(crate) fn crates_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
db.relevant_crates(file_id).iter().copied().collect()
}

#[cfg(test)]
Expand Down Expand Up @@ -147,7 +145,7 @@ $0
mod foo;
"#,
);
assert_eq!(analysis.crate_for(file_id).unwrap().len(), 1);
assert_eq!(analysis.crates_for(file_id).unwrap().len(), 1);
}

#[test]
Expand All @@ -162,6 +160,6 @@ mod baz;
mod baz;
"#,
);
assert_eq!(analysis.crate_for(file_id).unwrap().len(), 2);
assert_eq!(analysis.crates_for(file_id).unwrap().len(), 2);
}
}
2 changes: 1 addition & 1 deletion crates/ide/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub(crate) fn status(db: &RootDatabase, file_id: Option<FileId>) -> String {

if let Some(file_id) = file_id {
format_to!(buf, "\nFile info:\n");
let crates = crate::parent_module::crate_for(db, file_id);
let crates = crate::parent_module::crates_for(db, file_id);
if crates.is_empty() {
format_to!(buf, "Does not belong to any crate");
}
Expand Down
6 changes: 1 addition & 5 deletions crates/rust-analyzer/src/cargo_target_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ impl CargoTargetSpec {
global_state_snapshot: &GlobalStateSnapshot,
file_id: FileId,
) -> Result<Option<CargoTargetSpec>> {
let crate_id = match &*global_state_snapshot.analysis.crate_for(file_id)? {
&[crate_id, ..] => crate_id,
_ => return Ok(None),
};
let (cargo_ws, target) = match global_state_snapshot.cargo_target_for_crate_root(crate_id) {
let (cargo_ws, target) = match global_state_snapshot.cargo_target_for_file_id(file_id) {
Some(it) => it,
None => return Ok(None),
};
Expand Down
4 changes: 2 additions & 2 deletions crates/rust-analyzer/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<'a> RequestDispatcher<'a> {
let _pctx = stdx::panic_context::enter(panic_context);
f(self.global_state, params)
};
if let Ok(response) = result_to_response::<R>(req.id.clone(), result) {
if let Ok(response) = result_to_response::<R>(req.id, result) {
self.global_state.respond(response);
}

Expand Down Expand Up @@ -80,7 +80,7 @@ impl<'a> RequestDispatcher<'a> {
f(global_state_snapshot, params)
});

if let Ok(response) = thread_result_to_response::<R>(req.id.clone(), result) {
if let Ok(response) = thread_result_to_response::<R>(req.id, result) {
self.global_state.respond(response);
}

Expand Down
7 changes: 3 additions & 4 deletions crates/rust-analyzer/src/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{sync::Arc, time::Instant};
use crossbeam_channel::{unbounded, Receiver, Sender};
use flycheck::FlycheckHandle;
use ide::{Analysis, AnalysisHost, Cancellable, Change, FileId};
use ide_db::base_db::{CrateId, FileLoader, SourceDatabase};
use ide_db::base_db::{FileLoader, SourceDatabase};
use lsp_types::{SemanticTokens, Url};
use parking_lot::{Mutex, RwLock};
use proc_macro_api::ProcMacroServer;
Expand Down Expand Up @@ -398,11 +398,10 @@ impl GlobalStateSnapshot {
url_from_abs_path(path)
}

pub(crate) fn cargo_target_for_crate_root(
pub(crate) fn cargo_target_for_file_id(
&self,
crate_id: CrateId,
file_id: FileId,
) -> Option<(&CargoWorkspace, Target)> {
let file_id = self.analysis.crate_root(crate_id).ok()?;
let path = self.vfs.read().0.file_path(file_id);
let path = path.as_path()?;
self.workspaces.iter().find_map(|ws| match ws {
Expand Down
4 changes: 2 additions & 2 deletions crates/rust-analyzer/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ pub(crate) fn handle_parent_module(

// check if invoked at the crate root
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let crate_id = match snap.analysis.crate_for(file_id)?.first() {
let crate_id = match snap.analysis.crates_for(file_id)?.first() {
Some(&crate_id) => crate_id,
None => return Ok(None),
};
Expand Down Expand Up @@ -1782,7 +1782,7 @@ fn run_rustfmt(
) -> Result<Option<Vec<lsp_types::TextEdit>>> {
let file_id = from_proto::file_id(snap, &text_document.uri)?;
let file = snap.analysis.file_text(file_id)?;
let crate_ids = snap.analysis.crate_for(file_id)?;
let crate_ids = snap.analysis.crates_for(file_id)?;

let line_index = snap.file_line_index(file_id)?;

Expand Down
2 changes: 1 addition & 1 deletion crates/rust-analyzer/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ impl GlobalState {
let analysis = this.analysis_host.analysis();
// Crates containing or depending on the saved file
let crate_ids: Vec<_> = analysis
.crate_for(file_id)?
.crates_for(file_id)?
.into_iter()
.flat_map(|id| {
this.analysis_host
Expand Down

0 comments on commit 4d4c05d

Please sign in to comment.