Skip to content

internal: use current folder's rustfmt.toml with all rustfmt configurations #15564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
55 changes: 29 additions & 26 deletions crates/rust-analyzer/src/handlers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ pub(crate) fn handle_hover(
PositionOrRange::Range(range) => range,
};

let file_range = from_proto::file_range(&snap, params.text_document, range)?;
let file_range = from_proto::file_range(&snap, &params.text_document, range)?;
let info = match snap.analysis.hover(&snap.config.hover(), file_range)? {
None => return Ok(None),
Some(info) => info,
Expand Down Expand Up @@ -1130,7 +1130,7 @@ pub(crate) fn handle_code_action(

let line_index =
snap.file_line_index(from_proto::file_id(&snap, &params.text_document.uri)?)?;
let frange = from_proto::file_range(&snap, params.text_document.clone(), params.range)?;
let frange = from_proto::file_range(&snap, &params.text_document, params.range)?;

let mut assists_config = snap.config.assist();
assists_config.allowed = params
Expand Down Expand Up @@ -1383,7 +1383,7 @@ pub(crate) fn handle_ssr(
let selections = params
.selections
.iter()
.map(|range| from_proto::file_range(&snap, params.position.text_document.clone(), *range))
.map(|range| from_proto::file_range(&snap, &params.position.text_document, *range))
.collect::<Result<Vec<_>, _>>()?;
let position = from_proto::file_position(&snap, params.position)?;
let source_change = snap.analysis.structural_search_replace(
Expand All @@ -1403,7 +1403,7 @@ pub(crate) fn handle_inlay_hints(
let document_uri = &params.text_document.uri;
let FileRange { file_id, range } = from_proto::file_range(
&snap,
TextDocumentIdentifier::new(document_uri.to_owned()),
&TextDocumentIdentifier::new(document_uri.to_owned()),
params.range,
)?;
let line_index = snap.file_line_index(file_id)?;
Expand Down Expand Up @@ -1455,7 +1455,7 @@ pub(crate) fn handle_call_hierarchy_incoming(
let item = params.item;

let doc = TextDocumentIdentifier::new(item.uri);
let frange = from_proto::file_range(&snap, doc, item.selection_range)?;
let frange = from_proto::file_range(&snap, &doc, item.selection_range)?;
let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };

let call_items = match snap.analysis.incoming_calls(fpos)? {
Expand Down Expand Up @@ -1490,7 +1490,7 @@ pub(crate) fn handle_call_hierarchy_outgoing(
let item = params.item;

let doc = TextDocumentIdentifier::new(item.uri);
let frange = from_proto::file_range(&snap, doc, item.selection_range)?;
let frange = from_proto::file_range(&snap, &doc, item.selection_range)?;
let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };

let call_items = match snap.analysis.outgoing_calls(fpos)? {
Expand Down Expand Up @@ -1596,7 +1596,7 @@ pub(crate) fn handle_semantic_tokens_range(
) -> anyhow::Result<Option<SemanticTokensRangeResult>> {
let _p = profile::span("handle_semantic_tokens_range");

let frange = from_proto::file_range(&snap, params.text_document, params.range)?;
let frange = from_proto::file_range(&snap, &params.text_document, params.range)?;
let text = snap.analysis.file_text(frange.file_id)?;
let line_index = snap.file_line_index(frange.file_id)?;

Expand Down Expand Up @@ -1679,7 +1679,7 @@ pub(crate) fn handle_move_item(
) -> anyhow::Result<Vec<lsp_ext::SnippetTextEdit>> {
let _p = profile::span("handle_move_item");
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let range = from_proto::file_range(&snap, params.text_document, params.range)?;
let range = from_proto::file_range(&snap, &params.text_document, params.range)?;

let direction = match params.direction {
lsp_ext::MoveItemDirection::Up => ide::Direction::Up,
Expand Down Expand Up @@ -1902,23 +1902,7 @@ fn run_rustfmt(
let mut cmd = process::Command::new(toolchain::rustfmt());
cmd.envs(snap.config.extra_env());
cmd.args(extra_args);
// try to chdir to the file so we can respect `rustfmt.toml`
// FIXME: use `rustfmt --config-path` once
// https://github.com/rust-lang/rustfmt/issues/4660 gets fixed
match text_document.uri.to_file_path() {
Ok(mut path) => {
// pop off file name
if path.pop() && path.is_dir() {
cmd.current_dir(path);
}
}
Err(_) => {
tracing::error!(
"Unable to get file path for {}, rustfmt.toml might be ignored",
text_document.uri
);
}
}

if let Some(edition) = edition {
cmd.arg("--edition");
cmd.arg(edition.to_string());
Expand All @@ -1937,7 +1921,7 @@ fn run_rustfmt(
.into());
}

let frange = from_proto::file_range(snap, text_document, range)?;
let frange = from_proto::file_range(snap, &text_document, range)?;
let start_line = line_index.index.line_col(frange.range.start()).line;
let end_line = line_index.index.line_col(frange.range.end()).line;

Expand All @@ -1956,12 +1940,31 @@ fn run_rustfmt(
}
RustfmtConfig::CustomCommand { command, args } => {
let mut cmd = process::Command::new(command);

cmd.envs(snap.config.extra_env());
cmd.args(args);
cmd
}
};

// try to chdir to the file so we can respect `rustfmt.toml`
// FIXME: use `rustfmt --config-path` once
// https://github.com/rust-lang/rustfmt/issues/4660 gets fixed
match text_document.uri.to_file_path() {
Ok(mut path) => {
// pop off file name
if path.pop() && path.is_dir() {
command.current_dir(path);
}
}
Err(_) => {
tracing::error!(
text_document = ?text_document.uri,
"Unable to get path, rustfmt.toml might be ignored"
);
}
}

let mut rustfmt = command
.stdin(Stdio::piped())
.stdout(Stdio::piped())
Expand Down
2 changes: 1 addition & 1 deletion crates/rust-analyzer/src/lsp/from_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub(crate) fn file_position(

pub(crate) fn file_range(
snap: &GlobalStateSnapshot,
text_document_identifier: lsp_types::TextDocumentIdentifier,
text_document_identifier: &lsp_types::TextDocumentIdentifier,
range: lsp_types::Range,
) -> anyhow::Result<FileRange> {
file_range_uri(snap, &text_document_identifier.uri, range)
Expand Down