about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Barsky <me@davidbarsky.com>2023-09-06 15:32:38 -0400
committerDavid Barsky <me@davidbarsky.com>2023-09-06 15:32:38 -0400
commit42f77f89ff39546cc8cf0f3a8d2513424d479a58 (patch)
tree0d60238b7d71d06bc7547572dd03524cd3adb8f6
parentf29867bd26ec0ed34a8c26cd39fc02df657a13f8 (diff)
downloadrust-42f77f89ff39546cc8cf0f3a8d2513424d479a58.tar.gz
rust-42f77f89ff39546cc8cf0f3a8d2513424d479a58.zip
internal: use current subcrate's `rustfmt.toml` with all rustfmt configurations
-rw-r--r--crates/rust-analyzer/src/handlers/request.rs55
-rw-r--r--crates/rust-analyzer/src/lsp/from_proto.rs2
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, &params.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, &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
@@ -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(
@@ -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)?;
@@ -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<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)?;
 
@@ -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,
@@ -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<FileRange> {
     file_range_uri(snap, &text_document_identifier.uri, range)