Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/rust-analyzer/src/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;

Expand Down
28 changes: 14 additions & 14 deletions crates/rust-analyzer/src/handlers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
Expand Down Expand Up @@ -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(_)) => {
Expand Down