From 42f77f89ff39546cc8cf0f3a8d2513424d479a58 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Wed, 6 Sep 2023 15:32:38 -0400 Subject: [PATCH] internal: use current subcrate's `rustfmt.toml` with all rustfmt configurations --- crates/rust-analyzer/src/handlers/request.rs | 55 +++++++++++--------- crates/rust-analyzer/src/lsp/from_proto.rs | 2 +- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/crates/rust-analyzer/src/handlers/request.rs b/crates/rust-analyzer/src/handlers/request.rs index 36158e8115e..4b81b40436a 100644 --- a/crates/rust-analyzer/src/handlers/request.rs +++ b/crates/rust-analyzer/src/handlers/request.rs @@ -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, ¶ms.text_document, range)?; let info = match snap.analysis.hover(&snap.config.hover(), file_range)? { None => return Ok(None), Some(info) => info, @@ -1130,7 +1130,7 @@ pub(crate) fn handle_code_action( let line_index = 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(); assists_config.allowed = params @@ -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, ¶ms.position.text_document, *range)) .collect::, _>>()?; let position = from_proto::file_position(&snap, params.position)?; 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 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)?; @@ -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)? { @@ -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)? { @@ -1596,7 +1596,7 @@ pub(crate) fn handle_semantic_tokens_range( ) -> anyhow::Result> { 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 line_index = snap.file_line_index(frange.file_id)?; @@ -1679,7 +1679,7 @@ pub(crate) fn handle_move_item( ) -> anyhow::Result> { let _p = profile::span("handle_move_item"); 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 { lsp_ext::MoveItemDirection::Up => ide::Direction::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()); @@ -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; @@ -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()) diff --git a/crates/rust-analyzer/src/lsp/from_proto.rs b/crates/rust-analyzer/src/lsp/from_proto.rs index 06efe6ca911..69d6aba94c3 100644 --- a/crates/rust-analyzer/src/lsp/from_proto.rs +++ b/crates/rust-analyzer/src/lsp/from_proto.rs @@ -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 { file_range_uri(snap, &text_document_identifier.uri, range)