Unnamed repository; edit this file 'description' to name the repository.
-rw-r--r--crates/rust-analyzer/src/global_state.rs8
-rw-r--r--crates/rust-analyzer/src/handlers/request.rs28
2 files changed, 22 insertions, 14 deletions
diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs
index f557dd5cb0..91f7db7854 100644
--- a/crates/rust-analyzer/src/global_state.rs
+++ b/crates/rust-analyzer/src/global_state.rs
@@ -781,6 +781,14 @@ impl GlobalStateSnapshot {
pub(crate) fn target_spec_for_crate(&self, crate_id: Crate) -> Option<TargetSpec> {
let file_id = self.analysis.crate_root(crate_id).ok()?;
+ self.target_spec_for_file(file_id, crate_id)
+ }
+
+ pub(crate) fn target_spec_for_file(
+ &self,
+ file_id: FileId,
+ crate_id: Crate,
+ ) -> Option<TargetSpec> {
let path = self.vfs_read().file_path(file_id).clone();
let path = path.as_path()?;
diff --git a/crates/rust-analyzer/src/handlers/request.rs b/crates/rust-analyzer/src/handlers/request.rs
index 7e52673690..d15b519d69 100644
--- a/crates/rust-analyzer/src/handlers/request.rs
+++ b/crates/rust-analyzer/src/handlers/request.rs
@@ -2348,21 +2348,9 @@ fn run_rustfmt(
let file_id = try_default!(from_proto::file_id(snap, &text_document.uri)?);
let file = snap.analysis.file_text(file_id)?;
- // Determine the edition of the crate the file belongs to (if there's multiple, we pick the
- // highest edition).
- let Ok(editions) = snap
- .analysis
- .relevant_crates_for(file_id)?
- .into_iter()
- .map(|crate_id| snap.analysis.crate_edition(crate_id))
- .collect::<Result<Vec<_>, _>>()
- else {
- return Ok(None);
- };
- let edition = editions.iter().copied().max();
-
let line_index = snap.file_line_index(file_id)?;
let source_root_id = snap.analysis.source_root_id(file_id).ok();
+ let crates = snap.analysis.relevant_crates_for(file_id)?;
// try to chdir to the file so we can respect `rustfmt.toml`
// FIXME: use `rustfmt --config-path` once
@@ -2383,6 +2371,17 @@ fn run_rustfmt(
let mut command = match snap.config.rustfmt(source_root_id) {
RustfmtConfig::Rustfmt { extra_args, enable_range_formatting } => {
+ // Determine the edition of the crate the file belongs to (if there's multiple, we pick the
+ // highest edition).
+ let Ok(editions) = crates
+ .iter()
+ .map(|&crate_id| snap.analysis.crate_edition(crate_id))
+ .collect::<Result<Vec<_>, _>>()
+ else {
+ return Ok(None);
+ };
+ let edition = editions.iter().copied().max();
+
// FIXME: Set RUSTUP_TOOLCHAIN
let mut cmd = toolchain::command(
toolchain::Tool::Rustfmt.path(),
@@ -2429,7 +2428,8 @@ fn run_rustfmt(
}
RustfmtConfig::CustomCommand { command, args } => {
let cmd = Utf8PathBuf::from(&command);
- let target_spec = TargetSpec::for_file(snap, file_id)?;
+ let target_spec =
+ crates.first().and_then(|&crate_id| snap.target_spec_for_file(file_id, crate_id));
let extra_env = snap.config.extra_env(source_root_id);
let mut cmd = match target_spec {
Some(TargetSpec::Cargo(_)) => {