Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a843e46

Browse files
committed
Auto merge of rust-lang#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 rust-lang/rust-analyzer#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.
2 parents f29867b + 42f77f8 commit a843e46

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

crates/rust-analyzer/src/handlers/request.rs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ pub(crate) fn handle_hover(
974974
PositionOrRange::Range(range) => range,
975975
};
976976

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

11311131
let line_index =
11321132
snap.file_line_index(from_proto::file_id(&snap, &params.text_document.uri)?)?;
1133-
let frange = from_proto::file_range(&snap, params.text_document.clone(), params.range)?;
1133+
let frange = from_proto::file_range(&snap, &params.text_document, params.range)?;
11341134

11351135
let mut assists_config = snap.config.assist();
11361136
assists_config.allowed = params
@@ -1383,7 +1383,7 @@ pub(crate) fn handle_ssr(
13831383
let selections = params
13841384
.selections
13851385
.iter()
1386-
.map(|range| from_proto::file_range(&snap, params.position.text_document.clone(), *range))
1386+
.map(|range| from_proto::file_range(&snap, &params.position.text_document, *range))
13871387
.collect::<Result<Vec<_>, _>>()?;
13881388
let position = from_proto::file_position(&snap, params.position)?;
13891389
let source_change = snap.analysis.structural_search_replace(
@@ -1403,7 +1403,7 @@ pub(crate) fn handle_inlay_hints(
14031403
let document_uri = &params.text_document.uri;
14041404
let FileRange { file_id, range } = from_proto::file_range(
14051405
&snap,
1406-
TextDocumentIdentifier::new(document_uri.to_owned()),
1406+
&TextDocumentIdentifier::new(document_uri.to_owned()),
14071407
params.range,
14081408
)?;
14091409
let line_index = snap.file_line_index(file_id)?;
@@ -1455,7 +1455,7 @@ pub(crate) fn handle_call_hierarchy_incoming(
14551455
let item = params.item;
14561456

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

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

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

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

1599-
let frange = from_proto::file_range(&snap, params.text_document, params.range)?;
1599+
let frange = from_proto::file_range(&snap, &params.text_document, params.range)?;
16001600
let text = snap.analysis.file_text(frange.file_id)?;
16011601
let line_index = snap.file_line_index(frange.file_id)?;
16021602

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

16841684
let direction = match params.direction {
16851685
lsp_ext::MoveItemDirection::Up => ide::Direction::Up,
@@ -1902,23 +1902,7 @@ fn run_rustfmt(
19021902
let mut cmd = process::Command::new(toolchain::rustfmt());
19031903
cmd.envs(snap.config.extra_env());
19041904
cmd.args(extra_args);
1905-
// try to chdir to the file so we can respect `rustfmt.toml`
1906-
// FIXME: use `rustfmt --config-path` once
1907-
// https://github.com/rust-lang/rustfmt/issues/4660 gets fixed
1908-
match text_document.uri.to_file_path() {
1909-
Ok(mut path) => {
1910-
// pop off file name
1911-
if path.pop() && path.is_dir() {
1912-
cmd.current_dir(path);
1913-
}
1914-
}
1915-
Err(_) => {
1916-
tracing::error!(
1917-
"Unable to get file path for {}, rustfmt.toml might be ignored",
1918-
text_document.uri
1919-
);
1920-
}
1921-
}
1905+
19221906
if let Some(edition) = edition {
19231907
cmd.arg("--edition");
19241908
cmd.arg(edition.to_string());
@@ -1937,7 +1921,7 @@ fn run_rustfmt(
19371921
.into());
19381922
}
19391923

1940-
let frange = from_proto::file_range(snap, text_document, range)?;
1924+
let frange = from_proto::file_range(snap, &text_document, range)?;
19411925
let start_line = line_index.index.line_col(frange.range.start()).line;
19421926
let end_line = line_index.index.line_col(frange.range.end()).line;
19431927

@@ -1956,12 +1940,31 @@ fn run_rustfmt(
19561940
}
19571941
RustfmtConfig::CustomCommand { command, args } => {
19581942
let mut cmd = process::Command::new(command);
1943+
19591944
cmd.envs(snap.config.extra_env());
19601945
cmd.args(args);
19611946
cmd
19621947
}
19631948
};
19641949

1950+
// try to chdir to the file so we can respect `rustfmt.toml`
1951+
// FIXME: use `rustfmt --config-path` once
1952+
// https://github.com/rust-lang/rustfmt/issues/4660 gets fixed
1953+
match text_document.uri.to_file_path() {
1954+
Ok(mut path) => {
1955+
// pop off file name
1956+
if path.pop() && path.is_dir() {
1957+
command.current_dir(path);
1958+
}
1959+
}
1960+
Err(_) => {
1961+
tracing::error!(
1962+
text_document = ?text_document.uri,
1963+
"Unable to get path, rustfmt.toml might be ignored"
1964+
);
1965+
}
1966+
}
1967+
19651968
let mut rustfmt = command
19661969
.stdin(Stdio::piped())
19671970
.stdout(Stdio::piped())

crates/rust-analyzer/src/lsp/from_proto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub(crate) fn file_position(
7272

7373
pub(crate) fn file_range(
7474
snap: &GlobalStateSnapshot,
75-
text_document_identifier: lsp_types::TextDocumentIdentifier,
75+
text_document_identifier: &lsp_types::TextDocumentIdentifier,
7676
range: lsp_types::Range,
7777
) -> anyhow::Result<FileRange> {
7878
file_range_uri(snap, &text_document_identifier.uri, range)

0 commit comments

Comments
 (0)