diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-01-06 09:03:38 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-01-06 09:03:38 +0000 |
commit | c3104466596e85d7fa43b8e3ac015bcabd08fcce (patch) | |
tree | 78a5476e17bcc07087dc7a7311e42fb2bce64aa0 /crates | |
parent | 861a54727003e054629b5bca5d94f8e7a4554cef (diff) | |
parent | c49d5f757cccc7fc2ef02f6a8b3cf8a459c8f326 (diff) |
Merge #7174
7174: Normalize line endings when formatting r=matklad a=Jesse-Bakker
Fixes #7166
Co-authored-by: Jesse Bakker <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/rust-analyzer/src/handlers.rs | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index c13cdc4e3..071b34cda 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs | |||
@@ -31,12 +31,13 @@ use serde_json::to_value; | |||
31 | use stdx::{format_to, split_once}; | 31 | use stdx::{format_to, split_once}; |
32 | use syntax::{algo, ast, AstNode, TextRange, TextSize}; | 32 | use syntax::{algo, ast, AstNode, TextRange, TextSize}; |
33 | 33 | ||
34 | use crate::diff::diff; | ||
35 | use crate::{ | 34 | use crate::{ |
36 | cargo_target_spec::CargoTargetSpec, | 35 | cargo_target_spec::CargoTargetSpec, |
37 | config::RustfmtConfig, | 36 | config::RustfmtConfig, |
37 | diff::diff, | ||
38 | from_json, from_proto, | 38 | from_json, from_proto, |
39 | global_state::{GlobalState, GlobalStateSnapshot}, | 39 | global_state::{GlobalState, GlobalStateSnapshot}, |
40 | line_endings::LineEndings, | ||
40 | lsp_ext::{self, InlayHint, InlayHintsParams}, | 41 | lsp_ext::{self, InlayHint, InlayHintsParams}, |
41 | lsp_utils::all_edits_are_disjoint, | 42 | lsp_utils::all_edits_are_disjoint, |
42 | to_proto, LspError, Result, | 43 | to_proto, LspError, Result, |
@@ -906,14 +907,25 @@ pub(crate) fn handle_formatting( | |||
906 | } | 907 | } |
907 | } | 908 | } |
908 | 909 | ||
909 | if *file == captured_stdout { | 910 | let (new_text, new_line_endings) = LineEndings::normalize(captured_stdout); |
911 | |||
912 | if file_line_endings != new_line_endings { | ||
913 | // If line endings are different, send the entire file. | ||
914 | // Diffing would not work here, as the line endings might be the only | ||
915 | // difference. | ||
916 | Ok(Some(to_proto::text_edit_vec( | ||
917 | &file_line_index, | ||
918 | new_line_endings, | ||
919 | TextEdit::replace(TextRange::up_to(TextSize::of(&*file)), new_text), | ||
920 | ))) | ||
921 | } else if *file == new_text { | ||
910 | // The document is already formatted correctly -- no edits needed. | 922 | // The document is already formatted correctly -- no edits needed. |
911 | Ok(None) | 923 | Ok(None) |
912 | } else { | 924 | } else { |
913 | Ok(Some(to_proto::text_edit_vec( | 925 | Ok(Some(to_proto::text_edit_vec( |
914 | &file_line_index, | 926 | &file_line_index, |
915 | file_line_endings, | 927 | file_line_endings, |
916 | diff(&file, &captured_stdout), | 928 | diff(&file, &new_text), |
917 | ))) | 929 | ))) |
918 | } | 930 | } |
919 | } | 931 | } |