diff options
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 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; | |||
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, |
@@ -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 | } |