aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Bakker <[email protected]>2021-01-05 21:29:53 +0000
committerJesse Bakker <[email protected]>2021-01-05 22:58:51 +0000
commitc49d5f757cccc7fc2ef02f6a8b3cf8a459c8f326 (patch)
tree66364e586f95934c40937983ac700e4acfa3127b
parent41454eb1ebc87c0f35d247bfb600e775abe022f4 (diff)
Normalize line endings when formatting
-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 dd486070b..3e49904c3 100644
--- a/crates/rust-analyzer/src/handlers.rs
+++ b/crates/rust-analyzer/src/handlers.rs
@@ -31,12 +31,13 @@ use serde_json::to_value;
31use stdx::{format_to, split_once}; 31use stdx::{format_to, split_once};
32use syntax::{algo, ast, AstNode, TextRange, TextSize}; 32use syntax::{algo, ast, AstNode, TextRange, TextSize};
33 33
34use crate::diff::diff;
35use crate::{ 34use 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,
@@ -909,14 +910,25 @@ pub(crate) fn handle_formatting(
909 } 910 }
910 } 911 }
911 912
912 if *file == captured_stdout { 913 let (new_text, new_line_endings) = LineEndings::normalize(captured_stdout);
914
915 if file_line_endings != new_line_endings {
916 // If line endings are different, send the entire file.
917 // Diffing would not work here, as the line endings might be the only
918 // difference.
919 Ok(Some(to_proto::text_edit_vec(
920 &file_line_index,
921 new_line_endings,
922 TextEdit::replace(TextRange::up_to(TextSize::of(&*file)), new_text),
923 )))
924 } else if *file == new_text {
913 // The document is already formatted correctly -- no edits needed. 925 // The document is already formatted correctly -- no edits needed.
914 Ok(None) 926 Ok(None)
915 } else { 927 } else {
916 Ok(Some(to_proto::text_edit_vec( 928 Ok(Some(to_proto::text_edit_vec(
917 &file_line_index, 929 &file_line_index,
918 file_line_endings, 930 file_line_endings,
919 diff(&file, &captured_stdout), 931 diff(&file, &new_text),
920 ))) 932 )))
921 } 933 }
922} 934}