Auto merge of #15564 - davidbarsky:davidbarsky/use-current-subcrates-rustfmt-toml-with-custom-command, r=Veykril
internal: use current folder's `rustfmt.toml` with all rustfmt configurations ## Introduction Resolves https://github.com/rust-lang/rust-analyzer/issues/15540. I moved the `chdir` functionality outside of the `match` to ensure that this functionality wouldn’t fall through again. As part of this PR, I also changed `from_proto::file_range` to accept a `TextDocumentIdentifier` by reference instead of by value, but I can undo this change if desired. ## Testing I added a `rustfmt.toml` will the contents below at `crates/rust-analyzer/rustfmt.toml`: ```toml reorder_modules = false use_small_heuristics = "Max" # this is the only difference from the `rustfmt.toml` at the root of the repo tab_spaces = 8 ``` In addition, I've also added `"rust-analyzer.rustfmt.overrideCommand": ["rustfmt"]` to my VS Code configuration. With the above changes, saving `crates/rust-analyzer/src/handlers/request.rs` results in 8-space indentation. Meanwhile, saving `crates/toolchain/src/lib.rs` _does not_ result in any formatting changes.
This commit is contained in:
commit
a843e4699e
@ -974,7 +974,7 @@ pub(crate) fn handle_hover(
|
|||||||
PositionOrRange::Range(range) => range,
|
PositionOrRange::Range(range) => range,
|
||||||
};
|
};
|
||||||
|
|
||||||
let file_range = from_proto::file_range(&snap, params.text_document, range)?;
|
let file_range = from_proto::file_range(&snap, ¶ms.text_document, range)?;
|
||||||
let info = match snap.analysis.hover(&snap.config.hover(), file_range)? {
|
let info = match snap.analysis.hover(&snap.config.hover(), file_range)? {
|
||||||
None => return Ok(None),
|
None => return Ok(None),
|
||||||
Some(info) => info,
|
Some(info) => info,
|
||||||
@ -1130,7 +1130,7 @@ pub(crate) fn handle_code_action(
|
|||||||
|
|
||||||
let line_index =
|
let line_index =
|
||||||
snap.file_line_index(from_proto::file_id(&snap, ¶ms.text_document.uri)?)?;
|
snap.file_line_index(from_proto::file_id(&snap, ¶ms.text_document.uri)?)?;
|
||||||
let frange = from_proto::file_range(&snap, params.text_document.clone(), params.range)?;
|
let frange = from_proto::file_range(&snap, ¶ms.text_document, params.range)?;
|
||||||
|
|
||||||
let mut assists_config = snap.config.assist();
|
let mut assists_config = snap.config.assist();
|
||||||
assists_config.allowed = params
|
assists_config.allowed = params
|
||||||
@ -1383,7 +1383,7 @@ pub(crate) fn handle_ssr(
|
|||||||
let selections = params
|
let selections = params
|
||||||
.selections
|
.selections
|
||||||
.iter()
|
.iter()
|
||||||
.map(|range| from_proto::file_range(&snap, params.position.text_document.clone(), *range))
|
.map(|range| from_proto::file_range(&snap, ¶ms.position.text_document, *range))
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
let position = from_proto::file_position(&snap, params.position)?;
|
let position = from_proto::file_position(&snap, params.position)?;
|
||||||
let source_change = snap.analysis.structural_search_replace(
|
let source_change = snap.analysis.structural_search_replace(
|
||||||
@ -1403,7 +1403,7 @@ pub(crate) fn handle_inlay_hints(
|
|||||||
let document_uri = ¶ms.text_document.uri;
|
let document_uri = ¶ms.text_document.uri;
|
||||||
let FileRange { file_id, range } = from_proto::file_range(
|
let FileRange { file_id, range } = from_proto::file_range(
|
||||||
&snap,
|
&snap,
|
||||||
TextDocumentIdentifier::new(document_uri.to_owned()),
|
&TextDocumentIdentifier::new(document_uri.to_owned()),
|
||||||
params.range,
|
params.range,
|
||||||
)?;
|
)?;
|
||||||
let line_index = snap.file_line_index(file_id)?;
|
let line_index = snap.file_line_index(file_id)?;
|
||||||
@ -1455,7 +1455,7 @@ pub(crate) fn handle_call_hierarchy_incoming(
|
|||||||
let item = params.item;
|
let item = params.item;
|
||||||
|
|
||||||
let doc = TextDocumentIdentifier::new(item.uri);
|
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 fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
|
||||||
|
|
||||||
let call_items = match snap.analysis.incoming_calls(fpos)? {
|
let call_items = match snap.analysis.incoming_calls(fpos)? {
|
||||||
@ -1490,7 +1490,7 @@ pub(crate) fn handle_call_hierarchy_outgoing(
|
|||||||
let item = params.item;
|
let item = params.item;
|
||||||
|
|
||||||
let doc = TextDocumentIdentifier::new(item.uri);
|
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 fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
|
||||||
|
|
||||||
let call_items = match snap.analysis.outgoing_calls(fpos)? {
|
let call_items = match snap.analysis.outgoing_calls(fpos)? {
|
||||||
@ -1596,7 +1596,7 @@ pub(crate) fn handle_semantic_tokens_range(
|
|||||||
) -> anyhow::Result<Option<SemanticTokensRangeResult>> {
|
) -> anyhow::Result<Option<SemanticTokensRangeResult>> {
|
||||||
let _p = profile::span("handle_semantic_tokens_range");
|
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, ¶ms.text_document, params.range)?;
|
||||||
let text = snap.analysis.file_text(frange.file_id)?;
|
let text = snap.analysis.file_text(frange.file_id)?;
|
||||||
let line_index = snap.file_line_index(frange.file_id)?;
|
let line_index = snap.file_line_index(frange.file_id)?;
|
||||||
|
|
||||||
@ -1679,7 +1679,7 @@ pub(crate) fn handle_move_item(
|
|||||||
) -> anyhow::Result<Vec<lsp_ext::SnippetTextEdit>> {
|
) -> anyhow::Result<Vec<lsp_ext::SnippetTextEdit>> {
|
||||||
let _p = profile::span("handle_move_item");
|
let _p = profile::span("handle_move_item");
|
||||||
let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
|
let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
|
||||||
let range = from_proto::file_range(&snap, params.text_document, params.range)?;
|
let range = from_proto::file_range(&snap, ¶ms.text_document, params.range)?;
|
||||||
|
|
||||||
let direction = match params.direction {
|
let direction = match params.direction {
|
||||||
lsp_ext::MoveItemDirection::Up => ide::Direction::Up,
|
lsp_ext::MoveItemDirection::Up => ide::Direction::Up,
|
||||||
@ -1902,23 +1902,7 @@ fn run_rustfmt(
|
|||||||
let mut cmd = process::Command::new(toolchain::rustfmt());
|
let mut cmd = process::Command::new(toolchain::rustfmt());
|
||||||
cmd.envs(snap.config.extra_env());
|
cmd.envs(snap.config.extra_env());
|
||||||
cmd.args(extra_args);
|
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 {
|
if let Some(edition) = edition {
|
||||||
cmd.arg("--edition");
|
cmd.arg("--edition");
|
||||||
cmd.arg(edition.to_string());
|
cmd.arg(edition.to_string());
|
||||||
@ -1937,7 +1921,7 @@ fn run_rustfmt(
|
|||||||
.into());
|
.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 start_line = line_index.index.line_col(frange.range.start()).line;
|
||||||
let end_line = line_index.index.line_col(frange.range.end()).line;
|
let end_line = line_index.index.line_col(frange.range.end()).line;
|
||||||
|
|
||||||
@ -1956,12 +1940,31 @@ fn run_rustfmt(
|
|||||||
}
|
}
|
||||||
RustfmtConfig::CustomCommand { command, args } => {
|
RustfmtConfig::CustomCommand { command, args } => {
|
||||||
let mut cmd = process::Command::new(command);
|
let mut cmd = process::Command::new(command);
|
||||||
|
|
||||||
cmd.envs(snap.config.extra_env());
|
cmd.envs(snap.config.extra_env());
|
||||||
cmd.args(args);
|
cmd.args(args);
|
||||||
cmd
|
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
|
let mut rustfmt = command
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
|
@ -72,7 +72,7 @@ pub(crate) fn file_position(
|
|||||||
|
|
||||||
pub(crate) fn file_range(
|
pub(crate) fn file_range(
|
||||||
snap: &GlobalStateSnapshot,
|
snap: &GlobalStateSnapshot,
|
||||||
text_document_identifier: lsp_types::TextDocumentIdentifier,
|
text_document_identifier: &lsp_types::TextDocumentIdentifier,
|
||||||
range: lsp_types::Range,
|
range: lsp_types::Range,
|
||||||
) -> anyhow::Result<FileRange> {
|
) -> anyhow::Result<FileRange> {
|
||||||
file_range_uri(snap, &text_document_identifier.uri, range)
|
file_range_uri(snap, &text_document_identifier.uri, range)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user