about summary refs log tree commit diff
path: root/crates/rust-analyzer/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-01-06 09:03:38 +0000
committerGitHub <noreply@github.com>2021-01-06 09:03:38 +0000
commitc3104466596e85d7fa43b8e3ac015bcabd08fcce (patch)
tree78a5476e17bcc07087dc7a7311e42fb2bce64aa0 /crates/rust-analyzer/src
parent861a54727003e054629b5bca5d94f8e7a4554cef (diff)
parentc49d5f757cccc7fc2ef02f6a8b3cf8a459c8f326 (diff)
downloadrust-c3104466596e85d7fa43b8e3ac015bcabd08fcce.tar.gz
rust-c3104466596e85d7fa43b8e3ac015bcabd08fcce.zip
Merge #7174
7174: Normalize line endings when formatting r=matklad a=Jesse-Bakker

Fixes #7166


Co-authored-by: Jesse Bakker <github@jessebakker.com>
Diffstat (limited to 'crates/rust-analyzer/src')
-rw-r--r--crates/rust-analyzer/src/handlers.rs18
1 files changed, 15 insertions, 3 deletions
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs
index c13cdc4e383..071b34cda5a 100644
--- a/crates/rust-analyzer/src/handlers.rs
+++ b/crates/rust-analyzer/src/handlers.rs
@@ -31,12 +31,13 @@ use serde_json::to_value;
 use stdx::{format_to, split_once};
 use syntax::{algo, ast, AstNode, TextRange, TextSize};
 
-use crate::diff::diff;
 use crate::{
     cargo_target_spec::CargoTargetSpec,
     config::RustfmtConfig,
+    diff::diff,
     from_json, from_proto,
     global_state::{GlobalState, GlobalStateSnapshot},
+    line_endings::LineEndings,
     lsp_ext::{self, InlayHint, InlayHintsParams},
     lsp_utils::all_edits_are_disjoint,
     to_proto, LspError, Result,
@@ -906,14 +907,25 @@ pub(crate) fn handle_formatting(
         }
     }
 
-    if *file == captured_stdout {
+    let (new_text, new_line_endings) = LineEndings::normalize(captured_stdout);
+
+    if file_line_endings != new_line_endings {
+        // If line endings are different, send the entire file.
+        // Diffing would not work here, as the line endings might be the only
+        // difference.
+        Ok(Some(to_proto::text_edit_vec(
+            &file_line_index,
+            new_line_endings,
+            TextEdit::replace(TextRange::up_to(TextSize::of(&*file)), new_text),
+        )))
+    } else if *file == new_text {
         // The document is already formatted correctly -- no edits needed.
         Ok(None)
     } else {
         Ok(Some(to_proto::text_edit_vec(
             &file_line_index,
             file_line_endings,
-            diff(&file, &captured_stdout),
+            diff(&file, &new_text),
         )))
     }
 }